Advertisement
Guest User

Untitled

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