hishlishter

Labirint

May 16th, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <future>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. const int wall = 0, path = 1;
  10. void Print(int** maze, int& height, int& width, bool& is_file)
  11. {
  12.     int x;
  13.     ofstream fout;
  14.     fout.open("lab.txt");
  15.     x = rand() % (width - 1) + 1;
  16.     std::cout << "||";
  17.     fout << "█";
  18.     maze[0][x] = -2;
  19.     maze[1][x] = -2;
  20.     maze[2][x] = -2;
  21.     x = rand() % (width - 1) + 1;
  22.     maze[height - 1][x] = -2;
  23.     maze[height - 2][x] = -2;
  24.     maze[height - 3][x] = -2;
  25.     for (int j = 1; j < width; j++)
  26.     {
  27.         if (maze[0][j] == -2) { std::cout << "    ";  fout << "00"; }
  28.         else { std::cout << "||"; fout << "█"; }
  29.     }
  30.     std::cout << "||" << endl;
  31.     fout << "█" << endl;
  32.     for (int i = 0; i < height; i++)
  33.     {
  34.         std::cout << "||";//левая граница
  35.         fout << "█";
  36.         for (int j = 0; j < width; j++)
  37.         {
  38.             if (i % 2 == 0)
  39.             {
  40.                 if (maze[i][j] == -1)
  41.                 {
  42.                     std::cout << "██";//стена
  43.                     fout << "█";
  44.                 }
  45.                 else { cout << "  "; fout << "0"; }
  46.             }
  47.             else
  48.             {
  49.                 if (maze[i][j] == wall)
  50.                 {
  51.                     std::cout << "██";//нижняя граница
  52.                     fout << "█";
  53.                 }
  54.                 else { std::cout << "  "; fout << "0"; }
  55.             }
  56.         }
  57.         std::cout << "||" << endl;//правая граница
  58.         fout << "█" << endl;
  59.     }
  60.     std::cout << "||";
  61.     fout << "#";
  62.     for (int j = 1; j < width; j++)
  63.     {
  64.         if (maze[height - 1][j] == -2) { std::cout << "    "; fout << "00"; }
  65.         else { std::cout << "||"; fout << "█"; }
  66.     }
  67.     std::cout << "||";
  68.     fout << "█";
  69.     fout.close();
  70. }
  71. void DoTurns(int** maze, int& height, int& width, bool& is_file, int& ur)
  72. {
  73.     int temp = 0;
  74.     int is_gran;
  75.     bool first1 = true;
  76.     int* tv = new int[width];
  77.     for (int i = 0; i < height; ++i)
  78.     {
  79.         if (i % 2 == 0)
  80.         {
  81.             if (first1 == true)
  82.             {
  83.                 for (int j = 0; j < width; j++)
  84.                 {
  85.                     maze[i][j] = temp;
  86.                     ++temp;
  87.                 }
  88.                 temp = maze[i][0];
  89.                 for (int j = 0; j < width; j++)
  90.                 {
  91.                     is_gran = rand() % ur;
  92.                     if (is_gran != 0)
  93.                     {
  94.                         if (maze[i][j - 1] != -1) { maze[i][j] = maze[i][j - 1]; }
  95.                     }
  96.                     else { maze[i][j] = -1; }
  97.                 }
  98.                 first1 = false;
  99.             }
  100.             else
  101.             {
  102.                 for (int j = 0; j < width; j++) { maze[i][j] = tv[j]; }
  103.                 for (int j = 0; j < width; j++)
  104.                 {
  105.                     if (maze[i][0] == -1) { maze[i][j] = 0; }
  106.                     else if (maze[i][j] == -1) { maze[i][j] = 1 + maze[i][j - 1]; }
  107.                 }
  108.                 for (int j = 0; j < width; j++)
  109.                 {
  110.                     is_gran = rand() % ur;
  111.                     if (is_gran != 0)
  112.                     {
  113.                         if (maze[i][j - 1] != -1 && j != 0)
  114.                         {
  115.                             maze[i][j] = maze[i][j - 1];
  116.                             if (maze[i][j + 1] == maze[i][j]) { maze[i][j] = -1; }
  117.                         }
  118.                     }
  119.                     else { maze[i][j] = -1; }
  120.                 }
  121.             }
  122.             temp = 0;
  123.             for (int j = 0; j < width; j++) { tv[j] = maze[i][j]; }
  124.         }
  125.         else
  126.         {
  127.             for (int j = 0; j < width; j++)
  128.             {
  129.                 if (j == 0)
  130.                 {
  131.                     if (tv[1] == tv[0]) { maze[i][j] = rand() % 2; }
  132.                     else { maze[i][j] = path; }
  133.                 }
  134.                 else
  135.                 {
  136.                     if (tv[j] == tv[j - 1] || tv[j] == tv[j + 1])
  137.                     {
  138.                         maze[i][j] = rand() % 2;
  139.                     }
  140.                     else { maze[i][j] = path; }
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     delete[]tv;//очистка временного массива
  146.     Print(maze, height, width, is_file);//функция для печати на консоль
  147. }
  148.  
  149. int main()
  150. {
  151.     setlocale(LC_ALL, "rus");
  152.     int height, width, cl = 3;
  153.     string level, answer;
  154.     bool is_file;
  155.     cout << "Выберите уровень сложности (Сложный / Средний / Легкий) : ";
  156.       cin >> level;
  157.     cout << "Введите длину : ";
  158.       cin >> width;
  159.     cout << "Введите ширину : ";
  160.       cin >> height;
  161.     cout << "Записывать ли лабиринт в файл ?(Да/Нет) : ";
  162.       cin >> answer;
  163.     cout << endl;
  164.     if (level == "Легкий") { cl = 5; }
  165.     if (level == "Средний") { cl = 4; }
  166.     if (level == "Сложный") { cl = 3; }
  167.     if (answer == "Да") { is_file = true; }
  168.     int** maze = new int* [height];//создание лабиринта
  169.     for (int i = 0; i < height; i++) { maze[i] = new int[width]; }//заполнение массива массивами(двумерный массив)
  170.     cout << endl;
  171.     DoTurns(maze, height, width, is_file, cl);//принимает указатель на двумерный массив и длину и ширину
  172.     cout << endl;
  173.     for (int i = 0; i < height; ++i)//чистим данные
  174.     {
  175.         delete[] maze[i];
  176.     }
  177.     delete[] maze;
  178.     std::system("pause");//чтобы консоль не закрывалась
  179.     return 0;
  180. }
  181.  
Add Comment
Please, Sign In to add comment