user4874529
user4874529

Reputation:

How to stop a program exiting the bounds of a 2d array

I'm doing a question where I start at 1 on an 8 by 8 2d array. Everywhere else is 0 by default. I can move only once to a new position and can continue to do so only to other 0's. My new position is represented by a counter.

Anyways, I can't seem to prevent my new position from exiting my array bounds! My code so far:

package com.stackoverflow.example;

import java.util.Random;


public class Chess
{
    public static void main(String args[]) 
    {
        int[][]board=new int[8][8];
        //vertical co-ordinates
        int[] vertical={-1,-2,-2,-1,1,2,2,1};
        //horizontal co-ordinates
        int[] horizontal={2,1,-1,-2,-2,-1,1,2};

        boolean moving = true;
        int counter=1;
        int currentrow;
        int currentcolumn;
        int check=0;
        Random rand=new Random();
        int rnum = rand.nextInt(8);

        currentrow = rnum;
        currentcolumn = rnum;

        //set the knight in a random position on the board first
        board[currentrow][currentcolumn]=counter;

        while(moving==true)
        {
            int rnum1 = rand.nextInt(8);

            currentrow += vertical[rnum1] ;

            currentcolumn += horizontal[rnum1] ;

            if(board[currentrow][currentcolumn]==0)
            {
                ++counter;
                board[currentrow][currentcolumn]=counter;
            }
            else if(board[currentrow][currentcolumn]>0)
            {
                currentrow -= vertical[rnum1] ;
                currentcolumn -= horizontal[rnum1] ;
            }
        }
    }
}

Upvotes: 0

Views: 93

Answers (2)

priya log
priya log

Reputation: 90

In order to stop your program exiting the bounds of a 2d array, you need concentrate more on boundary conditions with number of rows and columns in your 2d array.

Upvotes: 1

akhil_mittal
akhil_mittal

Reputation: 24157

It seems you are not validating the array bounds and the following lines can create problem (what if vertical[rnum1] is 2 and currentrow is 8):

int rnum1 = rand.nextInt(8);
currentrow += vertical[rnum1];

You need to place the condition to verify the bounds before you proceed. I am pasting your code be fixing that:

       public static void main(String[] args) {
       int[][] board = new int[8][8];
       //vertical co-ordinates
       int[] vertical = {-1, -2, -2, -1, 1, 2, 2, 1};
       //horizontal co-ordinates
       int[] horizontal = {2, 1, -1, -2, -2, -1, 1, 2};


       boolean moving = true;
       int counter = 1;
       int currentrow;
       int currentcolumn;
       int check = 0;
       Random rand = new Random();
       int rnum = rand.nextInt(8);

       currentrow = rnum;
       currentcolumn = rnum;

       //set the knight in a random position on the board first
       board[currentrow][currentcolumn] = counter;
       while (moving == true)
       {
           int rnum1 = rand.nextInt(8);
           currentrow += vertical[rnum1];
           currentcolumn += horizontal[rnum1];
           if(currentcolumn <8 && currentrow<8) {
               if (board[currentrow][currentcolumn] == 0) {
                   ++counter;
                   board[currentrow][currentcolumn] = counter;
               } else if (board[currentrow][currentcolumn] > 0) {
                   currentrow -= vertical[rnum1];
                   currentcolumn -= horizontal[rnum1];
               }
           }
       }
   }

I have not made any logic change as you need to take care of that part.

Upvotes: 0

Related Questions