我一直在尝试实现返回黑白棋/黑白棋游戏的有效移动的简单程序。不幸的是,它不起作用,我真的不明白为什么。它返回 [2,2] 这肯定不是有效的移动。
//myColor, opponentColor, Reversi move etc. are already defined.
如果您指出我的系统存在缺陷,我会很高兴。
@Override
public ReversiMove makeNextMove(int[][] board) {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (board[y][x] != myColor && board[y][x] != opponentColor) {
int nextX = x + 1;
while (nextX < 7 && board[y][nextX] == this.opponentColor) {
if (board[y][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
}
nextX = x - 1;
while (nextX > 0 && board[y][nextX] == this.opponentColor) {
if (board[y][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
}
int nextY = y + 1;
while (nextY < 7 && board[nextY][x] == this.opponentColor) {
if (board[nextY + 1][x] == this.myColor) {
return new ReversiMove(y,x);
}
nextY++;
}
nextY = y - 1;
while (nextY > 0 && board[nextY][x] == this.opponentColor) {
if (board[nextY - 1][x] == this.myColor) {
return new ReversiMove(y,x);
}
nextY--;
}
nextX = x + 1;
nextY = y + 1;
while (nextX < 7 && nextY < 7 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY + 1][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
nextY++;
}
nextX = x - 1;
nextY = y - 1;
while (nextX > 0 && nextY > 0 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY - 1][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
nextY--;
}
nextX = x + 1;
nextY = y - 1;
while (nextX < 7 && nextY > 0 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY - 1][nextX + 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX++;
nextY--;
}
nextX = x - 1;
nextY = y + 1;
while (nextX > 0 && nextY < 7 && board[nextY][nextX] == this.opponentColor) {
if (board[nextY + 1][nextX - 1] == this.myColor) {
return new ReversiMove(y,x);
}
nextX--;
nextY++;
}
}
}
}
return new ReversiMove(-1, -1);
}
}