Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 骑士';s Tour随机移动选取器不工作_Java - Fatal编程技术网

Java 骑士';s Tour随机移动选取器不工作

Java 骑士';s Tour随机移动选取器不工作,java,Java,我正在研究随机移动我的骑士在我的骑士的旅游代码。然而,出于某种原因,我的switch语句总是默认,而不是执行可能的移动。我知道我的可能移动方法是有效的,因为我已经单独运行了firstMoveChoice(),并且成功了。所以我知道问题出在我的随机移动选择器上,但我似乎无法解决它 感谢您的帮助和提前的时间!我非常感激,因为我已经仔细研究了这个问题,却找不到答案 public class MoveKnight extends Moves { public int[][] moveTheKnight

我正在研究随机移动我的骑士在我的骑士的旅游代码。然而,出于某种原因,我的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[][] 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;
}

这是因为
random.nextInt()
返回一个介于-2147483648和2147483647之间的值

您应该使用
random.nextInt(int n)
函数,该函数返回的值介于0(包含)和n(排除)之间

因此,替换

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


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

您应该检查
nextInt()
返回的可能整数值的范围。。。另外,每次你想创建一个新的随机整数时,创建一个新的
Random
实例是不明智的。我现在有个问题。当它执行
案例3:
时,它将向下移动2个方块,向左移动1个方块(将其移出电路板),我的if。。。我上面代码中的else语句不处理它,它给我一个
ArrayIndexOutOfBoundsException:-1
异常。有什么想法吗?既然我没有实现thirdMoveChoice(),它将变得很复杂。现在,它为什么不在无限循环中运行呢?显然,如果它不能达到64与我提供的移动,但它至少应该尝试在董事会周围移动。相反,每次我运行它时,它都会随机移动并终止在你的while循环结束时。我取消了中断,现在它部分工作。它到了董事会的右边,试图离开,但它不能,所以它尝试了另一个移动,并去了其他地方,这起了作用。然而,当我再次运行它时,它试图立即离开,这给了我相同的
越界异常-1
int randomMove = new Random().nextInt(); b
int randomMove = 1 + new Random().nextInt(3);