Hey guys,
I have written a recursive function mazeTraverse which finds the path of the mouse to find its food which is placed inside the maze. I am given a maze of of 15 rows and 25 columns. Mouse's starting position is (8,1)[eighth row and 1st column] and the food is placed in (5, 10).
In my maze, 'A' represents the way is blocked
' ' represnts an opening
'M' represents starting position for the mouse and 'T' represents the target which is food.
I am supposed to print X which will start moving from starting position to the target. X basically represents the mouse movement from the starting position to the target. I know I need to use recursive function so when mouse find dead end it has to do backtracking. I have written a function but it is not correct. Can anyone look at my code and guide me. I am very interested to learn something new.
Here is my code :
class MazeMania
{
public:
MazeMania(const int maze[][], int start, int end);
int traverse( int x, int y);
private:
const int ROWS = 15;
const int COLS = 25;
char mouse = 'X';
};
# include "mazeMania.h"
#define FALSE 0
#define TRUE 1
// initializes the constructor
MazeMania::MazeMania(const int maze[][], int start, int end)
{
int start = 8;
int end = 1;
}
int MazeMania::mazeTraverse(int x, int y)
{
if ( x < 0 || x > COLS -1 || y < 0 || y > ROWS -1)
{
return FALSE;
}
if (maze[y][x] == 'A')
{
return FALSE;
}
cout <<" "<<mouse;
// if x, y is the goal, return true
if (maze[y][x] == 'T')
{
return TRUE;
}
if (mazeTraserse(x-1, y) == TRUE)
{
return TRUE;
}
else if (mazeTraverse (x, y-1) == TRUE)
{
return TRUE;
}
else if (mazeTraverse (x + 1, y) == TRUE)
{
return TRUE;
}
else if (mazeTraverse (x, y + 1) == TRUE)
{
return TRUE;
}
I am confused now. Is my algorithm correct? How can I print 'X' now? and how can I display like X is moving from starting position to find the target? Please guide me through. I am willing to learn. Thanks