gpt4 book ai didi

c - 在 Tic-Tac-Toe 游戏中需要一些关于变量的帮助(初级编码员)

转载 作者:行者123 更新时间:2023-11-30 15:51:33 25 4
gpt4 key购买 nike

我在使用 C 语言创建的 Tic-Tac-Toe 游戏时遇到一些问题。

我有一个名为 int cats 的变量(猫游戏是平局游戏),用于帮助计算和显示平局游戏的文本。

我几乎整个星期都在尝试找出如何让比赛打平的方法。即使你们能给我指明正确的方向,那就太好了!

-弗伦丁

(P.S.开头的空if语句是针对玩家与电脑游戏的。如果我能弄清楚如何让游戏平局,我可以毫无问题地编写该部分!再次感谢大家!)

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

void printTheBoard(char boardPieces[3][3]);
int playerOne(char boardPieces[3][3], int cats);
int playerTwo(char boardPieces[3][3], int cats);
void checkBoard(char boardPieces[3][3], int cats);

int main(void)
{
char boardPieces[3][3] = {'\0'};
int cats = 0; //A tie game in Tic-Tac-Toe is called a Cats Game.
int choice;

printf("Would you like to play against the computer or another player?\n");
printf("Press 1 to play against another player.\n");
printf("Press 2 to play against the computer.\n");
scanf("%d", &choice);

if(choice == 1)
{
for(int i = 0; i < 5; ++i)
{
printTheBoard(boardPieces);
playerOne(boardPieces, cats);
checkBoard(boardPieces, cats);

printTheBoard(boardPieces);
playerTwo(boardPieces, cats);
checkBoard(boardPieces, cats);
}
}

if(choice == 2)
{

}

return 0;
}

void printTheBoard(char boardPieces[3][3])
{
printf("\n %c | %c | %c\n", boardPieces[0][0], boardPieces[0][1], boardPieces[0][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n", boardPieces[1][0], boardPieces[1][1], boardPieces[1][2]);
printf("---|---|---\n");
printf(" %c | %c | %c\n\n", boardPieces[2][0], boardPieces[2][1], boardPieces[2][2]);
}

int playerOne(char boardPieces[3][3], int cats)
{
int choice;

printf("It is your turn. You are player O. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);

if(choice == 1)
{
boardPieces[0][0] = 'O';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'O';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'O';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'O';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'O';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'O';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'O';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'O';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'O';
++cats;
return cats;
}
}

int playerTwo(char boardPieces[3][3], int cats)
{
int choice;

printf("It is your turn. You are player X. Please enter in the space you would like to take. The first space is 1, continuing left to right, top to bottom\n");
scanf("%d", &choice);

if(choice == 1)
{
boardPieces[0][0] = 'X';
++cats;
return cats;
}
else if(choice == 2)
{
boardPieces[0][1] = 'X';
++cats;
return cats;
}
else if(choice == 3)
{
boardPieces[0][2] = 'X';
++cats;
return cats;
}
else if(choice == 4)
{
boardPieces[1][0] = 'X';
++cats;
return cats;
}
else if(choice == 5)
{
boardPieces[1][1] = 'X';
++cats;
return cats;
}
else if(choice == 6)
{
boardPieces[1][2] = 'X';
++cats;
return cats;
}
else if(choice == 7)
{
boardPieces[2][0] = 'X';
++cats;
return cats;
}
else if(choice == 8)
{
boardPieces[2][1] = 'X';
++cats;
return cats;
}
else if(choice == 9)
{
boardPieces[2][2] = 'X';
++cats;
return cats;
}
}

void checkBoard(char boardPieces[3][3], int cats)
{
if(boardPieces[0][0] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[0][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'O')
{
if(boardPieces[1][1] == 'O')
{
if(boardPieces[2][0] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][2] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'O')
{
if(boardPieces[0][1] == 'O')
{
if(boardPieces[2][1] == 'O')
{
printf("Player O has won the game!\n");
}
}
else if(boardPieces[1][0] == 'O')
{
if(boardPieces[1][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'O')
{
if(boardPieces[2][1] == 'O')
{
if(boardPieces[2][2] == 'O')
{
printf("Player O has won the game!\n");
}
}
}




if(boardPieces[0][0] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[0][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[0][2] == 'X')
{
if(boardPieces[1][1] == 'X')
{
if(boardPieces[2][0] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][2] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[1][1] == 'X')
{
if(boardPieces[0][1] == 'X')
{
if(boardPieces[2][1] == 'X')
{
printf("Player X has won the game!\n");
}
}
else if(boardPieces[1][0] == 'X')
{
if(boardPieces[1][2] == 'X')
{
printf("Player X has won the game!\n");
}
}
}
else if(boardPieces[2][0] == 'X')
{
if(boardPieces[2][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player O has won the game!\n");
}
}
}



else if(cats == 9)
{
printf("There was a tie game!\n");
}
}

最佳答案

在函数 playerOne()playerTwo() 中,您递增声明为函数参数的变量 cats。您应该将该值存储在您在 main 开头声明的 cats 变量中,例如:

for(int i = 0; i < 5; ++i)
{
printTheBoard(boardPieces);
cats = playerOne(boardPieces, cats);
checkBoard(boardPieces, cats);

printTheBoard(boardPieces);
cats = playerTwo(boardPieces, cats);
checkBoard(boardPieces, cats);
}

更详细的解释如下。考虑以下代码:

void func(int var)
{
var = 2;
}

int main(void)
{
int var;
var = 1;
func(var);
}

这里有两个不同的变量,其名称为 var 其中一个在 main 的开头声明,其作用域或生命周期在 main 函数内。

另一个被声明为 func 的参数,其作用域是 func 函数的本地范围。 这意味着当函数返回时,变量及其值将不再存在。 func 函数返回。

现在,如果我们想在函数返回后使用 func 的局部变量的值,我们可以:

  • 返回func修改的值:

    int func(int var)
    {
    var++;
    return var;
    }
    ...
    new_var = func(old_var);
  • func 通过指针就地修改变量:

    void func(int *var)
    {
    (*var)++;
    }
    ...
    func(&var);
<小时/>

此外,在 checkBoard() 末尾打印平局游戏消息的 else if 条件永远不会在您当前的代码中运行:

...
else if(boardPieces[2][0] == 'X')
{
if(boardPieces[2][1] == 'X')
{
if(boardPieces[2][2] == 'X')
{
printf("Player O has won the game!\n");
}
}
}

else if(cats == 9)
{
printf("There was a tie game!\n");
}

考虑在打印游戏结果后从 checkBoard() 函数返回。也许可以考虑返回一个值来指示游戏是否已经结束,这样您就可以在发生这种情况时结束程序。

关于c - 在 Tic-Tac-Toe 游戏中需要一些关于变量的帮助(初级编码员),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15035196/

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