that_one_nigga_you_k

gerador mais potente

Jun 10th, 2024 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <algorithm>
  5. #include <fstream>
  6. #include <stack>
  7.  
  8. using namespace std;
  9.  
  10. void gerarCaminho(vector<vector<char>>& layout, int startX, int startY) {
  11.     // mover pra cima, baixo, esquerda e direita
  12.     vector<pair<int, int>> directions = {{-2, 0}, {2, 0}, {0, -2}, {0, 2}};
  13.     random_device rd;
  14.     mt19937 g(rd());
  15.    
  16.     //criando a pilha pra não ter overflow e lidar com numeros grandes
  17.     stack<pair<int, int>> s;
  18.     s.push({startX, startY});
  19.    
  20.     //tive de deixar de ser recursivo para não fritar meu pc
  21.     while (!s.empty()) {
  22.         auto [x, y] = s.top();
  23.         s.pop();
  24.  
  25.         // embaralhando as direções
  26.         shuffle(directions.begin(), directions.end(), g);
  27.  
  28.         for (auto& dir : directions) {
  29.             int nx = x + dir.first;
  30.             int ny = y + dir.second;
  31.  
  32.             if (nx > 0 && nx < layout.size() - 1 && ny > 0 && ny < layout[0].size() - 1 && layout[nx][ny] == '|') {
  33.                 layout[nx][ny] = ' ';
  34.                 layout[x + dir.first / 2][y + dir.second / 2] = ' ';
  35.                 s.push({nx, ny});
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. int main() {
  42.     //altura e largura
  43.     int largura = 99;
  44.     int comprimento = 99;
  45.  
  46.     // enche de paredes
  47.     vector<vector<char>> layout(largura, vector<char>(comprimento, '|'));
  48.  
  49.     random_device rd;
  50.     mt19937 g(rd());
  51.     int entrada = rand() % (comprimento - 2) + 1;
  52.     int saida = rand() % (comprimento - 2) + 1;
  53.  
  54.     // definir o começo
  55.     layout[0][entrada] = ' ';
  56.     gerarCaminho(layout, 1, entrada);
  57.  
  58.     vector<int> possibilidades_saida;
  59.  
  60.     for (int i = 0; i < comprimento; i++) {
  61.         if (layout[largura - 2][i] == ' ') {
  62.             possibilidades_saida.push_back(i);
  63.         }
  64.     }
  65.  
  66.     if (!possibilidades_saida.empty()) {
  67.         saida = possibilidades_saida[rand() % possibilidades_saida.size()];
  68.         layout[largura - 1][saida] = ' ';
  69.     }
  70.  
  71.     // ver o resultado
  72.     for (int i = 0; i < largura; i++) {
  73.         for (int j = 0; j < comprimento; j++) {
  74.             cout << layout[i][j] << " ";
  75.         }
  76.         cout << endl;
  77.     }
  78.  
  79.     // escrever num .txr
  80.     ofstream outputFile("labirinto.txt");
  81.     if (outputFile.is_open()) {
  82.         for (int i = 0; i < largura; i++) {
  83.             for (int j = 0; j < comprimento; j++) {
  84.                 outputFile << layout[i][j] << " ";
  85.             }
  86.             outputFile << endl;
  87.         }
  88.         outputFile.close();
  89.         cout << "Labirinto.txt foi criado" << endl;
  90.     } else {
  91.         cout << "o criacionismo falhou." << endl;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
Add Comment
Please, Sign In to add comment