gpt4 book ai didi

c - c中的井字游戏,试图确定谁赢了

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:15 25 4
gpt4 key购买 nike

试图确定谁在井字游戏中获胜,我是编程新手。目前让用户在仅输入 1 个 X 或 O 输入后就赢了。输入必须使用 2 个整数、行和列输入。非常感谢任何帮助!

#include <stdio.h>
#include <stdlib.h>

void drawBoard(char board[][3])
{
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 3 ; columns++ )
{
if(board[rows][columns]){
printf( "|%c", board[rows][columns] );
}else{
printf("| ");
}
}
printf("|\n");
}
}


int main()
{
char game[3][3]={{0}};
int totalEntry =0,row,column;
char value;

while(totalEntry<=9){
printf("Please choose x or o: ");
scanf("%c",&value);
getchar();
printf("Enter row number: ");
scanf("%d",&row);
getchar();
printf("Enter Column number: ");
scanf("%d",&column);
getchar();
game[row][column] = value;
drawBoard(game);

if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'x')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'x')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'x')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'x')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'x')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'x')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'x')
printf("User x has won!");


if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'o')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'o')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'o')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'o')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'o')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'o')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'o')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'o');
printf("User o has won!");
break;


}




return 0;
}

最佳答案

我会使用像下面这样的东西,使用循环。请注意,由于棋盘被初始化为零,因此将获胜者分配到仍然为零的棋盘位置意味着即使测试所有相等的通过也找不到获胜者。

//declare this outside the main while-loop:
char winner=0;

for(i=0;i<3 && !winner;i++) {
if(game[i][0]==game[i][1] && game[i][1]==game[i][2])
winner=game[i][0]; // across
else if(game[0][i]==game[1][i] && game[1][i]==game[2][i])
winner=game[0][i]; // down
}
if( !winner && ( game[0][0]==game[1][1] && game[1][1]==game[2][2] ||
game[0][2]==game[1][1] && game[1][1]==game[2][0] ) )
winner = game[1][1]; // diagonal

if(winner) {
printf( "Winner is %c!\n",winner );
break;
}

关于c - c中的井字游戏,试图确定谁赢了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25024153/

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