I need help with this: Part 2: Recursively searching a Maze. Your next task is t
ID: 3640419 • Letter: I
Question
I need help with this:Part 2: Recursively searching a Maze. Your next task is to write a program that simulates the movements of an untrained mouse searching for a path through a maze from a starting point S to an ending point E. To do this, you must define a recursive function that calls itself to move N,S,E,W through open space starting at S and ending at E. For example, if the mouse is at location (row,col) and there is a wall at (row+1,col) and the mouse has already visited (row-1,col) then you must recursively search (row,col+1) and (row,col-1) to see if they eventually lead to the ending point E.
If you manually search the maze above, you should see that the there are times when the mouse will reach a dead end and they must "backtrack" to find the correct path. In order for your program to support this you must store and update information about which (row,col) Maze positions have been visited by the mouse and which positions are part of the final path from S to E.
Finally, when your program solves the maze, you need to output/display this information in some way. One option would be to print a sequence of (row,col) coordinates taken by the mouse. Another option would be to display the correct path on the maze itself with another character. This output/display may also be of help when debugging your recursive maze search function.
AND HERE IS WHAT I HAVE(IT COMPILES PERFECTLY):
Maze.h
#include
#include
#include
using namespace std;
//-----------------------------------------------------------
// Define the Maze class interface
//-----------------------------------------------------------
class Maze
{
public:
// Constructors
Maze();
~Maze();
// Methods
void ReadMaze(string name);
void WriteMaze(string name);
void PrintMaze();
void GetMaze(int r, int c, char &value);
void SetMaze(int r, int c, char value);
void GetStart(int &r, int &c);
void SetStart(int r, int c);
void GetEnd(int &r, int &c);
void SetEnd(int r, int c);
private:
int num_rows, num_cols;
int start_row, start_col;
int end_row, end_col;
char maze[100][100];
};
Maze.cpp
#include "Maze.h"
#include
#include
using namespace std;
//-----------------------------------------------------------
// Constructor function
//-----------------------------------------------------------
Maze::Maze()
{
num_rows = 0;
num_cols = 0;
start_row = 0;
start_col = 0;
end_row = 0;
end_col = 0;
}
//-----------------------------------------------------------
// Destructor function
//-----------------------------------------------------------
Maze::~Maze()
{
}
//-----------------------------------------------------------
// Read maze from ascii file
//-----------------------------------------------------------
void Maze::ReadMaze(string name)
{
// Read maze.txt header
ifstream din;
din.open(name.c_str());
din >> num_rows >> num_cols;
din >> start_row >> start_col;
din >> end_row >> end_col;
// Read maze.txt body
string line;
getline(din, line);
for (int r=0; r {
getline(din, line);
for (int c=0; c if (c < (int)line.length())
maze[r][c] = line[c];
}
din.close();
}
//-----------------------------------------------------------
// Write maze to ascii file
//-----------------------------------------------------------
void Maze::WriteMaze(string name)
{
// Write maze.txt header
ofstream dout;
dout.open(name.c_str());
dout << num_rows << " " << num_cols << endl;
dout << start_row << " " << start_col << endl;
dout << end_row << " " << end_col << endl;
// Write maze.txt body
for (int r=0; r {
for (int c=0; c dout << maze[r][c];
dout << endl;
}
dout.close();
}
//-----------------------------------------------------------
// Print maze to screen
//-----------------------------------------------------------
void Maze::PrintMaze()
{
// Print maze
for (int r=0; r {
for (int c=0; c cout << maze[r][c];
cout << endl;
}
}
//-----------------------------------------------------------
// Get start location
//-----------------------------------------------------------
void Maze::GetStart(int &r, int &c)
{
r = start_row;
c = start_col;
}
//-----------------------------------------------------------
// Set start location
//-----------------------------------------------------------
void Maze::SetStart(int r, int c)
{
start_row = r;
start_col = c;
}
//-----------------------------------------------------------
// Get end location
//-----------------------------------------------------------
void Maze::GetEnd(int &r, int &c)
{
r = end_row;
c = end_col;
}
//-----------------------------------------------------------
// Set end location
//-----------------------------------------------------------
void Maze::SetEnd(int r, int c)
{
end_row = r;
end_col = c;
}
main.cpp
#include "Maze.h"
int main()
{
Maze m;
m.ReadMaze("maze.txt");
m.PrintMaze();
m.WriteMaze("maze.out");
return 0;
}