0

I'm trying to make a knights tour program. Right now I have it so the knight starts at around the middle of the board and moves in a circle. But now if I want the knight to start at 0,0 I just get errors saying array out of bounds. I'm guessing that I need a method to check to see if its safe to move the next point but I'm not too sure.

Here is the code I have right now.

public class Knight1 {

    public static void main(String[] args) {
        int board[][] = new int[8][8];
        int horizontal[] = new int[8];
        int vertical[] = new int[8];

        horizontal[0] = 2;
        horizontal[1] = 1;
        horizontal[2] = -1;
        horizontal[3] = -2;
        horizontal[4] = -2;
        horizontal[5] = -1;
        horizontal[6] = 1;
        horizontal[7] = 2;

        vertical[0] = -1;
        vertical[1] = -2;
        vertical[2] = -2;
        vertical[3] = -1;
        vertical[4] = 1;
        vertical[5] = 2;
        vertical[6] = 2;
        vertical[7] = 1;

        int moveNumber = 0;
        int currentRow = 0;
        int currentCol = 0;
        int counter = 1;
        int x = 0;
        int y = 7;
        int xOffset[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
        int yOffset[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
        for (int i = 0; i < 8; i++) {
            if (xOffset[i] >= 0 && yOffset[i] >= 0 && xOffset[i] < 8 && yOffset[i] < 8) {
                currentRow = xOffset[i];
                currentCol =  yOffset[i];
                Board[currentRow][currentCol] = counter;
                counter++;
            }
        }
        printBoard(board);
    }

    public static void printBoard(int[][] board) {
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                System.out.print("  " + board[x][y]);
            }
            System.out.println();
        }
    }
}
false
  • 10,264
  • 13
  • 101
  • 209
swen
  • 3
  • 5
  • What's the line that gives you the `ArrayIndexOutOfBoundsException`? – Oneiros Mar 29 '17 at 18:28
  • Put some `System.out.println` inside that while loop and check the values of those `currentRow` and `currentCol`. There's surely something wrong with your list of steps (that is, indeed, written in a very confusing way) – Oneiros Mar 29 '17 at 18:31
  • I get the ArrayIndexOutOfBoundsException when i set the currentCol/currentRow to 0 – swen Mar 29 '17 at 18:33
  • It looks like you are mixing up a move identifier (one of eight possible moves for a knight) with a move number (incrementing by one for each move). For example, starting at (7,4), your first move may not necessarily be "move identifier 0", which is (-1, +2) ... which would take you to (6,6). For a Knight's Tour, you need to search the board for the correct move to make, possibly using recursion. A Knights Tour on an 8x8 board will be composed of a total of 64 moves, which is, of course, much larger than the 8 possible moves for a knight. – AJNeufeld Mar 29 '17 at 18:36
  • Ahh okay I think I see what you mean, Ill try using recursion and see what happens. – swen Mar 29 '17 at 18:37

1 Answers1

0

Problem is in while condition, a hint for solution is that you must exclude -2 and -1 when currentRow/Column is 0(only exclude -2 if it is 1), also exclude +1 and +2 when currentRow/Column is 7(only +2 when it is 6) otherwise your index in board[currentRow][currentCol] = counter; will be out of bounds.
Here is how rules for knight movement could look:

    int x = 0;
    int y = 7;
    int xOffset[] = {x + 1, x + 1, x + 2, x + 2, x - 1, x - 1, x - 2, x - 2};
    int yOffset[] = {y - 2, y + 2, y - 1, y + 1, y - 2, y + 2, y - 1, y + 1};
    for (int i = 0; i < 8; i++) {
        if (xOffset[i] >= 0 && yOffset[i] >= 0 && xOffset[i] < 8 && yOffset[i] < 8) {
            // X and Y are currentRow and currentColumn in your example
            // Code is copied from my java chess game and I was lazy to change variable everywhere
            x = xOffset[i];
            y = yOffset[i];
        }
    }
FilipRistic
  • 2,661
  • 4
  • 22
  • 31
  • I added the board[][] to the if block, so i can set the move number. It seems to only do one move. Even if i add a while loop in the if block it only goes through once. Probably something I'm missing/doing wrong. – swen Mar 29 '17 at 20:49
  • Can you post updated code so we can see what is problem? – FilipRistic Mar 29 '17 at 21:35
  • Updated the original code I posted. Its alittle messy still – swen Mar 29 '17 at 23:14
  • Your code works for me, here is output for x = 0 and y = 5: 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 0 3 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 0 0 0 0 0 0 0 0 0 0 0 0 0 – FilipRistic Mar 30 '17 at 00:21