Question
C++ Maze ( I provided what I have so far for an answer at the bottom. ) Assume that a maze is a rectangular array
C++ Maze ( I provided what I have so far for an answer at the bottom. )
Assume that a maze is a rectangular array of squares, some of which are blocked to represent walls. The maze has one entrance and one exit. For example, if xs represent the walls, a maze could appear as follows:
A creature, indicated in the previous diagram by O, sits just inside the maze at the entrance (bottom row). Assume that the creature can move in only four directions: north, south, east, and west. In the diagram, north is up, south is down, east is to the right, and west is to the left. The problem is to move the creature through the maze from the entrance to the exit (top row), if possible. As the creature moves, it should mark its path. At the conclusion of the trip through the maze, you should see both the correct path and incorrect attempts. Write a program to solve this problem.
Squares in the maze have one of several states: CLEAR (the square is clear), WALL (the square is blocked and represents part of the wall), PATH (the square lies on the path to the exit), and VISITED (the square was visited, but going that way led to an impasse). This problem uses two ADTs that must interact. The ADT creature represents the creatures current position and contains operations that move the creature. The creature should be able to move north, south, east, and west one square at a time. It should also be able to report its position and mark its trail. The ADT maze represents the maze itself, which is a two-dimensional rectangular arrangement of squares. You could number the rows of squares from the top beginning with zero, and number the columns of squares from the left beginning with zero. You could then use a row number and a column number to uniquely identify any square within the maze. The ADT clearly needs a data structure to represent the maze. It also needs such data as the height and width of the maze given in numbers of squares; the length of a side of a square, and the row and column coordinates of both the entrance to and the exit from the maze. The ADT maze should also contain, for example, operations that create a specic maze given descriptive data that we will detail to display a maze, determine whether a particular square is part of the wall, determine whether a particular square is part of the path, and so on. The search algorithm and its supporting functions are outside both of the ADTs creature and maze. Thus, the maze and the creature will be arguments that you must pass to these functions. If you are at the mazes entrance, you can systematically nd your way out of the maze by using the following search algorithm. This involves backtrackingthat is, retracing your steps when you reach an impasse.
Step1. First check whether you are at the exit. If you are, youre done (a very simple maze); if you are not, go to step 2.
Step2. Try to move to the square directly to the north by calling the function goNorth (step 3).
Step3. If goNorth was successful, you are done. If it was unsuccessful, try to move to the square directly to the west by calling the function goWest (step 4).
Step4. If goWest was successful, you are done. If it was unsuccessful, try to move to the square directly to the south by calling the function goSouth (step 5).
Step5. If goSouth was successful, you are done. If it was unsuccessful, try to move to the square directly to the east by calling the function goEast (step 6).
Step6. If goEast was successful, you are done. If it was unsuccessful, you are still done, because no path exists from the entrance to the exit.
The function goNorth will examine all the paths that start at the square to the north of the present square as follows. If the square directly to the north is clear, is inside the maze, and has not been visited before, move into this square and mark it as part of the path. (Note that you are moving from the south.) Check whether you are at the exit. If you are, youre done. Otherwise, try to nd a path to the exit from here by try-ing all paths leaving this square except the one going south (going south would put you back in the square from which you just came) as follows. Call goNorth; if it is not successful, call goWest and, if it is not suc-cessful, call goEast. If goEast is not successful, mark this square as visited, move back into the square to the south, and return.
The following pseudocode describes the goNorth algorithm:
goNorth(maze, creature)
if (the square to the north is clear, inside the maze, and unvisited)
{
Move to the north
Mark the square as part of the path
if (at exit)
success = true
else
{
success = goNorth(maze, creature)
if (!success)
{
success = goWest(maze, creature)
if (!success)
{
success = goEast(maze, creature)
if (!success)
{
Mark square visited
Backtrack south
}
}
}
}
}
else
success = false
return success
The goWest function will examine all the paths that start at the square to the west of the present square as follows. If the square directly to the west is clear, is inside the maze, and has not been visited before, move into this square and mark it as part of the path. (Note that you are moving from the east.) Check whether you are at the exit. If you are, youre done. Otherwise, try to nd a path to the exit from here by trying all paths leaving this square except the one going east (this would put you back in the square from which you just came) as follows. Call goNorth; if it is not successful, call goWest; and if it is not successful, call goSouth. If goSouth is not suc-cessful, mark this square as visited, move back into the square to the east, and return. The functions goEast and goSouth are analogous to goWest and goNorth. The input data to represent a maze is simple. For example, the previously given maze is represented by the following lines of input, which can be in a text le:
20 7
0 18
6 12
After the rst three lines of numeric data in the le, each of the next lines corresponds to a row in the maze, and each character in a line corresponds to a column in the maze. An x indicates a blocked square (part of the wall), and a blank indicates a clear square. This notation is convenient, because you can see what the maze looks like as you design it.
WORK SO FAR
Maze.cpp~
****************************************************** #include "Maze.h" #include Maze.h ****************************************************** // Maze ADT #ifndef MAZE_H #define MAZE_H #include #include #include #include "Location.h" // A maze is made of row*col squares // the state of a square can be // CLEAR: the creature can move to the square // WALL: the square is blocked (part of a wall) // VISITED: the creature has already been on that square // PATH: this square is part of a path that leads to the exit // const int CLEAR=0; const int WALL=1; const int VISITED=2; const int PATH=3; class Maze{ public: // Constructor // ----------- // Read the maze from the file filename // The creature is at the entrance Maze(std::string filename); // Other public member functions // ----------------------------- // Move the creature // return true if the creature can be moved bool goNorth(); bool goSouth(); bool goEast(); bool goWest(); // Is the creature out bool isOut() const; // Display a solution path from the current creature position void Maze::showPath(std::ostream& out); // friend function // --------------- // Display the maze friend std::ostream& operator private: // Dimensions of the maze int row; int col; // 2D array to represent the maze (on the heap) int** maze; // entrance and exit points of the maze Location entrance; Location exit; // Location of the creature Location creature; // auxiliary functions // ------------------- // find a winning path from l // if found set success to true void Maze::findPath(Location l, bool& success); // Mark the square in row r and column c as VISITED // Precondition: l must be inside of the maze // there is no wall as l void markSquareAsVisited(const Location& l); // Mark the square in Location l as PATH // Precondition: l must be inside of the maze // there is no wall as l void markSquareAsPath(const Location& l); // What is on square in Location l? // Precondition: l must be inside of the maze int square(const Location& l) const; // Is this location part of the maze? bool insideMaze(const Location& l) const; }; #endif Location.h ****************************************************** // ADT location #ifndef LOCATION_H #define LOCATION_H // simple struct to store a location in the maze // r and c are the row and column of the location in the maze struct Location{ int r; int c; // Comparison of 2 locations bool operator==(const Location& l) const { return (r==l.r && c==l.c); } bool operator!=(const Location& l) const { return (r!=l.r || c!=l.c); } }; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started