所以我在一周前学会了编程。我决定做一个tic tac toe游戏作为一个爱好项目,我有一个基本的游戏输入方法启动和运行但一方面不起作用。我制作了一段非常简化的代码,以验证一旦有5个动作播放,是否有人赢了。一旦有人获胜,它应该显示“Game Over!”但它不是出于某种原因。任何人都可以提供帮助吗? _ __ _ __ _ __ _ ___ 注意:验证某人是否已获胜的方法在代码中未完成。它只适用于行,因为当我测试它时,它不起作用。
#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";
}
}
答案 0 :(得分:5)
在C ++中,==
符号不像代数类那样工作。表达式a == b == c
在C ++中具有非常不同的含义。在C ++中,它仅适用于项目对,并返回true
或false
值。 (见下文。)
要查看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]
进行比较。这将产生true
或false
布尔值(C ++ bool
),具体取决于它们是否相等。到目前为止,非常好。
比较后,它会将bool
结果与grid[0][2]
进行比较。由于grid[0][2]
是char
,因此会将bool
提升为数值。 true
变为1而false
变为0.您的网格值均不具有值0或1 - 它们都是'-'
,'X'
或'O'
- 所以第二次比较总是失败。
所有三行都重复相同的情况。
答案 1 :(得分:3)
根据我的理解,你不能写:
grid[0][0] == grid[0][1] == grid[0][2]
你应该:
grid[0][0] == grid[0][1] && grid[0][1]== grid[0][2]
在C ++中,这些是二元运算符,因此==
只能应用于两个变量,而不是三个变量。现在它将第一个结果(真/假)与第三个数字进行比较。
我的消息来源:
答案 2 :(得分:1)
如果您使用一个数组并对方框1到9进行编号,那么您的程序会小得多:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
阵列会这样:
const unsigned int BOARD_CAPACITY = 9;
unsigned int board[BOARD_CAPACITY + 1];
board
数组声明了一个额外的插槽,因此您可以安全地使用索引1到9。
单行检查:
bool row_wins = false;
if ((board[1] == board[2]) && (board[2] == board[3]))
{
row_wins = true;
}