-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
128 lines (116 loc) · 4.28 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
namespace ConsolePractice
{
internal class Program
{
public class Cells
{
public int Neighbors;
public bool IsBomb;
public bool IsOpen;
}
public static Cells[,] Grid = new Cells[10,10];
public static Random Rnd = new Random();
public static int Bombs;
public static int Fields;
public static bool IsOnPlay;
public static void Main()
{
Console.ForegroundColor = ConsoleColor.Cyan;
GridGenerator();
Console.WriteLine("Number of bombs: " + Bombs.ToString());
while (IsOnPlay)
{
Console.Clear();
Show();
Selector();
if (Fields != -1) continue;
IsOnPlay = false;
Show();
Console.WriteLine("Win!");
}
}
public static void GridGenerator()
{
for (var row = 0; row < 10; row++)
for (var col = 0; col < 10; col++)
{
Grid[row,col] = new Cells();
var chanser = Rnd.Next(0,7);
if (chanser == 3)
{
Grid[row, col].IsBomb = true;
Bombs++;
}
}
Fields = 100 - Bombs;
for (var row = 0; row < 10; row++)
for (var col = 0; col < 10; col++)
if (Grid[row, col].IsBomb)
for (var yoff = row - 1; yoff < row + 2; yoff++)
for (var xoff = col - 1; xoff < col + 2; xoff++)
if (yoff > -1 && yoff < 10 && xoff > -1 && xoff < 10)
Grid[yoff, xoff].Neighbors++;
IsOnPlay = true;
}
public static void Selector()
{
Console.Write("Row: ");
string Row = Console.ReadLine();
Console.Write("Coll: ");
string Col = Console.ReadLine();
int row, col;
bool Rsuc = int.TryParse(Row, out row);
bool Csuc = int.TryParse(Col, out col);
if (Rsuc && Csuc)
{
if (row >= 10 || row <= -1 || col >= 10 || col <= -1) return;
if (Grid[row, col].IsBomb)
{
IsOnPlay = false;
Grid[row, col].IsOpen = true;
Show();
Console.WriteLine("Loss.\nPress any key for restart...");
Console.ReadKey();
Main();
}
Grid[row, col].IsOpen = true;
if (Grid[row, col].Neighbors == 0)
{
SpaceChecker(row, col);
}
Fields--;
}
}
public static void SpaceChecker(int row, int col)
{
for (var yoff = row - 1; yoff <= row + 1; yoff++)
for (var xoff = col - 1; xoff <= col + 1; xoff++)
{
if (yoff <= -1 || yoff >= 10 || xoff <= -1 || xoff >= 10) continue;
if (Grid[yoff,xoff].Neighbors == 0 && Grid[yoff,xoff].IsOpen == false)
{
Grid[yoff, xoff].IsOpen = true;
Fields--;
SpaceChecker(yoff,xoff);
}
if (Grid[yoff, xoff].IsBomb) continue;
Fields--;
Grid[yoff, xoff].IsOpen = true;
}
}
public static void Show()
{
for (var row = 0; row < 10; row++)
{
for (var col = 0; col < 10; col++)
{
if (Grid[row,col].IsOpen && Grid[row,col].IsBomb == false) Console.Write( $".{Grid[row,col].Neighbors.ToString()}.");
else if (Grid[row, col].IsBomb && Grid[row,col].IsOpen) Console.Write(".b.");
else Console.Write(".+.");
}
Console.WriteLine();
}
}
}
}