You will write program that reads in a coded map (of the maze) and prints it out
ID: 667016 • Letter: Y
Question
You will write program that reads in a coded map (of the maze) and prints it out as a map. The format of the input file will be lines that contain the following:- @ Two example lines:- 20 17 @A 21 29 @2 Valid room coordinates range from 1-30. If a coordinate is outside the range 0-30 it is a bad map, and the program should catch that and exit with error 1. If there are additional characters on the line it is still a good map, but the additional characters should be ignored. The input data can just end, or it can end with a line containing a coordinate that is zero. Any lines past that should be ignored. If the input file just ends, a map is produced as output, otherwise not. The output should be a 30x30 square grid. A period should be used to indicate a room that is not accessible. The other rooms should be marked with the code letter.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
/********* Maze class****/
class Maze
{
public:
Maze(int rowsq, int colsq);
int map_row() const;
int map_col() const;
int cellOpen(int k1, int k2) const;
void FillMark(int rowq, int colq, char cellMarker);
void printMazeMap();
int fillMaze(ifstream& mazeInput);
private:
char mazeMap[30][30];
int map_row;
int map_col;
};
/
static void errorFillMaze(const char* err1_msg)
{
cout << "ERROR OCCURED: " << err1_msg<< endl;
exit(1);
}
/***** CONSTRUCTOR*****/
Maze::Maze(int rowsq, int colsq)
{
map_row = rowsq;
map_col = colsq;
for(int k1 = 1; k1 <= rowsq; k1++)
for(int k2 = 1; k2 <= colsq; k2++)
mazeMap[k1][k1] = '.';
}
int Maze::map_row() const
{ return map_row;
}
int Maze::map_col() const
{
return map_col;
}
/**** FIND WHETHER THE CELL IS OPEN****/
int Maze::cellOpen(int k1, int k2) const
{
if (k2 < 1 || k2 > map_row)
errorFillMaze("CELL ROW IS OUTSIDE");
if (k2 < 1 || k2> map_
col)
errorFillMaze("CELL COLUMN IS OUTSIDE");
if (mazeMap[k1][k2] == '.')
return 1;
else
return 0;
}
/***FILL CELL IN THE MAZE*****/
void Maze::FillMark(int rowq, int colq, char cellMarker)
{
if (!cellOpen(rowq,colq))
errorFillMaze("NOT VALID CELL");
mazeMap[rowq][colq] = cellMarker;
}
/****** PRINT THE MAZE******/
void Maze::printMazeMap()
{
cout<<"MAZE MAP"<<endl;
for(int k1 = 1; k1 <= map_row; k1++)
{
for(int k2 = 1; k2 <= map_col; k2++)
cout<<" "<<mazeMap[k1][k2];
cout << endl;
}
}
/***** READ INPUT FOR THE CELL IN THE MAZE FROM FILE********/
int Maze::fillMaze(ifstream& mazeInput)
{
int rowValue,colValue;
string mark;
char c[2];
while(mazeInput>>rowValue>>colValue>>mark)
{
FillMark(rowValue,colValue,mark[1]);
mazeInput.ignore(" ");
}
}
/****** MAIN METHOD******/
int main()
{
Maze myMaze(30,30);
ifstream mazeInputFile;
mazeInputFile.open("mazeFile.txt");
myMaze.printMazeMap();
myMaze.fillMaze(mazeInputFile);
myMaze.printMazeMap();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.