public class KnightTour {
static int[][]board = new int[8][8];
static int globalCount = 0;
public static void main(String[] args) {
init(board);
System.out.println(traverse(0,0,0));
System.out.println(globalCount);
print(board);
}
public static boolean traverse(int count, int ver, int hor){
//if we are in the proper bounds we can assign the value to the board
if(count >= 64){
return true;
}
//up 2 left 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count+1,ver-2, hor -1);
}
//up 2 right 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver-2, hor+1);
}
//down 2 right 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver+2, hor+1);
}
//down 2 left 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver+2, hor-1);
}
//left 2 down 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver+1, hor-2);
}
//left 2 up 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver-1, hor-2);
}
//right 2 down 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver+1, hor+2);
}
//right 2 up 1
if(hor <= 7 && hor >=0 && ver >= 0 && ver <= 7){
board[ver][hor] = count;
traverse(count + 1, ver-1, hor+2);
}
return false;
}
public static boolean traverse(int count, int ver, int hor){
board[ver][hor] = count; //store the count number at that location at the board
if(count >= 64){
return true;
}
//up 2 left 1
else if (ver > 1 && hor > 0 && board[ver-2][hor-1] == 0 && traverse(count + 1, ver-2, hor-1))
return true;
//up 2 right 1
else if(ver > 1 && hor < 7 && board[ver-2][hor+1] == 0 && traverse(count + 1,ver-2,hor+1))
return true;
//down 2 right 1
else if(ver < 6 && hor < 7 && board[ver+2][hor+1]== 0 && traverse(count + 1,ver+2,hor+1))
return true;
//down 2 left 1
else if(ver < 6 && hor > 0 && board[ver+2][hor-1]== 0 && traverse(count + 1,ver+2,hor-1))
return true;
//right 2 up 1
else if(hor < 6 && ver > 0 && board[ver-1][hor+2]== 0 && traverse(count + 1,ver-1,hor+2))
return true;
//right 2 down 1
else if(hor < 6 && ver < 7 && board[ver+1][hor+2]== 0 && traverse(count + 1,ver+1,hor+2))
return true;
//left 2 up 1
else if(hor > 1 && ver > 0 && board[ver-1][hor-2]== 0 && traverse(count + 1,ver-1,hor-2))
return true;
//left 2 down 1
else if(hor > 1 && ver < 7 && board[ver+1][hor-2]== 0 && traverse(count + 1,ver+1,hor-2))
return true;
if(count >= 57)
globalCount = count; //test to see how high the program gets to, it does not get past a count of 57;
return false;
}
public static void init(int[][] array){
for(int i = 0; i < array[0].length; i++){
for(int a = 0; a < array.length; a++){
array[i][a] = 0;
}
}
}
public static void print(int[][] array){
for(int i = 0; i < array[0].length; i++){
System.out.println();
for(int a = 0; a < array.length; a++){
System.out.print(" " + array[i][a] + " ");
}
}
}
public static boolean boardTraversed(int board[][]){
//checks to see if board is not traversed
for(int i = 0; i < board[0].length; i++)
for(int a = 0; a < board.length; a++)
if(board[i][a] == 0)
return false;
return true;
}
}