-2

I have this Knight's Tour code but am getting a java.lang.StackOverflowError on line 37. I'm not really sure where to go from here to fix it. I think the code in my main has something to do with it but I'm not sure what to do with it at this point. Any help is greatly appreciated thanks.

public class Main {
public static void main(String[] args) {
    Main tour = new Main();
    tour.solveKnightTour();
}
private static int chessboard[][];
boolean a = true;
public Main() {
    chessboard = new int[8][8];
}
private void matrixChessBoard() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            System.out.printf("%5d", chessboard[i][j]);
        }
        System.out.println();
    }
}
static boolean tour(int advance, int horizontal, int vertical, int xMove[], int yMove[]) {
    int i = 0;
    int moveHoriz = 0;
    int moveVert = 0;
    boolean a = true;
    chessboard[horizontal][vertical] = advance;
    if (advance == 63) {
        for (int t = 0; t < 8; t++) {
            for (int u = 0; u < 8; u++) {
                System.out.printf("%5d", chessboard[t][u]);
            }
            System.out.println("\n");
        }
    } else {
        for (int j = 0; j < 8; j++) {
            if ((horizontal + xMove[j] < 8 & (vertical + yMove[j]) >= 0 & (vertical + yMove[i]) < 8)
                    & (horizontal + xMove[i]) >= 0){
                if (chessboard[horizontal + xMove[i]][vertical + yMove[i]] == -1){
//line 37           if (tour(moveHoriz, moveVert, advance + 1, xMove, yMove)){
                        break;
                    }
                }
            }
        }
        a = false;
        chessboard[horizontal][vertical] = -1;
    }
    return a;
}
public boolean solveKnightTour() {
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            chessboard[x][y] = -1;
        }
    }
    int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
    int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
    chessboard[0][0] = 0;
    if (!tour(0, 0, 1, xMove, yMove)) {
        return false;
    } else {
        matrixChessBoard();
    }
    return true;
}

}

false
  • 10,264
  • 13
  • 101
  • 209
mattx2
  • 1
  • 1
  • 4
    Have you traced through the code in your IDE debugger? That is the place to start. – Jim Garrison Nov 22 '17 at 01:18
  • Use the debugger to step through the code and isolate where the trouble is with your code. And when you post here and say *Line 37*, it helps a lot if you put a comment in your code on line 37, so we don't have to count lines. – Ken White Nov 22 '17 at 01:18
  • sorry about that, I just updated it to show line 37. I ran the debugger but it ultimately failed to respond. Under the Main at localhost, it says – mattx2 Nov 22 '17 at 01:24
  • In your `tour()` method, you have a variable `i`, initialized to zero, and never changed. What are you using it for? Does that make sense to you? What are `moveHoriz` and `moveVert` set to? Where are they changed? How are they used? Does that make sense to you? – AJNeufeld Nov 22 '17 at 06:33

1 Answers1

0

Consider a stripped down version of your tour() method:

static boolean tour(int advance, int horizontal, int vertical, ...) {
    int moveHoriz = 0;
    int moveVert = 0;
    if (false) {
        ...
    } else {
        if (true)
            tour(moveHoriz, moveVert, advance + 1, ...);
    }
}

Called with tour(0, 0, 1, ...), the function will enter with advance=0. Various assignments will be done, various tests will be made, and then the tour(moveHoriz, moveVert, advance+1, ...) statement will be executed, which evaluates to the call tour(0, 0, 1, ...). Repeat until stack overflow.

  • moveHoriz is passed to the advance parameter
  • moveVert is passed to the horizontal parameter
  • advance+1 is passed to the vertical parameter

This is clearly not what you intended. Since advance+1 is not passed to advance, the if (advance == 63) condition will always evaluate to false, and you never break out of the recursion.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44