The goal of this programming assignment is to implement a \"mouse in a maze\" si
ID: 3640067 • Letter: T
Question
The goal of this programming assignment is to implement a "mouse in a maze" simulation using recursion. You will be given a number of mazes stored in ascii files, and your program must calculate and output the path a mouse must take to go from a starting point S to the ending point E without going through any walls in the maze.I just need part 1, which is read the file and display the maze.
Part 1: Creating and testing a Maze class. To start with you need some way to represent the maze and an interface for your program to check to see if a position P is the start point S, the end point E, a wall, or open space. To keep things simple, assume that your maze file has the following format:
int int
int int
int int
cccccccccccccccccccc
cccccccccccccccccccc
cccccccccccccccccccc
cccccccccccccccccccc
cccccccccccccccccccc
cccccccccccccccccccc
where the first two integers are the num_rows and num_cols of the maze, the second two integers are the (row,col) location of the starting point, and the third two integers are the (row,col) location of the ending point. After the integers there are height lines of width characters with 'x' for wall, and ' ' for open space. For example, here is a simple maze:
7 20
6 12
0 18
xxxxxxxxxxxxxxxxxx x
x xxxx x
x xxxxx xxxx xx x
x xxxxx xxxxxxx xx x
x xx xx xx x
xxxxxxxxxxxx xx x
xxxxxxxxxxxx xxxxxxx
You must create and test a Maze class that stores the information above in private variables and implements the following methods: "ReadMaze(filename)", "WriteMaze(filename)", "PrintMaze()", "getMaze(row,col)", "setMaze(row,col,value)", "getStart(row,col)", "setStart(row,col)". "getEnd(row,col)", and "setEnd(row,col)".
Explanation / Answer
Here is the basic outline of the file input/output for the Maze Class:
This is the Maze.h header file. It includes the reading of the file, and the printing of the file, and that is all.
And it reads this file in perfectly, so copy this and put it in a text file named Maze.txt, and put it in the root folder of the program and it will work fine:
10 20
0 4
9 13
xxx xxxxxxxxxxxxxxxx
xxx xxxxxxxxxxxxxxxx
xxx xxxxxxxxxxxxxxxx
xxx xxxxxxxxxxxxxx
xxxxx xxxxxxxxxxxxxx
xxxxx xxxxxxxxxxx
xxxxxxxx xxxxxxxxxxx
xxxxxxxx xxxxxxx
xxxxxxxxxxxx xxxxxxx
xxxxxxxxxxxx xxxxxxx
Maze.h
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class Maze
{
private:
int _numRows;
int _numCols;
int _startRow;
int _startCol;
int _endRow;
int _endCol;
char **theMaze;
public:
void ReadMaze(string fileName)
{
ifstream file(fileName); //open the file
string convert1;
stringstream convert2; //These are to convert from the text of the file to actual ints
//Read in all the rows and cols variables
file >> convert1; //_numRows;
convert2 << convert1;
convert2 >> _numRows;
cout << _numRows << endl;
convert2.clear();
file >> convert1;//_numCols
convert2 << convert1;
convert2 >> _numCols;
++_numCols; //The +1 will hold all of the characters in the file
cout << _numCols << endl;
convert2.clear();
file >> convert1;//_startRow
convert2 << convert1;
convert2 >> _startRow;
cout << _startRow << endl;
convert2.clear();
file >> convert1;//_startCol
convert2 << convert1;
convert2 >> _startCol;
cout << _startCol << endl;
convert2.clear();
file >> convert1;//_endRow
convert2 << convert1;
convert2 >> _endRow;
cout << _endRow << endl;
convert2.clear();
file >> convert1;//_endCol
convert2 << convert1;
convert2 >> _endCol;
cout << _endCol << endl;
theMaze = new char*[_numRows]; //Create the board
for(int i = 0; i < _numRows; ++i)
theMaze[i] = new char[_numCols];
char temp;
for(int i = 0; i < _numRows; ++i)
{
for(int j = 0; j < _numCols; ++j)
{
temp = file.get();
theMaze[i][j] = temp;
}
}
//Set the start and the end points
theMaze[_startRow][_startCol] = 'S';
theMaze[_endRow][_endCol] = 'E';
}
void PrintMaze()
{
for(int i = 0; i < _numRows; ++i)
{
for(int j = 0; j < _numCols; ++j)
{
cout << theMaze[i][j];
}
}
}
};
Here is my Main.cpp incase you want to test this as it is:
#include "Maze.h"
int main()
{
Maze myMaze;
myMaze.ReadMaze("Maze.txt");
myMaze.PrintMaze();
cout << endl << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.