包含整数值的C++字符数组
C++ character array holding integer values
我正在尝试创建一个包含数字的动态字符数组,然后将其更改为X或0,
我正在创建一个井字游戏,它创建一个网格到你想要的特定数字,数组需要包含正方形的所有数字,然后根据玩家的不同将这个数字更改为X,0,
我目前有;
const int grid_size = size * size; // determines the size of the grid height x width
char **number = new char*[grid_size]; // create the array to the size of the grid
for ( int i = 1; i <= grid_size+1; i++ ) {
number[i] = i;
}
但我得到了一个错误:-error C2440:"=":无法在行号[I]=I上从"int"转换为"char*";
我该如何创建包含数字-1、grid_size的数组,并将这些数字更改为X、0?
我认为您不必在数组中存储单元格独立项。你只是用它们来打印当前网格给用户,对吧?这个解决方案怎么样:
enum class Marker { PlayerX, PlayerO, None };
class TicTacToeGrid
{
public:
TicTacToeGrid(size_t size) :
grid(size* size, Marker::None), size(size)
{}
void Set(size_t cellId, Marker player)
{
if (cellId < 1 || cellId >= size * size)
throw std::runtime_error("invalid cell");
if (grid[cellId - 1] != Marker::None)
throw std::runtime_error("cell is not free");
grid[cellId - 1] = player;
}
Marker Get(size_t cellId) const
{
return grid[cellId - 1];
}
void Print(std::ostream& os) const
{
for(size_t row = 0; row < size; ++row)
{
for (size_t col = 0; col < size; ++col)
{
size_t index = size * row + col;
os << CellContent(index) << "t";
}
os << "n";
}
}
std::string CellContent(size_t index) const
{
Marker marker = grid[index];
switch(marker)
{
case Marker::PlayerX:
return "X";
case Marker::PlayerO:
return "O";
case Marker::None:
// If it's not X or O let's show the cell index.
return std::to_string(index + 1);
}
}
private:
std::vector<Marker> grid;
size_t size;
};
int main()
{
TicTacToeGrid grid(3);
grid.Print(std::cout);
grid.Set(1, Marker::PlayerO);
grid.Set(2, Marker::PlayerX);
grid.Print(std::cout);
}
您有两种解决方案:创建一个数组来存储字符(这就是矩阵的本质(,或者创建一个char数组,它将是矩阵的线性化版本,即一个包含矩阵所有行的数组。
第一种情况很琐碎:
const int grid_size = size * size; // determines the size of the grid height x width
char **number = new char*[size]; // create the array to the size of the grid
for ( int i = 0; i < size; i++ ) {
number[i] = new char[grid_size];
for( int j = 0; j < size; j++)
number[i][j] = i * size + j;
}
// access any char with a double bracket notation
// beware that coordinates start at `0`
number[2][1] = 'X';
第二种解决方案比较棘手,我建议使用辅助函数将矩阵坐标转换为数组索引:
const int grid_size = size * size;
int idx(int x, int y){
return y * size + x;
}
char *number = new char[grid_size];
for (int i = 0; i < grid_size; i++)
number[i] = i;
number[idx(2,1)] = 'O';
字符实际上是伪装的数字,但其值远高于9
,所以对于一个普通的井字游戏来说,这应该不是问题,而且你应该能够让它在8*8的大小下工作。从65开始,数字开始表示正则字母字符(即65是A
,66是B
,依此类推,请查阅ASCII代码表了解更多详细信息(。如果你想在单元格中存储单元格编号并跟踪游戏状态,解决这个问题的一种方法是使用254和255作为表示玩家移动的值,而不是O
和X
,但你仍然会被限制为7*7的网格大小。
对于较大的大小,您可能需要考虑另一个数据结构来跟踪游戏状态。
char **number = new char*[size]; // create the rows
for ( int i = 0; i < size; i++ ) {
number[i] = new char[size]; // create the columns for the current row
for (int j = 0; j < size; j++) {
number[i][j] = (char)i*j; // set the cell number
}
}
请注意,我已经很久没有用c++编码了,所以我可能会遇到一些语法问题。但这就是方法。
首先,如果您需要一个数字数组,您应该使用int* number = new int[grid_size]
,而绝对不能使用char**
(如果您存储字符串的话(。
其次,即使您希望数字从1到grid_size
,存储它们的数组索引也应该在0..grid_size-1
范围内,否则您将访问不存在的元素。
const int grid_size = size * size; // determines the size of the grid height x width
int *number = new int*[grid_size]; // create the array to the size of the grid
for ( int i = 0; i < grid_size; i++ ) {
number[i] = i + 1;
}
正确的方法是。。。。
#define Row_num 3
#define Col_num 3
char **number = new char*[Row_num];
for(int i=0;i<Row_num;i++){
number[i] = new char[Col_num];
for(int j=0;j<Col_num;j++){
number[i][j] = j;
}
}
通过这条线路
char **number = new char*[Row_num];
您实际上创建了一个指针数组。所以你不能做这个作业。。。
number[i] = i;
由于数字[i]包含一个指向数组的指针。
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?