Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ps5.h"
- #include <cassert>
- /////////////////////
- // Private helpers //
- /////////////////////
- int Maze::getArrayIndex(const Location loc) const{
- int loc_index = loc.row*(2*numCols+1)+loc.col;
- return loc_index;
- }
- Location Maze::getCellArrayCoord(int cellRow, int cellCol) const{
- assert(!(cellRow<0));
- assert(!(cellRow>numRows));
- assert(!(cellCol<0));
- assert(!(cellCol>numCols));
- int exp_r = 2*cellRow+1;
- int exp_c = 2*cellCol+1;
- Location exp_cell = Location(exp_r, exp_c);
- return exp_cell;
- }
- Location Maze::getWallArrayCoord(int cellRow, int cellCol, Direction direction) const{
- Location exp_wall = getCellArrayCoord(cellRow, cellCol);
- switch (direction) {
- case Direction::NORTH :
- exp_wall.row--;
- break;
- case Direction::SOUTH :
- exp_wall.row++;
- break;
- case Direction::EAST :
- exp_wall.col++;
- break;
- case Direction::WEST :
- exp_wall.col--;
- break;
- default :
- assert(false);
- }
- return exp_wall;
- }
- //////////////////////////////////////////
- // Constructors - Deconstructors - Copy //
- //////////////////////////////////////////
- Maze::Maze(int rows, int cols){
- assert(!(rows<=0));
- assert(!(cols<=0));
- int exp_size = (2*rows+1)*(2*cols+1);
- numRows = rows;
- numCols = cols;
- cells = new MazeCell[exp_size];
- for (int i=0; i<exp_size; ++i){
- cells[i] = MazeCell::EMPTY;
- }
- start = Location();
- end = Location();
- }
- Maze::Maze(const Maze &m){
- int exp_size = (2*m.getNumRows() + 1)*(2*m.getNumCols() + 1);
- numRows = m.getNumRows();
- numCols = m.getNumCols();
- cells = new MazeCell[exp_size];
- clear();
- for (int i = 0; i < m.getNumRows(); ++i) {
- for (int j = 0; j < m.getNumCols(); ++j) {
- if (m.hasWall(i, j, Direction::NORTH)){
- setWall(i, j, Direction::NORTH);
- }
- if (m.hasWall(i, j, Direction::SOUTH)){
- setWall(i, j, Direction::SOUTH);
- }
- 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.isVisited(i,j)){
- setVisited(i,j);
- }
- }
- }
- start = m.getStart();
- end = m.getEnd();
- }
- Maze::~Maze(){
- delete[] cells;
- }
- ///////////////////////
- // Operator overload //
- ///////////////////////
- Maze& Maze::operator=(const Maze &m){
- if (this != &m){
- delete[] cells;
- int exp_size = (2*m.getNumRows() + 1)*(2*m.getNumCols() + 1);
- numRows=m.getNumRows();
- numCols=m.getNumCols();
- cells = new MazeCell[exp_size];
- clear();
- for (int i = 0; i < m.getNumRows(); ++i) {
- for (int j = 0; j < m.getNumCols(); ++j) {
- if (m.hasWall(i, j, Direction::NORTH)){
- setWall(i, j, Direction::NORTH);
- }
- if (m.hasWall(i, j, Direction::SOUTH)){
- setWall(i, j, Direction::SOUTH);
- }
- 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.isVisited(i,j)){
- setVisited(i,j);
- }
- }
- }
- start = m.getStart();
- end = m.getEnd();
- }
- 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 exp_cell = getCellArrayCoord(cellRow, cellCol);
- int exp_cell_index = getArrayIndex(exp_cell);
- return cells[exp_cell_index];
- }
- Location Maze::getNeighborCell(int cellRow, int cellCol, Direction direction) const{
- assert(!(cellRow<0));
- assert(!(cellRow>numRows));
- assert(!(cellCol<0));
- assert(!(cellCol>numCols));
- switch (direction) {
- case Direction::NORTH :
- assert(!(cellRow == 0));
- cellRow--;
- break;
- case Direction::SOUTH :
- assert(!(cellRow == numRows));
- cellRow++;
- break;
- case Direction::EAST :
- assert(!(cellCol == numCols));
- cellCol++;
- break;
- case Direction::WEST :
- assert(!(cellCol == 0));
- cellCol--;
- break;
- default :
- assert(false);
- }
- Location n_cell = Location(cellRow, cellCol);
- return n_cell;
- }
- bool Maze::hasWall(int cellRow, int cellCol, Direction direction) const{
- Location cwall = getWallArrayCoord(cellRow, cellCol, direction);
- int cwall_index = getArrayIndex(cwall);
- if (cells[cwall_index] == MazeCell::WALL){
- return true;
- }else{
- return false;
- }
- }
- bool Maze::isVisited(int cellRow, int cellCol) const{
- Location exp_cell = getCellArrayCoord(cellRow, cellCol);
- int exp_cell_index = getArrayIndex(exp_cell);
- if (cells[exp_cell_index] == MazeCell::VISITED){
- return true;
- }else{
- return false;
- }
- }
- ////////////////
- // Operations //
- ////////////////
- void Maze::setStart(int row, int col){
- assert(!(row<0));
- assert(!(row>numRows));
- assert(!(col<0));
- assert(!(col>numCols));
- Location new_start = Location(row, col);
- start = new_start;
- return;
- }
- void Maze::setEnd(int row, int col){
- assert(!(row < 0));
- assert(!(row > numRows));
- assert(!(col < 0));
- assert(!(col > numCols));
- Location new_end = Location(row, col);
- end = new_end;
- return;
- }
- void Maze::clear(){
- int exp_size = (2*numRows+1)*(2*numCols+1);
- for (int i=0; i<exp_size; ++i){
- cells[i] = MazeCell::EMPTY;
- }
- return;
- }
- void Maze::setAllWalls(){
- int exp_size = (2*numRows+1)*(2*numCols+1);
- for (int i=1; i<exp_size; i=i+2){
- cells[i] = MazeCell::WALL;
- }
- return;
- }
- void Maze::setCell(int cellRow, int cellCol, MazeCell val){
- Location exp_cell = getCellArrayCoord(cellRow, cellCol);
- int exp_cell_index = getArrayIndex(exp_cell);
- cells[exp_cell_index] = val;
- return;
- }
- void Maze::setWall(int cellRow, int cellCol, Direction direction){
- Location exp_cell = getWallArrayCoord(cellRow, cellCol, direction);
- int exp_cell_index = getArrayIndex(exp_cell);
- cells[exp_cell_index] = MazeCell::WALL;
- return;
- }
- void Maze::clearWall(int cellRow, int cellCol, Direction direction){
- Location exp_cell = getWallArrayCoord(cellRow, cellCol, direction);
- int exp_cell_index = getArrayIndex(exp_cell);
- cells[exp_cell_index] = MazeCell::EMPTY;
- return;
- }
- void Maze::setVisited(int cellRow, int cellCol){
- Location exp_cell = getCellArrayCoord(cellRow, cellCol);
- int exp_cell_index = getArrayIndex(exp_cell);
- cells[exp_cell_index] = MazeCell::VISITED;
- return;
- }
- void Maze::print(ostream &os) const{
- int R = getNumRows();
- int C = getNumCols();
- int ext_size = (2*R+1)*(2*C+1);
- Location exp_S = getCellArrayCoord(start.row, start.col);
- int S_index = getArrayIndex(exp_S);
- Location exp_E = getCellArrayCoord(end.row, end.col);
- int E_index = getArrayIndex(exp_E);
- os << R << " " << C << endl;
- for (int i=0; i<ext_size; i=i+(2*C+1)){
- for (int j=i; j<(i+(2*C+1)); j++){
- if (i%2==0){
- if (j%2==0){
- os << "+";
- }else{
- if (cells[j]==MazeCell::WALL){
- os << "---";
- }else{
- os << " ";
- }
- }
- }else {
- if (j%2==1){
- if (cells[j]==MazeCell::WALL){
- os << "|";
- }else{
- os << " ";
- }
- }else{
- if (j==S_index){
- os << " S ";
- }else if (j==E_index){
- os << " E ";
- }else{
- os << " ";
- }
- }
- }
- }
- os << endl;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement