我现在遇到的问题是,我不确定我的代码在逻辑上是否正确,所以我寻求帮助和意见
dir是来自枚举方向的枚举。基本上有八个方向的出口:北,西北,东北等。
因此,在每个方向上,我都会扫描是否有玩家的、对手的或空白的令牌
Y是y坐标,x是x坐标。在这段代码中,我看的是玩家坐标的北面。
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;
}
}
}
}
} 如果还有改进的余地,请告诉我。
发布于 2016-08-22 16:22:30
前两个if语句可以移到内部for循环之外。
用else if替换if else (您应该会从编译器中得到错误)。
检查玩家/对手令牌根本不会检查对手的棋子。我建议重写如下:
//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。
https://stackoverflow.com/questions/39074091
复制相似问题