Reversi(奥赛罗)Java简单程序返回错误的动作

时间:2015-12-07 15:59:55

标签: java reversi

我一直在努力实现简单的程序,返回Reversi / Othello游戏的有效移动。 不幸的是,它不起作用,我不明白为什么。它返回[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);
}

}

1 个答案:

答案 0 :(得分:0)

董事会[x] [y]结构......

有时请参考:

board [y] [nextX]而不是board [nextX] [y]