Reputation: 316
so I'm working on a school assignement, as I'm learning C++. I'm not so much looking for code to be given to me as help understanding/coming up with the right algorithm for this problem.
I need to create a (5x5x5) 3d maze of 1's and 0's. Populate it randomly (except 0,0,0 being a 1 for start and 4,4,4 being a 1 at the finish.
Here's what I've done. I made a cube object:
#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){}
in my maze managing class I initialize like this
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;
}
The header for that class: #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();
};
So I'm trying to populate it and this is what I have, it might not make a ton of sense:
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);
}
}
}
}
I'm attempting to fill all the z axis and trace to the right on x, then move up one y and do the same, is this a good approach, or is there a better way of doing this? I'm just getting pretty confused.
Upvotes: 1
Views: 1301
Reputation: 316
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;
}
This worked! Now on to maze traversal! :/
Upvotes: 1