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