二维数组不会在控制台网格中显示正确的内容
2D array won't display correct contents in console grid
我正在使用 C++ 开发一个简单的基于文本的 Battle Ship 游戏。我目前正在尝试在控制台中正确显示 grid/board。我的格式正确,但我发现二维数组的元素不正确。
下面是一个例子。 我已将二维网格中的所有元素设置为 Z,但出于某种原因它们都显示为 Y。为什么要更改变量?
#include <iostream>
using namespace std;
enum Grid {X, Y, Z};
const int GRID_SIZE = 10;
Grid grid[GRID_SIZE][GRID_SIZE] = {Z};
void displayGrid();
int main()
{
displayGrid();
cin.get();
return 0;
}
void displayGrid()
{
// Display top column of the grid, containing numbers.
cout << "\t |";
for (int i = 0; i < GRID_SIZE; i++)
cout << i << "|";
cout << endl;
Grid gridContent;
for (int y = 0; y < GRID_SIZE; y++)
{
cout << "\t" << y << "|";
for (int x = 0; x < GRID_SIZE; x++)
{
gridContent = grid[y][x];
if (gridContent = X)
cout << "X|";
else if (gridContent = Y)
cout << "Y|";
else if (gridContent = Z)
cout << "Z|";
}
cout << "\n";
}
}
第一个:
Grid grid[GRID_SIZE][GRID_SIZE] = {Z}
只用 Z
初始化数组 grid
的第一个元素(其余元素为 0,参见 aggregate initialization)。您需要在 main
内嵌套循环,将 所有 元素初始化为 Z
,例如
for(int i = 0; i < GRID_SIZE; ++i)
for(int j = 0; j < GRID_SIZE; ++j)
grid[i][j] = Z;
第二:
if (gridContent = X)
将 gridContent
设置为 X
(此错误也发生在其他 if
中)。要测试相等性,需要使用 ==
代替。
第三:如果你真的想明白为什么之前显示Y
,那是因为
中的条件
if(gridContent = X)
计算为 false
,因为 X
转换为 0
,然后分配给 gridContent
。因此,程序进入另一个
if(gridContent = Y)
其中它将 gridContent
设置为 Y
,并且由于后者非零,因此 if
条件计算为 true
。您在循环中执行此操作,因此最终将所有元素显示为 Y
。不是你想要的。
最佳实践
避免这些错误的一种方法是在编译时打开所有警告。比如g++ -Wall -Wextra test.cpp
吐出
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (gridContent = X)
clang++ 更有用
warning: using the result of an assignment as a condition
without parentheses [-Wparentheses]
所以你肯定知道出问题了。
另一种方法是始终将 rvalue 放在相等测试的左侧,例如
if(X == gridContent)
这里的X
是一个右值,如果你不小心输入了=
而不是==
,那么编译器就会报错,比如
error: lvalue required as left operand of assignment
因为您不能分配给右值。
最后,尝试使用标准容器而不是原始数组,例如 std::vector<>
。
我正在使用 C++ 开发一个简单的基于文本的 Battle Ship 游戏。我目前正在尝试在控制台中正确显示 grid/board。我的格式正确,但我发现二维数组的元素不正确。 下面是一个例子。 我已将二维网格中的所有元素设置为 Z,但出于某种原因它们都显示为 Y。为什么要更改变量?
#include <iostream>
using namespace std;
enum Grid {X, Y, Z};
const int GRID_SIZE = 10;
Grid grid[GRID_SIZE][GRID_SIZE] = {Z};
void displayGrid();
int main()
{
displayGrid();
cin.get();
return 0;
}
void displayGrid()
{
// Display top column of the grid, containing numbers.
cout << "\t |";
for (int i = 0; i < GRID_SIZE; i++)
cout << i << "|";
cout << endl;
Grid gridContent;
for (int y = 0; y < GRID_SIZE; y++)
{
cout << "\t" << y << "|";
for (int x = 0; x < GRID_SIZE; x++)
{
gridContent = grid[y][x];
if (gridContent = X)
cout << "X|";
else if (gridContent = Y)
cout << "Y|";
else if (gridContent = Z)
cout << "Z|";
}
cout << "\n";
}
}
第一个:
Grid grid[GRID_SIZE][GRID_SIZE] = {Z}
只用 Z
初始化数组 grid
的第一个元素(其余元素为 0,参见 aggregate initialization)。您需要在 main
内嵌套循环,将 所有 元素初始化为 Z
,例如
for(int i = 0; i < GRID_SIZE; ++i)
for(int j = 0; j < GRID_SIZE; ++j)
grid[i][j] = Z;
第二:
if (gridContent = X)
将 gridContent
设置为 X
(此错误也发生在其他 if
中)。要测试相等性,需要使用 ==
代替。
第三:如果你真的想明白为什么之前显示Y
,那是因为
if(gridContent = X)
计算为 false
,因为 X
转换为 0
,然后分配给 gridContent
。因此,程序进入另一个
if(gridContent = Y)
其中它将 gridContent
设置为 Y
,并且由于后者非零,因此 if
条件计算为 true
。您在循环中执行此操作,因此最终将所有元素显示为 Y
。不是你想要的。
最佳实践
避免这些错误的一种方法是在编译时打开所有警告。比如
g++ -Wall -Wextra test.cpp
吐出warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (gridContent = X)
clang++ 更有用
warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
所以你肯定知道出问题了。
另一种方法是始终将 rvalue 放在相等测试的左侧,例如
if(X == gridContent)
这里的
X
是一个右值,如果你不小心输入了=
而不是==
,那么编译器就会报错,比如error: lvalue required as left operand of assignment
因为您不能分配给右值。
最后,尝试使用标准容器而不是原始数组,例如
std::vector<>
。