I have this Knight's Tour code but am getting a java.lang.StackOverflowError on line 37. I'm not really sure where to go from here to fix it. I think the code in my main has something to do with it but I'm not sure what to do with it at this point. Any help is greatly appreciated thanks.
public class Main {
public static void main(String[] args) {
Main tour = new Main();
tour.solveKnightTour();
}
private static int chessboard[][];
boolean a = true;
public Main() {
chessboard = new int[8][8];
}
private void matrixChessBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
System.out.printf("%5d", chessboard[i][j]);
}
System.out.println();
}
}
static boolean tour(int advance, int horizontal, int vertical, int xMove[], int yMove[]) {
int i = 0;
int moveHoriz = 0;
int moveVert = 0;
boolean a = true;
chessboard[horizontal][vertical] = advance;
if (advance == 63) {
for (int t = 0; t < 8; t++) {
for (int u = 0; u < 8; u++) {
System.out.printf("%5d", chessboard[t][u]);
}
System.out.println("\n");
}
} else {
for (int j = 0; j < 8; j++) {
if ((horizontal + xMove[j] < 8 & (vertical + yMove[j]) >= 0 & (vertical + yMove[i]) < 8)
& (horizontal + xMove[i]) >= 0){
if (chessboard[horizontal + xMove[i]][vertical + yMove[i]] == -1){
//line 37 if (tour(moveHoriz, moveVert, advance + 1, xMove, yMove)){
break;
}
}
}
}
a = false;
chessboard[horizontal][vertical] = -1;
}
return a;
}
public boolean solveKnightTour() {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
chessboard[x][y] = -1;
}
}
int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
chessboard[0][0] = 0;
if (!tour(0, 0, 1, xMove, yMove)) {
return false;
} else {
matrixChessBoard();
}
return true;
}
}