首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reversi (Othello) Java简单程序返回错误的步骤

Reversi (Othello) Java简单程序返回错误的步骤
EN

Stack Overflow用户
提问于 2015-12-07 23:59:55
回答 1查看 582关注 0票数 0

我一直在尝试实现简单的程序,返回Reversi / Othello游戏的有效移动。不幸的是,它不起作用,我真的看不出原因。它返回2,2,这肯定不是有效的移动。

//myColor, opponentColor, Reversi move etc. are already defined.

如果你指出我系统中的缺陷,我会很高兴的。

代码语言:javascript
复制
@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);
}

}
EN

回答 1

Stack Overflow用户

发布于 2015-12-08 00:11:12

木板结构。

有时指的是:

boardy而不是boardnextX

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34137878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档