Maze Program - What am I doing wrong?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Maze Program - What am I doing wrong?

  1. #1
    Join Date
    Aug 2007
    Posts
    1

    Maze Program - What am I doing wrong?

    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;
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Texas
    Posts
    151
    You may be stepping 'out of bounds' in one of your arrays, in that your code is trying to access(or write or whatever) an element of an array that doesn't exist.

Similar Threads

  1. solving a maze game
    By sylvia in forum C++
    Replies: 2
    Last Post: 03-19-2006, 12:16 PM
  2. Help! Simple Java Program.
    By SlickWilly440 in forum Java
    Replies: 2
    Last Post: 01-28-2006, 10:17 PM
  3. Help with a program
    By stormswimmer in forum Java
    Replies: 2
    Last Post: 01-02-2006, 04:17 PM
  4. Replies: 7
    Last Post: 08-04-2005, 08:28 PM
  5. The invisible program
    By James Abbott in forum VB Classic
    Replies: 0
    Last Post: 11-24-2001, 02:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center