public static string[] GenerateMinesweeperMap(int width, int height, int bombsCount)
string[] result_map = new string[height];
int[,] num_map = new int[width+2, height+2];
Random rnd = new Random();
row = rnd.Next(height)+1;
if (num_map[row,col] != 9)
while (del_mines != bombsCount);
for (row = 1; row <= height; row ++)
for(col=1; col< width; col++)
if (num_map[row,col] != 9)
if (num_map[row-1,col-1]==9) neigh_mines++;
if (num_map[row-1,col]==9) neigh_mines++;
if (num_map[row-1,col+1]==9) neigh_mines++;
if (num_map[row,col-1]==9) neigh_mines++;
if (num_map[row,col+1]==9) neigh_mines++;
if (num_map[row+1,col-1]==9) neigh_mines++;
if (num_map[row+1,col]==9) neigh_mines++;
if (num_map[row+1,col+1]==9) neigh_mines++;
num_map[row,col] = neigh_mines;
for (int i=0; i< height; i++)
for (int j=0;j<width;j++)
if(num_map[i,j]==0) map_line += "''";
else if(num_map[i,j]==9) map_line += "*";
map_line += num_map[i,j].ToString();
public static void Main()
Console.Write("Width: ");
int width = Convert.ToInt32(Console.ReadLine());
throw new Exception("There must be at least 3 columns!");
Console.Write("Height: ");
int height = Convert.ToInt32(Console.ReadLine());
throw new Exception("There must be at least 3 lines!");
Console.Write("Number of bombs: ");
int bombsCount = Convert.ToInt32(Console.ReadLine());
if (bombsCount >= height*width)
throw new Exception("Too many bombs, dude!");
else if (bombsCount <= 0)
throw new Exception("Insufficient number of bombs!");
string[] result_map = GenerateMinesweeperMap(width, height, bombsCount);
foreach(string line in result_map)
System.Console.WriteLine(line);
Console.WriteLine("Something went wrong!");