gpt4 book ai didi

c++ - Tic Tac Toe 游戏无法运行

转载 作者:太空宇宙 更新时间:2023-11-04 14:54:52 24 4
gpt4 key购买 nike

所以我大约一周前学习了编程。我决定制作一个井字游戏作为一个业余爱好项目,我启动并运行了一个基本的游戏输入法,但有一个方面不起作用。我做了一段非常简化的代码来验证是否有人在玩了 5 步之后就赢了。一旦有人赢了,它就应该显示“游戏结束!”但它不是出于某种原因。任何人都可以帮忙吗?_____________ 注意:代码中验证某人是否获胜的方法尚未完成。它仅适用于行,因为当我测试它时,它不起作用。

#include <iostream>
#include <cstdlib>

using namespace std;
int main()
{
char grid[3][3];
int p;
cout << "Enter column number. \n";
cin >> p;
int o;
cout << "Enter row number. \n";
cin >> o;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
cout << grid[x][y] << " ";
}
cout << endl;
}
int q;
cout << "Enter column number. \n";
cin >> q;
int r;
cout << "Enter row number. \n";
cin >> r;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
cout << grid[x][y] << " ";
}
cout << endl;
}
int s;
cout << "Enter column number. \n";
cin >> s;
int t;
cout << "Enter row number. \n";
cin >> t;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
cout << grid[x][y] << " ";
}
cout << endl;
}
int v;
cout << "Enter column number. \n";
cin >> v;
int b;
cout << "Enter row number. \n";
cin >> b;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
cout << grid[x][y] << " ";
}
cout << endl;
}
int f;
cout << "Enter column number. \n";
cin >> f;
int g;
cout << "Enter row number. \n";
cin >> g;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
grid[g][f] = 'X';
cout << grid[x][y] << " ";
}
cout << endl;
}
if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
{
cout << "Game Over! \n\n";
}
int i;
cout << "Enter column number. \n";
cin >> i;
int u;
cout << "Enter row number. \n";
cin >> u;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
grid[g][f] = 'X';
grid[u][i] = 'O';
cout << grid[x][y] << " ";
}
cout << endl;
}
if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
{
cout << "Game Over! \n\n";
}
int a1;
cout << "Enter column number. \n";
cin >> a1;
int e1;
cout << "Enter row number. \n";
cin >> e1;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
grid[g][f] = 'X';
grid[u][i] = 'O';
grid[e1][a1] = 'X';
cout << grid[x][y] << " ";
}
cout << endl;
}
if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
{
cout << "Game Over! \n\n";
}
int a8;
cout << "Enter column number. \n";
cin >> a8;
int b8;
cout << "Enter row number. \n";
cin >> b8;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
grid[g][f] = 'X';
grid[u][i] = 'O';
grid[e1][a1] = 'X';
grid[b8][a8] = 'O';
cout << grid[x][y] << " ";
}
cout << endl;
}
if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
{
cout << "Game Over! \n\n";
}
int a9;
cout << "Enter column number. \n";
cin >> a9;
int b9;
cout << "Enter row number. \n";
cin >> b9;
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
grid[x][y] = '-';
grid[o][p] = 'X';
grid[r][q] = 'O';
grid[t][s] = 'X';
grid[b][v] = 'O';
grid[g][f] = 'X';
grid[u][i] = 'O';
grid[e1][a1] = 'X';
grid[b8][a8] = 'O';
grid[b9][a9] = 'X';
cout << grid[x][y] << " ";
}
cout << endl;
}
if (grid[0][0] == grid[0][1] == grid[0][2] || grid[1][0] == grid[1][1] == grid[1][2] || grid[2][0] == grid[2][1] == grid[2][2])
{
cout << "Game Over! \n\n";
}
}

最佳答案

在 C++ 中,== 符号的作用与它在代数类中的作用不同。表达式 a == b == c 在 C++ 中具有非常不同的含义。在 C++ 中,它仅适用于成对的项,并返回 truefalse 值。 (更多信息见下文。)

要查看 3 个事物是否彼此相等,您需要说 (a == b && b == c)。您的网格检查需要如下所示:

if ( (grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2]) || 
(grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2]) ||
(grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2]) )

您还会注意到我在其中放置了额外的括号。它们不是严格必需的,但是当您在同一表达式中混合使用 &&|| 时,一些编译器会发出警告。括号清楚地表明您打算将哪些项目组合在一起,并将消除这些警告。


那么 C++ 编译器对您的代码做了什么?您已经编写了 grid[0][0] == grid[0][1] == grid[0][2]。根据 its operator precedence rules.,编译器将其解释为 (grid[0][0] == grid[0][1]) == grid[0][2]

编译器首先会比较grid[0][0]grid[0][1]。这将产生一个 truefalse bool 值 (C++ bool),具体取决于它们是否相等。到目前为止,还不错。

比较后,它会将 bool 结果与 grid[0][2] 进行比较。由于 grid[0][2] 是一个 char,它会将 bool 提升为一个数值。 true 变为 1,false 变为 0。您的网格值都没有值 0 或 1 — 它们都是 '-''X''O'——所以第二次比较总是失败。

所有三行重复相同的情况。

关于c++ - Tic Tac Toe 游戏无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20732544/

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