我现在遇到的问题是我不确定我的代码在逻辑上是否正确所以我要求帮助和意见
dir是enum方向的枚举。基本上有八个方向exp:NORTH,NORTHWEST,NORTHEAST等。
所以每个方向,我会扫描玩家,对手的标记或是否为空白
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;
}
}
}
}
}
如果有任何改进的余地,请告诉我
答案 0 :(得分:0)
前两个if
语句可以移到内部for
循环之外。
将if else
替换为else if
(您应该从编译器中获取错误)。
对玩家/对手令牌的检查根本不检查对手棋子。 我建议进行以下重写:
//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。