Maze Program - What am I doing wrong?

This is a discussion on Maze Program - What am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; > srand((unsigned)time(0)); If you want a consistent set of instructions which always cause it to fail, then comment out this ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > srand((unsigned)time(0));
    If you want a consistent set of instructions which always cause it to fail, then comment out this line. Then you'll always end up with the same maze which crashes on the 5 move left say.

    > project3new.exe!maze::moveright() Line 83 + 0x23 bytes C++
    Double-click on this line, then it will take you to your code.
    Then you can click on your variables to examine the current values to see if they make sense.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Code:
    Access violation writing location 0xcccccccc
    means that you have an uninitialised pointer variable.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #18
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Instead of storing the moves as strings you can store them as a value from 0 to 3 with:

    • 0 = Up
    • 1 = Right
    • 2 = Down
    • 3 = Left


    If your array is not large enough, and trust me it will not be for large mazes, then your program will crash as it overruns the bounds of the array. Either limit the size of the maze that can be generated and solved or move to std::vector. Since you cannot use std::vector I would recommend creating an array that is at least 65535 elements in size. By eliminating the string and using values you can then allocate an array of 65535 bytes and your program would be able to solve and generate very large mazes.

    The size of the array will not affect the speed or performance of the program since you are not using it for iteration from start to finish as in a for loop. You are using the array as a stack and you always know where you are in the stack so access time is instantaneous.

    There is a formula I used to have that would tell you the max size needed for an array given the size of the maze requested. I believe if you google for maze generation techniques you will find it. If you find this formula then you can allocate the array after you ask the user the size needed or you can hardcode this into your program if the assignment has already stated the size of maze you need to generate.

    So your program then becomes:
    • Ask user for maze size (width, height)
    • Compute array size for requested width, height
    • Attempt to allocate array based on size; if this fails warn the user the maze is too large and return to top of list
    • Generate maze
    • Solve maze
    • De-allocate array prior to program exiting
    Last edited by VirtualAce; 08-02-2007 at 01:56 AM.

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    13

    Finally!

    I finally got it. I had up and down messed up for my coordinates so everything was going nuts.

    Here is the code if anyone cares.

    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;
    	movehistory[0]="Start";
    	   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; //Change the 3 to a bigger number for an easier maze
        }		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[e][i]<<' ';
    		}
    	}
    }
    
    
    void moveup(){
    	movehistory[moves]="Up";
    	mylocationy--;
    	mymaze[mylocationx][mylocationy]='*';
    	moves++;
    }
    
    void movedown(){
    	movehistory[moves]="Down";
    	mylocationy=mylocationy+1;
    	mymaze[mylocationx][mylocationy]='*';
    	moves++;
    }
    
    void moveleft(){
    	movehistory[moves]="Left";
    	mylocationx=mylocationx-1;
    	mymaze[mylocationx][mylocationy]='*';
    	moves++;
    }
    
    void moveright(){
    	movehistory[moves]="Right";
    	mylocationx++;
    	mymaze[mylocationx][mylocationy]='*';
    	moves++;
    }
    
    
    bool canmoveup(){
    	if (movehistory[moves-1]=="Down"){
    		return false;
    	}
    	int newy=mylocationy-1;
    	if(mymaze[mylocationx][newy]=='O'){	
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmovedown(){
    	if (movehistory[moves-1]=="Up"){
    		return false;
    	}
    	int newy=mylocationy+1;
    	if(mymaze[mylocationx][newy]=='O'){	
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmoveleft(){
    	if (movehistory[moves-1]=="Right"){
    		return false;
    	}
    	int newx=mylocationx-1;
    	if(mymaze[newx][mylocationy]=='O'){		
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
    bool canmoveright(){
    	if (movehistory[moves-1]=="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[e][i]<<' ';
    		}
    	}
    }
    
    bool impossible(){
    	if (mymaze[1][1]=='X'){
    		cout<<endl<<"The maze cheated!  The Entrance is Blocked!"<<endl;
    		return true;
    	}
    	if (mymaze[20][20]=='X'){
    		cout<<endl<<"The maze cheated!  The Exit is Blocked!"<<endl;
    		return true;
    	}
    
    return false;
    }
    
    bool end(){
    
    	if (mylocationx==20 && mylocationy==20){
    		return true;
    	}
    	if (moves!=0 && (mylocationx==1 && mylocationy==1)){
    			return true;
    	}
    	return false;
    }
    
    int getmoves(){
    return moves;
    }
    
    string gethistory(int movenumber){ 
    	return movehistory[movenumber];
    }
    };
    
    
    int main(){
    
    	maze gameboard;
    	gameboard.printmaze();
    	bool ended=false;
    
    	if (gameboard.impossible()){
    		return 0;
    	}
    		while (!ended){
    			if(gameboard.end()){
    				cout<<endl<<"You got out!"<<endl;
    				ended=true;
    				continue;
    			}
    
    			if (gameboard.canmoveright()){
    				gameboard.moveright();
    				continue;
    			}
    			if(gameboard.end()){
    				cout<<endl<<"You got out!"<<endl;
    				ended=true;
    				continue;
    			}
    			if(gameboard.canmovedown()){
    				gameboard.movedown();
    				continue;
    			}
    			if(gameboard.end()){
    				cout<<endl<<"You got out!"<<endl;
    				ended=true;
    				continue;
    			}
    			if (gameboard.canmoveleft()){
    				gameboard.moveleft();
    				continue;
    			}
    			if(gameboard.end()){
    				cout<<endl<<"You got out!"<<endl;
    				ended=true;
    				continue;
    			}
    			if (gameboard.canmoveup()){
    				gameboard.moveup();
    				continue;
    			}
    			if(gameboard.end()){
    				cout<<endl<<"You got out!"<<endl;
    				ended=true;
    				continue;
    			}
    				cout<<endl<<"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;
    }

  5. #20
    Registered User
    Join Date
    Jul 2007
    Posts
    13
    Thanks for all of your help by the way. I was beating my head against a wall far too long before I finally gave up and asked for help. Once I was put on the right path it wasn't as bad to find the issues.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    BTW, instead of
    Code:
    while(condition) {
        if(a) {
            /* code for a */
            continue;
        }
        if(b) {
            /* code for b */
            continue;
        }
        /* ... */
        /* code for z */
    }
    you can go
    Code:
    while(condition) {
        if(a) {
            /* code for a */
        }
        else if(b) {
            /* code for b */
        }
        /* ... */
        else {
            /* code for z */
        }
    }
    which is much more readable and standard practice. Not to mention that all of the repeated cases of
    Code:
    if(gameboard.end()){
    are redundant. You only need one (preferably before every other if), since if the position is changed, continue is called. Think about it.

    Also, what is the purpose of this?
    Code:
        for(int index=0; index<1; index++){ 
            random_integer = (rand()&#37;3)+1; //Change the 3 to a bigger number for an easier maze
        }
    You're overwriting random_integer each time, so you might as well eliminate the for loop and just call it once.

    If you felt up to it, I'm sure you could combine canmoveright() and canmoveleft() etc into one function.

    You might also want to prevent the starting and ending positions from being set to 'X' where you generate the maze, rather than calling impossible(). Or maybe generate the maze again if that is the case. Just a thought.

    Your program doesn't have to find the optimal escape solution, right? Just one possible solution? Because right now that's what it does.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  5. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21