Advertisement
Sanlover

Untitled

Oct 15th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. using namespace std;
  4.  
  5. const static size_t FIELD_SIZE = 30;
  6.  
  7. const static size_t MIN_NUMBER_OF_TUNNELS = 30;
  8. const static size_t MAX_NUMBER_OF_TUNNELS = 50;
  9.  
  10. const static size_t MIN_BORDER = 1;
  11. const static size_t MAX_BORDER = FIELD_SIZE - 1;
  12.  
  13.  
  14. const static size_t MAX_TUNNEL_SIZE = 30;
  15. const static size_t MIN_TUNNEL_SIZE = 2;
  16.  
  17. const static char SYMBOL_FIELD = static_cast<char>(987); //987
  18. const static char SYMBOL_WHOLE = static_cast<char>(511); // 944
  19.  
  20. #define TRUE (int)1
  21. #define FALSE (int)0
  22.  
  23. struct cell
  24. {
  25. size_t x;
  26. size_t y;
  27.  
  28. cell(size_t a, size_t b) : x(a), y(b) {};
  29. };
  30.  
  31. void swapTrue(bool& flag)
  32. {
  33. flag = flag ? false : true;
  34. }
  35.  
  36. void generateTunnels(char**& mattrix)
  37. {
  38. //Зависимости рандома
  39. random_device RANDOM_SEED;
  40. mt19937 GENERATOR(RANDOM_SEED());
  41. // Начальная координаты
  42. uniform_int_distribution<size_t> GET_COORD(MIN_BORDER+MIN_TUNNEL_SIZE, MAX_BORDER-MIN_TUNNEL_SIZE);
  43. //Параметры генерации тунелей
  44. uniform_int_distribution<size_t> GEN_NUMBER_OF_TUNNELS(MIN_NUMBER_OF_TUNNELS, MAX_NUMBER_OF_TUNNELS);
  45. uniform_int_distribution<int> GET_BOOL(FALSE, TRUE);
  46.  
  47.  
  48. size_t numberOfTunnels = GEN_NUMBER_OF_TUNNELS(GENERATOR); // Кол-во будущих тунелей
  49. cell position(GET_COORD(GENERATOR), GET_COORD(GENERATOR)); // Позиция
  50. vector<vector<cell>> tunnels; // Массив туннелей
  51.  
  52. for (size_t i = 0; i < numberOfTunnels; i++)
  53. {
  54.  
  55. // Размер будущего туннеля
  56. vector<cell> newTunnel; // Будущий туннель
  57.  
  58. if ( i%2 == 0)
  59. {
  60. bool isCorrect = false;
  61. bool isAimToLeft = GET_BOOL(GENERATOR);
  62.  
  63. size_t gran = 0;
  64. while (!isCorrect)
  65. {
  66. if (!isAimToLeft)
  67. {
  68. uniform_int_distribution<size_t> GEN_TUNNEL_SIZE(position.x,MAX_BORDER);
  69. size_t currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
  70. if (static_cast<int>(position.x) + static_cast<int>(currentTunnelSize) > static_cast<int>(MAX_BORDER))
  71. {
  72. if (gran > 2)
  73. {
  74. isAimToLeft = false;
  75. gran = 0;
  76. }
  77. currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
  78. gran++;
  79. }
  80. else
  81. isCorrect = true;
  82. }
  83. else
  84. {
  85. if (static_cast<int>(position.x) - static_cast<int>(currentTunnelSize) < static_cast<int>(MIN_BORDER))
  86. {
  87. if (gran > 2)
  88. {
  89. isAimToLeft = false;
  90. gran = 0;
  91. }
  92. currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
  93. gran++;
  94. }
  95. else
  96. isCorrect = true;
  97. }
  98. }
  99.  
  100.  
  101. if (!isAimToLeft)
  102. for (size_t i = 0; i < currentTunnelSize; i++)
  103. newTunnel.push_back(cell(position.x++, position.y));
  104. else
  105. for (size_t i = 0; i < currentTunnelSize; i++)
  106. newTunnel.push_back(cell(position.x--, position.y));
  107. }
  108. else
  109. {
  110. bool isCorrect = false;
  111. bool isAimToUp = GET_BOOL(GENERATOR);
  112.  
  113. size_t gran = 0;
  114. while (!isCorrect)
  115. {
  116. if (!isAimToUp)
  117. {
  118. if (static_cast<int>(position.y) + static_cast<int>(currentTunnelSize) > static_cast<int>(MAX_BORDER))
  119. {
  120. if (gran > 2)
  121. {
  122. gran = 0;
  123. isAimToUp = false;
  124. }
  125. currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
  126. gran++;
  127. }
  128. else
  129. isCorrect = true;
  130. }
  131. else
  132. {
  133. if (static_cast<int>(position.y) - static_cast<int>(currentTunnelSize) < static_cast<int>(MIN_BORDER))
  134. {
  135. if (gran > 2)
  136. {
  137. gran = 0;
  138. isAimToUp = false;
  139. }
  140. currentTunnelSize = GEN_TUNNEL_SIZE(GENERATOR);
  141. gran++;
  142. }
  143. else
  144. isCorrect = true;
  145. }
  146. }
  147.  
  148.  
  149. if (!isAimToUp)
  150. for (size_t i = 0; i < currentTunnelSize; i++)
  151. newTunnel.push_back(cell(position.x, position.y++));
  152. else
  153. for (size_t i = 0; i < currentTunnelSize; i++)
  154. newTunnel.push_back(cell(position.x, position.y--));
  155. }
  156. tunnels.push_back(newTunnel);
  157. uniform_int_distribution<size_t> tunnelUsing(0, tunnels.size() - 1);
  158. size_t tunnelId = tunnelUsing(GENERATOR);
  159. uniform_int_distribution<size_t> tunnelCell(0, tunnels[tunnelId].size() - 1);
  160. size_t cellId = tunnelCell(GENERATOR);
  161.  
  162.  
  163.  
  164.  
  165. position.x = tunnels[tunnelId][cellId].x;
  166. position.y = tunnels[tunnelId][cellId].y;
  167. }
  168.  
  169. for (size_t i = 0; i < numberOfTunnels; i++)
  170. for (size_t j = 0; j < tunnels[i].size(); j++)
  171. {
  172. mattrix[tunnels[i][j].x][tunnels[i][j].y] = SYMBOL_WHOLE;
  173. }
  174. return;
  175. }
  176.  
  177. int main()
  178. {
  179. char** mattrix = new char* [FIELD_SIZE];
  180. for (size_t i = 0; i < FIELD_SIZE; i++)
  181. mattrix[i] = new char[FIELD_SIZE];
  182.  
  183.  
  184. for (size_t i = 0; i < FIELD_SIZE; i++)
  185. for (size_t j = 0; j < FIELD_SIZE; j++)
  186. {
  187. mattrix[i][j] = SYMBOL_FIELD;
  188. }
  189.  
  190.  
  191. generateTunnels(mattrix);
  192. //food 174 178
  193.  
  194. cout << endl << endl << endl;
  195. for (size_t i = 0; i < FIELD_SIZE; i++)
  196. {
  197. for (size_t j = 0; j < FIELD_SIZE; j++)
  198. {
  199. cout << mattrix[i][j] << " ";
  200. }
  201. cout << endl;
  202.  
  203. }
  204.  
  205. /*for (size_t j = 0; j < 1000; j++)
  206. {
  207. cout << "N = " << j << " | Symb = " << static_cast<char>(j) << endl;
  208. }*/
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement