Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "maze.hh"
- #include <assert.h>
- using namespace std;
- /////////////////////
- // Private helpers //
- /////////////////////
- int Maze::getArrayIndex(const Location loc) const{
- int cell;
- assert(loc.row >= 0 );
- assert(loc.row <= 2 * numRows + 1);
- assert(loc.col >= 0 );
- assert(loc.col <= 2 * numCols + 1);
- cell = loc.row *(2 * numCols + 1) + loc.col ;
- return cell;
- }
- Location Maze::getCellArrayCoord(int cellRow, int cellCol) const{
- int ExpandedRow = 2 * cellRow + 1;
- int ExpandedCol = 2 * cellCol + 1;
- Location loc(ExpandedRow, ExpandedCol);
- return loc;
- }
- Location Maze::getWallArrayCoord(int cellRow, int cellCol,
- Direction direction) const{
- Location loc;
- loc = getCellArrayCoord(cellRow, cellCol);
- int wallRow, wallCol;
- switch (direction)
- {
- case Direction::NORTH: //North
- wallRow = loc.row - 1;
- wallCol = loc.col;
- break;
- case Direction::EAST: //East
- wallCol = loc.col + 1;
- wallRow = loc.row;
- break;
- case Direction::WEST: //West
- wallCol = loc.col - 1;
- wallRow = loc.row;
- break;
- case Direction::SOUTH: //South
- wallRow = loc.row + 1;
- wallCol = loc.col;
- break;
- }
- assert(loc.row >= 0);
- assert(loc.col >= 0);
- Location helper(wallRow, wallCol);
- return helper;
- }
- //////////////////////////////////////////
- // Constructors - Deconstructors - Copy //
- //////////////////////////////////////////
- Maze::Maze(int rows, int cols) {
- numRows = rows;
- numCols = cols;
- int size = (2 * numRows + 1)*(2 * numCols + 1), i = 0;
- cells = new MazeCell[size];
- for (i = 0; i < size; i++)
- {
- cells[i] = MazeCell::EMPTY;
- }
- }
- Maze::Maze(const Maze &m) {
- numCols = m.getNumCols();
- numRows = m.getNumRows();
- start = m.getStart();
- end = m.getEnd();
- int size = (2 * numRows + 1)*(2 * numCols + 1), i = 0;
- cells = new MazeCell[size];
- clear();
- for (int i = 0; i < numRows; i++)
- {
- for (int j = 0; j < numCols; j++)
- {
- setCell(i, j, m.getCell(i, j));
- if (m.hasWall(i, j, Direction::NORTH))
- {
- setWall(i, j, Direction::NORTH);
- }
- if (m.hasWall(i, j, Direction::EAST))
- {
- setWall(i, j, Direction::EAST);
- }
- if (m.hasWall(i, j, Direction::WEST))
- {
- setWall(i, j, Direction::WEST);
- }
- if (m.hasWall(i, j, Direction::SOUTH))
- {
- setWall(i, j, Direction::SOUTH);
- }
- }
- }
- }
- Maze::~Maze(){
- delete[] cells;
- }
- ///////////////////////
- // Operator overload //
- ///////////////////////
- Maze& Maze::operator=(const Maze &m){
- if (this == &m)
- return *this;
- numCols = m.getNumCols();
- numRows = m.getNumRows();
- start = m.getStart();
- end = m.getEnd();
- int size = (2 * numRows + 1)*(2 * numCols + 1), i = 0;
- cells = new MazeCell[size];
- clear();
- for (int i = 0; i < numRows; i++)
- {
- for (int j = 0; j < numCols; j++)
- {
- setCell(i, j, m.getCell(i, j));
- if (m.hasWall(i, j, Direction::NORTH))
- {
- setWall(i, j, Direction::NORTH);
- }
- if (m.hasWall(i, j, Direction::EAST))
- {
- setWall(i, j, Direction::EAST);
- }
- if (m.hasWall(i, j, Direction::WEST))
- {
- setWall(i, j, Direction::WEST);
- }
- if (m.hasWall(i, j, Direction::SOUTH))
- {
- setWall(i, j, Direction::SOUTH);
- }
- }
- }
- return *this;
- }
- ///////////////
- // Accessors //
- ///////////////
- int Maze::getNumRows() const{
- return numRows;
- }
- int Maze::getNumCols() const{
- return numCols;
- }
- Location Maze::getStart() const{
- return start;
- }
- Location Maze::getEnd() const{
- return end;
- }
- MazeCell Maze::getCell(int cellRow, int cellCol) const{
- Location loc;
- loc = getCellArrayCoord(cellRow, cellCol);
- int position = getArrayIndex(loc);
- return cells[position];
- }
- Location Maze::getNeighborCell(int cellRow, int cellCol, Direction direction) const{
- int NeighRow, NeighCol;
- switch (direction)
- {
- case Direction::NORTH: //North
- NeighRow = cellRow - 1;
- NeighCol = cellCol;
- break;
- case Direction::EAST: //East
- NeighCol = cellCol + 1;
- NeighRow = cellRow;
- break;
- case Direction::WEST: //West
- NeighCol = cellCol - 1;
- NeighRow = cellRow;
- break;
- case Direction::SOUTH: //South
- NeighRow = cellRow + 1;
- NeighCol = cellCol;
- break;
- }
- Location helper(NeighRow, NeighCol);
- return helper;
- }
- bool Maze::hasWall(int cellRow, int cellCol, Direction direction) const{
- Location loc;
- loc = getWallArrayCoord(cellRow, cellCol, direction);
- int position = getArrayIndex(loc);
- if (cells[position] == MazeCell::WALL)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool Maze::isVisited(int cellRow, int cellCol) const{
- Location loc;
- loc = getCellArrayCoord(cellRow, cellCol);
- int position = getArrayIndex(loc);
- if (cells[position] == MazeCell::VISITED)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- ////////////////
- // Operations //
- ////////////////
- void Maze::setStart(int row, int col){
- start.row = row;
- start.col = col;
- }
- void Maze::setEnd(int row, int col){
- end.row = row;
- end.col = col;
- }
- void Maze::clear() {
- int size = (2 * numRows + 1)*(2 * numCols + 1), i = 0;
- for (i = 0; i < size; i++)
- {
- cells[i] = MazeCell::EMPTY;
- }
- }
- void Maze::setAllWalls(){
- int k, size = (2 * numRows + 1)*(2 * numCols + 1);
- for (k = 1; k < size; k=k+2)
- {
- cells[k] = MazeCell::WALL;
- }
- }
- void Maze::setCell(int cellRow, int cellCol, MazeCell val){
- Location loc;
- loc = getCellArrayCoord(cellRow, cellCol);
- int position = getArrayIndex(loc);
- cells[position] = val;
- }
- void Maze::setWall(int cellRow, int cellCol, Direction direction){
- Location loc;
- loc = getWallArrayCoord(cellRow, cellCol, direction);
- int position = getArrayIndex(loc);
- cells[position] = MazeCell::WALL;
- }
- void Maze::clearWall(int cellRow, int cellCol, Direction direction){
- Location loc;
- loc = getWallArrayCoord(cellRow, cellCol, direction);
- int position = getArrayIndex(loc);
- cells[position] = MazeCell::EMPTY;
- }
- void Maze::setVisited(int cellRow, int cellCol){
- Location loc;
- loc = getCellArrayCoord(cellRow, cellCol);
- int position = getArrayIndex(loc);
- cells[position] = MazeCell::VISITED;
- }
- void Maze::print(ostream &os) const{
- Location loc;
- os << numRows << " " << numCols << endl;
- int i=0,j;
- for (i = 0; i < numRows ; i++)
- {
- os << "+"; //the first + of each row
- for (j = 0; j < numCols; j++)
- {
- if (hasWall(i, j, Direction::NORTH))
- {
- os << "---+"; //print the wall
- }
- else
- os << " +"; //if there is no wall
- }
- os<< endl;
- for (j = 0; j < numCols; j++)
- {
- if (hasWall(i, j, Direction::WEST))
- {
- loc = Location(i, j);
- if (loc==start)
- {
- os << "| S "; //print S
- }
- else if (loc==end)
- {
- os << "| E "; //print E
- }
- else
- {
- os << "| "; //
- }
- }else
- {
- os << " "; //if there is no wall
- }
- if (j == numCols - 1) //the last wall of each row
- os << "|";
- }
- os<< endl;
- }
- os << "+"; //for
- for (i = 0; i < numCols; i++) //the
- os << "---+"; //last
- os << endl; //row
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement