[英]Tic Tac Toe game won't work
So I learnt programming about a week ago. 因此,大约一周前我学习了编程。 I decided to make a tic tac toe game as a hobby project, I got a rudimentary game input method up and running but one aspect doesn't work.
我决定制作一个井字游戏作为一项业余爱好项目,我启动并运行了基本的游戏输入法,但其中一个方面不起作用。 I made a very simplified piece of code to verify if somebody has won once 5 moves have been played.
我编写了一段非常简单的代码,以验证有人打5步棋是否赢了。 Once somebody wins, it's supposed to display "Game Over!"
一旦有人获胜,就应该显示“ Game Over!”。 but it doesn't for some reason.
但这不是出于某种原因。 Can anyone help?
有人可以帮忙吗? _ __ _ __ _ __ _ ___ Note: The method to verify if somebody has won yet isn't complete in the code.
_ __ _ __ _ __ _ ___ 注意:代码中验证人是否成功的方法还不完整。 It is only for rows because when I tested it, it didn't work.
它仅用于行,因为当我对其进行测试时,它不起作用。
#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";
}
}
In C++, the ==
sign doesn't work the way it does in algebra class. 在C ++中,
==
符号不能像代数类那样工作。 The expression a == b == c
has a very different meaning in C++. 在C ++中,表达式
a == b == c
具有非常不同的含义。 In C++, it only works on pairs of items, and returns a true
or false
value. 在C ++中,它仅适用于成对的项,并返回
true
或false
值。 (See more below.) (请参阅下文。)
To see if 3 things all equal each other, you need to say (a == b && b == c)
. 要查看3个事物是否彼此相等,您需要说
(a == b && b == c)
。 Your grid check would need to look like this: 您的网格检查将需要如下所示:
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]) )
You'll also notice I put extra parentheses in there. 您还会注意到我在其中加了多余的括号。 They are not strictly necessary, but some compilers warn when you mix
&&
and ||
它们不是严格必需的,但是某些编译器会在混合
&&
和||
时发出警告。 in the same expression. 在相同的表达中。 The parentheses make it clear which items you intend to group together and will quell those warnings.
括号使您可以将要分组的项目明确,并将平息这些警告。
So what was the C++ compiler doing with your code? 那么,什么是 C ++编译器与您的代码在做什么? You had written
grid[0][0] == grid[0][1] == grid[0][2]
. 您已经编写了
grid[0][0] == grid[0][1] == grid[0][2]
。 The compiler interprets that as (grid[0][0] == grid[0][1]) == grid[0][2]
according to its operator precedence rules. 编译器根据其运算符优先级规则将其解释为
(grid[0][0] == grid[0][1]) == grid[0][2]
。
The compiler will first compare grid[0][0]
to grid[0][1]
. 编译器将首先比较
grid[0][0]
与grid[0][1]
。 This will produce a true
or false
boolean value (C++ bool
) depending on whether they were equal. 根据它们是否相等,这将产生一个
true
或false
布尔值(C ++ bool
)。 So far, so good. 到现在为止还挺好。
After the comparison, it will compare that bool
result against grid[0][2]
. 比较之后,它将
bool
结果与grid[0][2]
。 Since grid[0][2]
is a char
, it promotes the bool
to a numeric value. 由于
grid[0][2]
是一个char
,它将bool
值提升为数值。 true
becomes 1 and false
becomes 0. None of your grid values have the value 0 or 1—they are all either '-'
, 'X'
or 'O'
—so the second comparison always fails. true
变为1, false
变为0。所有网格值都不具有值0或1-它们全都是'-'
, 'X'
或'O'
,因此第二次比较总是失败。
The same situation repeats for all three rows. 对于所有三行重复相同的情况。
For my understanding you cannot write: 据我了解,您不能写:
grid[0][0] == grid[0][1] == grid[0][2]
You should have: 你应该有:
grid[0][0] == grid[0][1] && grid[0][1]== grid[0][2]
In C++ these are binary operators, and thus ==
can only be applied to two variables, not chained to three. 在C ++中,它们是二进制运算符,因此
==
只能应用于两个变量,不能链接到三个变量。 Now it compares first result (true/false) to the third number. 现在,它将第一个结果(是/否)与第三个数字进行比较。
My sources: 我的资料来源:
Your program would be a lot smaller if you used one array and number the boxes 1 through 9: 如果您使用一个数组并将框1到9编号,则程序将小很多:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
The array would like this: 该数组将如下所示:
const unsigned int BOARD_CAPACITY = 9;
unsigned int board[BOARD_CAPACITY + 1];
The board
array is declared with one extra slot so you can safely use indices 1 through 9. board
数组声明有一个额外的插槽,因此您可以安全地使用索引1到9。
A single row check: 单行检查:
bool row_wins = false;
if ((board[1] == board[2]) && (board[2] == board[3]))
{
row_wins = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.