我在Xcode中使用命令行创建了一个TicTacToe游戏。我已经掌握了一切正常工作,但我无法弄清楚如何避免玩家吸引其他玩家。因此,如果玩家选择现场[1] [1],如果任一玩家在整个游戏中进入现场[1] [1]他们将获得"无效的条目,请再试一次"他们将不得不选择另一个地方。有什么建议吗?
- (void) getChoice1
{
do
{
if (player == 1 && winner == 0 && turns < 9)
{
do {
//player 1's input
NSLog(@"Player 1, please choose a cell.");
NSLog(@"Enter row number(1, 2, or 3).");
scanf("%ld", &row);
} while ((row != 1 && row != 2 && row != 3));
do{
NSLog(@"Enter column number(1, 2, or 3).");
scanf("%ld", &column);
} while ((column != 1 && column != 2 && column != 3));
//decrement players input -1
row--;
column--;
//ensure spot is unselected
if (arr[row][column] == 1 || arr[row][column] == 2) {
NSLog(@"Invalid entry, try another spot");
[self getChoice1];
}
else
{
//add input to board
arr [row][column] = 1;
//display updated board
[self showBoard];
//switch players
player = 2;
//add a turn
turns++;
//check for winner
[self checkWinner];
}
}
} while (turns < 9 && winner == 0 );
}
答案 0 :(得分:0)
您必须为该块禁用userInteraction。
例如,如果触摸了block1,请执行:
block1.userInteractionEnabled = NO;
一旦游戏完成,请将所有块的userInteractionEnabled再次设置为YES。
这将起作用,因为我也制作了一个类似的游戏。
在命令行的情况下,您可以设置一些变量,例如,对于x set 1,对于0 set 2,对于no play set 0。
然后,一旦新人按下那里,检查1和2并执行所需的
答案 1 :(得分:0)
在朋友的帮助下,我意识到我必须完全重构这个方法。这是我的工作答案。
do
{
if (winner == 0 && turns < 9)
{
do {
//player 1's input
if (player == 1) {
NSLog(@"Player 1, please choose a cell.");
} else {
NSLog(@"Player 2, please choose a cell.");
}
NSLog(@"Enter row number(1, 2, or 3).");
scanf("%ld", &row);
} while ((row != 1 && row != 2 && row != 3));
do{
NSLog(@"Enter column number(1, 2, or 3).");
scanf("%ld", &column);
} while ((column != 1 && column != 2 && column != 3));
//decrement players input -1
row--;
column--;
//add input to board
if (arr [row][column] == 0) {
arr [row][column] = player;
if (player == 1) {
player = 2;
} else {
player = 1;
}
//display updated board
[self showBoard];
//add a turn
turns++;
} else {
NSLog (@"Cell already used pick a different cell.");
}
//check for winner
[self checkWinner];
}
} while (turns < 9 && winner == 0 );
}