Advertisement
force1987

Minesweeper

May 13th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. setlocale(LC_ALL, "RUS");
  7. srand(time(NULL));
  8. short score = 0, bombs, counter, size;
  9. const short max = 99;
  10. bool end, debug;
  11. cout << "\tИгра САПЁР" << endl << endl;
  12. cout << "Включить debug-режим? 1-да, 0-нет\n";
  13. cin >> debug;
  14. do
  15. {
  16. char pole[max][max]{};
  17. bool check[max][max]{};
  18. counter = 0;
  19. do
  20. {
  21. cout << "Введите размер поля от 5 до 99: ";
  22. cin >> size;
  23. } while (size < 5 || size>99);
  24. for (short i = 0; i < size; i++)
  25. {
  26. for (short j = 0; j < size; j++)
  27. {
  28. pole[i][j] = '0';
  29. }
  30. }
  31. do
  32. {
  33. cout << "Введите количество бомб: ";
  34. cin >> bombs;
  35. if (bombs < 1 || bombs>(size * size) - 1) cout << "Некорректный ввод, введите ещё раз\n";
  36. } while (bombs < 1 || bombs>(size * size) - 1);
  37. short a, b;
  38. for (short i = 0; i < bombs; i++)
  39. {
  40. a = rand() % size;
  41. b = rand() % size;
  42. if (pole[a][b] != 'X')
  43. {
  44. pole[a][b] = 'X';
  45. }
  46. else i--;
  47. }
  48. cout << " ";
  49. for (short i = 1; i <= size; i++)
  50. {
  51. if (i % 10 != 0)cout << i % 10 << " ";
  52. else cout << " ";
  53. }
  54. cout << endl;
  55. for (short i = 0; i < size; i++)
  56. {
  57. if (i < 9)cout << " ";
  58. cout << i + 1 << " ";
  59. for (short j = 0; j < size; j++)
  60. {
  61. if (pole[i][j] != 'X')
  62. {
  63. if (pole[i + 1][j + 1] == 'X' && i + 1 >= 0 && i + 1 < size && j + 1 >= 0 && j + 1 < size)pole[i][j] ++;
  64. if (pole[i + 1][j] == 'X' && i + 1 >= 0 && i + 1 < size && j >= 0 && j < size)pole[i][j] ++;
  65. if (pole[i + 1][j - 1] == 'X' && i + 1 >= 0 && i + 1 < size && j - 1 >= 0 && j - 1 < size)pole[i][j] ++;
  66. if (pole[i][j + 1] == 'X' && i >= 0 && i < size && j + 1 >= 0 && j + 1 < size)pole[i][j] ++;
  67. if (pole[i][j - 1] == 'X' && i >= 0 && i < size && j - 1 >= 0 && j - 1 < size)pole[i][j] ++;
  68. if (pole[i - 1][j + 1] == 'X' && i - 1 >= 0 && i - 1 < size && j + 1 >= 0 && j + 1 < size)pole[i][j] ++;
  69. if (pole[i - 1][j] == 'X' && i - 1 >= 0 && i - 1 < size && j >= 0 && j < size)pole[i][j] ++;
  70. if (pole[i - 1][j - 1] == 'X' && i - 1 >= 0 && i - 1 < size && j - 1 >= 0 && j - 1 < size)pole[i][j] ++;
  71. }
  72. if (debug == 1)
  73. {
  74. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  75. }
  76. else
  77. {
  78. if (check[i][j] == 1)
  79. {
  80. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  81. }
  82. else cout << "* ";
  83. }
  84. }
  85. cout << endl;
  86. }
  87. while (counter < (size * size) - bombs)
  88. {
  89. do
  90. {
  91. cout << "Введите номер строки и номер столбца: ";
  92. cin >> a >> b;
  93. system("cls");
  94. if (a < 1 || a > size || b < 1 || b>size)
  95. {
  96. cout << " ";
  97. for (short i = 1; i <= size; i++)
  98. {
  99. if (i % 10 != 0)cout << i % 10 << " ";
  100. else cout << " ";
  101. }
  102. cout << endl;
  103. for (short i = 0; i < size; i++)
  104. {
  105. if (i < 9)cout << " ";
  106. cout << i + 1 << " ";
  107. for (short j = 0; j < size; j++)
  108. {
  109. if (check[i][j] == 1)
  110. {
  111. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  112. }
  113. else cout << "* ";
  114. }
  115. cout << endl;
  116. }
  117. cout << "Некорректный ввод, введите ещё раз\n";
  118. }
  119. else if (check[a - 1][b - 1] == 1)
  120. {
  121. cout << " ";
  122. for (short i = 1; i <= size; i++)
  123. {
  124. if (i % 10 != 0)cout << i % 10 << " ";
  125. else cout << " ";
  126. }
  127. cout << endl;
  128. for (short i = 0; i < size; i++)
  129. {
  130. if (i < 9)cout << " ";
  131. cout << i + 1 << " ";
  132. for (short j = 0; j < size; j++)
  133. {
  134. if (check[i][j] == 1)
  135. {
  136. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  137. }
  138. else cout << "* ";
  139. }
  140. cout << endl;
  141. }
  142. cout << "Нет смысла разминировать одну и ту же ячейку, введите ещё раз\n";
  143. }
  144. } while (check[a - 1][b - 1] == 1 || (a < 1 || a > size || b < 1 || b>size));
  145. if (pole[a - 1][b - 1] == 'X')
  146. {
  147. for (short i = 0; i < size; i++)
  148. {
  149. if (i < 9)cout << " ";
  150. cout << i + 1 << " ";
  151.  
  152. for (short j = 0; j < size; j++)
  153. {
  154. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  155. }
  156. cout << endl;
  157.  
  158. }
  159. cout << "BOOM!!!!!Game over\nУспешных попаданий: " << score << endl;
  160. score = 0;
  161. break;
  162. }
  163. else
  164. {
  165. score++;
  166. check[a - 1][b - 1] = 1;
  167. counter++;
  168. if (pole[a - 1][b - 1] == '0')
  169. {
  170. short ip1[max * max]{}, ip2[max * max]{}, index = 0, indexcounter = 0;
  171. ip1[index] = a;
  172. ip2[index] = b;
  173. for (short x, y; ip1[index] != 0; index++)
  174. {
  175. x = ip1[index];
  176. y = ip2[index];
  177. if (pole[x][y] == '0' && x >= 0 && x < size && y >= 0 && y < size && check[x][y] == 0)
  178. {
  179. indexcounter++;
  180. ip1[indexcounter] = x + 1;
  181. ip2[indexcounter] = y + 1;
  182. }
  183. if (check[x][y] == 0 && x >= 0 && x < size && y >= 0 && y < size)
  184. {
  185. check[x][y] = 1;
  186. counter++;
  187. }
  188. if (pole[x][y - 1] == '0' && x >= 0 && x < size && y - 1 >= 0 && y - 1 < size && check[x][y - 1] == 0)
  189. {
  190. indexcounter++;
  191. ip1[indexcounter] = x + 1;
  192. ip2[indexcounter] = y;
  193. }
  194. if (check[x][y - 1] == 0 && x >= 0 && x < size && y - 1 >= 0 && y - 1 < size)
  195. {
  196. check[x][y - 1] = 1;
  197. counter++;
  198. }
  199. if (pole[x][y - 2] == '0' && x >= 0 && x < size && y - 2 >= 0 && y - 2 < size && check[x][y - 2] == 0)
  200. {
  201. indexcounter++;
  202. ip1[indexcounter] = x + 1;
  203. ip2[indexcounter] = y - 1;
  204. }
  205. if (check[x][y - 2] == 0 && x >= 0 && x < size && y - 2 >= 0 && y - 2 < size)
  206. {
  207. check[x][y - 2] = 1;
  208. counter++;
  209. }
  210. if (pole[x - 1][y] == '0' && x - 1 >= 0 && x - 1 < size && y >= 0 && y < size && check[x - 1][y] == 0)
  211. {
  212. indexcounter++;
  213. ip1[indexcounter] = x;
  214. ip2[indexcounter] = y + 1;
  215. }
  216. if (check[x - 1][y] == 0 && x - 1 >= 0 && x - 1 < size && y >= 0 && y < size)
  217. {
  218. check[x - 1][y] = 1;
  219. counter++;
  220. }
  221. if (pole[x - 1][y - 2] == '0' && x - 1 >= 0 && x - 1 < size && y - 2 >= 0 && y - 2 < size && check[x - 1][y - 2] == 0)
  222. {
  223. indexcounter++;
  224. ip1[indexcounter] = x;
  225. ip2[indexcounter] = y - 1;
  226. }
  227. if (check[x - 1][y - 2] == 0 && x - 1 >= 0 && x - 1 < size && y - 2 >= 0 && y - 2 < size)
  228. {
  229. check[x - 1][y - 2] = 1;
  230. counter++;
  231. }
  232. if (pole[x - 2][y] == '0' && x - 2 >= 0 && x - 2 < size && y >= 0 && y < size && check[x - 2][y] == 0)
  233. {
  234. indexcounter++;
  235. ip1[indexcounter] = x - 1;
  236. ip2[indexcounter] = y + 1;
  237. }
  238. if (check[x - 2][y] == 0 && x - 2 >= 0 && x - 2 < size && y >= 0 && y < size)
  239. {
  240. check[x - 2][y] = 1;
  241. counter++;
  242. }
  243. if (pole[x - 2][y - 1] == '0' && x - 2 >= 0 && x - 2 < size && y - 1 >= 0 && y - 1 < size && check[x - 2][y - 1] == 0)
  244. {
  245. indexcounter++;
  246. ip1[indexcounter] = x - 1;
  247. ip2[indexcounter] = y;
  248. }
  249. if (check[x - 2][y - 1] == 0 && x - 2 >= 0 && x - 2 < size && y - 1 >= 0 && y - 1 < size)
  250. {
  251. check[x - 2][y - 1] = 1;
  252. counter++;
  253. }
  254. if (pole[x - 2][y - 2] == '0' && x - 2 >= 0 && x - 2 < size && y - 2 >= 0 && y - 2 < size && check[x - 2][y - 2] == 0)
  255. {
  256. indexcounter++;
  257. ip1[indexcounter] = x - 1;
  258. ip2[indexcounter] = y - 1;
  259. }
  260. if (check[x - 2][y - 2] == 0 && x - 2 >= 0 && x - 2 < size && y - 2 >= 0 && y - 2 < size)
  261. {
  262. check[x - 2][y - 2] = 1;
  263. counter++;
  264. }
  265. }
  266. }
  267. }
  268. if (debug == 1)
  269. {
  270. cout << "Режим отладки\n";
  271. for (short i = 0; i < size; i++)
  272. {
  273. for (short j = 0; j < size; j++)
  274. {
  275. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  276. }
  277. cout << endl;
  278. }
  279. cout << endl;
  280. }
  281. cout << " ";
  282. for (short i = 1; i <= size; i++)
  283. {
  284. if (i % 10 != 0)cout << i % 10 << " ";
  285. else cout << " ";
  286. }
  287. cout << endl;
  288. for (short i = 0; i < size; i++)
  289. {
  290. if (i < 9)cout << " ";
  291. cout << i + 1 << " ";
  292. for (short j = 0; j < size; j++)
  293. {
  294. if (check[i][j] == 1)
  295. {
  296. if (pole[i][j] == '0')cout << "- "; else cout << pole[i][j] << " ";
  297. }
  298. else cout << "* ";
  299. }
  300. cout << endl;
  301. }
  302. if (counter == (size * size) - bombs)
  303. {
  304. cout << "Congratulations!!!\nУспешных попаданий: " << score << endl;
  305. }
  306. }
  307. cout << "\tНачать сначала?\n1-да\n0-нет\n";
  308. cin >> end;
  309. system("cls");
  310. } while (end == 1);
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement