Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Recursive Solution to Knights tour

1. ## Recursive Solution to Knights tour

When I run this program as it is I think the program ends up leaving me in a infinite loop of recursion. Basically this program is suppose to traverse a chess board in the way a knight moves, recursively. The recursive method traverse is support to backtrack each time the knight lands on a square that is not valid, off the board or in programming terms off the 2D array. Anyway as it is, like I said it doesn't work. I can't figure out what part of the recursive method should be tweaked and more importantly how it should be tweaked. So, if anyone who has done wrote this program before or is just a genius when it comes to recursive algorithms I really need some advice on where to go from now. If you could just point out where and how I should fix this method with some snippets of code to illustrate the problem, it would be great. Thanks.

```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;
}

}```