我正在尝试创建一个包含数字的动态字符数组,然后将它们更改为X或0,
我正在创建一个tic tac toe游戏,它为你想要的特定数字创建一个网格,数组需要包含方块的所有数字,然后将这个数字改为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;
}
但有了这个我得到一个错误: - 错误C2440:'=':无法在行号[i] = i上从'int'转换为'char *';
如何创建包含数字的数组 - 1,grid_size,将这些数字更改为X,0?
答案 0 :(得分:2)
我认为您不必将单元格存储在数组中。您只需使用它们将当前网格打印给用户,对吗?这个解决方案怎么样:
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);
}
答案 1 :(得分:0)
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 ++编码,所以我可能会遇到一些语法问题。但这是做到这一点的方法。
答案 2 :(得分:0)
正确的方法是......
#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;
由于number [i]持有指向数组的指针。
答案 3 :(得分:0)
首先,如果你需要一个数字数组,你应该使用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;
}
答案 4 :(得分:0)
你有两个解决方案:创建一个数组数组来存储你的角色(这就是矩阵基本上是什么),或者创建一个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网格尺寸。
对于较大的尺寸,您可能需要考虑另一种数据结构来跟踪游戏状态。