Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <SFML\System.hpp>
  5. #include <SFML\Graphics.hpp>
  6. #include "boost\smart_ptr\shared_ptr.hpp"
  7.  
  8. enum Walls {TOP, BOT, LEFT, RIGHT};
  9.  
  10. struct Cell
  11. {
  12. unsigned int CoordX, CoordY;
  13. std::vector<Walls> WallList;
  14. bool Edge, Start, Exit, Visited;
  15. };
  16.  
  17. typedef std::vector< std::vector< boost::shared_ptr <Cell> > > MazeType;
  18.  
  19. class Maze
  20. {
  21. public:
  22. Maze(unsigned int pBoardWidth, unsigned int pBoardHeight);
  23. void PrintBoard(sf::RenderWindow &pApp);
  24. bool GenerateMaze(sf::RenderWindow &pApp);
  25.  
  26. private:
  27. unsigned int mBoardWidth, mBoardHeight, mBoardSize, mVisitedCells;
  28. bool IsEdge(unsigned int pX, unsigned int pY);
  29. MazeType MazeBoard;
  30. MazeType InitBoard();
  31. std::stack<boost::shared_ptr<Cell> > PathStack;
  32. };
  33.  
  34. bool Maze::GenerateMaze(sf::RenderWindow &pApp)
  35. {
  36. boost::shared_ptr<Cell> CurrentCell (MazeBoard[1][1]);
  37. CurrentCell->Start = true;
  38. CurrentCell->Visited = true;
  39. MazeBoard[mBoardWidth-2][mBoardHeight-2]->Exit = true;
  40. while( (mVisitedCells <= mBoardSize) && pApp.IsOpened())
  41. {
  42. PrintBoard(pApp);
  43. sf::Event Event;
  44. while(pApp.GetEvent(Event))
  45. {
  46. if(Event.Type == sf::Event::Closed) pApp.Close();
  47.  
  48. if(Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Escape)) pApp.Close();
  49. }
  50.  
  51. CurrentCell->Visited = true;
  52. unsigned int x = CurrentCell->CoordX;
  53. unsigned int y = CurrentCell->CoordY;
  54. std::vector<boost::shared_ptr<Cell> > NBList;
  55. NBList.push_back(MazeBoard[y+1][x]);
  56. NBList.push_back(MazeBoard[y-1][x]);
  57. NBList.push_back(MazeBoard[y][x+1]);
  58. NBList.push_back(MazeBoard[y][x-1]);
  59.  
  60. std::vector<boost::shared_ptr<Cell> > ValidNBList;
  61. for(unsigned int i = 0; i < NBList.size(); ++i)
  62. {
  63. boost::shared_ptr<Cell> TempCell (NBList[i]);
  64. if(TempCell->WallList.size() == 0 && !TempCell->Edge)
  65. {
  66. ValidNBList.push_back(TempCell);
  67. }
  68. }
  69.  
  70. if(ValidNBList.size())
  71. {
  72. unsigned int n = rand()%ValidNBList.size();
  73. boost::shared_ptr<Cell> NewCell (ValidNBList[n]);
  74.  
  75. if(y < NewCell->CoordY )
  76. {
  77. CurrentCell->WallList.push_back(RIGHT);
  78. NewCell->WallList.push_back(LEFT);
  79. }
  80.  
  81. if(y > NewCell->CoordY)
  82. {
  83. CurrentCell->WallList.push_back(LEFT);
  84. NewCell->WallList.push_back(RIGHT);
  85. }
  86.  
  87. if(x < NewCell->CoordX)
  88. {
  89. CurrentCell->WallList.push_back(BOT);
  90. NewCell->WallList.push_back(TOP);
  91. }
  92.  
  93. if(x > NewCell->CoordX)
  94. {
  95. CurrentCell->WallList.push_back(TOP);
  96. NewCell->WallList.push_back(BOT);
  97. }
  98. mVisitedCells++;
  99. PathStack.push(CurrentCell);
  100. CurrentCell = NewCell;
  101. sf::Sleep(0.05f);
  102. }
  103. else
  104. {
  105. if(PathStack.size())
  106. {
  107. CurrentCell = PathStack.top();
  108. PathStack.pop();
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114.  
  115. bool Maze::IsEdge(unsigned int pX, unsigned int pY)
  116. {
  117. if(pY == 0 || pY == mBoardWidth-1) return true;
  118. if(pX == 0 || pX == mBoardHeight-1) return true;
  119.  
  120. return false;
  121. }
  122.  
  123. void Maze::PrintBoard(sf::RenderWindow &pApp)
  124. {
  125. pApp.Clear();
  126. for(unsigned int i = 0; i < mBoardHeight; ++i)
  127. {
  128. for(unsigned int j = 0; j < mBoardWidth; ++j)
  129. {
  130. int x = i * 12;
  131. int y = j * 12;
  132.  
  133. if (MazeBoard[i][j]->Visited)
  134. {
  135. pApp.Draw(sf::Shape::Rectangle(x+3, y+3, x+9, y+9, sf::Color(255, 255, 255)));
  136. for(unsigned int k = 0; k < MazeBoard[i][j]->WallList.size(); ++k)
  137. {
  138. switch (MazeBoard[i][j]->WallList.at(k))
  139. {
  140. case TOP:
  141. pApp.Draw(sf::Shape::Rectangle(x+3, y, x+9, y+3, sf::Color(255, 255, 255)));
  142. break;
  143.  
  144. case BOT:
  145. pApp.Draw(sf::Shape::Rectangle(x+3, y+9, x+9, y+12, sf::Color(255, 255, 255)));
  146. break;
  147.  
  148. case LEFT:
  149. pApp.Draw(sf::Shape::Rectangle(x, y+3, x+3, y+9, sf::Color(255, 255, 255)));
  150. break;
  151.  
  152. case RIGHT:
  153. pApp.Draw(sf::Shape::Rectangle(x+9, y+3, x+12, y+9, sf::Color(255, 255, 255)));
  154. break;
  155. }
  156. }
  157. }
  158. if(MazeBoard[i][j]->Edge) pApp.Draw(sf::Shape::Rectangle(x, y, x+12, y+12, sf::Color(139, 137, 137)));
  159.  
  160. if(MazeBoard[i][j]->Exit) pApp.Draw(sf::Shape::Rectangle(x, y, x+12, y+12, sf::Color(0, 0, 255)));
  161.  
  162. if(MazeBoard[i][j]->Start) pApp.Draw(sf::Shape::Rectangle(x, y, x+12, y+12, sf::Color(0, 255, 0)));
  163. }
  164. }
  165. pApp.Display();
  166. return;
  167. }
  168.  
  169. MazeType Maze::InitBoard()
  170. {
  171. for(unsigned int i = 0; i < mBoardHeight; ++i)
  172. {
  173. std::vector<boost::shared_ptr<Cell> > TempVector;
  174. for(unsigned int j = 0; j < mBoardWidth; ++j)
  175. {
  176. boost::shared_ptr<Cell> TempPointer (new Cell);
  177. TempPointer->CoordX = j;
  178. TempPointer->CoordY = i;
  179. TempPointer->Edge = IsEdge(i,j);
  180. TempPointer->Start = false;
  181. TempPointer->Exit = false;
  182. TempPointer->Visited = false;
  183. if(TempPointer->Edge) mVisitedCells++;
  184. TempVector.push_back(TempPointer);
  185. }
  186. MazeBoard.push_back(TempVector);
  187. }
  188. return MazeBoard;
  189. }
  190.  
  191. Maze::Maze(unsigned int pBoardWidth, unsigned int pBoardHeight)
  192. {
  193. mBoardWidth = pBoardWidth;
  194. mBoardHeight = pBoardHeight;
  195. mBoardSize = mBoardWidth * mBoardHeight;
  196. mVisitedCells = 0;
  197. srand( time(NULL) );
  198. InitBoard();
  199. }
  200.  
  201. int main(int argc, char *argv[])
  202. {
  203. if(argc != 3)
  204. {
  205. std::cerr << "Incorrect parameter pattern." << std::endl;
  206. return 1;
  207. }
  208.  
  209. unsigned int MazeWidth = atoi(argv[1]);
  210. unsigned int MazeHeight = atoi(argv[2]);
  211.  
  212. if(MazeWidth < 4 || MazeHeight < 4)
  213. {
  214. std::cerr << "Smallest possible maze is 4x4." << std::endl;
  215. return 1;
  216. }
  217.  
  218. unsigned int CellWidth = 12, CellHeight = 12;
  219.  
  220. sf::RenderWindow App(sf::VideoMode(CellWidth * MazeWidth, CellHeight * MazeHeight, 32), "MazeGen", sf::Style::Close);
  221.  
  222. Maze MyMaze (MazeWidth, MazeHeight);
  223. MyMaze.GenerateMaze(App);
  224. return 0;
  225. }
Add Comment
Please, Sign In to add comment