Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- using namespace std;
- const static size_t FIELD_SIZE = 30;
- const static size_t MIN_NUMBER_OF_TUNNELS = 30;
- const static size_t MAX_NUMBER_OF_TUNNELS = 50;
- const static size_t MIN_BORDER = 1;
- const static size_t MAX_BORDER = FIELD_SIZE - 1;
- const static size_t MAX_TUNNEL_SIZE = 30;
- const static size_t MIN_TUNNEL_SIZE = 2;
- const static char SYMBOL_FIELD = static_cast<char>(987); //987
- const static char SYMBOL_WHOLE = static_cast<char>(511); // 944
- #define TRUE (int)1
- #define FALSE (int)0
- struct cell
- {
- size_t x;
- size_t y;
- cell(size_t a, size_t b) : x(a), y(b) {};
- };
- void swapTrue(bool& flag)
- {
- flag = flag ? false : true;
- }
- void generateTunnels(char**& mattrix)
- {
- //Зависимости рандома
- random_device RANDOM_SEED;
- mt19937 GENERATOR(RANDOM_SEED());
- // Начальная координаты
- uniform_int_distribution<size_t> GET_COORD(MIN_BORDER+MIN_TUNNEL_SIZE, MAX_BORDER-MIN_TUNNEL_SIZE);
- //Параметры генерации тунелей
- uniform_int_distribution<size_t> GEN_NUMBER_OF_TUNNELS(MIN_NUMBER_OF_TUNNELS, MAX_NUMBER_OF_TUNNELS);
- uniform_int_distribution<int> GET_BOOL(FALSE, TRUE);
- size_t numberOfTunnels = GEN_NUMBER_OF_TUNNELS(GENERATOR); // Кол-во будущих тунелей
- cell position(GET_COORD(GENERATOR), GET_COORD(GENERATOR)); // Позиция
- vector<vector<cell>> tunnels; // Массив туннелей
- for (size_t i = 0; i < numberOfTunnels; i++)
- {
- // Размер будущего туннеля
- vector<cell> newTunnel; // Будущий туннель
- if ( i%2 == 0)
- {
- bool isCorrect = false;
- bool isAimToLeft = GET_BOOL(GENERATOR);
- size_t gran = 0;
- while (!isCorrect)
- {
- if (!isAimToLeft)
- {
- uniform_int_distribution<size_t> GEN_TUNNEL_SIZE(position.x,MAX_BORDER);
- size_t currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
- if (static_cast<int>(position.x) + static_cast<int>(currentTunnelSize) > static_cast<int>(MAX_BORDER))
- {
- if (gran > 2)
- {
- isAimToLeft = false;
- gran = 0;
- }
- currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
- gran++;
- }
- else
- isCorrect = true;
- }
- else
- {
- if (static_cast<int>(position.x) - static_cast<int>(currentTunnelSize) < static_cast<int>(MIN_BORDER))
- {
- if (gran > 2)
- {
- isAimToLeft = false;
- gran = 0;
- }
- currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
- gran++;
- }
- else
- isCorrect = true;
- }
- }
- if (!isAimToLeft)
- for (size_t i = 0; i < currentTunnelSize; i++)
- newTunnel.push_back(cell(position.x++, position.y));
- else
- for (size_t i = 0; i < currentTunnelSize; i++)
- newTunnel.push_back(cell(position.x--, position.y));
- }
- else
- {
- bool isCorrect = false;
- bool isAimToUp = GET_BOOL(GENERATOR);
- size_t gran = 0;
- while (!isCorrect)
- {
- if (!isAimToUp)
- {
- if (static_cast<int>(position.y) + static_cast<int>(currentTunnelSize) > static_cast<int>(MAX_BORDER))
- {
- if (gran > 2)
- {
- gran = 0;
- isAimToUp = false;
- }
- currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
- gran++;
- }
- else
- isCorrect = true;
- }
- else
- {
- if (static_cast<int>(position.y) - static_cast<int>(currentTunnelSize) < static_cast<int>(MIN_BORDER))
- {
- if (gran > 2)
- {
- gran = 0;
- isAimToUp = false;
- }
- currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
- gran++;
- }
- else
- isCorrect = true;
- }
- }
- if (!isAimToUp)
- for (size_t i = 0; i < currentTunnelSize; i++)
- newTunnel.push_back(cell(position.x, position.y++));
- else
- for (size_t i = 0; i < currentTunnelSize; i++)
- newTunnel.push_back(cell(position.x, position.y--));
- }
- tunnels.push_back(newTunnel);
- uniform_int_distribution<size_t> tunnelUsing(0, tunnels.size() - 1);
- size_t tunnelId = tunnelUsing(GENERATOR);
- uniform_int_distribution<size_t> tunnelCell(0, tunnels[tunnelId].size() - 1);
- size_t cellId = tunnelCell(GENERATOR);
- position.x = tunnels[tunnelId][cellId].x;
- position.y = tunnels[tunnelId][cellId].y;
- }
- for (size_t i = 0; i < numberOfTunnels; i++)
- for (size_t j = 0; j < tunnels[i].size(); j++)
- {
- mattrix[tunnels[i][j].x][tunnels[i][j].y] = SYMBOL_WHOLE;
- }
- return;
- }
- int main()
- {
- char** mattrix = new char* [FIELD_SIZE];
- for (size_t i = 0; i < FIELD_SIZE; i++)
- mattrix[i] = new char[FIELD_SIZE];
- for (size_t i = 0; i < FIELD_SIZE; i++)
- for (size_t j = 0; j < FIELD_SIZE; j++)
- {
- mattrix[i][j] = SYMBOL_FIELD;
- }
- generateTunnels(mattrix);
- //food 174 178
- cout << endl << endl << endl;
- for (size_t i = 0; i < FIELD_SIZE; i++)
- {
- for (size_t j = 0; j < FIELD_SIZE; j++)
- {
- cout << mattrix[i][j] << " ";
- }
- cout << endl;
- }
- /*for (size_t j = 0; j < 1000; j++)
- {
- cout << "N = " << j << " | Symb = " << static_cast<char>(j) << endl;
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement