Knight's Tour随机移动选择器无法正常工作

时间:2014-11-05 16:41:42

标签: java

我正在努力随机化我的骑士在我的骑士游览代码中采取的行动。但是出于某种原因,我的switch语句总是变为默认值,而不是执行可能的移动。我知道我可能的移动方法是有效的,因为我已经单独运行firstMoveChoice()并且它有效。所以我知道问题在于我的随机移动选择器,但我似乎无法弄明白。

感谢您的帮助和提前的时间!我非常感激,因为我已经看过这个问题而无法找到答案。

public class MoveKnight extends Moves {

public int[][] moveTheKnight() {

    Moves switchBetweenMoves = new Moves();
    switchBetweenMoves.startingLocation();

    while (knight != 64) {
        int randomMove = new Random().nextInt();
        switch (randomMove) {
            case 1:
                switchBetweenMoves.firstMoveChoice();
                break;

            case 2:
                switchBetweenMoves.secondMoveChoice();
                break;

            case 3:
                switchBetweenMoves.thirdMoveChoice();
                break;

            // other possible moves

            default:
                System.out.println("No more possible moves.");
                break;
        }
        break;
    }
    return board;
}
}

以上是上述代码的结果:

1  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  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
0  0  0  0  0  0  0  0  
No more possible moves.
BUILD SUCCESSFUL (total time: 0 seconds)

这是我的主要方法:

public static void main(String[] args) {

    MoveKnight myKnight = new MoveKnight();
    myKnight.moveTheKnight();
}

这是我的ChessBoard课程:

public class ChessBoard {

int[][] board;

public ChessBoard() {
    this.board = new int[8][8];
}
}

以下是firstMoveChoice()

public int[][] firstMoveChoice() {

System.out.println();

x += 1;
y += 2;

if (x >= board.length) { // this tests to make sure the knight does not move off the row
    System.out.println("Cannot move off board on x axis\n");
    x -= 1;
    y -= 2;
    return board;
}
else if (y >= board.length) { // this tests to make sure the knight does not move off the column
    System.out.println("Cannot move off board on y axis\n");
    x -= 1;
    y -= 2;
    return board;
}
else if (board[x][y] != 0) { // this prevents the knight from landing on a previously landed on square
    x -= 1;
    y -= 2;
    System.out.println("Cannot move onto a used square\n");
    return board;
}
else { // this moves the knight when the above statements are false
    board[x][y] = ++knight;
    System.out.println("This executed and the knight moved\n");

    for(int[] row : board) {
        printRow(row);
    }

    if (knight == 64) {
        System.out.println("Knight has completed the tour");
        }

        return board;
    }
}

这是thirdMoveChoice():除了不同动作的数字变化外,它几乎与所有其他动作相同。

public int[][] thirdMoveChoice() {

System.out.println();

x += 2;
y -= 1;

if (x >= board.length) { // this tests to make sure the knight does not move off the row
    System.out.println("Cannot move off board on x axis\n");
    x -= 2;
    y += 1;
    return board;
}
else if (y >= board.length) { // this tests to make sure the knight does not move off the column
    System.out.println("Cannot move off board on y axis\n");
    x -= 2;
    y += 1;
    return board;
}
else if (board[x][y] != 0) { // this prevents the knight from landing on a previously landed on square
    x -= 2;
    y += 1;
    System.out.println("Cannot move onto a used square\n");
    return board;
}
else { // this moves the knight when the above statements are false
    board[x][y] = ++knight;
    System.out.println("This executed and the knight moved\n");
}

for(int[] row : board) {
    printRow(row);
}

if (knight == 64) {
    System.out.println("Knight has completed the tour");
    return board;
}

return board;
}

1 个答案:

答案 0 :(得分:1)

这是因为random.nextInt()返回-2,147,483,648和2,147,483,647之间的值

你应该使用返回0和0之间的值的random.nextInt(int n)

所以替换

int randomMove = new Random().nextInt(); b

通过

int randomMove = 1 + new Random().nextInt(3);

此外,您应该将new Random()分配给可以重复使用的字段。