Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <future>
- #include <fstream>
- using namespace std;
- const int wall = 0, path = 1;
- void Print(int** maze, int& height, int& width, bool& is_file)
- {
- int x;
- ofstream fout;
- fout.open("lab.txt");
- x = rand() % (width - 1) + 1;
- std::cout << "||";
- fout << "█";
- maze[0][x] = -2;
- maze[1][x] = -2;
- maze[2][x] = -2;
- x = rand() % (width - 1) + 1;
- maze[height - 1][x] = -2;
- maze[height - 2][x] = -2;
- maze[height - 3][x] = -2;
- for (int j = 1; j < width; j++)
- {
- if (maze[0][j] == -2) { std::cout << " "; fout << "00"; }
- else { std::cout << "||"; fout << "█"; }
- }
- std::cout << "||" << endl;
- fout << "█" << endl;
- for (int i = 0; i < height; i++)
- {
- std::cout << "||";//левая граница
- fout << "█";
- for (int j = 0; j < width; j++)
- {
- if (i % 2 == 0)
- {
- if (maze[i][j] == -1)
- {
- std::cout << "██";//стена
- fout << "█";
- }
- else { cout << " "; fout << "0"; }
- }
- else
- {
- if (maze[i][j] == wall)
- {
- std::cout << "██";//нижняя граница
- fout << "█";
- }
- else { std::cout << " "; fout << "0"; }
- }
- }
- std::cout << "||" << endl;//правая граница
- fout << "█" << endl;
- }
- std::cout << "||";
- fout << "#";
- for (int j = 1; j < width; j++)
- {
- if (maze[height - 1][j] == -2) { std::cout << " "; fout << "00"; }
- else { std::cout << "||"; fout << "█"; }
- }
- std::cout << "||";
- fout << "█";
- fout.close();
- }
- void DoTurns(int** maze, int& height, int& width, bool& is_file, int& ur)
- {
- int temp = 0;
- int is_gran;
- bool first1 = true;
- int* tv = new int[width];
- for (int i = 0; i < height; ++i)
- {
- if (i % 2 == 0)
- {
- if (first1 == true)
- {
- for (int j = 0; j < width; j++)
- {
- maze[i][j] = temp;
- ++temp;
- }
- temp = maze[i][0];
- for (int j = 0; j < width; j++)
- {
- is_gran = rand() % ur;
- if (is_gran != 0)
- {
- if (maze[i][j - 1] != -1) { maze[i][j] = maze[i][j - 1]; }
- }
- else { maze[i][j] = -1; }
- }
- first1 = false;
- }
- else
- {
- for (int j = 0; j < width; j++) { maze[i][j] = tv[j]; }
- for (int j = 0; j < width; j++)
- {
- if (maze[i][0] == -1) { maze[i][j] = 0; }
- else if (maze[i][j] == -1) { maze[i][j] = 1 + maze[i][j - 1]; }
- }
- for (int j = 0; j < width; j++)
- {
- is_gran = rand() % ur;
- if (is_gran != 0)
- {
- if (maze[i][j - 1] != -1 && j != 0)
- {
- maze[i][j] = maze[i][j - 1];
- if (maze[i][j + 1] == maze[i][j]) { maze[i][j] = -1; }
- }
- }
- else { maze[i][j] = -1; }
- }
- }
- temp = 0;
- for (int j = 0; j < width; j++) { tv[j] = maze[i][j]; }
- }
- else
- {
- for (int j = 0; j < width; j++)
- {
- if (j == 0)
- {
- if (tv[1] == tv[0]) { maze[i][j] = rand() % 2; }
- else { maze[i][j] = path; }
- }
- else
- {
- if (tv[j] == tv[j - 1] || tv[j] == tv[j + 1])
- {
- maze[i][j] = rand() % 2;
- }
- else { maze[i][j] = path; }
- }
- }
- }
- }
- delete[]tv;//очистка временного массива
- Print(maze, height, width, is_file);//функция для печати на консоль
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- int height, width, cl = 3;
- string level, answer;
- bool is_file;
- cout << "Выберите уровень сложности (Сложный / Средний / Легкий) : ";
- cin >> level;
- cout << "Введите длину : ";
- cin >> width;
- cout << "Введите ширину : ";
- cin >> height;
- cout << "Записывать ли лабиринт в файл ?(Да/Нет) : ";
- cin >> answer;
- cout << endl;
- if (level == "Легкий") { cl = 5; }
- if (level == "Средний") { cl = 4; }
- if (level == "Сложный") { cl = 3; }
- if (answer == "Да") { is_file = true; }
- int** maze = new int* [height];//создание лабиринта
- for (int i = 0; i < height; i++) { maze[i] = new int[width]; }//заполнение массива массивами(двумерный массив)
- cout << endl;
- DoTurns(maze, height, width, is_file, cl);//принимает указатель на двумерный массив и длину и ширину
- cout << endl;
- for (int i = 0; i < height; ++i)//чистим данные
- {
- delete[] maze[i];
- }
- delete[] maze;
- std::system("pause");//чтобы консоль не закрывалась
- return 0;
- }
Add Comment
Please, Sign In to add comment