#include #include #include #include "maze.h" #define MAZE(row,col) maze[(row*mazecols)+col] #define BLANK_SQUARE '.' #define WALL_SQUARE '#' #define START_SQUARE 'S' #define GOAL_SQUARE 'X' #define CURR_SQUARE 'O' #define BLANK_SQUARE_VISITED ':' #define GOAL_SQUARE_VISITED '*' void Maze::NewPos(direction dir, int& newrow, int& newcol) { newrow = currow; newcol = curcol; switch (dir) { case north: newrow--; break; case south: newrow++; break; case east: newcol++; break; case west: newcol--; break; default: cerr << "ERROR: unrecognized direction" << endl; exit(6); break; } } Maze::Maze(char* filename) { ifstream infile(filename, ios::in | ios::nocreate); int i; int j; char achar; if (infile == NULL) { cerr << "ERROR: cannot open file \"" << filename << "\"" << endl; exit(1); } infile >> mazerows >> mazecols; maze = new char[mazerows*mazecols]; currow = -1; curcol = -1; goalrow = -1; goalcol = -1; for (i=0; i> achar; MAZE(i,j) = achar; if (achar == START_SQUARE) { if (currow == -1) { currow = i; curcol = j; } else { cerr << "ERROR: maze contains two start positions: (" << currow << "," << curcol << ") and (" << i << "," << j << ")" << endl; exit(2); } } if (achar == GOAL_SQUARE) { if (goalrow == -1) { goalrow = i; goalcol = j; } else { cerr << "ERROR: maze contains two goal positions: (" << goalrow << "," << goalcol << ") and (" << i << "," << j << ")" << endl; exit(3); } } } } if (currow == -1) { cerr << "ERROR: maze contains no start position" << endl; exit(4); } if (goalrow == -1) { cerr << "ERROR: maze contains no goal position" << endl; exit(5); } numlooks = 0; nummoves = 0; numjumps = 0; } Maze::Maze(const Maze& rhs) { cerr << "ERROR: You may not make copies of the maze; pass by reference if necessary" << endl; exit(10); } const Maze& Maze::operator=(const Maze& rhs) { cerr << "ERROR: You may not make copies of the maze" << endl; exit(11); return NULL; // This is just to satisfy MSVC++ } Maze::~Maze() { cout << "Operation summary" << endl; cout << "-----------------" << endl; cout << "Looks: " << numlooks << endl; cout << "Moves: " << nummoves << endl; cout << "Jumps: " << numjumps << endl; } void Maze::Size(int& numrows, int& numcols) { numrows = mazerows; numcols = mazecols; } void Maze::Position(int& row, int& col) { row = currow; col = curcol; } void Maze::Goal(int& row, int& col) { row = goalrow; col = goalcol; } status Maze::Look(direction dir) { int lookrow, lookcol; char mazeloc; numlooks++; NewPos(dir,lookrow,lookcol); mazeloc = MAZE(lookrow,lookcol); switch (mazeloc) { case BLANK_SQUARE: case BLANK_SQUARE_VISITED: case START_SQUARE: return POS_CLEAR; case GOAL_SQUARE: case GOAL_SQUARE_VISITED: return POS_GOAL; case WALL_SQUARE: return POS_BLOCKED; default: cerr << "ERROR: unrecognized maze character `" << mazeloc << "`" << endl; exit(7); return POS_BLOCKED; // This is to satisfy MSVC++ } } status Maze::Move(direction dir) { int newrow, newcol; char mazeloc; status retval; NewPos(dir,newrow,newcol); mazeloc = MAZE(newrow,newcol); switch (mazeloc) { case BLANK_SQUARE: MAZE(newrow,newcol) = BLANK_SQUARE_VISITED; case BLANK_SQUARE_VISITED: case START_SQUARE: retval = POS_CLEAR; break; case GOAL_SQUARE: MAZE(newrow,newcol) = GOAL_SQUARE_VISITED; case GOAL_SQUARE_VISITED: retval = POS_GOAL; break; case WALL_SQUARE: numlooks++; return POS_BLOCKED; default: cerr << "ERROR: unrecognized maze character `" << mazeloc << "`" << endl; exit(8); } currow = newrow; curcol = newcol; nummoves++; return retval; } status Maze::Jump(int row, int col) { char mazeloc; status retval; numjumps++; mazeloc = MAZE(row,col); switch (mazeloc) { case BLANK_SQUARE: case GOAL_SQUARE: return POS_UNVISITED; case START_SQUARE: case BLANK_SQUARE_VISITED: retval = POS_CLEAR; break; case GOAL_SQUARE_VISITED: retval = POS_GOAL; break; case WALL_SQUARE: return POS_BLOCKED; default: cerr << "ERROR: unrecognized maze character `" << mazeloc << "`" << endl; exit(9); } currow = row; curcol = col; return retval; } void Maze::Print(int pretty) { int i, j; char mazeloc; for (i=0;i