Original Post
Argh, for a lab assignment, I have to write a program that randomly generates a maze, and a random starting position. The program then needs to find a path out of the maze, if it''s possible, to exit the maze.
The functions and their implementations given are Rnd(...), CreateMaze(...) and DisplayMaze(...). Then the rest of the functions SolveMaze(...), MazeSolution(...), and GetMove(...) have to be implemented by me. So far, this is what the source looks like (below). I''m not asking anyone to write it for me ''cos this is for a grade and all. I just need help. I need help particularly in the function GetMove(). I don''t know why, but whenever we have an assignment that asks to get a move, I can''t do it! It''s so frustrating.
Anyway, here''s the source. If any one could give me any pointers or hints on what I could do (such as a helpful algorithm), it would help out tremendously. If you need the source for the include files, just ask, if they''re needed for you to understand what I''m doing here.
If you were to compile this and run it, you''d get an illegal vector index 12, max index = 11 (basically, I went out of range, which is baaaad), but only if you used APMATRIX.H (it provides out-of-range protection by giving you an error, instead of crashing your system).
Also, I''ve looked everywhere on the internet for help, and I can''t find any sites that are helpful at all. Hopefully you guys could help, a little at least?
Oh, btw, in the maze, you can go any direction including diagonally, so that makes 8.
#include "UTILITY2.H"
#include "APMATRIX.H"
#include "APSTACK.H"
struct Coordinate
{
int Row, Col;
};
double Rnd(double &Seed)
{
const double PI = 3.14159;
Seed = (Seed + PI) * (Seed + PI);
Seed = Seed - floor(Seed);
return Seed;
}
typedef apmatrix <char> Matrix;
void CreateMaze(Matrix &Maze, Coordinate &Start);
void DisplayMaze(const Matrix &Maze);
void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free);
void MazeSolution(bool Free);
void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free);
void main()
{
OutputSelection();
Heading H("35A","11-12-01","100","RANDOM MAZE");
Execution();
Matrix Maze(12,12);
Coordinate Start;
bool Free;
do
{
clrscr();
CreateMaze(Maze, Start);
DisplayMaze(Maze);
SolveMaze(Maze,Start,Free);
DisplayMaze(Maze);
MazeSolution(Free);
}
while (NotFinished());
Terminate();
}
void CreateMaze(Matrix &Maze, Coordinate &Start)
{
double Seed;
cout << "ENTER A RANDOM SEED ===>> ";
cin >> Seed;
int R,C,L;
for (R = 0; R < 12; R++)
for (C = 0; C < 12; C++)
{
if (R == 0 || C == 0 || R == 11 || C == 11)
Maze[R][C] = ''X'';
else
{
L = floor(2 * Rnd(Seed));
if (L == 0)
Maze[R][C] = ''X'';
else
Maze[R][C] = ''O'';
}
}
Maze[0][0] = ''E'';
Start.Row = floor(11 * Rnd(Seed)) + 1;
Start.Col = 11;
Maze[Start.Row][Start.Col] = ''S'';
}
void DisplayMaze(const Matrix &Maze)
{
Title("DISPLAY MAZE");
int R, C;
for (R = 0; R < 12; R++)
{
for (C = 0; C < 12; C++)
cout << Maze[R][C] << '' '';
cout << endl;
}
getch();
}
void SolveMaze(Matrix &Maze, Coordinate Start, bool &Free)
{
Title("SOLVE MAZE");
int R, C;
Free=false;
apstack <Coordinate> S;
S.makeEmpty();
Coordinate Current,Move;
Current = Start;
S.push(Start);
while (!(S.isEmpty() && Maze[Start.Row][Start.Col]==''S''))
{
GetMove(Maze,Current,Move,Free);
if (Free==true)
{
Maze[Current.Row][Current.Col]= ''.'';
S.push(Current);
}
else
S.pop(Current);
}
}
void MazeSolution(bool Free)
{
Title("MAKE SOLUTION");
if (Free==true)
cout << "There is a solution to the maze" << endl;
else
cout << "There is no solution to the maze" << endl;
}
void GetMove(const Matrix &Maze, Coordinate Current, Coordinate &Move, bool &Free)
{
apstack <Coordinate> S;
/////////
if (Maze[Current.Row][Current.Col]==''S'' && Maze[10][10]==''O'') //lower left
{
S.push(Current);
Free=true;
}
else if (Maze[Current.Row][Current.Col]==''S'' && Maze[9][10]==''O'') //upper left
{
S.push(Current);
Free=true;
}
/*else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col-1]==''O'') // up
{
Maze[Move.Row][Move.Col-1];
S.push(Current);
//Current.Col--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col-1]==''O'') // upper right
{
Maze[Move.Row+1][Move.Col-1];
S.push(Current);
//Current.Row++;
//Current.Col--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col]==''O'') // right
{
Maze[Move.Row+1][Move.Col];
S.push(Current);
//Current.Row++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row+1][Current.Col+1]==''O'') // lower right
{
Maze[Move.Row+1][Move.Col+1];
S.push(Current);
//Current.Row++;
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row][Current.Col+1]==''O'') // down
{
Maze[Move.Row][Move.Col+1];
S.push(Current);
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col+1]==''O'') // lower left
{
Maze[Move.Row-1][Move.Col+1];
S.push(Current);
//Current.Row--;
//Current.Col++;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col]==''O'') // left
{
Maze[Move.Row-1][Move.Col];
S.push(Current);
//Current.Row--;
Free=true;
}
else if (!(Maze[Current.Row][Current.Col]==''S'') && Maze[Current.Row-1][Current.Col-1]==''O'') // upper left
{
Maze[Move.Row-1][Move.Col-1];
S.push(Current);
//Current.Row--;
//Current.Col--;
Free=true;
}*/
else
{
Free=false;
//S.pop(Current);
}
//if (Maze[Current.Row][Current.Col]==''S'')
//{Free = true;
//S.push(Current);
//Move = Current;
}
|