Code:
//We define the size of the Maze here so if it changes,
//we can alter it easily and in one place only
#define SizeOfMazeX 10
#define SizeOfMazeY 10
//different states the any element of the maze may be in
#define BORDER 0
#define OPEN 1
#define BLOCKED 2
//The Maze array
char maze[SizeOfMazeX][SizeOfMazeY] =
{
'x','x','x','x','x','x','x','x','x','x',
'x','o','o','o','o','o','o','o','o','x',
'x','o','o','o','o','o','o','o','o','x',
'x','o','o','o','c','c','c','o','o','x',
'x','o','o','o','c','m','c','o','o','x',
'x','o','o','o','c','o','c','o','o','x',
'x','o','o','o','c','o','c','o','o','x',
'x','o','o','o','c','o','c','o','o','x',
'x','o','o','o','c','o','c','o','o','x',
'x','x','x','x','x','x','x','x','x','x',
};
//The position of our little rodent (0 by default)
char MouseX = 0, MouseY = 0;
int main (void)
{
int x, y;
//Apparently we want to take the maze and convert its contents
//into Wall/Open/Exit data.
for (x = 0; x < SizeOfMazeX; x++)
{
for (y = 0; y < SizeOfMazeY; y++)
{
//I dont know why we're doing this but we're doing it anyhow
//We could just as easily compare the character values later
//instead of converting them to these predefined values...
//Any reason you do this?
//print out each element of the array (to show the maze)
cout << maze[x][y];
if (maze[x][y] == 'x')
maze[x][y] = BORDER;
else if (maze[x][y] == 'o')
maze[x][y] = OPEN;
else if (maze[x][y] == 'm')
{
//I assume that the 'm' is where we want the mouse
//to start. So we assign our Mouse coordinates to
//the position we're currently reading
MouseX = x;
MouseY = y;
//We'll, the mouse starts here so I assume its open...
maze[x][y] = OPEN;
}
else if (maze[x][y] == 'c')
maze[x][y] = BLOCKED;
}
//move to next line
cout << endl;
}
do
{
//Move around
//IMPORTANT: This wont work to solve the maze in any way.
//This is just an example. You said you would do your own
//algorithm to solve this. This just moves the mouse to
//the first empty space it finds near it. Using this code
//will just make the mouse run around in circles (back and
//for, back and forth). Put your own algorithm here.
if (maze[MouseX][MouseY-1] == OPEN)
MouseY --;
else if (maze[MouseX][MouseY+1] == OPEN)
MouseY ++;
else if (maze[MouseX-1][MouseY] == OPEN)
MouseX --;
else if (maze[MouseX+1][MouseY] == OPEN)
MouseX ++;
}while (maze[MouseX][MouseY] != BORDER);
// if (maze[x][y] == 0)
//We dont need to check that because if we got out of the
//while loop than we obviously reached the BORDER
cout << "maze finished" << endl;
return 0;
}