在C++中动态创建三维数组或二维数组



我目前正在尝试制作一个程序,该程序将生成一个导出到游戏中的迷宫。该程序将使用用户输入来设置迷宫的一些属性。我希望其中一个选择是迷宫只有两个维度(单层(,还是三个维度(两层或多层(。为了实现这一点,我在Maze类中动态分配一个数组,如下所示:

在迷宫中。hpp:

class Maze {
private:
unsigned int width_, length_, height_;
Cell*** matrix = nullptr;
};

在Maze.cpp中:

Maze::Maze() {           // Default constructor
width_ = 20;
length_ = 20;
height_ = 0;
matrix = new Cell**[width_];
for (unsigned int x {}; x < width_; ++x) {
matrix[x] = new Cell*[length_];
for (unsigned int y {}; y < length_; ++y) {
matrix[x][y] = new Cell(x, y);
}
}
}
Maze::Maze(int width, int length) {           // 2D maze constructor
width_ = width;
length_ = length;
height_ = 0;
matrix = new Cell**[width_];
for (unsigned int x {}; x < width_; ++x) {
matrix[x] = new Cell*[length_];
for (unsigned int y {}; y < length_; ++y) {
matrix[x][y] = new Cell(x, y);
}
}
}
Maze::Maze(int width, int length, int height) {    // 3D maze constructor
width_ = width;
length_ = length;
height_ = height;
matrix = new Cell**[width_];
for (unsigned int x {}; x < width_; ++x) {
matrix[x] = new Cell*[length_];
for (unsigned int y {}; y < length_; ++y) {
matrix[x][y] = new Cell[height];
for (unsigned int z {}; z < height_; ++z) {
matrix[x][y][z] = Cell(x, y, z);
}
}
}
}

但正如你所看到的,如果我使用二维,我最终会为迷宫中的每一个细胞都有一个指针,同时,在三维中,我会得到一个细胞对象。我更希望在这两种情况下都能有一个单元格对象,但我不知道如何实现。

有办法做到这一点吗?还是这是我唯一的选择?

正如所问,以下是Cell:的声明

Cell.hpp:

class Cell {
private:
unsigned int xPos_, yPos_, zPos_;
public:
Cell(unsigned int xPos, unsigned int yPos);
Cell(unsigned int xPos, unsigned int yPos, unsigned int zPos);
Cell();
};

Cell.cpp:

Cell::Cell(unsigned int xPos, unsigned int yPos) {
xPos_ = xPos;
yPos_ = yPos;
}
Cell::Cell(unsigned int xPos, unsigned int yPos, unsigned int zPos) {
xPos_ = xPos;
yPos_ = yPos;
zPos_ = zPos;
}

正如问题评论中所建议的,我将改为std::vector,而不是使用三重指针。

此外,由于2D数组只是一个在三维中只有一个值的3D数组,我将把代码改为这个值。(这也在评论中提出(

当我完成后,我会用新代码更新这个答案。

更新:

以下是最终代码的样子:

Cell.hpp:

class Cell {
private:
unsigned xPos_, yPos_, zPos_;
public:
Cell(unsigned xPos, unsigned yPos, unsigned zPos);
Cell();
};

Cell.cpp:

Cell::Cell(unsigned xPos, unsigned yPos, unsigned zPos) {
xPos_ = xPos;
yPos_ = yPos;
zPos_ = zPos;
}

Maze.hpp:

class Maze {
private:
unsigned width_, length_, height_;
std::vector<std::vector<std::vector<Cell>>> matrix;
void generateMatrix();
public:
Maze();
Maze(unsigned width, unsigned length);
Maze(unsigned width, unsigned length, unsigned height);
};

Maze.cpp:

Maze::Maze() {                                      // Default constructor
width_ = 20;
length_ = 20;
height_ = 1;
Maze::generateMatrix();
}
Maze::Maze(unsigned width, unsigned length) {                 // 2D maze constructor
width_ = width;
length_ = length;
height_ = 1;
Maze::generateMatrix();
}
Maze::Maze(unsigned width, unsigned length, unsigned height) {    // 3D maze constructor
width_ = width;
length_ = length;
height_ = height;
Maze::generateMatrix();
}
void Maze::generateMatrix() {
for (unsigned x {}; x < width_; ++x) {
matrix.push_back(std::vector<std::vector<Cell>>());
for (unsigned y {}; y < length_; ++y) {
matrix.at(x).push_back(std::vector<Cell>());
for (unsigned z {}; z < height_; ++z) {
matrix.at(x).at(y).push_back(Cell(x,y,z));
}
}
}
}

最新更新