gpt4 book ai didi

c - Tic Tac Toe 一维数组和我的 'Check_for_win' 函数

转载 作者:行者123 更新时间:2023-11-30 20:37:16 25 4
gpt4 key购买 nike

我用 C 创建了一个简单的 Tic Tac Toe 程序。除了 check_for_win 函数之外,一切正常(仍然需要一些清理)。我不确定我是否在正确的位置(即 main 函数)声明它。我将其切换到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 语句。

关于c - Tic Tac Toe 一维数组和我的 'Check_for_win' 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com