I'm trying to make a knights tour program. Right now I have it so the knight starts at around the middle of the board and moves in a circle. But now if I want the knight to start at 0,0 I just get errors saying array out of bounds. I'm guessing that I need a method to check to see if its safe to move the next point but I'm not too sure.
Here is the code I have right now.
public class Knight1 {
public static void main(String[] args) {
int board[][] = new int[8][8];
int horizontal[] = new int[8];
int vertical[] = new int[8];
horizontal[0] = 2;
horizontal[1] = 1;
horizontal[2] = -1;
horizontal[3] = -2;
horizontal[4] = -2;
horizontal[5] = -1;
horizontal[6] = 1;
horizontal[7] = 2;
vertical[0] = -1;
vertical[1] = -2;
vertical[2] = -2;
vertical[3] = -1;
vertical[4] = 1;
vertical[5] = 2;
vertical[6] = 2;
vertical[7] = 1;
int moveNumber = 0;
int currentRow = 0;
int currentCol = 0;
int counter = 1;
int x = 0;
int y = 7;
int xOffset[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
int yOffset[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
for (int i = 0; i < 8; i++) {
if (xOffset[i] >= 0 && yOffset[i] >= 0 && xOffset[i] < 8 && yOffset[i] < 8) {
currentRow = xOffset[i];
currentCol = yOffset[i];
Board[currentRow][currentCol] = counter;
counter++;
}
}
printBoard(board);
}
public static void printBoard(int[][] board) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
System.out.print(" " + board[x][y]);
}
System.out.println();
}
}
}