0

我正在努力随机化我的骑士在我的骑士的旅游代码中采取的行动。但由于某种原因,我的 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();
}

这是我的棋盘类:

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;
}
4

1 回答 1

1

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

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

所以更换

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

经过

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

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

于 2014-11-05T16:45:14.050 回答