Advertisement
Guest User

ps5

a guest
Dec 13th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. #include "ps5.h"
  2. #include <cassert>
  3.  
  4. /////////////////////
  5. // Private helpers //
  6. /////////////////////
  7.  
  8. int Maze::getArrayIndex(const Location loc) const{
  9.  
  10.   int loc_index = loc.row*(2*numCols+1)+loc.col;
  11.   return loc_index;
  12. }
  13.  
  14. Location Maze::getCellArrayCoord(int cellRow, int cellCol) const{
  15.  
  16.   assert(!(cellRow<0));
  17.   assert(!(cellRow>numRows));
  18.   assert(!(cellCol<0));
  19.   assert(!(cellCol>numCols));
  20.  
  21.   int exp_r = 2*cellRow+1;
  22.   int exp_c = 2*cellCol+1;
  23.  
  24.   Location exp_cell = Location(exp_r, exp_c);
  25.   return exp_cell;
  26. }
  27.  
  28. Location Maze::getWallArrayCoord(int cellRow, int cellCol, Direction direction) const{
  29.  
  30.   Location exp_wall = getCellArrayCoord(cellRow, cellCol);
  31.  
  32.   switch (direction) {
  33.     case Direction::NORTH :
  34.       exp_wall.row--;
  35.       break;
  36.     case Direction::SOUTH :
  37.       exp_wall.row++;
  38.       break;
  39.     case Direction::EAST :
  40.       exp_wall.col++;
  41.       break;
  42.     case Direction::WEST :
  43.       exp_wall.col--;
  44.       break;
  45.     default :
  46.       assert(false);
  47.   }
  48.  
  49.   return exp_wall;
  50. }
  51.  
  52. //////////////////////////////////////////
  53. // Constructors - Deconstructors - Copy //
  54. //////////////////////////////////////////
  55.  
  56. Maze::Maze(int rows, int cols){
  57.  
  58.   assert(!(rows<=0));
  59.   assert(!(cols<=0));
  60.  
  61.   int exp_size = (2*rows+1)*(2*cols+1);
  62.  
  63.   numRows = rows;
  64.   numCols = cols;
  65.  
  66.   cells = new MazeCell[exp_size];
  67.   for (int i=0; i<exp_size; ++i){
  68.     cells[i] = MazeCell::EMPTY;
  69.   }
  70.  
  71.   start = Location();
  72.     end = Location();
  73. }
  74.  
  75. Maze::Maze(const Maze &m){
  76.  
  77.   int exp_size = (2*m.getNumRows() + 1)*(2*m.getNumCols() + 1);
  78.  
  79.   numRows = m.getNumRows();
  80.   numCols = m.getNumCols();
  81.  
  82.   cells = new MazeCell[exp_size];
  83.   clear();
  84.  
  85.   for (int i = 0; i < m.getNumRows(); ++i) {
  86.         for (int j = 0; j < m.getNumCols(); ++j) {
  87.          
  88.           if (m.hasWall(i, j, Direction::NORTH)){
  89.           setWall(i, j, Direction::NORTH);
  90.         }
  91.  
  92.         if (m.hasWall(i, j, Direction::SOUTH)){
  93.           setWall(i, j, Direction::SOUTH);
  94.         }
  95.  
  96.         if (m.hasWall(i, j, Direction::EAST)){
  97.           setWall(i, j, Direction::EAST);
  98.         }
  99.  
  100.         if (m.hasWall(i, j, Direction::WEST)){
  101.           setWall(i, j, Direction::WEST);
  102.         }
  103.  
  104.         if (m.isVisited(i,j)){
  105.           setVisited(i,j);
  106.         }
  107.         }
  108.     }
  109.  
  110.   start = m.getStart();
  111.   end = m.getEnd();
  112.  
  113. }
  114.  
  115.  
  116. Maze::~Maze(){
  117.  
  118.   delete[] cells;
  119. }
  120.  
  121. ///////////////////////
  122. // Operator overload //
  123. ///////////////////////
  124.  
  125. Maze& Maze::operator=(const Maze &m){
  126.  
  127.   if (this != &m){
  128.     delete[] cells;
  129.  
  130.     int exp_size = (2*m.getNumRows() + 1)*(2*m.getNumCols() + 1);
  131.  
  132.     numRows=m.getNumRows();
  133.     numCols=m.getNumCols();
  134.  
  135.     cells = new MazeCell[exp_size];
  136.     clear();
  137.  
  138.     for (int i = 0; i < m.getNumRows(); ++i) {
  139.         for (int j = 0; j < m.getNumCols(); ++j) {
  140.            
  141.             if (m.hasWall(i, j, Direction::NORTH)){
  142.             setWall(i, j, Direction::NORTH);
  143.           }
  144.  
  145.           if (m.hasWall(i, j, Direction::SOUTH)){
  146.             setWall(i, j, Direction::SOUTH);
  147.           }
  148.  
  149.           if (m.hasWall(i, j, Direction::EAST)){
  150.             setWall(i, j, Direction::EAST);
  151.           }
  152.  
  153.           if (m.hasWall(i, j, Direction::WEST)){
  154.             setWall(i, j, Direction::WEST);
  155.           }
  156.  
  157.           if (m.isVisited(i,j)){
  158.             setVisited(i,j);
  159.           }
  160.           }
  161.       }
  162.  
  163.     start = m.getStart();
  164.     end = m.getEnd();
  165.   }
  166.   return *this;
  167. }
  168.  
  169. ///////////////
  170. // Accessors //
  171. ///////////////
  172.  
  173. int Maze::getNumRows() const{
  174.  
  175.   return numRows;
  176. }
  177.  
  178. int Maze::getNumCols() const{
  179.  
  180.   return numCols;
  181. }
  182.  
  183. Location Maze::getStart() const{
  184.  
  185.   return start;          
  186. }
  187.  
  188. Location Maze::getEnd() const{
  189.  
  190.   return end;
  191. }
  192.  
  193. MazeCell Maze::getCell(int cellRow, int cellCol) const{
  194.  
  195.   Location exp_cell = getCellArrayCoord(cellRow, cellCol);
  196.   int exp_cell_index = getArrayIndex(exp_cell);
  197.  
  198.   return cells[exp_cell_index];
  199. }
  200.  
  201. Location Maze::getNeighborCell(int cellRow, int cellCol, Direction direction) const{
  202.  
  203.   assert(!(cellRow<0));
  204.   assert(!(cellRow>numRows));
  205.   assert(!(cellCol<0));
  206.   assert(!(cellCol>numCols));
  207.  
  208.   switch (direction) {
  209.     case Direction::NORTH :
  210.       assert(!(cellRow == 0));
  211.       cellRow--;
  212.       break;
  213.     case Direction::SOUTH :
  214.       assert(!(cellRow == numRows));
  215.       cellRow++;
  216.       break;
  217.     case Direction::EAST :
  218.       assert(!(cellCol == numCols));
  219.       cellCol++;
  220.       break;
  221.     case Direction::WEST :
  222.       assert(!(cellCol == 0));
  223.       cellCol--;
  224.       break;
  225.     default :
  226.       assert(false);
  227.   }
  228.  
  229.   Location n_cell = Location(cellRow, cellCol);
  230.  
  231.   return n_cell;
  232. }
  233.  
  234. bool Maze::hasWall(int cellRow, int cellCol, Direction direction) const{
  235.  
  236.   Location cwall = getWallArrayCoord(cellRow, cellCol, direction);
  237.   int cwall_index = getArrayIndex(cwall);
  238.  
  239.   if (cells[cwall_index] == MazeCell::WALL){
  240.     return true;
  241.   }else{
  242.     return false;
  243.   }
  244.  
  245. }
  246.  
  247. bool Maze::isVisited(int cellRow, int cellCol) const{
  248.  
  249.   Location exp_cell = getCellArrayCoord(cellRow, cellCol);
  250.   int exp_cell_index = getArrayIndex(exp_cell);
  251.  
  252.   if (cells[exp_cell_index] == MazeCell::VISITED){
  253.     return true;
  254.   }else{
  255.     return false;
  256.   }
  257. }
  258.  
  259. ////////////////
  260. // Operations //
  261. ////////////////
  262.  
  263. void Maze::setStart(int row, int col){
  264.  
  265.   assert(!(row<0));
  266.   assert(!(row>numRows));
  267.   assert(!(col<0));
  268.   assert(!(col>numCols));
  269.  
  270.   Location new_start = Location(row, col);
  271.   start = new_start;
  272.   return;
  273. }
  274.  
  275. void Maze::setEnd(int row, int col){
  276.  
  277.   assert(!(row < 0));
  278.   assert(!(row > numRows));
  279.   assert(!(col < 0));
  280.   assert(!(col > numCols));
  281.  
  282.   Location new_end = Location(row, col);
  283.   end = new_end;
  284.   return;
  285. }
  286.  
  287. void Maze::clear(){
  288.  
  289.   int exp_size = (2*numRows+1)*(2*numCols+1);
  290.  
  291.   for (int i=0; i<exp_size; ++i){
  292.     cells[i] = MazeCell::EMPTY;
  293.   }
  294.   return;
  295. }
  296.  
  297. void Maze::setAllWalls(){
  298.  
  299.   int exp_size = (2*numRows+1)*(2*numCols+1);
  300.  
  301.   for (int i=1; i<exp_size; i=i+2){
  302.     cells[i] = MazeCell::WALL;
  303.   }
  304.   return;
  305. }
  306.  
  307. void Maze::setCell(int cellRow, int cellCol, MazeCell val){
  308.  
  309.   Location exp_cell = getCellArrayCoord(cellRow, cellCol);
  310.   int exp_cell_index = getArrayIndex(exp_cell);
  311.  
  312.   cells[exp_cell_index] = val;
  313.   return;
  314. }
  315.  
  316. void Maze::setWall(int cellRow, int cellCol, Direction direction){
  317.  
  318.   Location exp_cell = getWallArrayCoord(cellRow, cellCol, direction);
  319.   int exp_cell_index = getArrayIndex(exp_cell);
  320.  
  321.   cells[exp_cell_index] = MazeCell::WALL;
  322.   return;
  323. }
  324.  
  325. void Maze::clearWall(int cellRow, int cellCol, Direction direction){
  326.  
  327.   Location exp_cell = getWallArrayCoord(cellRow, cellCol, direction);
  328.   int exp_cell_index = getArrayIndex(exp_cell);
  329.  
  330.   cells[exp_cell_index] = MazeCell::EMPTY;
  331.   return;
  332. }
  333.  
  334. void Maze::setVisited(int cellRow, int cellCol){
  335.  
  336.   Location exp_cell = getCellArrayCoord(cellRow, cellCol);
  337.   int exp_cell_index = getArrayIndex(exp_cell);
  338.  
  339.   cells[exp_cell_index] = MazeCell::VISITED;
  340.   return;
  341. }
  342.  
  343. void Maze::print(ostream &os) const{
  344.  
  345.   int R = getNumRows();
  346.   int C = getNumCols();
  347.   int ext_size = (2*R+1)*(2*C+1);
  348.  
  349.   Location exp_S = getCellArrayCoord(start.row, start.col);
  350.   int S_index = getArrayIndex(exp_S);
  351.  
  352.   Location exp_E = getCellArrayCoord(end.row, end.col);
  353.   int E_index = getArrayIndex(exp_E);
  354.  
  355.   os << R << " " << C << endl;
  356.  
  357.   for (int i=0; i<ext_size; i=i+(2*C+1)){
  358.     for (int j=i; j<(i+(2*C+1)); j++){
  359.       if (i%2==0){
  360.         if (j%2==0){
  361.           os << "+";
  362.         }else{
  363.           if (cells[j]==MazeCell::WALL){
  364.             os << "---";
  365.           }else{
  366.             os << "   ";
  367.           }
  368.         }
  369.       }else  {
  370.         if (j%2==1){
  371.           if (cells[j]==MazeCell::WALL){
  372.             os << "|";
  373.           }else{
  374.             os << " ";
  375.           }
  376.         }else{
  377.           if (j==S_index){
  378.             os << " S ";
  379.           }else if (j==E_index){
  380.             os << " E ";
  381.           }else{
  382.             os << "   ";
  383.           }
  384.         }
  385.       }
  386.     }
  387.     os << endl;
  388.   }
  389.   return;
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement