Random Maze Generate
Apr 28, 2018 at 1:50pm UTC
can you help me with this function? it's not working. looping infinitely.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
void Maze::generate(){
int width = 30;
int height = 30;
string str = "" ;
theMaze.clear();
for (int i = 0; i < width; ++i){
str = str+ "+" ;
if (i==29){str+="\n" ;}
}
for (int j = 0; j < height; ++j){
theMaze.push_back(str);
}
theMaze[1,1] = ' ' ;
theMaze[2,2] = '!' ;
stack<Coord> corridors;
Coord current(1,1);
corridors.push(current);
while (corridors.size()!=0){
vector<Coord> compass;
Coord north = Coord(0,-2);
Coord n = current + north;
if (n.y > 0 && n.y < theMaze[0].size()-1){
if (n.x > 0 && n.x < theMaze.size()-1){
if (theMaze[n.y][n.x] == '+' ){
compass.push_back(n);
}
}
}
Coord south = Coord(0,2);
Coord s = current + south;
if (s.y > 0 && s.y < theMaze[0].size()-1){
if (s.x > 0 && s.x < theMaze.size()-1){
if (theMaze[s.y][s.x] == '+' ){
compass.push_back(s);
}
}
}
Coord east = Coord(2,0);
Coord e = current + east;
if (e.y > 0 && e.y < theMaze[0].size()-1){
if (e.x > 0 && e.x < theMaze.size()-1){
if (theMaze[e.y][e.x] == '+' ){
compass.push_back(e);
}
}
}
Coord west = Coord(-2,0);
Coord w = current + west;
if (w.y > 0 && w.y < theMaze[0].size()-1){
if (w.x > 0 && w.x < theMaze.size()-1){
if (theMaze[w.y][w.x] == '+' ){
compass.push_back(w);
}
}
}
if (compass.size()!=0 && compass.size() <= 3){
srand(time(NULL));
int randy = rand()%29;
while (randy==0){
randy=rand()%29;
}
Coord next = compass[randy, compass.size()];
corridors.push(current);
theMaze[(current.y+next.y)/2][(current.x+next.x)/2]=' ' ;
theMaze[next.y][next.x]= ' ' ;
current = next;
}else if (corridors.size() !=0){
current = corridors.top();
corridors.pop();
}
}
save("generate.txt" );
}
Apr 28, 2018 at 3:33pm UTC
while (corridors.size()!=0)
You don't change corridors inside this loop so how can its size ever become 0 ?
Apr 28, 2018 at 7:41pm UTC
But i do. That's what corridors.pop () does.
Apr 28, 2018 at 8:14pm UTC
if (compass.size() != 0 && compass.size() <= 3)
You pop only if this condition is false. Are you sure it will be?
Maybe add some output statement to check the compass.size() and corridors.size()
Apr 28, 2018 at 10:28pm UTC
thank you. i understand now, but I still can't get it to be random
Apr 29, 2018 at 7:05am UTC
Can you show the complete code we I can run it.
Topic archived. No new replies allowed.