首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Reversi有效移动算法

Reversi有效移动算法
EN

Stack Overflow用户
提问于 2016-08-22 15:58:20
回答 1查看 2K关注 0票数 0

我现在遇到的问题是,我不确定我的代码在逻辑上是否正确,所以我寻求帮助和意见

dir是来自枚举方向的枚举。基本上有八个方向的出口:北,西北,东北等。

因此,在每个方向上,我都会扫描是否有玩家的、对手的或空白的令牌

Y是y坐标,x是x坐标。在这段代码中,我看的是玩家坐标的北面。

代码语言:javascript
复制
for(dir = NORTH; dir <= SOUTH_WEST; dir++){
    if(dir == NORTH){
        for(int i = 1; i <= BOARD_HEIGHT - y; i++){
//If there is no token, it would return false
            if(board[y+1][x] == BLANK){ 
                return FALSE;
            }
//if there is the player's token, it would return false
            if else(board[y+1][x] == player_token){ 
                return FALSE;
            }
//if there is the opponents token, it would continue up till it finds the player's token, if not then it would return false.
            if else(board[y+i][x] != player_token){ 
                if(board[y+i][x] == player_token){
                    captured_pieces++;
                }
                else{
                    return FALSE;
                }    

            }

        }
    }
} 

如果还有改进的余地,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2016-08-22 16:22:30

前两个if语句可以移到内部for循环之外。

else if替换if else (您应该会从编译器中得到错误)。

检查玩家/对手令牌根本不会检查对手的棋子。我建议重写如下:

代码语言:javascript
复制
//if there is the opponents token, it would continue up till it finds the player's token, if not then it would return false.
            switch (board[y+i][x]){ 
                case opponent_token:
                    // Another token potentially captured
                    captured_pieces++;
                case player_token:
                    // Stop counting
                    break;
                default:
                    // Empty square
                    return FALSE;
                }    

            }

在内部循环之后,您需要检查是否确实存在player_token。如果循环因为到达电路板的末尾而终止,则返回FALSE。

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

https://stackoverflow.com/questions/39074091

复制
相关文章

相似问题

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