Mouse Maze Problem

This is a discussion on Mouse Maze Problem within the C++ Programming forums, part of the General Programming Boards category; First of all, this is my first post on this forum =) k, anywho.. im having problems with the algorithm ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    Mouse Maze Problem

    First of all, this is my first post on this forum =)

    k, anywho..

    im having problems with the algorithm of the mouse getting through a maze created. The maze is a 2-dim char array, the walls consists of X's and the inside is randomly generated.

    The mouse has 2 variables, the x and y coordinate, and the direction

    When the mouse runs into a wall in the direction it's facing, the mouse just goes up again, and down, and repeats infinitively.

    I have tried many things, but none have worked.

    PS: i can send the working exe if needed.

    Code:
    //in main.cpp
    do
    	{
    		if(takeastep(maze, x, y, direction))
    		{
    			counter ++;
    			showmouse(x, y, width, height, counter, direction);
    		}
    		delay(timer);
    	}while(!entrance(x, y, entout) && !exit(x, y, entout, width));
    
    
    bool takeastep(const char * const * const maze, unsigned int & x, unsigned int & y,char & direction)
    {
    	bool outcome;
    	if(righthandonwall(maze, x, y, direction))
    	{
    		if(!straightahead(maze, x, y, direction))
    		{
    			stepstraight(x, y, direction);
    			outcome = true;
    			return outcome;
    		}
    		while(straightahead(maze, x, y, direction))
    		{
    			rotate90counter(direction);
    		}
    		stepstraight(x,y,direction);
    		outcome = true;
    		return outcome;
    	}
    	else
    	{
    		rotate90clock(direction);
    		stepstraight(x, y, direction);
    		outcome = true;
    		return outcome;
    	}
    }
    
    
    bool righthandonwall(const char * const * const maze, unsigned int x, unsigned int y, char direction)
    {
    	bool answer = false;
    	if (direction == 'N')
    		if(maze[y][x+1] == 'X')
    			answer = true;
    	if (direction == 'S')
    		if(maze[y][x-1] == 'X')
    			answer = true;
    	if (direction == 'E')
    		if(maze[y+1][x] == 'X')
    			answer = true;
    	if (direction == 'W')
    		if(maze[y-1][x] == 'X')
    			answer = true;
    	return answer;
    }
    
    bool straightahead(const char * const * const maze, unsigned int x, unsigned int y, char direction)
    {
    	bool answer = false;
    	if (direction == 'N')
    		if(maze[y-1][x] == 'X')
    			answer = true;
    	if (direction == 'S')
    		if(maze[y+1][x] == 'X')
    			answer = true;
    	if (direction == 'E')
    		if(maze[y][x+1] == 'X')
    			answer = true;
    	if (direction == 'W')
    		if(maze[y][x-1] == 'X')
    			answer = true;
    	return answer;
    }
    
    void stepstraight(unsigned int & x, unsigned int & y, char direction)
    {
    	if (direction == 'N')
    		y--;
    	if (direction == 'S')
    		y++;
    	if (direction == 'E')
    		x++;
    	if (direction == 'W')
    		x--;
    }
    
    void rotate90counter(char & direction)
    {
    	if (direction == 'N')
    		direction = 'W';
    	if (direction == 'S')
    		direction = 'E';
    	if (direction == 'E')
    		direction = 'N';
    	if (direction == 'W')
    		direction = 'S';
    }
    
    void rotate90clock(char & direction)
    {
    	if (direction == 'N')
    		direction = 'E';
    	if (direction == 'S')
    		direction = 'W';
    	if (direction == 'E')
    		direction = 'S';
    	if (direction == 'W')
    		direction = 'N';
    }

    Any help is appreciated!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You say that as though that's not what is supposed to happen. What is supposed to happen, and what is the situation when it does not?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    ok, this is what happens now....

    Code:
    
    XXXXXXXX
    X X    X
    M  X   X
    X      X
    XXXXXXXX
    
    
    XXXXXXXX
    X X    X
    #M X   X
    X      X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    XM     X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    #M X  
    X#     X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    XM     X
    XXXXXXXX
    
    Repeats
    


    What's suppose to happen is

    Code:
    
    XXXXXXXX
    X X    X
    M  X   X
    X      X
    XXXXXXXX
    
    
    XXXXXXXX
    X X    X
    #M X   X
    X      X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    XM     X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    X#M    X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    X##M   X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    X###M  X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  
    X####M X
    XXXXXXXX
    
    
    XXXXXXXX
    X X    X
    ## X  
    X#####MX
    XXXXXXXX
    
    
    XXXXXXXX
    X X    X
    ## X  M 
    X#######X
    XXXXXXXX
    
    XXXXXXXX
    X X    X
    ## X  #M
    X######X
    XXXXXXXX
    
    End
    



  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see anything wrong, I'm afraid. Can you print out bits when a function is called to see what's going on?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    i dont know how to do that...

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't see anything wrong either. If it doesn't conflict with NSA guidelines could you attach your C file to a post so we can compile and edit the code for yourselves (sorry for the sarcasm... most people think posting their code is going to ruin their chances of working at Microsoft when they graduate or something -_-).

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Put a printf at the top of every function that says "Now doing <<whatever function this is>>".

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    k, here is my visual studio folder, it has all the .cpp files and .h files

    http://187combat.com/forums/index.ph...e=post&id=1440

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    while(straightahead(maze, x, y, direction))
    {
    	rotate90counter(direction);
    }
    stepstraight(x,y,direction);
    If you can't move straight on, you should try going left or right first, and only then go back if neither is available. If you try new directions in 90 degree incrementations, you are trying "turn-right", "turn-back", "turn-left" in that order - that is, you'll never discover that you can go left.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It should be ok, since it turns right immediately if right is available, then looks ahead, then looks left, then looks back.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by tabstop View Post
    It should be ok, since it turns right immediately if right is available, then looks ahead, then looks left, then looks back.
    Yeah, sorry. Too little coffee

    However, it is very easy to make it work. Think: is it possible that the rotate functions as they are will not make only a 90 degree turn, but perhaps turn more than once?!

    Code:
    void rotate90counter(char & direction)
    {
    	if (direction == 'N')
    		direction = 'W';
    	if (direction == 'S')
    		direction = 'E';
    	if (direction == 'E')
    		direction = 'N';
    	if (direction == 'W')
    		direction = 'S';
    }
    When direction is initially 'S', what will it be when the function exits and how to fix it?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by anon View Post
    When direction is initially 'S', what will it be when the function exits and how to fix it?
    Bingo! Good catch.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    OMG i love u anon

    here is the fixed version

    Code:
    void rotate90counter(char & direction)
    {
    	if (direction == 'N')
    		direction = 'W';
    	else if (direction == 'S')
    		direction = 'E';
    	else if (direction == 'E')
    		direction = 'N';
    	else if (direction == 'W')
    		direction = 'S';
    }

  14. #14
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    This is a great program. (I'd love to feature it on my site.) A couple of quesitons:
    that's alot of if..else ifs going on. Why don't you use a case. For instance:
    Code:
    bool righthandonwall(const char * const * const maze, unsigned int x, unsigned int y, char direction)
    {
    	bool answer = false;
    	if (direction == 'N')
    		if(maze[y][x+1] == 'X')
    			answer = true;
    	if (direction == 'S')
    		if(maze[y][x-1] == 'X')
    			answer = true;
    	if (direction == 'E')
    		if(maze[y+1][x] == 'X')
    			answer = true;
    	if (direction == 'W')
    		if(maze[y-1][x] == 'X')
    			answer = true;
    	return answer;
    }
    vs
    Code:
    bool righthandonwall(const char * const * const maze, unsigned int x, unsigned int y, char direction)
    {
    	bool answer = false;
    	switch (direction) {
                case 'N' : if(maze[y][x+1] == 'X') answer = true;
                               break;
    	    case 'S' : if(maze[y][x-1] == 'X') answer = true;
                               break;
    	    case 'E' : if(maze[y+1][x] == 'X') answer = true;
                               break;
    	    case 'W' : if(maze[y-1][x] == 'X') answer = true;
    	}
    	return answer;
    }
    I suppose it's purely an aesthetic choice.

    Next, why do you insist in making direction a character value. Why not just make it a number? That would make the body of your turn90degree function be nothing more than:
    Code:
    direction = (direction + 1) % 4;
    And output to the user could be as easy as indexing an array with the direction variable.

    Again, it's all personal choice, the way I do things. I still think the program rocks. Are you eventually going to make a more robust maze generator?
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble solving maze.
    By eurus in forum C Programming
    Replies: 3
    Last Post: 02-17-2006, 12:52 AM
  2. Direct Input and Mouse Coords
    By Khelder in forum Game Programming
    Replies: 5
    Last Post: 12-30-2003, 01:29 PM
  3. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. Another problem swith my wide system MouseProc
    By darcome in forum Windows Programming
    Replies: 0
    Last Post: 10-03-2002, 12:34 PM

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