gpt4 book ai didi

java - 骑士之旅随机移动选择器不起作用

转载 作者:行者123 更新时间:2023-12-01 12:19:53 27 4
gpt4 key购买 nike

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

最佳答案

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

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

所以替换

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

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

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

关于java - 骑士之旅随机移动选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com