包含整数值的C++字符数组

C++ character array holding integer values

本文关键字:字符 数组 C++ 整数 包含      更新时间:2023-10-16

我正在尝试创建一个包含数字的动态字符数组,然后将其更改为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作为表示玩家移动的值,而不是OX,但你仍然会被限制为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]包含一个指向数组的指针。