-
Notifications
You must be signed in to change notification settings - Fork 126
/
Program.cs
205 lines (195 loc) · 5.47 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
const int mine = -1;
(int Value, bool Visible)[,] board;
Console.WriteLine("Minesweeper");
Console.WriteLine();
int selectedWidth = GetIntegerInput($"Enter board width (10-{Math.Min(Console.LargestWindowWidth, 50)}): ", 10, Math.Min(Console.LargestWindowWidth, 50));
int selectedHeight = GetIntegerInput($"Enter board height (10-{Math.Min(Console.LargestWindowHeight, 50)}): ", 10, Math.Min(Console.LargestWindowHeight, 50));
double mineRatio = GetMineRatio("Enter mine ratio (example: 0.1): ");
int mineCount = (int)(selectedWidth * selectedHeight * mineRatio);
if (OperatingSystem.IsWindows())
{
Console.WindowHeight = selectedHeight;
Console.WindowWidth = selectedWidth;
}
(int Column, int Row) = (selectedWidth / 2, selectedHeight / 2);
GenerateBoard();
Console.Clear();
RenderBoard();
int height = Console.WindowHeight;
int width = Console.WindowWidth;
while (true)
{
if (Console.WindowHeight != height || Console.WindowWidth != width)
{
RenderBoard();
}
Console.SetCursorPosition(Column, Row);
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow: Row = Math.Max(Row - 1, 0); break;
case ConsoleKey.DownArrow: Row = Math.Min(Row + 1, selectedHeight - 1); break;
case ConsoleKey.LeftArrow: Column = Math.Max(Column - 1, 0); break;
case ConsoleKey.RightArrow: Column = Math.Min(Column + 1, selectedWidth - 1); break;
case ConsoleKey.Enter:
if (!board[Column, Row].Visible)
{
if (board[Column, Row].Value == mine)
{
for (int column = 0; column < selectedWidth; column++)
{
for (int row = 0; row < selectedHeight; row++)
{
board[column, row].Visible = true;
}
}
RenderBoard();
Console.SetCursorPosition(0, selectedHeight - 1);
Console.Write("You Lose. Press Enter To Exit...");
Console.ReadLine();
Console.Clear();
Console.Write("Minesweeper was closed.");
return;
}
else if (board[Column, Row].Value is 0)
{
Reveal(Column, Row);
RenderBoard();
}
else
{
board[Column, Row].Visible = true;
RenderBoard();
}
int visibleCount = 0;
for (int column = 0; column < selectedWidth; column++)
{
for (int row = 0; row < selectedHeight; row++)
{
if (board[column, row].Visible)
{
visibleCount++;
}
}
}
if (visibleCount == selectedWidth * selectedHeight - mineCount)
{
Console.SetCursorPosition(0, selectedHeight - 1);
Console.Write("You Win. Press Enter To Exit...");
Console.ReadLine();
Console.Clear();
Console.Write("Minesweeper was closed.");
return;
}
}
break;
case ConsoleKey.Escape:
Console.Clear();
Console.Write("Minesweeper was closed.");
return;
}
}
IEnumerable<(int Row, int Column)> AdjacentTiles(int column, int row)
{
// A B C
// D + E
// F G H
/* A */ if (row > 0 && column > 0) yield return (row - 1, column - 1);
/* B */ if (row > 0) yield return (row - 1, column);
/* C */ if (row > 0 && column < selectedWidth - 1) yield return (row - 1, column + 1);
/* D */ if (column > 0) yield return (row, column - 1);
/* E */ if (column < selectedWidth - 1) yield return (row, column + 1);
/* F */ if (row < selectedHeight - 1 && column > 0) yield return (row + 1, column - 1);
/* G */ if (row < selectedHeight - 1) yield return (row + 1, column);
/* H */ if (row < selectedHeight - 1 && column < selectedWidth - 1) yield return (row + 1, column + 1);
}
int GetIntegerInput(string prompt, int min, int max)
{
int inputValue;
Console.Write(prompt);
while (!int.TryParse(Console.ReadLine(), out inputValue) || inputValue < min || max < inputValue)
{
Console.WriteLine("Invalid Input. Try Again...");
Console.Write(prompt);
}
return inputValue;
}
double GetMineRatio(string prompt)
{
double inputValue;
Console.Write(prompt);
while (!double.TryParse(Console.ReadLine(), out inputValue) || (mineCount = (int)(selectedWidth * selectedHeight * inputValue)) < 0 || mineCount > selectedHeight * selectedWidth)
{
Console.WriteLine("Invalid Input. Try Again...");
Console.Write(prompt);
}
return inputValue;
}
void GenerateBoard()
{
board = new (int Value, bool Visible)[selectedWidth, selectedHeight];
var coordinates = new List<(int Row, int Column)>();
for (int column = 0; column < selectedWidth; column++)
{
for (int row = 0; row < selectedHeight; row++)
{
coordinates.Add((column, row));
}
}
for (int i = 0; i < mineCount; i++)
{
int randomIndex = Random.Shared.Next(0, coordinates.Count);
(int column, int row) = coordinates[randomIndex];
coordinates.RemoveAt(randomIndex);
board[column, row] = (mine, false);
foreach (var tile in AdjacentTiles(column, row))
{
if (board[tile.Column, tile.Row].Value != mine)
{
board[tile.Column, tile.Row].Value++;
}
}
}
}
char Render(int value) => value switch
{
mine => '@',
0 => ' ',
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
_ => throw new NotImplementedException(),
};
void RenderBoard()
{
for (int row = 0; row < selectedHeight; row++)
{
for (int column = 0; column < selectedWidth; column++)
{
Console.SetCursorPosition(column, row);
Console.Write(board[column, row].Visible
? Render(board[column, row].Value)
: '█');
}
}
}
void Reveal(int column, int row)
{
board[column, row].Visible = true;
if (board[column, row].Value is 0)
{
foreach (var (r, c) in AdjacentTiles(column, row))
{
if (!board[c, r].Visible)
{
Reveal(c, r);
}
}
}
}