这只是一个简单的tic tac toe程序,目前我无法自动在用户之间切换,因为我无法理解在哪里放置for循环
播放器和标志位于selectLocation()函数中。
void checkForWin(int);
char board[3][3] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int setTurn(int, char [][3], int, char);
int selectLocation(char [][3], int *, char*);
void displayBoard();
int main(void)
{
int choice;
int player;
int x;
int winner=0;
int round=0;
char sign;
do{
for(x = 0; x<9; ++x){
choice = selectLocation(board, &player, &sign);
winner = setTurn(choice, board, player, sign);
}
checkForWin(winner);
printf("Press 1 if you would like to play another round: ");
scanf("%d", &round);
}while(round == 1);
return 0;
}
void displayBoard(){
printf("\n TIC TAC TOE GAME");
printf("\nPlayer 1: X Player 2: O");
printf("\n %c\t%c\t%c\n %c\t%c\t%c\n %c\t%c\t%c\n",
board[0][0], board[0][1], board[0][2],
board[1][0], board[1][1], board[1][2],
board[2][0], board[2][1], board[2][2]);
}
int selectLocation(char board[3][3], int *playerP, char *signP){
int x=0;
char sign;
int player=0;
int choice=0;
玩家1(x)是唯一的一位玩家 我也在这里尝试了for循环,但是它没有改变结果
if (x % 2 == 0){
player = 1;
sign = 'x';
}else{
player = 2;
sign = 'o';
}
displayBoard();
printf("\nPlayer %d Turn, "
"Enter %c where you would like: ", player, sign);
scanf("%d", &choice);
*playerP = player;
*signP = sign;
return choice;
}