我需要创建一个1x和0的(5x5x5)3d迷宫。随机填充(除了0,0,0为1开始,4,4,4为1完成。
这就是我所做的。 我制作了一个立方体对象:
#include "cube.h"
Cube :: Cube(int cube_value)
{
cube_value = 0;
chk_up = false;
chk_down = false;
chk_left = false;
chk_right = false;
chk_front = false;
chk_back = false;
}
Cube :: ~Cube(void){}
在我的迷宫管理课中我像这样初始化
PathFinder::PathFinder()
{
// initializing / sizing 5x5x5 Maze
Maze.resize(5);
for(int y = 0; y < 5 ; ++y)
{
Maze[y].resize(5);
for(int z = 0; z<5 ; ++z)
{
Maze[y][z].resize(5);
}
}
int at_x = 0;
int at_y = 0;
int at_z = 0;
}
该课程的标题: #include“PathfinderInterface.h” #include“cube.h”
class PathFinder : public PathfinderInterface {
private:
int at_x;
int at_y;
int at_z;
public:
vector<vector<vector<Cube> > > Maze;
PathFinder();
virtual ~PathFinder();
string getMaze();
void createRandomMaze();
bool importMaze(string file_name);
vector<string> solveMaze();
};
所以我试图填充它,这就是我所拥有的,它可能没有多大意义:
void PathFinder :: fillmaze()
{
Maze[0][0][0].cube_value = 1;
Maze[4][4][4].cube_value = 1;
int atx = 0 , aty = 0 , atz = 0;
while(atx<5 && aty < 5 && atz < 5)
{
if(atz == 5)
{
aty = aty + 1;
}
if(aty == 5)
{
atx = atx + 1;
atx = 0;
}
for(atz=0 ; atz<5 ; ++atz)
{
if((atx!= 0 && aty!=0 && atz!=0) || (atx!=4 && aty!=4 && atz!= 4) )
{
Maze[atx][aty][atz].cube_value = (rand() % 2);
}
}
}
}
我正在尝试填充所有z轴并在x上向右移动,然后向上移动一年并执行相同操作,这是一个好方法,还是有更好的方法来做到这一点?我只是很困惑。
答案 0 :(得分:1)
void PathFinder :: fillmaze()
{
int atx = 0 , aty = 0 , atz = 0;
while(atz<=4)
{
if(atx == 5)
{
aty = aty + 1;
}
if(aty == 5)
{
aty = 0;
atz = atz + 1;
}
if(atz < 5)
{
for(atx=0 ; atx<5 ; ++atx)
{
Maze[atx][aty][atz].cube_value = (rand() % 2);
}
}
}
Maze[0][0][0].cube_value = 1;
Maze[4][4][4].cube_value = 1;
}
这很有效!现在进行迷宫遍历! :/