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!