# Mouse Maze Problem

• 04-26-2008
Furbiesandbeans
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!
• 04-26-2008
tabstop
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?
• 04-27-2008
Furbiesandbeans
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 ```

• 04-27-2008
tabstop
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?
• 04-27-2008
Furbiesandbeans
i dont know how to do that...
• 04-27-2008
master5001
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 -_-).
• 04-27-2008
tabstop
Put a printf at the top of every function that says "Now doing <<whatever function this is>>".
• 04-27-2008
Furbiesandbeans
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
• 04-27-2008
anon
Quote:

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.
• 04-27-2008
tabstop
It should be ok, since it turns right immediately if right is available, then looks ahead, then looks left, then looks back.
• 04-27-2008
anon
Quote:

Originally Posted by tabstop
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?
• 04-27-2008
tabstop
Quote:

Originally Posted by anon
When direction is initially 'S', what will it be when the function exits and how to fix it?

Bingo! Good catch.
• 04-27-2008
Furbiesandbeans
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'; }```
• 04-28-2008
guesst
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?