gpt4 book ai didi

c - 井字游戏中的显示板

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

我目前正在编写一个简单的井字游戏,事实证明它并不那么简单(至少对我来说,我是一个初学者)。我在编写应该打印如下内容的 display_board 函数时遇到问题:

_|_|_
_|_|_
| |

并在玩家将棋盘标记到特定位置时添加 X 或 O。我打算做的是将所有这些都放入一个字符串中,但这有点令人困惑,尤其是对于我想添加的新行,以便电路板正确显示。换行符算作字符串中的一个或两个字符吗?如果你想看我的游戏代码,这里是:

#include <stdio.h>
#include <stdbool.h>

int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};

int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[];

while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", &move);

if(valid_location(move)) {
mark_location(user1, move);
display_board();
else if(won_the_game(user1) {
printf("Congratulations User-1, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
printf("User-2, please enter your move:");
scanf("%s", &move);

if(valid_location(move)) {
mark_location(user2, move);
display_board();
else if(won_the_game(user2) {
printf("Congratulations User-2, You Won the Game!");
break;
}
else {
printf("Invalid Move");
}
}
}

bool valid_location(char str[]) {
if (str[] == "upperLeft" || str[] == "up" || str[] == "upperRight" || str[] == "left" || str[] == "center" || str[] == "right" || str[] == "lowerLeft" || str[] == "down" || str[] == "lowerRight") {
return true;
}
}

void mark_location(int userU, char str[]) {
if (str[] == "upperLeft") {
board[0][0] = userU;
else if (str[] == "up") {
board[0][1] = userU;
else if (str[] == "upperRight") {
board[0][2] = userU;
else if (str[] == "left") {
board[1][0] = userU;
else if (str[] == "center") {
board[1][1] = userU;
else if (str[] == "right") {
board[1][2] = userU;
else if (str[] == "lowerLeft") {
board[2][0] = userU;
else if (str[] == "down") {
board[2][1] = userU;
else if (str[] == "lowerRight") {
board [2][2] = userU;
}
}

有点乱,正如我所说,我也是新手。如果您有任何清理建议,请随时帮助我。

最佳答案

您的代码中有很多错误。这里有一些:

在C语言中使用strcmp函数比较字符串

str[] == "upperLeft"

不是有效的 C 表达式。

还有这个定义:

char move[];

不是有效的数组定义,它缺少 [] 之间的元素数。

此外

scanf("%s", &move);

%s 转换规范将指向 char 的指针作为参数。 &move 的值是指向数组的指针,而不是指向 char 的指针。以这种方式调用函数:

scanf("%s", move);

关于c - 井字游戏中的显示板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8857161/

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