using System.Collections.Generic;
public static void Main()
bool success_conv = false;
Console.Write("width = ");
string w_str = Console.ReadLine();
w = Convert.ToInt32(w_str);
Console.WriteLine("Не цифра");
catch (OverflowException)
Console.WriteLine("Слишком большое число");
Console.WriteLine(String.Format("Непредвиденная ошибка {0}", ex.Message));
Console.WriteLine("width должно быть больше 1");
Console.Write("height = ");
string h_str = Console.ReadLine();
h = Convert.ToInt32(h_str);
Console.WriteLine("Не цифра");
catch (OverflowException)
Console.WriteLine("Слишком большое число");
Console.WriteLine(String.Format("Непредвиденная ошибка {0}", ex.Message));
Console.WriteLine("height должно быть больше 1");
#region input bombs count
catch (OverflowException)
Console.WriteLine("Слишком большое число бомб");
Console.WriteLine(String.Format("Непредвиденная ошибка {0}", ex.Message));
Console.WriteLine("Выход из программы");
Console.WriteLine(String.Format("max bombs count: {0}", max_bc));
Console.Write("bombs count = ");
string bc_str = Console.ReadLine();
bc = Convert.ToInt32(bc_str);
Console.WriteLine(String.Format("Максимальное число бомб должно быть не больше {0}", max_bc));
Console.WriteLine("Не цифра");
catch (OverflowException)
Console.WriteLine("Слишком большое число");
Console.WriteLine(String.Format("Непредвиденная ошибка {0}", ex.Message));
Console.WriteLine("bombs count должно быть больше 0");
var res = GenerateMinesweeperMap(w, h, bc);
Console.WriteLine(String.Format("Результат работы метода GenerateMinesweeperMap({0}, {1}, {2})", w, h, bc));
Console.WriteLine("null");
static string GenerateMinesweeperMap(int width, int height, int bombsCount)
int[,] mas = new int[height, width];
Random rng = new Random();
int lenght = width * height;
List<int> list_random_uniqe_numbers = new List<int>();
int countAddedRandVal = 0;
randVal = rng.Next(lenght);
if (!list_random_uniqe_numbers.Contains(randVal))
list_random_uniqe_numbers.Add(randVal);
while (countAddedRandVal != bombsCount);
foreach (var val in list_random_uniqe_numbers)
GetPozAtMas(val, width, out w, out h);
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++)
if (mas[h + 1, w + 1] == 9)
if (mas[h + 1, w - 1] == 9)
if ((w != 0) && (w != width - 1))
if (mas[h + 1, w - 1] == 9)
if (mas[h + 1, w + 1] == 9)
if (mas[h - 1, w + 1] == 9)
if (mas[h - 1, w - 1] == 9)
if ((w != 0) && (w != width - 1))
if (mas[h - 1, w - 1] == 9)
if (mas[h - 1, w + 1] == 9)
if ((h != 0) && (h != height - 1))
if (mas[h - 1, w + 1] == 9)
if (mas[h + 1, w + 1] == 9)
if (mas[h - 1, w - 1] == 9)
if (mas[h + 1, w - 1] == 9)
if ((w != width - 1) && (w != 0))
if (mas[h - 1, w + 1] == 9)
if (mas[h + 1, w + 1] == 9)
if (mas[h - 1, w - 1] == 9)
if (mas[h + 1, w - 1] == 9)
string res = ConvertMasToStringBuilder(mas, width, height);
static string ConvertMasToStringBuilder(int[,] mas, int width, int height)
StringBuilder sb = new StringBuilder(width * height + height);
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
sb.Append((char)(mas[i, j] + 48));
static void GetPozAtMas(int poz, int w_lenght, out int w, out int h)
var intPath = poz / w_lenght;
w = poz - (w_lenght * h);
static int GetPozAtMas(int w, int h, int w_lenght)
if ((w == 0) && (h == 0))