Where is the error in in this similar to Knight Tours program?


#1

The problem I am trying to solve is similar to the Knight Tours problem, except that here I am not trying to make the knight visit every square only once. I am taking the position of the knight in rows and columns as input and then moving the knight around, and then printing the number of moves after every move has been exhausted. Here is the program:

    import java.util.concurrent.ThreadLocalRandom;
  public class KnightTours {
int chessboard[][]={{1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8}};

int horizontal[]={2,1,-1,-2,-2,-1,1,2,0};
int vertical[]={-1,-2,-2,-1,1,2,2,1,0};
private int counter=1;
int currentRow;
int currentColumn;
private int check;
void KnightTours(int curr,int curc){
    currentRow=curr;
    currentColumn=curc;
}
public void move(){
    for(int i=0;i<8;i++){
        int randomNum = ThreadLocalRandom.current().nextInt(0, 8);
        System.out.println("Random Number="+randomNum);
       int currentRow1=currentRow+vertical[randomNum];
        int currentColumn1;
        currentColumn1 = currentColumn+horizontal[randomNum];
        System.out.println("Current Row1="+currentRow1);
        System.out.println("Current Column1="+currentColumn1);
        if((currentRow1-1<0)||(currentColumn1-1<0)||(currentRow1-1>7)||(currentColumn1-1>7))
            continue;
        check = chessboard[currentRow1][currentColumn1];
        if(check==0){
           continue;
       }
       else{
           currentRow=currentRow+vertical[randomNum];
           currentColumn=currentColumn+horizontal[randomNum];
            System.out.println("Current Row="+currentRow);
            System.out.println("Current Column="+currentColumn);
           chessboard[currentRow-1][currentColumn-1]=0;
            counter=counter+1;
            System.out.println("Counter Check="+counter);
       }
    }

}
public void check(){
    KnightTours obj=new KnightTours();
    int i=1;
    while(i<=64){
        obj.move();
        i++;
    }
    System.out.println("Counter="+obj.counter);
}



 }

And then the main function:

     import java.util.*;
     public class KnightToursTest {
public static void main(String args[]){
    System.out.println("Enter position");
    Scanner scanner=new Scanner(System.in);
    int row=scanner.nextInt();
    int column=scanner.nextInt();
    KnightTours obj=new KnightTours();
    obj.KnightTours(row,column);
    obj.move();
    obj.check();
}
 }

The problem is, in the first function, I am getting an ArrayIndexOutofBounds exception in this line:

    check = chessboard[currentRow1][currentColumn1];

Please help. I want to know both as to whether my algorithm is correct and how to remove the ArrayIndexOutofBounds exception. The problem is because even when the variables column1 and row1 are within the chessboard’s array range, I am getting an error. Here is a sample output:

   Exception in thread "main" Random Number=4
   Current Row1=2
   Current Column1=-1
   Random Number=3
   Current Row1=0
   Current Column1=-1
   Random Number=3  
   Current Row1=0
   Current Column1=-1
   Random Number=2
   Current Row1=-1
   Current Column1=0
   Random Number=5
   Current Row1=3
   Current Column1=0
   Random Number=7
   Current Row1=2
   Current Column1=3
   java.lang.ArrayIndexOutOfBoundsException: 2
   at Deitel.KnightTours.move(KnightTours.java:28)
   at Deitel.KnightToursTest.main(KnightToursTest.java:12)

Thus, even when Current Row1=2 and Current Column1=3, despite the array size of chessboard being 8, I am getting an ArrayIndexOutofBoundsException.