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