fork download
  1. using System;
  2.  
  3. class Minesweeper
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("=== ГРА 'САПЕР' ===");
  8.  
  9. try
  10. {
  11. Console.Write("Ширина поля: ");
  12. int width = int.Parse(Console.ReadLine());
  13.  
  14. Console.Write("Висота поля: ");
  15. int height = int.Parse(Console.ReadLine());
  16.  
  17. Console.Write("Кількість мін: ");
  18. int mines = int.Parse(Console.ReadLine());
  19.  
  20. if (mines >= width * height)
  21. {
  22. Console.WriteLine("Забагато мін!");
  23. return;
  24. }
  25.  
  26. char[,] board = new char[height, width];
  27. char[,] visible = new char[height, width];
  28. Random random = new Random();
  29.  
  30. // Ініціалізація
  31. for (int y = 0; y < height; y++)
  32. {
  33. for (int x = 0; x < width; x++)
  34. {
  35. board[y, x] = ' ';
  36. visible[y, x] = '.';
  37. }
  38. }
  39.  
  40. // Розставлення мін
  41. for (int i = 0; i < mines; i++)
  42. {
  43. int x, y;
  44. do
  45. {
  46. x = random.Next(width);
  47. y = random.Next(height);
  48. } while (board[y, x] == 'X');
  49. board[y, x] = 'X';
  50. }
  51.  
  52. // Заповнення чисел
  53. for (int y = 0; y < height; y++)
  54. {
  55. for (int x = 0; x < width; x++)
  56. {
  57. if (board[y, x] != 'X')
  58. {
  59. int count = CountMines(board, x, y, width, height);
  60. board[y, x] = count > 0 ? (char)(count + '0') : ' ';
  61. }
  62. }
  63. }
  64.  
  65. // Геймплей
  66. bool gameOver = false;
  67. while (!gameOver)
  68. {
  69. PrintBoard(visible, width, height);
  70.  
  71. Console.Write("\nКоординати (x y): ");
  72. string input = Console.ReadLine();
  73.  
  74. if (input == "q") break;
  75.  
  76. string[] coords = input.Split();
  77. if (coords.Length != 2)
  78. {
  79. Console.WriteLine("Невірний формат! Введіть x y");
  80. continue;
  81. }
  82.  
  83. if (!int.TryParse(coords[0], out int x) || !int.TryParse(coords[1], out int y))
  84. {
  85. Console.WriteLine("Невірні координати!");
  86. continue;
  87. }
  88.  
  89. if (x < 0 || x >= width || y < 0 || y >= height)
  90. {
  91. Console.WriteLine($"Координати поза межами поля! (0-{width-1}, 0-{height-1})");
  92. continue;
  93. }
  94.  
  95. if (board[y, x] == 'X')
  96. {
  97. Console.WriteLine("BOOM! Ви програли!");
  98. gameOver = true;
  99. }
  100. else if (visible[y, x] == '.')
  101. {
  102. Reveal(board, visible, x, y, width, height);
  103. if (CheckWin(visible, board, width, height))
  104. {
  105. Console.WriteLine("Перемога!");
  106. gameOver = true;
  107. }
  108. }
  109. else
  110. {
  111. Console.WriteLine("Ця клітинка вже відкрита!");
  112. }
  113. }
  114.  
  115. // Показати всі міни після гри
  116. for (int y = 0; y < height; y++)
  117. {
  118. for (int x = 0; x < width; x++)
  119. {
  120. if (board[y, x] == 'X')
  121. {
  122. visible[y, x] = 'X';
  123. }
  124. }
  125. }
  126.  
  127. PrintBoard(visible, width, height);
  128. }
  129. catch (Exception ex)
  130. {
  131. Console.WriteLine($"Помилка: {ex.Message}");
  132. }
  133. }
  134.  
  135. static int CountMines(char[,] board, int x, int y, int w, int h)
  136. {
  137. int count = 0;
  138. for (int dy = -1; dy <= 1; dy++)
  139. {
  140. for (int dx = -1; dx <= 1; dx++)
  141. {
  142. if (dx == 0 && dy == 0) continue;
  143.  
  144. int nx = x + dx, ny = y + dy;
  145. if (nx >= 0 && nx < w && ny >= 0 && ny < h && board[ny, nx] == 'X')
  146. count++;
  147. }
  148. }
  149. return count;
  150. }
  151.  
  152. static void Reveal(char[,] board, char[,] visible, int x, int y, int w, int h)
  153. {
  154. if (x < 0 || x >= w || y < 0 || y >= h || visible[y, x] != '.')
  155. return;
  156.  
  157. visible[y, x] = board[y, x];
  158.  
  159. if (board[y, x] == ' ')
  160. {
  161. for (int dy = -1; dy <= 1; dy++)
  162. {
  163. for (int dx = -1; dx <= 1; dx++)
  164. {
  165. if (dx == 0 && dy == 0) continue;
  166. Reveal(board, visible, x + dx, y + dy, w, h);
  167. }
  168. }
  169. }
  170. }
  171.  
  172. static bool CheckWin(char[,] visible, char[,] board, int w, int h)
  173. {
  174. for (int y = 0; y < h; y++)
  175. {
  176. for (int x = 0; x < w; x++)
  177. {
  178. if (board[y, x] != 'X' && visible[y, x] == '.')
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184.  
  185. static void PrintBoard(char[,] board, int w, int h)
  186. {
  187. Console.Write("\n ");
  188. for (int x = 0; x < w; x++)
  189. Console.Write(x.ToString().PadRight(2));
  190.  
  191. Console.WriteLine("\n " + new string('-', w * 2 + 1));
  192.  
  193. for (int y = 0; y < h; y++)
  194. {
  195. Console.Write(y.ToString().PadRight(2) + "|");
  196. for (int x = 0; x < w; x++)
  197. Console.Write(board[y, x] + " ");
  198. Console.WriteLine();
  199. }
  200. }
  201. }
Success #stdin #stdout 0.06s 32520KB
stdin
Standard input is empty
stdout
=== ГРА 'САПЕР' ===
Ширина поля: Помилка: Value cannot be null. (Parameter 's')