-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
362 lines (255 loc) · 11.2 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _2DArrayProject
{
class Program
{
static void Main(string[] args)
{
// * = Mine
// | = Empty
// ? = Unknown (Player Only)
// Initialise/Declare variables needed
Random rnd = new Random();
int mines = 0;
int winConField = 0;
bool gameOver = false;
int winConUser;
int rowInput;
int columnInput;
string[,] mineField = new string[6, 6];
string[,] userField = new string[6, 6];
// Number of mines tends towards 8 or higher. Keeps random but in a general area.
while (mines < 8)
{
for (int row = 0; row < 6; row++)
{
// Randomly place mines along the map
for (int column = 0; column < 6; column++)
{
userField[row, column] = " ? ";
// Fifth chance of a mine, ensures they are spread out
if (rnd.Next(1, 6) == 5 && mineField[row, column] != " * ")
{
mineField[row, column] = " * ";
mines++;
}
// Indicates it as empty
else
{
mineField[row, column] = " | ";
}
// Print the real, hidden minefield. Used for testing.
Console.Write(mineField[row, column]);
}
Console.WriteLine("\n");
}
}
// Iterate through minefield to count number of mines
for (int row = 0; row < 6; row++)
{
// Separate to previous iteration to avoid runtime error
for (int column = 0; column < 6; column++)
{
if (mineField[row, column] == " * ")
{
// Count is used later for win condition
winConField++;
}
}
}
// Welcoming Message for First Play
Console.WriteLine("Welcome to Minesweeper!");
// Print the empty minefield
for (int row = 0; row < 6; row++)
{
for (int column = 0; column < 6; column++)
{
Console.Write(userField[row, column]);
}
Console.WriteLine("\n");
}
// Loop until win/loss condition is met
while (!gameOver)
{
// Reset mineCount
int mineCount = 0;
// Gain user input once game has started
Console.WriteLine("Please input a vertical number (0-5):");
rowInput = Int32.Parse(Console.ReadLine());
Console.WriteLine("Please input a horizontal number (0-5):");
columnInput = Int32.Parse(Console.ReadLine());
// Loss condition activates and game is over
if (mineField[rowInput, columnInput] == " * ")
{
Console.WriteLine("You just hit a mine! You lose!");
gameOver = true;
}
// Must have hit an empty space
else
{
// If the value is the top line, but not the corners
if (rowInput == 0 && columnInput != 0 && columnInput != 5)
{
// Iterate specific row/column numbers based on unique position
for (int row = 0; row < 2; row++)
{
for (int column = columnInput - 1; column <= columnInput + 1; column++)
{
// Count all mines within a square of the chosen space
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the bottom line, but not the corners
else if (rowInput == 5 && columnInput != 0 && columnInput != 5)
{
for (int row = 4; row < 6; row++)
{
for (int column = columnInput - 1; column <= columnInput + 1; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the left line, but not the corners
else if (columnInput == 0 && rowInput != 0 && rowInput != 5)
{
for (int row = rowInput - 1; row <= rowInput + 1; row++)
{
for (int column = 0; column < 2; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the right line, but not the corners
else if (columnInput == 5 && rowInput != 0 && rowInput != 5)
{
for (int row = rowInput - 1; row <= rowInput + 1; row++)
{
for (int column = 4; column < 6; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the top left corner
else if (rowInput == 0 && columnInput == 0)
{
for (int row = 0; row < 2; row++)
{
for (int column = 0; column < 2; column++)
{
if (mineField[row, column] == " * ")
{
mineCount ++;
}
}
}
}
// If the value is the top right corner
else if (rowInput == 0 && columnInput == 5)
{
for (int row = 0; row < 2; row++)
{
for (int column = 4; column < 6; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the bottom left corner
else if (rowInput == 5 && columnInput == 0)
{
for (int row = 4; row < 6; row++)
{
for (int column = 0; column < 2; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is the bottom right corner
else if (rowInput == 5 && columnInput == 5)
{
for (int row = 4; row < 6; row++)
{
for (int column = 4; column < 6; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// If the value is anywhere that isn't next to an edge
else
{
for (int row = rowInput - 1; row <= rowInput + 1; row++)
{
for (int column = columnInput - 1; column <= columnInput + 1; column++)
{
if (mineField[row, column] == " * ")
{
mineCount++;
}
}
}
}
// Change what the user sees to compensate
// The number in the box states how many mines are within the square around it
userField[rowInput, columnInput] = " " + mineCount.ToString() + " ";
Console.Clear();
// Reset win conditions to 0 for each attempt
winConUser = 0;
// Iterate to check win condition and write out the table the user should see.
for (int row = 0; row < 6; row++)
{
for (int column = 0; column < 6; column++)
{
// Print the minefield the user has
Console.Write(userField[row, column]);
// Count unchecked spaces
if (userField[row, column] == " ? ")
{
winConUser++;
}
}
Console.WriteLine("\n");
}
// Checks if player has checked every box and has not hit a mine
if (winConUser == winConField)
{
// Win condition, game ends and user wins
Console.WriteLine("Congratulations! You get every square that wasn't a mine! You win!");
gameOver = true;
}
}
}
Console.ReadKey();
}
}
}