can you help me with this function? it\'s not working. looping infinitely. also,
ID: 3718506 • Letter: C
Question
can you help me with this function? it's not working. looping infinitely. also, I believe rand is causing infinite loop.
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+=" ";}
}
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");
}
Explanation / Answer
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+=" ";}
}
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+1; //see the c++ reference
while(randy==0){
randy=rand()%29+1; // the way to initialize a random number in the c++ reference
}
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");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.