I'm doing a school assignment where I have to randomly generate a maze, solve the maze, and print out the steps I took to solve it. I think I'm pretty close to solving it but when I run my program it crashes (no error in windows...just a message saying the program has stopped working).
When I debug the program I get the following error:
Unhandled exception at 0x6754d63c (msvcr80d.dll) in project3new.exe: 0xC0000005: Access violation writing location 0xcccccccc.
What am I doing wrong?
Code:#include <iostream> #include <ctime> #include <string> using namespace std; class maze{ private: char mymaze[22][22]; int moves; string movehistory[200]; int mylocationx; int mylocationy; public: maze(){ moves=0; srand((unsigned)time(0)); for (int i=0;i<22;i++){ for(int e=0;e<22;e++){ int random_integer; for(int index=0; index<1; index++){ random_integer = (rand()%3)+1; } if (random_integer==1){ mymaze[i][e]='X'; } else{ mymaze[i][e]='O'; } } } for (int i=0;i<22;i++){ mymaze [0][i]='X'; mymaze [i][0]='X'; mymaze [i][21]='X'; mymaze [21][i]='X'; } mylocationx=1; mylocationy=1; } void printmaze(){ for (int i=0;i<22;i++){ cout<<endl; for(int e=0;e<22;e++){ cout<<mymaze[i][e]<<' '; } } } void moveup(){ movehistory[moves]="Up"; mylocationy++; moves++; } void movedown(){ movehistory[moves]="Down"; mylocationy--; moves++; } void moveleft(){ movehistory[moves]="Left"; mylocationx--; moves++; } void moveright(){ movehistory[moves]="Right"; mylocationx++; moves++; } bool canmoveup(){ if(mylocationx==1 && mylocationy==1){ return false; } if (movehistory[moves]=="Down"){ return false; } int newy=mylocationy +1; if(mymaze[mylocationx][newy]=='O'){ return true; } else{ return false; } } bool canmovedown(){ if (movehistory[moves]=="Up"){ return false; } int newy=mylocationy-1; if(mymaze[mylocationx][newy]=='O'){ return true; } else{ return false; } } bool canmoveleft(){ if(mylocationx==1 && mylocationy==1){ return false; } if (movehistory[moves]=="Right"){ return false; } int newx=mylocationx-1; if(mymaze[newx][mylocationy]=='O'){ return true; } else{ return false; } } bool canmoveright(){ if (movehistory[moves]=="Left"){ return false; } int newx=mylocationx+1; if(mymaze[newx][mylocationy]=='O'){ return true; } else{ return false; } } void printsolution(){ for (int i=0;i<22;i++){ cout<<endl; for(int e=0;e<22;e++){ cout<<mymaze[i][e]<<' '; } } } bool end(){ if (mylocationx==20 && mylocationy==20){ return true; } else{ return false; } } int getmoves(){ return moves; } string gethistory(int movenumber){ return movehistory[movenumber]; } }; int main(){ maze gameboard; gameboard.printmaze(); bool ended=false; if(!gameboard.end()){ while (!ended){ if (gameboard.canmoveright()){ gameboard.moveright(); continue; } if(gameboard.canmovedown()){ gameboard.movedown(); continue; } if (gameboard.canmoveleft()){ gameboard.moveleft (); continue; } if (gameboard.canmoveup()){ gameboard.moveup (); continue; } if(!gameboard.canmoveright() && !gameboard.canmovedown() && !gameboard.canmoveleft() && !gameboard.canmoveup()){ cout<<"Escape is Impossible!"<<endl; ended=true; } } } cout<<endl<<endl; for(int i=0;i<gameboard.getmoves();i++){ cout<<gameboard.gethistory(i)<<endl; } cout<<endl; gameboard.printsolution(); cout<<endl; }


Reply With Quote


Bookmarks