-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minesweeper.cs
121 lines (102 loc) · 3.04 KB
/
Minesweeper.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
namespace Minesweeper;
public static class Minesweeper
{
static void Main()
{
var field = new Minefield(5, 5);
bool gameOver = false;
//set the bombs...
field.SetBomb(0, 0);
field.SetBomb(0, 1);
field.SetBomb(1, 1);
field.SetBomb(1, 4);
field.SetBomb(4, 2);
//the mine field should look like this now:
// 01234
//4|1X1
//3|11111
//2|2211X
//1|XX111
//0|X31
// Game code...
Console.WriteLine("Minefield:");
PrintBoard(field);
do
{
var usersInput = GetUserInput();
if (usersInput?.Length == 2 && int.TryParse(usersInput[0], out int row) && int.TryParse(usersInput[1], out int col))
{
if (field.IsInsideGameGrid(row, col))
{
if (field.IsBomb(row, col))
{
PrintBoard(field);
Console.WriteLine("You hit a bomb, game over");
gameOver = true;
}
else if (!field.IsUncovered(row, col))
{
field.UncoverCell(row, col);
PrintBoard(field);
}
else
{
Console.WriteLine("This cell is already uncovered.");
}
}
else
{
Console.WriteLine("Invalid row or column.");
}
}
else
{
Console.WriteLine("Invalid input.");
}
}
while (!gameOver);
}
public static string[] GetUserInput()
{
Console.WriteLine("Your move: (X Y)");
string[] input = Console.ReadLine().Split(' ');
return input;
}
private static void PrintBoard(Minefield field)
{
Console.Write(" ");
for (int i = 0; i < field.Rows; i++)
{
Console.Write(i);
}
Console.WriteLine();
for (int i = field.Columns - 1; i >= 0; i--)
{
Console.Write(i + "|");
for (int j = 0; j < field.Columns; j++)
{
if (field.IsUncovered(j, i))
{
if (field.IsBomb(j, i))
{
Console.Write("X");
}
else
{
// Cell doesn't contain a bomb, display the count of neighboring bombs.
int neighboringBombs = field.CountNeighborBombs(j, i);
if (neighboringBombs != 0)
{
Console.Write(neighboringBombs);
}
}
}
else
{
Console.Write("?");
}
}
Console.WriteLine();
}
}
}