Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. void crswrd_fill(char **array1, bool **array2, int ROW, int COL) {
  7.  
  8. for (int i = 0; i < ROW; i++)
  9. for (int j = 0; j < COL; j++) {
  10. array1[i][j] = char(65 + rand() % 26);
  11. array2[i][j] = false;
  12. }
  13. }
  14.  
  15. void crswrd_lay(char **array1, bool **array2, std::string *term, std::string *find, int ROW, int COL, int NUM, int DEX) {
  16.  
  17. int row, column, orientation, selection, term_dex, find_dex, count;
  18. bool repeat;
  19. find_dex = count = 0;
  20.  
  21. while (count < NUM) {
  22. orientation = rand() % 3;
  23. do {
  24. selection = rand() % DEX;
  25. }
  26. while (term[selection] == "");
  27. term_dex = 0;
  28. repeat = false;
  29.  
  30. switch (orientation) {
  31. case 0: column = rand() % COL;
  32. row = ROW - term[selection].length() - (rand() % (ROW - term[selection].length()));
  33.  
  34. for (int i = row; i < (row + term[selection].length()); i++)
  35. if (array2[i][column]) {
  36. repeat = true; break;
  37. }
  38.  
  39. if (!repeat)
  40. for (int i = row; i < (row + term[selection].length()); i++) {
  41. array1[i][column] = (term[selection])[term_dex++];
  42. array2[i][column] = true;
  43. }
  44. break;
  45.  
  46. case 1: row = rand() % ROW;
  47. column = COL - (term[selection].length()) - (rand() % (COL - (term[selection].length())));
  48.  
  49. for (int i = column; i < (column + term[selection].length()); i++)
  50. if (array2[row][i]) {
  51. repeat = true; break;
  52. }
  53.  
  54. if (!repeat)
  55. for (int i = column; i < (column + term[selection].length()); i++) {
  56. array1[row][i] = (term[selection])[term_dex++];
  57. array2[row][i] = true;
  58. }
  59. break;
  60.  
  61. case 2: row = ROW - term[selection].length() - (rand() % (ROW - term[selection].length()));
  62. column = COL - term[selection].length() - (rand() % (COL - term[selection].length()));
  63.  
  64. for (int i = 0; i < (row + term[selection].length()) && !repeat;) {
  65. for (int j = 0; j < (column + term[selection].length()); i++, j++) {
  66. if (array2[i][j]) {
  67. repeat = true;
  68. break;
  69. }
  70. }
  71. }
  72.  
  73. if (!repeat)
  74. for (int i = row; i < (row + term[selection].length());)
  75. for (int j = column; j < (column + term[selection].length()); i++, j++) {
  76. array1[i][j] = (term[selection])[term_dex++];
  77. array2[i][j] = true;
  78. }
  79. break;
  80. }
  81.  
  82. if (!repeat) {
  83. count++;
  84. find[find_dex++] = term[selection];
  85. term[selection] = "";
  86. }
  87. }
  88. }
  89.  
  90. void crswrd_print(char **array, int ROW, int COL) {
  91.  
  92. for (int i = 0; i < ROW; i++, std::cout << '\n')
  93. for (int j = 0; j < COL; j++)
  94. std::cout << array[i][j] << ' ';
  95. std::cout << '\n';
  96. }
  97.  
  98. bool validation(std::string str) {
  99. bool pr = true;
  100. for(int i = 0;i < str.length();i++){
  101. if(!isdigit(str.[i])){
  102. pr = false;
  103. std::cout << "\nНеверное значение!\n";
  104. return pr;
  105. }
  106. }
  107. return pr;
  108. }
  109.  
  110. int main() {
  111.  
  112. int ROW = 0,COL = 0,NUM = 0,DEX = 0;
  113. while (1) {
  114. std::string str;
  115.  
  116. std::cout << "Input row\n";
  117. std::cin >> str;
  118. if(validation(str))
  119. {
  120. ROW = atoi(str.c_str());
  121. break;
  122. }
  123. }
  124. std::cout << "Input col\n";
  125. std::cin >> COL;
  126. std::cout << "Input num\n";
  127. std::cin >> NUM;
  128. std::cout << "Input dex\n";
  129. std::cin >> DEX;
  130.  
  131. srand((unsigned int)time(0));
  132.  
  133. char **maze = new char* [ROW];
  134. for (int i = 0; i < ROW; i++)
  135. maze[i] = new char[COL];
  136.  
  137. bool **mark = new bool* [ROW];
  138. for (int i = 0; i < ROW; i++)
  139. mark[i] = new bool[COL];
  140.  
  141. std::string *find = new std::string[NUM];
  142.  
  143. std::string *term = new std::string[DEX];
  144. std::ifstream file("words.txt");
  145. std::string tmpstr;
  146.  
  147. for (int i = 0; i < DEX ; i++) {
  148. getline(file, tmpstr);
  149. term[i] = tmpstr;
  150. }
  151.  
  152. crswrd_fill(maze, mark, ROW, COL);
  153. crswrd_lay(maze, mark, term, find, ROW, COL, NUM, DEX);
  154. crswrd_print(maze, ROW, COL);
  155.  
  156. delete []term;
  157.  
  158. for (int i = 0; i < ROW ;i++){
  159. delete []maze[i];
  160. }
  161. delete []maze;
  162.  
  163. for (int i = 0; i < ROW ;i++){
  164. delete []mark[i];
  165. }
  166. delete []mark;
  167.  
  168. std::cout << "Words used list:\n";
  169. for (int i = 0; i < NUM ; i++)
  170. std::cout << i+1 << ". " << find[i] << '\n';
  171.  
  172. delete[] find;
  173.  
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement