1. ## 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))
{
{
stepstraight(x, y, direction);
outcome = true;
return outcome;
}
{
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)
{
if (direction == 'N')
if(maze[y][x+1] == 'X')
if (direction == 'S')
if(maze[y][x-1] == 'X')
if (direction == 'E')
if(maze[y+1][x] == 'X')
if (direction == 'W')
if(maze[y-1][x] == 'X')
}

bool straightahead(const char * const * const maze, unsigned int x, unsigned int y, char direction)
{
if (direction == 'N')
if(maze[y-1][x] == 'X')
if (direction == 'S')
if(maze[y+1][x] == 'X')
if (direction == 'E')
if(maze[y][x+1] == 'X')
if (direction == 'W')
if(maze[y][x-1] == 'X')
}

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. 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. 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. 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. i dont know how to do that...

6. 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. Put a printf at the top of every function that says "Now doing <<whatever function this is>>".

8. 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. 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.

10. It should be ok, since it turns right immediately if right is available, then looks ahead, then looks left, then looks back.

11. 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?

12. 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.

13. 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. 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)
{
if (direction == 'N')
if(maze[y][x+1] == 'X')
if (direction == 'S')
if(maze[y][x-1] == 'X')
if (direction == 'E')
if(maze[y+1][x] == 'X')
if (direction == 'W')
if(maze[y-1][x] == 'X')
}```
vs
Code:
```bool righthandonwall(const char * const * const maze, unsigned int x, unsigned int y, char direction)
{
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;
}
`direction = (direction + 1) % 4;`