# Java - Stack Overflow Error



## cc64 (Dec 21, 2007)

The following program is designed to move a Knight across a chess board until it has touched all 64 spaces. However, when I run it, it returns a stack overflow error. Any ideas?

public class Knight
{
static int[][] board = new int[8][8];
static int r,c,rmove,cmove, count = 0;
static int Knight = 2; //THIS IS THE COUNTER OF HOW MANY MOVES HE HAS MADE 
static int i=0;	static int j=0;

public static void main(String[] args)
{ 
//DRAW THE BOARD AND FILL WITH 0'S
for(r = 0; r<8; r++)
{
for (int c = 0; c<8; c++)
{
board[r][c]= 0;
}
}

r=0;
c=0;
board[r][c]= 1;

tour();

//PRINT THE BOARD WHEN DONE
for(c=0;c<8;c++){
for(r=0;r<8;r++){
System.out.print(board[r][c]);
System.out.print(" ");

}
System.out.print("\n");
}

}

public static int [][] tour(){
move();
if (board[r+rmove][c+cmove] == 0){
board[r+rmove][c+cmove] = Knight;
r = r + rmove;
c = c + cmove;
Knight++; 
tour();

}
else if(Knight > 64)
System.out.println("Hes done");
else
tour();

Knight++;
return board;
}
public static void move(){
rmove = (int)(Math.random()*2) +1 ;
if (rmove == 1)
cmove = 2;
else 
cmove = 1;

j++;
System.out.print(j + ", ");
int posneg = (int)(Math.random()*4)+1;
switch(posneg){
case 1:
rmove = rmove *-1;
break;
case 2:
cmove = cmove *-1;
break;
case 3:
rmove = rmove * -1;
cmove = cmove * -1;
default:
break;
}
if (r + rmove <0 || r + rmove > 7)
rmove = rmove * -1;
if (c + cmove < 0 || c + cmove > 7)
cmove = cmove * -1;

}

}


----------



## Chicon (Jul 29, 2004)

Hi cc64,

You should review the logic of the *tour* method because it is stuck into an endless loop. Therefore, the *System.out.print* statement of the *move* method called by *tour* produces the stack overflow error.


----------



## cc64 (Dec 21, 2007)

Thanks Chicon,

Now that I know where there the problem is, I can't figure out why the issue is there.

Below is the code that produces the Stack Over Flow error:

```
public static int [][] tour(){
		move();
		
		if (board[r+rmove][c+cmove] == 0)
		{
			System.out.println("made new move to: " + c + "," + r);
			
			board[r+rmove][c+cmove] = Knight;
			r = r + rmove;
			c = c + cmove;
			Knight++;
	
			tour();
		}
	
		else if(Knight > 64)
		{
			System.out.println("Hes done");
			System.exit(0);
		}
		
		else
		{
			System.out.println("old place, trying again");
			tour();
		}
	
	Knight++;
	return board;
}
```
Now if I take the "tour();" out of the else (right after "old place, trying again") this is what I get:

```
Selected move (1,2), checking point...made new move to: 1,2
Selected move (1,2), checking point...made new move to: 2,4
Selected move (2,-1), checking point...made new move to: 4,3
Selected move (-2,-1), checking point...made new move to: 2,2
Selected move (1,2), checking point...made new move to: 3,4
Selected move (-2,-1), checking point...made new move to: 1,3
Selected move (-1,2), checking point...made new move to: 0,5
Selected move (2,-1), checking point...old place, trying again
1     0     0     0     0     8     0     0     
0     0     2     7     0     0     0     0     
0     0     5     0     3     0     0     0     
0     0     0     0     6     0     0     0     
0     0     0     4     0     0     0     0     
0     0     0     0     0     0     0     0     
0     0     0     0     0     0     0     0     
0     0     0     0     0     0     0     0
```
So, for some reason, the tour(); in the else statement causes an infinite loop - but why doesn't the tour in the if section cause one?


----------



## Chicon (Jul 29, 2004)

I can't answer you on the fly as your coding is made tricky to test because of the 2 random instances in the *move* method.
Personally, I would have proceeded this way :
- at each occurence of the loop, I would have defined a list of possible legal moves of the knight, picked at random one of the moves in the list and then, _shifted_ the knight to its new location;
- the loop would have terminated as soon no legal move is allowed from the current location.


----------

