我用C语言创建了一个简单的Tic Tac Toe程序。除了我的check_for_win函数外,其他所有东西都正常运行(仍然需要一些清理)。我不确定是否在正确的位置即主函数中声明它。我将其切换到player_input函数,因为从逻辑上讲,这对我来说更有意义,即在每个玩家移动之后检查是否有获胜,但仍然无济于事。
这是我的语法:
#include <stdio.h>
#include <stdlib.h>
void menu()
{
int choice;
do
{
printf("Main Menu\n\n");
printf(" 1. Play Game\n");
printf(" 2. Quit Game\n");
scanf("%d",&choice);
switch(choice)
{
case 1: board();
break;
case 2: printf("Quitting program!\n");
exit(0);
break;
default: printf("Invalid choice!\n");
break;
}
} while (choice != 1);
}
int board() {
printf("\n\n");
printf(" 1 | 2 | 3 \n");
printf("--------------\n");
printf(" 4 | 5 | 6 \n");
printf("--------------\n");
printf(" 7 | 8 | 9 \n");
}
/*int display() {
printf("// 1 | 2 | 3\n");
printf("// ---------\n");
printf("// 4 | 5 | 6\n");
printf("// ---------\n");
printf("// 7 | 8 | 9\n\n\n");
}
int gameboard(int board[9]) {
printf("// %d | %d | %d \n", board[0], board[1], board[2]);
printf("// ---------\n");
printf("// %d | %d | %d \n", board[3], board[4], board[5]);
printf("// ---------\n");
printf("// %d | %d | %d \n\n\n", board[6], board[7], board[8]);
}*/
void player_input(char gameboard[])
{
int i;
int playerX, playerO;
for (i = 0; i <= 9; i++)
{
printf("\nPlayer 1, please select a square by entering a number between [1 - 9]:\n\n");
scanf("%d", &playerX);
///playerX = playerX - 1;
display_board(gameboard, playerX, playerO);
printf("\nPlayer 2, please select a square by entering a number between [1 - 9]:\n");
scanf("%d", &playerO);
///playerO = playerO - 1;
display_board(gameboard, playerX, playerO);
}
return(playerX);
return(playerO);
}
void display_board(char gameboard[], int playerX, int playerO) {
///INPUT FOR PLAYER O
if(playerO == 1)
{
gameboard[0] = 'O';
}
else if(playerO == 2)
{
gameboard[1] = 'O';
}
else if(playerO == 3)
{
gameboard[2] = 'O';
}
else if(playerO == 4)
{
gameboard[3] = 'O';
}
else if(playerO == 5)
{
gameboard[4] = 'O';
}
else if(playerO == 6)
{
gameboard[5] = 'O';
}
else if(playerO == 7)
{
gameboard[6] = 'O';
}
else if(playerO == 8)
{
gameboard[7] = 'O';
}
else if(playerO == 9)
{
gameboard[8] = 'O';
}
///INPUT FOR PLAYER X
if(playerX == 1)
{
gameboard[0] = 'X';
}
else if(playerX == 2)
{
gameboard[1] = 'X';
}
else if(playerX == 3)
{
gameboard[2] = 'X';
}
else if(playerX == 4)
{
gameboard[3] = 'X';
}
else if(playerX == 5)
{
gameboard[4] = 'X';
}
else if(playerX == 6)
{
gameboard[5] = 'X';
}
else if(playerX == 7)
{
gameboard[6] = 'X';
}
else if(playerX == 8)
{
gameboard[7] = 'X';
}
else if(playerX == 9)
{
gameboard[8] = 'X';
}
printf("\n\n");
printf(" %c | %c | %c \n", gameboard[0], gameboard[1], gameboard[2]);
printf("--------------\n");
printf(" %c | %c | %c \n", gameboard[3], gameboard[4], gameboard[5]);
printf("--------------\n");
printf(" %c | %c | %c \n", gameboard[6], gameboard[7], gameboard[8]);
}
void check_for_win(char gameboard[])
{
if (gameboard[0] == gameboard[1] == gameboard[2] == 'X')
{
printf("Player X has WON!");
}
else if (gameboard[3]==gameboard[4]==gameboard[5]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[6]==gameboard[7]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[3]==gameboard[6]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[1]==gameboard[4]==gameboard[7]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[2]==gameboard[5]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[4]==gameboard[8]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[2]==gameboard[4]==gameboard[6]=='X')
{
printf("Player X has WON!");
}
else if (gameboard[0]==gameboard[1]==gameboard[2]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[3]==gameboard[4]==gameboard[5]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[6]==gameboard[7]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[0]==gameboard[3]==gameboard[6]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[1]==gameboard[4]==gameboard[7]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[2]==gameboard[5]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[0]==gameboard[4]==gameboard[8]=='O')
{
printf("Player O has WON!");
}
else if (gameboard[2]==gameboard[4]==gameboard[6]=='O')
{
printf("Player O has WON!");
}
else
{
printf("THE GAME HAS COME TO A DRAW.");
}
}
int welcome() {
printf("ELEC 1520 Programming Assignment 1\n");
printf("Programmer: Anonymous\n\n");
printf("Press the enter key to start playing....\n");
char enter = 0;
while (enter != '\r' && enter != '\n') {
enter = getchar();
}
}
int main () {
char gameboard[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
welcome();
menu();
///player_input();
///process_input(board);
int playerX=0;
int playerO=0;
display_board(gameboard, playerX, playerO);
printf("\n\nRULES\n");
printf("Player 1 is X and goes first.\n");
printf("Player 2 is O.\n");
printf("Please select a square by choosing a number between [1 - 9]:\n\n");
player_input(gameboard);
check_for_win(gameboard);
return 0;
}
最佳答案
if (gameboard[0] == gameboard[1] == gameboard[2] == 'X')
不按照您的期望去做。由于运算符的优先级,它被评估为
if ( ((gameboard[0] == gameboard[1]) == gameboard[2]) == 'X')
不管
gameboard[0]
,gameboard[1]
和gameboard[2]
的值是什么,表达式((gameboard[0] == gameboard[1]) == gameboard[2]) == 'X'
始终将计算为false,因为((gameboard[0] == gameboard[1]) == gameboard[2])
为true(1)或false(0)。换句话说,上面的
if
等效于:if (0)
要修正您的逻辑,请使用
if (gameboard[0] == 'X' &&
gameboard[1] == 'X' &&
gameboard[2] == 'X')
类似地更新其他
if/else if
语句。