我創建了一個TTT遊戲。不過,我在AI取點上遇到了麻煩。我如何避免這種情況發生,以至於在chooseCellRow:Col
的方法中,它只會返回任何一個玩家尚未選擇的隨機點?TicTacToe作弊
- (void) chooseCellRow:(NSInteger)row Col:(NSInteger)col
{
//pick random spot for computer's turn
row = random()%2;
col = random()%2;
if (row == 0 && col == 0) {
[but00 setTitle:@"X" forState:0];
}
else if (row == 0 && col == 1) {
[but01 setTitle:@"X" forState:0];
}
else if (row == 0 && col == 2) {
[but02 setTitle:@"X" forState:0];
}
else if (row == 1 && col == 0) {
[but10 setTitle:@"X" forState:0];
}
else if (row == 1 && col == 1) {
[but11 setTitle:@"X" forState:0];
}
else if (row == 1 && col == 2) {
[but12 setTitle:@"X" forState:0];
}
else if (row == 2 && col == 0) {
[but20 setTitle:@"X" forState:0];
}
else if (row == 2 && col == 1) {
[but21 setTitle:@"X" forState:0];
}
else if (row == 2 && col == 2) {
[but22 setTitle:@"X" forState:0];
}
}
do row = random()%3; col = random()%3; (cell [row] [col]!='x')||(cell [row] [col]!='o')); – jsollo2
jsollo,在我的回答中看到我的評論,你應該使用'&&'而不是'||'。 – paxdiablo