Skip to content

Commit 460bec4

Browse files
committed
Minesweeeper!
1 parent 49f8912 commit 460bec4

File tree

4 files changed

+251
-2
lines changed

4 files changed

+251
-2
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ If you have any questions, you can contact me by email morasiu2@gmail.com
2727
* [~~17 Eurelian Path~~](#17)
2828
* [18 Simple File Explorer](#18)
2929
* [19 Count Words](#19)
30+
* [20 Count Words](#20)
3031

3132
## Bonus
3233

@@ -35,8 +36,8 @@ If you have any questions, you can contact me by email morasiu2@gmail.com
3536
## Progress
3637

3738
All - **100** <br>
38-
Done - **19** <br>
39-
Remain - **81** <br>
39+
Done - **20** <br>
40+
Remain - **80** <br>
4041

4142
* <a name="00">00</a> Name Generator - 29.01.2018 *Done* (`Python 3`) <br>
4243
![00](docs/images/00.png)
@@ -90,5 +91,7 @@ Maybe some day?
9091
![18](docs/images/18.png)
9192
* <a name="19">19</a> Count Words - 13.08.2018 *Done* (`C#`)<br>
9293
![19](docs/images/19.png)
94+
* <a name="19">20</a> Minesweeper - 14.08.2018 *Done* (`C#`)<br>
95+
![20](docs/images/20.png)
9396
## Bonus
9497
* <a name="bonus1">1.</a> Loading animation in console 24.02.2018 *Done* (`C#`)![Bonus 1](docs/images/bonus1.gif)
+246
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
using System;
2+
3+
internal class Minesweeper {
4+
private static readonly Field[][] board = new Field[10][];
5+
private static readonly int boardSize = 10;
6+
private static readonly int minesNumber = 10;
7+
private static int cursorX;
8+
private static int cursorY;
9+
10+
private static readonly char cursor = '×';
11+
private static readonly char mine = '¤';
12+
private static readonly char flag = '¶';
13+
private static readonly char notRevealed = '█';
14+
private static readonly char revealed = ' ';
15+
16+
private static void Main(string[] args) {
17+
Console.ForegroundColor = ConsoleColor.Green;
18+
Console.Title = "Minesweeper";
19+
Console.WriteLine("<------------Minesweeper-------------->");
20+
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
21+
Console.WriteLine("Press any key to start");
22+
Console.ReadKey();
23+
24+
Start();
25+
}
26+
27+
private static void Start() {
28+
cursorX = 0;
29+
cursorY = 0;
30+
InitEmptyBoard(boardSize);
31+
AddMines();
32+
while (true) {
33+
Console.Clear();
34+
Console.WriteLine("<| Minesweeper |>");
35+
PrintBoard();
36+
HandleInput();
37+
CheckWin();
38+
}
39+
}
40+
41+
private static void CheckWin() {
42+
int filedsLeft = boardSize * boardSize;
43+
for (int i = 0; i < board.Length; i++) {
44+
for (int j = 0; j < board[i].Length; j++) {
45+
if (board[i][j].isReviled) {
46+
filedsLeft--;
47+
}
48+
}
49+
}
50+
Console.WriteLine(filedsLeft);
51+
if (filedsLeft <= minesNumber) {
52+
Console.WriteLine("You won! Press any key to start again or Q to quit.");
53+
var key = Console.ReadKey().Key;
54+
if (key == ConsoleKey.Q) Environment.Exit(0);
55+
Start();
56+
}
57+
}
58+
59+
private static void AddMines() {
60+
var random = new Random();
61+
for (var i = 0; i < minesNumber; i++) {
62+
var x = random.Next(0, 10);
63+
var y = random.Next(0, 10);
64+
if (!board[x][y].hasMine) board[x][y].hasMine = true;
65+
else i--;
66+
}
67+
68+
AddInfoAboutMinesAround();
69+
}
70+
71+
private static void AddInfoAboutMinesAround() {
72+
for (var i = 0; i < board.Length; i++)
73+
for (var j = 0; j < board[i].Length; j++) {
74+
var bombAround = 0;
75+
if (i - 1 >= 0 && j - 1 >= 0 && board[i - 1][j - 1].hasMine) bombAround++;
76+
if (i - 1 >= 0 && board[i - 1][j].hasMine) bombAround++;
77+
if (i - 1 >= 0 && j + 1 <= board[i].Length - 1 && board[i - 1][j + 1].hasMine) bombAround++;
78+
if (j - 1 >= 0 && board[i][j - 1].hasMine) bombAround++;
79+
if (j + 1 <= board[i].Length - 1 && board[i][j + 1].hasMine) bombAround++;
80+
if (i + 1 <= board.Length - 1 && j - 1 >= 0 && board[i + 1][j - 1].hasMine) bombAround++;
81+
if (i + 1 <= board.Length - 1 && board[i + 1][j].hasMine) bombAround++;
82+
if (i + 1 <= board.Length - 1 && j + 1 <= board[i].Length - 1 && board[i + 1][j + 1].hasMine) bombAround++;
83+
board[i][j].mineAroundNumber = bombAround;
84+
}
85+
}
86+
87+
private static void HandleInput() {
88+
var key = Console.ReadKey().Key;
89+
switch (key) {
90+
case ConsoleKey.DownArrow:
91+
board[cursorX][cursorY].hasCursor = false;
92+
cursorX++;
93+
if (cursorX > boardSize - 1) cursorX = boardSize - 1;
94+
break;
95+
case ConsoleKey.UpArrow:
96+
board[cursorX][cursorY].hasCursor = false;
97+
cursorX--;
98+
if (cursorX < 0) cursorX = 0;
99+
break;
100+
case ConsoleKey.LeftArrow:
101+
board[cursorX][cursorY].hasCursor = false;
102+
cursorY--;
103+
if (cursorY < 0) cursorY = 0;
104+
break;
105+
case ConsoleKey.RightArrow:
106+
board[cursorX][cursorY].hasCursor = false;
107+
cursorY++;
108+
if (cursorY > boardSize - 1) cursorY = boardSize - 1;
109+
break;
110+
case ConsoleKey.F:
111+
board[cursorX][cursorY].isFlagged = !board[cursorX][cursorY].isFlagged;
112+
break;
113+
case ConsoleKey.Q:
114+
Environment.Exit(0);
115+
break;
116+
case ConsoleKey.Enter:
117+
if (board[cursorX][cursorY].isFlagged) {
118+
} else if (board[cursorX][cursorY].hasMine) {
119+
Console.WriteLine("You lost, press any key to play again or Q to quit");
120+
var consoleKey = Console.ReadKey().Key;
121+
if (consoleKey == ConsoleKey.Q) Environment.Exit(0);
122+
Start();
123+
} else {
124+
board[cursorX][cursorY].isReviled = true;
125+
if (board[cursorX][cursorY].mineAroundNumber == 0) CheckAroundEmptyField(cursorX, cursorY);
126+
}
127+
128+
break;
129+
}
130+
131+
board[cursorX][cursorY].hasCursor = true;
132+
}
133+
134+
private static void CheckAroundEmptyField(int x, int y) {
135+
if (x - 1 >= 0 && y - 1 >= 0 && !board[x - 1][y - 1].isReviled) {
136+
board[x - 1][y - 1].isReviled = true;
137+
if (board[x - 1][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y - 1);
138+
}
139+
140+
if (x - 1 >= 0 && !board[x - 1][y].isReviled) {
141+
board[x - 1][y].isReviled = true;
142+
if (board[x - 1][y].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y);
143+
}
144+
145+
if (x - 1 >= 0 && y + 1 <= board[x].Length - 1 && !board[x - 1][y + 1].isReviled) {
146+
board[x - 1][y + 1].isReviled = true;
147+
if (board[x - 1][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x - 1, y + 1);
148+
}
149+
150+
if (y - 1 >= 0 && !board[x][y - 1].isReviled) {
151+
board[x][y - 1].isReviled = true;
152+
if (board[x][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x, y - 1);
153+
}
154+
155+
if (y + 1 <= board[x].Length - 1 && !board[x][y + 1].isReviled) {
156+
board[x][y + 1].isReviled = true;
157+
if (board[x][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x, y + 1);
158+
}
159+
160+
if (x + 1 <= board.Length - 1 && y - 1 >= 0 && !board[x + 1][y - 1].isReviled) {
161+
board[x + 1][y - 1].isReviled = true;
162+
if (board[x + 1][y - 1].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y - 1);
163+
}
164+
165+
if (x + 1 <= board.Length - 1 && !board[x + 1][y].isReviled) {
166+
board[x + 1][y].isReviled = true;
167+
if (board[x + 1][y].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y);
168+
}
169+
170+
if (x + 1 <= board.Length - 1 && y + 1 <= board[x].Length - 1 && !board[x + 1][y + 1].isReviled) {
171+
board[x + 1][y + 1].isReviled = true;
172+
if (board[x + 1][y + 1].mineAroundNumber == 0) CheckAroundEmptyField(x + 1, y + 1);
173+
}
174+
}
175+
176+
private static void PrintBoard() {
177+
PrintTopBorder();
178+
179+
for (var i = 0; i < board.Length; i++) {
180+
Console.Write('║');
181+
for (var j = 0; j < board.Length; j++)
182+
if (board[i][j].hasCursor) {
183+
Console.Write(cursor);
184+
} else if (board[i][j].isFlagged) {
185+
Console.Write(flag);
186+
} else if (board[i][j].isReviled) {
187+
if (board[i][j].mineAroundNumber > 0) Console.Write(board[i][j].mineAroundNumber);
188+
else Console.Write(revealed);
189+
} else if (!board[i][j].isReviled) {
190+
Console.Write(notRevealed);
191+
} else {
192+
Console.Write('?');
193+
}
194+
195+
Console.Write("║ " + i + "\n");
196+
}
197+
198+
PrintLowBorder();
199+
200+
PrintXCords();
201+
202+
Console.Write("\n\n");
203+
Console.WriteLine("Arrows - Navigation, F - flag, Enter - check, Q - quit");
204+
}
205+
206+
private static void PrintXCords() {
207+
for (var i = 0; i < 10; i++) Console.Write(i);
208+
}
209+
210+
private static void PrintLowBorder() {
211+
Console.Write('╚');
212+
for (var i = 0; i < 10; i++) Console.Write('═');
213+
214+
Console.Write("╝\n ");
215+
}
216+
217+
private static void PrintTopBorder() {
218+
Console.Write('╔');
219+
for (var i = 0; i < boardSize; i++) Console.Write('═');
220+
221+
Console.Write("╗\n");
222+
}
223+
224+
private static void InitEmptyBoard(int size = 10) {
225+
for (var i = 0; i < boardSize; i++) {
226+
board[i] = new Field[10];
227+
for (var j = 0; j < boardSize; j++)
228+
board[i][j] = new Field {
229+
cordX = j,
230+
cordY = i
231+
};
232+
}
233+
234+
board[0][0].hasCursor = true;
235+
}
236+
}
237+
238+
internal class Field {
239+
public int cordX;
240+
public int cordY;
241+
public bool hasCursor;
242+
public bool hasMine;
243+
public bool isFlagged;
244+
public bool isReviled;
245+
public int mineAroundNumber;
246+
}
8.5 KB
Binary file not shown.

docs/images/20.png

6.09 KB
Loading

0 commit comments

Comments
 (0)