-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMinesweeper.cs
245 lines (212 loc) · 7.7 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
internal class Minesweeper {
private static readonly Field[][] board = new Field[10][];
private static readonly int boardSize = 10;
private static readonly int minesNumber = 10;
private static int cursorX;
private static int cursorY;
private static readonly char cursor = '×';
private static readonly char mine = '¤';
private static readonly char flag = '¶';
private static readonly char notRevealed = '█';
private static readonly char revealed = ' ';
private static void Main(string[] args) {
Console.ForegroundColor = ConsoleColor.Green;
Console.Title = "Minesweeper";
Console.WriteLine("<------------Minesweeper-------------->");
Console.WriteLine("Created by Morasiu ([email protected])");
Console.WriteLine("Press any key to start");
Console.ReadKey();
Start();
}
private static void Start() {
cursorX = 0;
cursorY = 0;
InitEmptyBoard(boardSize);
AddMines();
while (true) {
Console.Clear();
Console.WriteLine("<| Minesweeper |>");
PrintBoard();
HandleInput();
CheckWin();
}
}
private static void CheckWin() {
int filedsLeft = boardSize * boardSize;
for (int i = 0; i < board.Length; i++) {
for (int j = 0; j < board[i].Length; j++) {
if (board[i][j].isReviled) {
filedsLeft--;
}
}
}
Console.WriteLine(filedsLeft);
if (filedsLeft <= minesNumber) {
Console.WriteLine("You won! Press any key to start again or Q to quit.");
var key = Console.ReadKey().Key;
if (key == ConsoleKey.Q) Environment.Exit(0);
Start();
}
}
private static void AddMines() {
var random = new Random();
for (var i = 0; i < minesNumber; i++) {
var x = random.Next(0, 10);
var y = random.Next(0, 10);
if (!board[x][y].hasMine) board[x][y].hasMine = true;
else i--;
}
AddInfoAboutMinesAround();
}
private static void AddInfoAboutMinesAround() {
for (var i = 0; i < board.Length; i++)
for (var j = 0; j < board[i].Length; j++) {
var bombAround = 0;
if (i - 1 >= 0 && j - 1 >= 0 && board[i - 1][j - 1].hasMine) bombAround++;
if (i - 1 >= 0 && board[i - 1][j].hasMine) bombAround++;
if (i - 1 >= 0 && j + 1 <= board[i].Length - 1 && board[i - 1][j + 1].hasMine) bombAround++;
if (j - 1 >= 0 && board[i][j - 1].hasMine) bombAround++;
if (j + 1 <= board[i].Length - 1 && board[i][j + 1].hasMine) bombAround++;
if (i + 1 <= board.Length - 1 && j - 1 >= 0 && board[i + 1][j - 1].hasMine) bombAround++;
if (i + 1 <= board.Length - 1 && board[i + 1][j].hasMine) bombAround++;
if (i + 1 <= board.Length - 1 && j + 1 <= board[i].Length - 1 && board[i + 1][j + 1].hasMine) bombAround++;
board[i][j].mineAroundNumber = bombAround;
}
}
private static void HandleInput() {
var key = Console.ReadKey().Key;
switch (key) {
case ConsoleKey.DownArrow:
board[cursorX][cursorY].hasCursor = false;
cursorX++;
if (cursorX > boardSize - 1) cursorX = boardSize - 1;
break;
case ConsoleKey.UpArrow:
board[cursorX][cursorY].hasCursor = false;
cursorX--;
if (cursorX < 0) cursorX = 0;
break;
case ConsoleKey.LeftArrow:
board[cursorX][cursorY].hasCursor = false;
cursorY--;
if (cursorY < 0) cursorY = 0;
break;
case ConsoleKey.RightArrow:
board[cursorX][cursorY].hasCursor = false;
cursorY++;
if (cursorY > boardSize - 1) cursorY = boardSize - 1;
break;
case ConsoleKey.F:
board[cursorX][cursorY].isFlagged = !board[cursorX][cursorY].isFlagged;
break;
case ConsoleKey.Q:
Environment.Exit(0);
break;
case ConsoleKey.Enter:
if (board[cursorX][cursorY].isFlagged) {
} else if (board[cursorX][cursorY].hasMine) {
Console.WriteLine("You lost, press any key to play again or Q to quit");
var consoleKey = Console.ReadKey().Key;
if (consoleKey == ConsoleKey.Q) Environment.Exit(0);
Start();
} else {
board[cursorX][cursorY].isReviled = true;
if (board[cursorX][cursorY].mineAroundNumber == 0) CheckAroundEmptyField(cursorX, cursorY);
}
break;
}
board[cursorX][cursorY].hasCursor = true;
}
private static void CheckAroundEmptyField(int x, int y) {
if (x - 1 >= 0 && y - 1 >= 0 && !board[x - 1][y - 1].isReviled) {
board[x - 1][y - 1].isReviled = true;
if (board[x - 1][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y - 1);
}
if (x - 1 >= 0 && !board[x - 1][y].isReviled) {
board[x - 1][y].isReviled = true;
if (board[x - 1][y].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y);
}
if (x - 1 >= 0 && y + 1 <= board[x].Length - 1 && !board[x - 1][y + 1].isReviled) {
board[x - 1][y + 1].isReviled = true;
if (board[x - 1][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y + 1);
}
if (y - 1 >= 0 && !board[x][y - 1].isReviled) {
board[x][y - 1].isReviled = true;
if (board[x][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x, y - 1);
}
if (y + 1 <= board[x].Length - 1 && !board[x][y + 1].isReviled) {
board[x][y + 1].isReviled = true;
if (board[x][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x, y + 1);
}
if (x + 1 <= board.Length - 1 && y - 1 >= 0 && !board[x + 1][y - 1].isReviled) {
board[x + 1][y - 1].isReviled = true;
if (board[x + 1][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y - 1);
}
if (x + 1 <= board.Length - 1 && !board[x + 1][y].isReviled) {
board[x + 1][y].isReviled = true;
if (board[x + 1][y].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y);
}
if (x + 1 <= board.Length - 1 && y + 1 <= board[x].Length - 1 && !board[x + 1][y + 1].isReviled) {
board[x + 1][y + 1].isReviled = true;
if (board[x + 1][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y + 1);
}
}
private static void PrintBoard() {
PrintTopBorder();
for (var i = 0; i < board.Length; i++) {
Console.Write('║');
for (var j = 0; j < board.Length; j++)
if (board[i][j].hasCursor) {
Console.Write(cursor);
} else if (board[i][j].isFlagged) {
Console.Write(flag);
} else if (board[i][j].isReviled) {
if (board[i][j].mineAroundNumber > 0) Console.Write(board[i][j].mineAroundNumber);
else Console.Write(revealed);
} else if (!board[i][j].isReviled) {
Console.Write(notRevealed);
} else {
Console.Write('?');
}
Console.Write("║ " + i + "\n");
}
PrintLowBorder();
PrintXCords();
Console.Write("\n\n");
Console.WriteLine("Arrows - Navigation, F - flag, Enter - check, Q - quit");
}
private static void PrintXCords() {
for (var i = 0; i < 10; i++) Console.Write(i);
}
private static void PrintLowBorder() {
Console.Write('╚');
for (var i = 0; i < 10; i++) Console.Write('═');
Console.Write("╝\n ");
}
private static void PrintTopBorder() {
Console.Write('╔');
for (var i = 0; i < boardSize; i++) Console.Write('═');
Console.Write("╗\n");
}
private static void InitEmptyBoard(int size = 10) {
for (var i = 0; i < boardSize; i++) {
board[i] = new Field[10];
for (var j = 0; j < boardSize; j++)
board[i][j] = new Field {
cordX = j,
cordY = i
};
}
board[0][0].hasCursor = true;
}
}
internal class Field {
public int cordX;
public int cordY;
public bool hasCursor;
public bool hasMine;
public bool isFlagged;
public bool isReviled;
public int mineAroundNumber;
}