Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. MAZE.H
  2. #ifndef __CS_235__Maze__
  3. #define __CS_235__Maze__
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. enum class RoomType { Start, Wall, EmptyRoom, Outside, \
  9. Treasure, Visited };
  10. class Maze{
  11. public:
  12. Maze();
  13. RoomType GetRoomType(int x, int y);
  14. void MarkPath(int x, int y);
  15. void ClearPath(int x, int y);
  16. void GetStartCoordinates(int& x, int& y);
  17. friend std::ostream & operator<<(std::ostream& out, c\
  18. onst Maze& maze);
  19. std::vector< std::vector<char> > _theMaze;
  20. int _startX;
  21. int _startY;
  22. };
  23.  
  24. #endif /* defined(__CS_235__Maze__) */
  25.  
  26. MAZE.CPP
  27. #include <vector>
  28. #include <iostream>
  29. #include <initializer_list>
  30. using namespace std;
  31.  
  32. #include "Maze.h"
  33.  
  34. Maze::Maze() : _startX(1), _startY(1)
  35. {
  36. vector<char> r1 = {'#','#','#','#','#','#','#','#','#\
  37. ','\n'};
  38. vector<char> r2 = {'#','S','#',' ',' ',' ','#','T','#\
  39. ','\n'};
  40. vector<char> r3 = {'#',' ','#',' ','#',' ',' ',' ','#\
  41. ','\n'};
  42. vector<char> r4 = {'#',' ',' ',' ','#',' ','#',' ','#\
  43. ','\n'};
  44. vector<char> r5 = {'#','#','#','#','#','#','#','#','#\
  45. ','\n'};
  46.  
  47. _theMaze.push_back(r1);
  48. _theMaze.push_back(r2);
  49. _theMaze.push_back(r3);
  50. _theMaze.push_back(r4);
  51. _theMaze.push_back(r5);
  52. }
  53.  
  54. void Maze::GetStartCoordinates(int& x, int& y)
  55. {
  56. x = _startX;
  57. y = _startY;
  58. }
  59.  
  60. RoomType Maze::GetRoomType(int x, int y)
  61. {
  62. if(y > _theMaze.size() || x > _theMaze[y].size())
  63. return RoomType::Outside;
  64.  
  65. if(_theMaze[y][x] == '#')
  66. return RoomType::Wall;
  67.  
  68. if(_theMaze[y][x] == 'S')
  69. return RoomType::Start;
  70.  
  71. if(_theMaze[y][x] == 'T')
  72. return RoomType::Treasure;
  73.  
  74. if(_theMaze[y][x] == '.')
  75. return RoomType::Visited;
  76.  
  77. return RoomType::EmptyRoom;
  78. }
  79.  
  80. void Maze::MarkPath(int x, int y){
  81. RoomType rt = GetRoomType(x, y);
  82. if(rt == RoomType::EmptyRoom || rt == RoomType::Start\
  83. || rt == RoomType::Treasure)
  84. _theMaze[y][x] = '.';
  85. }
  86.  
  87. void Maze::ClearPath(int x, int y)
  88. {
  89. RoomType rt = GetRoomType(x, y);
  90. if(x == _startX && y == _startY)
  91. _theMaze[y][x] = 'S';
  92. else if(rt == RoomType::Visited)
  93. _theMaze[y][x] = ' ';
  94. }
  95. // Overloaded for stream output
  96. std::ostream & operator<<(std::ostream& out, const Maze\
  97. & maze)
  98. {
  99. for(int y = 0; y < maze._theMaze.size(); ++y){
  100. std::vector<char> mazeRow = maze._theMaze[y];
  101.  
  102. for(int x = 0; x < mazeRow.size(); ++x){
  103. out << mazeRow[x];
  104. }
  105. }
  106. out << std::endl;
  107. return out;
  108. }
  109.  
  110. MAIN.CPP
  111. #include <iostream>
  112. #include <vector>
  113. #include <string>
  114. using namespace std; //cout, endl
  115. #include "Maze.h"
  116. void GetRoomType(int x,int y);
  117. void GetStartCoordinates(int x,int y);
  118. void GetRoomType(int x, int y);
  119. bool FindPath(Maze& m, int x, int y)
  120. {
  121. RoomType r;
  122. if(r == RoomType::Treasure)
  123. {
  124. cout << "You won!";
  125. return true;
  126. FindPath(m,x,y);
  127. }
  128. if(r == RoomType::Outside)
  129. {
  130. return false;
  131. FindPath(m,x,y);
  132.  
  133. }
  134. if(r == RoomType::Wall)
  135. {
  136. return false;
  137. FindPath(m,x,y);
  138. }
  139. if(r == RoomType::Visited)
  140. {
  141. return false;
  142. FindPath(m,x,y);
  143. }
  144. m.MarkPath(x,y);
  145. if(m._theMaze[x][y++])
  146. {
  147. FindPath(m,x,y);
  148. return true;
  149. }
  150. if(m._theMaze[x][y--])
  151. {
  152. cout << x << "," << y << endl;
  153. FindPath(m,x,y);
  154. cout << m << endl;
  155. return true;
  156. }
  157. if(m._theMaze[x--][y])
  158. {
  159. cout << x << "," << y << endl;
  160. FindPath(m,x,y);
  161. cout << m << endl;
  162. return true;
  163. }
  164.  
  165. if(m._theMaze[x++][y])
  166. {
  167. cout << x << "," << y << endl;
  168. FindPath(m,x,y);
  169. cout << m << endl;
  170. return true;
  171. }
  172. else
  173. {
  174. m.ClearPath(x,y);
  175. return false;
  176. }
  177.  
  178. }
  179.  
  180. }
  181.  
  182. if(m._theMaze[x++][y])
  183. {
  184. cout << x << "," << y << endl;
  185. FindPath(m,x,y);
  186. cout << m << endl;
  187. return true;
  188. }
  189. else
  190. {
  191. m.ClearPath(x,y);
  192. return false;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement