Flickyyy

Maze generator SFML

Apr 17th, 2022
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1.  
  2. #include <SFML/Graphics.hpp>
  3. #include <vector>
  4. #include <stack>
  5. using namespace sf;
  6. using namespace std;
  7.  
  8.  
  9. VideoMode operator/(VideoMode mode, float cef) {
  10.     return { static_cast<unsigned int> (mode.width / cef),
  11.             static_cast<unsigned int> (mode.height / cef) };
  12. }
  13.  
  14.  
  15. void eventProcess(RenderWindow& win) {
  16.     Event e;
  17.     while (win.pollEvent(e)) {
  18.         if (e.type == Event::Closed) {
  19.             win.close();
  20.         }
  21.     }
  22. }
  23.  
  24.  
  25. // n = maximium empty sections in a column
  26. // m = maximum empty sections in a row
  27. //false == empty section
  28. auto genMaze(unsigned int n, unsigned int m) {
  29.     srand(time(nullptr));
  30.     vector< vector<bool> > maze(2 * n + 1, vector<bool>(2 * m + 1, true));
  31.     maze[1][1] = false;
  32.     stack< pair<int, int> > trail;
  33.     trail.push({ 1, 1 });
  34.     while (!trail.empty()) {
  35.         auto [ci, cj] = trail.top();
  36.         vector< pair<int, int> > next, mb = {
  37.             { ci + 2, cj }, { ci - 2, cj },
  38.             { ci, cj + 2 }, { ci, cj - 2 }
  39.         };
  40.         for (auto [i, j] : mb) {
  41.             if (i >= 0 and j >= 0 and i < 2 * n + 1 and j < 2 * m + 1 and maze[i][j]) {
  42.                 next.emplace_back(i, j);
  43.             }
  44.         }
  45.         if (next.empty()) {
  46.             trail.pop();
  47.             continue;
  48.         }
  49.         auto [i, j] = next[rand() % next.size()];
  50.         maze[i][j] = false;
  51.         maze[(i + ci) / 2][(j + cj) / 2] = false;
  52.         trail.emplace(i, j);
  53.     }
  54.     return maze;
  55. }
  56.  
  57.  
  58.  
  59. int main() {
  60.     RenderWindow window(VideoMode::getDesktopMode() / 1.5, "Maze");
  61.  
  62.     const int N = 18, M = 30;
  63.     const float rectWidth = (float)window.getSize().x / (2 * M + 1);
  64.     const float rectHeight = (float)window.getSize().y / (2 * N + 1);
  65.     auto maze = genMaze(N, M);
  66.  
  67.  
  68.     Clock clock;
  69.  
  70.  
  71.     while (window.isOpen()) {
  72.         eventProcess(window);
  73.  
  74.         if (clock.getElapsedTime().asSeconds() >= 7) {
  75.             maze = genMaze(N, M);
  76.             clock.restart();
  77.         }
  78.  
  79.  
  80.         window.clear();
  81.  
  82.  
  83.         for (int i = 0; i < 2 * N + 1; i++) {
  84.             for (int j = 0; j < 2 * M + 1; j++) {
  85.                 RectangleShape rect({ rectWidth, rectHeight });
  86.                 rect.setPosition({ rectWidth * j, rectHeight * i });
  87.                 rect.setFillColor((maze[i][j] ? Color::White : Color::Black));
  88.                 window.draw(rect);
  89.             }
  90.         }
  91.  
  92.  
  93.         window.display();
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment