What am I missing in my Knight's Tour program? (Java)?

KingArthur13th

New member
Mar 4, 2011
1
0
1
I'm attempting to create the Knight's Tour program in java but all I ever seem to get is a board filled with zeroes. I'm pretty sure I'm missing something but I'm not sure what.

public class Project1 {
public static int lengthOfSide;
public static Integer[][] board;
public static Integer[][] moves;
public static int spot = 1;

public static void main(String[] args){
//lengthOfSide = (args.length != 0) ? Integer.parseInt(args[0]) : 8;

lengthOfSide=8;
board = new Integer[lengthOfSide][lengthOfSide];
moves = new Integer[8][2];

int i=0;
moves[0] = 1;
moves[i++][1] = 2;
moves[0] = 1;
moves[i++][1] = -2;
moves[0] = 2;
moves[i++][1] = 1;
moves[0] = 2;
moves[i++][1] = -1;

moves[0] = -1;
moves[i++][1] = 2;
moves[0] = -1;
moves[i++][1] = -2;
moves[0] = -2;
moves[i++][1] = 1;
moves[0] = -2;
moves[i++][1] = -1;

for(i =0 ; i<lengthOfSide; i++){
for(int j = 0 ; j<lengthOfSide; j++){
board[j] = spot;
spot++;
}
}
traverse(0,0);
}

public static void traverse(int x, int y){
int i=0;

for(int count = 0 ; count < 8; count++){
int xC,yC;
xC=(x + moves[count][0]);
yC=(y + moves[count][1]);
if((xC >= 0) && (xC < lengthOfSide) && (yC >= 0) && (yC < lengthOfSide)&& (board[xC][yC] != 0)){
board[xC][yC] = i++;
traverse(xC, yC);
}

}
if(boardIsComplete()){
done();
}
else{
board[x]
 
Back
Top