I am having an issue seeing the errors with this. I am using TextPad4.7.3 and when i try to do a board 53×53 or larger the errors that i can see are at
else if (move[1][m] > 0 && move[1][m] < 9){
if(tour(movNum + 1, (x + movCol[move[0][m]]), (y + movRow[move[0][m]]), bSize, board)){
break;}}
Since this is a recursive program, I doubt that this line is the real issue. The error list is so long that I cannot see the top where it would point out the real position of the error. Anyone know of anything that can properly log my errors or a method to see the error. Or if someone knows why when i use a number 53 or larger that this gives me a huge list of errors instead of continuing to work? This was an assignment, but at this point I am just trying to fix it for my own self satisfaction. Any help would be appreciated.
-WBM
import java.util.Scanner;
public class Tour {
// Knight moves
static int movCol[] = { 1, 2, 2, 1, -1, -2, -2, -1};
static int movRow[] = {-2, -1, 1, 2, 2, 1, -1, -2};
// Main method
static public void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("What are the board's Dimentions?");
int bSize = scan.nextInt();
int board[][] = new int[bSize][bSize];
System.out.print("Start on what column?");
int positionY = scan.nextInt();
System.out.print("Start on what row?");
int positionX = scan.nextInt();
System.out.println();
for (int i = 0; i < bSize; i=i+1) {
for (int j = 0; j < bSize; j=j+1) {
board[i][j] = -1;
}
}
tour(0, positionX - 1, positionY - 1, bSize, board);
}
static void printBoard(int board[][]) {
for(int i = 0; i < board.length; i=i+1){
for(int j = 0; j < board[0].length; j=j+1){
if (board.length < 11){
System.out.printf("%3d", board[i][j]);
}
else if (board.length < 32){
System.out.printf("%4d", board[i][j]);
}
else if (board.length < 101){
System.out.printf("%5d", board[i][j]);
}
else if (board.length < 317){
System.out.printf("%6d", board[i][j]);
}
else if (board.length < 1001){
System.out.printf("%7d", board[i][j]);
}
else if (board.length < 3163){
System.out.printf("%8d", board[i][j]);
}
else if (board.length < 10001){
System.out.printf("%9d", board[i][j]);
}
}
System.out.println("\n");
}
}
static boolean tour(int movNum, int x, int y, int bSize, int board[][]) {
boolean success = true;
board[x][y] = movNum;
if((movNum < ((bSize * bSize) - 1))){
int move[][] = new int[2][8];
for(int i = 0; i < 8; i=i+1){
if((x + movCol[i]) >= 0 && (x + movCol[i]) < bSize && (y + movRow[i]) >= 0 && (y + movRow[i]) < bSize){
if(board[x + movCol[i]][y + movRow[i]] < 0){
move[0][i] = i; // marks all possible moves
for(int h=0; h<8; h=h+1){
if((x + movCol[i] + movCol[h]) >= 0 && (x + movCol[i] + movCol[h]) < bSize && (y + movRow[i] + movRow[h]) >= 0 && (y + movRow[i] + movRow[h]) < bSize){
if(board[x + movCol[i] + movCol[h]][y + movRow[i] + movRow[h]] < 0){
move[1][i] = move[1][i] + 1;
}
}
}
}
else {
move[0][i] = i;
move[1][i] = 9;
}
}
else {
move[0][i] = i;
move[1][i] = 9;
}
}
for(int j = 0; j < 7; j=j+1){
for(int k = 0; k < 7; k=k+1){
if (move[1][k]> move[1][k+1]){
int lower = move[1][k+1];
int lower2 = move[0][k+1];
int upper = move[1][k];
int upper2 = move[0][k];
move[1][k] = lower;
move[0][k] = lower2;
move[1][k+1] = upper;
move[0][k+1] = upper2;
}
}
}
for(int m = 0; m < 8; m=m+1){
if (move[1][m] == 0 && movNum == ((bSize * bSize) - 2)){
if(tour(movNum + 1, (x + movCol[move[0][m]]), (y + movRow[move[0][m]]), bSize, board)){
break;
}
}
else if (move[1][m] > 0 && move[1][m] < 9){
if(tour(movNum + 1, (x + movCol[move[0][m]]), (y + movRow[move[0][m]]), bSize, board)){
break;
}
}
}
success = false;
board[x][y] = -1;
}
else{
printBoard(board);
System.out.println("\n");
System.exit(0);
}
return success;
}
}
Sounds like it’s probably a
StackOverflowError. The Windows console has a preferences dialog associated with it. Use those preferences to set the “scroll buffer” to 4000 lines (instead of the default which is like 12 or something.) Then you’ll be able to scroll up and see what the error message actually is.Assuming that it is indeed a stack overflow problem, one of the recursive calls is probably being made without an appropriate exit condition; examine them carefully. There’s an
ifblock around the two recursive calls, but both the calls have exactly the same arguments; that suggests to me that maybe there’s a typo there?