-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
407 lines (333 loc) · 12 KB
/
functions.h
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//For usleep();
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
// ----------------------------------------------------------------------
void displayInstructions();
void displayMenu();
void randomizeMines (int arr[][10], int numOfMines, int rows, int columns);
void displayBoard(int arr[][10], int row, int columns);
int toNum(char letter);
void flag(int arr[][10], int row, char column);
void flagPrompt(int boardArr[][10]);
int countOfBombs(int board[][10], int row, char column);
void open(int arr[][10], int row, char column, bool &explosion);
void openPrompt(int boardArr[][10], bool &gameEnd);
void displayBoom();
void clearBoard(int arr[][10], int row, int columns);
void check_7 (int board[][10], int row, char column, int &count);
void check_8 (int board[][10], int row, char column, int &count);
void check_9 (int board[][10], int row, char column, int &count);
void check_4 (int board[][10], int row, char column, int &count);
void check_6 (int board[][10], int row, char column, int &count);
void check_1 (int board[][10], int row, char column, int &count);
void check_2 (int board[][10], int row, char column, int &count);
void check_3 (int board[][10], int row, char column, int &count);
// ----------------------------------------------------------------------
void displayInstructions() {
cout << "\n\tWelcome to Minesweeper!\n";
cout << "===========================================================================================\n";
cout << "-The objective of the game is to clear the board without hitting any bombs.\n";
cout << "-There are 10 bombs on the 10x10 board.\n";
cout << "-You will dig up one tile at a time, if the tile is a bomb, you lose.\n";
cout << "-If the tile is not a bomb, the tile will be replaced with a number indicating how many bombs are in adjacent tiles. \n";
cout << "-If you are sure a certain tile is a bomb, you can 'flag' it and the tile will appear as 'P'.\n";
cout << "===========================================================================================\n\n";
}
void displayMenu() {
cout << "\n\tWelcome to Minesweeper!\n"
<< "------------------------------\n"
<< "\t1. Play game\n"
<< "\t2. Instructions\n"
<< "\t3. Quit the Program\n"
<< "------------------------------\n"
<< "\n\tEnter your choice (1-3): ";
}
void randomizeMines (int arr[][10], int numOfMines, int rows, int columns) {
int count = 0;
int rPos, cPos, randomPos;
srand(time(NULL)); // Randomize Seed
while (count < numOfMines) // Until all mines placed
{
randomPos = rand() % (rows * columns); // Random in rangeof BoardSize(100)
rPos = randomPos / rows; // RowPosition dividedBy 10
randomPos = rand() % (rows * columns); // Random in rangeof BoardSize(100)
cPos = randomPos / columns;
if (arr[rPos][cPos] != 1){ // If Not Mine
arr[rPos][cPos] = 1; // Make mine
count++; // Count + 1
}
}
}
void displayBoard(int arr[][10], int row, int columns) {
cout << "\n\t\t" << "a b c d e f g h i j" << endl << endl;
for (size_t i = 0; i < row; i++) { // Iterate Through Board
for (size_t j = 0; j < columns; j++){
if (j == 0) {
cout << "\t";
cout << setw(2) << right << i + 1; // Nums on Left
cout << "\t";
}
switch (arr[i][j]){ // Contents of Arr
case 0:
cout << "/ "; // Closed Tile Empty
break;
case 1:
cout << "/ "; // Closed Tile Bomb
break;
case 10:
cout << " "; // Bomb Count : 0
break;
case 11:
cout << "1 "; // Bomb Count : 1
break;
case 12:
cout << "2 "; // Bomb Count : 2
break;
case 13:
cout << "3 "; // Bomb Count : 3
break;
case 14:
cout << "4 "; // Bomb Count : 4
break;
case 15:
cout << "5 "; // Bomb Count : 5
break;
case 16:
cout << "6 "; // Bomb Count : 6
break;
case 17:
cout << "7 "; // Bomb Count : 7
break;
case 18:
cout << "8 "; // Bomb Count : 8
break;
case 20: // Flagged Tile
cout << "P ";
break;
case 21: // Flagged Bomb
cout << "P ";
break;
default: // Shown if Error
cout << ". ";
break;
}
if(j == columns - 1){ // End of Row
cout << endl;
}
}
}
cout << endl;
}
int toNum(char letter) {
int x = 0;
char letters[10] = {'a','b','c','d','e','f','g','h','i','j'};
for(size_t i = 0; i <= 9; i++) {
if(letter == letters[i]) {
x = i;
return x;
}
}
return {};
}
void flag(int arr[][10], int row, char column){
if(arr[row - 1][toNum(column)] == 1){
arr[row - 1][toNum(column)] += 20;
}
if(arr[row - 1][toNum(column)] == 0){
arr[row - 1][toNum(column)] += 20;
}
}
void flagPrompt(int boardArr[][10]){
string wantFlag;
bool useFlag;
cout << "\n Would you like to flag? (y/n) ";
cin >> wantFlag;
if(wantFlag == "y") useFlag = true;
else if(wantFlag == "n") useFlag = false;
else useFlag = false;
char flagColumn;
int flagRow;
if (useFlag) {
cout << "\n Which tile would you like to flag? (ex. g6) ";
cin >> flagColumn;
cin >> flagRow;
flag(boardArr, flagRow, flagColumn);
}
}
int countOfBombs(int board[][10], int row, char column){
int count = 0;
int unopenedBomb = 1;
int flaggedBomb = 21;
if(row == 0 && toNum(column) == 0){ // 7 WORKING
check_6(board, row, column, count);
check_2(board, row, column, count);
check_3(board, row, column, count);
}
if(row == 0 && toNum(column) > 0 && toNum(column) < 9){ // 8 WORKING
check_4(board, row, column, count);
check_6(board, row, column, count);
check_1(board, row, column, count);
check_2(board, row, column, count);
check_3(board, row, column, count);
}
if(row == 0 && toNum(column) == 9){ // 9 WORKING
check_4(board, row, column, count);
check_1(board, row, column, count);
check_2(board, row, column, count);
}
if(row > 0 && row < 9 && toNum(column) == 0){ // 4 WORKING
check_8(board, row, column, count);
check_9(board, row, column, count);
check_6(board, row, column, count);
check_1(board, row, column, count);
check_2(board, row, column, count);
}
if(row > 0 && row < 9 && toNum(column) > 0 && toNum(column) < 9){ // 5 WORKING
check_1(board, row, column, count);
check_2(board, row, column, count);
check_3(board, row, column, count);
check_4(board, row, column, count);
check_6(board, row, column, count);
check_7(board, row, column, count);
check_8(board, row, column, count);
check_9(board, row, column, count);
}
if(row > 0 && row < 9 && toNum(column) == 9){ // 6 WORKING
check_7(board, row, column, count);
check_8(board, row, column, count);
check_4(board, row, column, count);
check_1(board, row, column, count);
check_2(board, row, column, count);
}
if(row == 9 && toNum(column) == 0){ // 1 WORKING
check_8(board, row, column, count);
check_9(board, row, column, count);
check_6(board, row, column, count);
}
if(row == 9 && toNum(column) > 0 && toNum(column) < 9){ // 2 WORKING
check_4(board, row, column, count);
check_6(board, row, column, count);
check_7(board, row, column, count);
check_8(board, row, column, count);
check_9(board, row, column, count);
}
if(row == 9 && toNum(column) == 9){ // 3 WORKING
check_4(board, row, column, count);
check_7(board, row, column, count);
check_8(board, row, column, count);
}
return count;
}
void open(int arr[][10], int row, char column, bool &explosion){
if(arr[row -1][toNum(column)] == 1 || arr[row -1][toNum(column)] == 21){
explosion = true;
displayBoom(); //Game End
}
if(arr[row - 1][toNum(column)] == 0 || arr[row - 1][toNum(column)] == 20){ // If Unopened Tile
arr[row - 1][toNum(column)] = countOfBombs(arr, row - 1, column) + 10;
if(arr[row - 1][toNum(column)] == 0){
arr[row - 1][toNum(column)] = 3;
}
}
}
void initialOpenPrompt(int boardArr[][10], bool &gameEnd){
char openColumn;
int openRow;
cout << "\n Which tile would you like to open? (ex. c4) ";
cin >> openColumn;
cin >> openRow;
boardArr[openRow][toNum(openColumn)] = 0;
open(boardArr, openRow, openColumn, gameEnd);
}
void openPrompt(int boardArr[][10], bool &gameEnd){
char openColumn;
int openRow;
cout << "\n Which tile would you like to open? (ex. c4) ";
cin >> openColumn;
cin >> openRow;
open(boardArr, openRow, openColumn, gameEnd);
}
void displayBoom() {
cout << "\n\t";
for (char c: "B O O O O M ! ! !") {
cout << c << std::flush;
usleep(80000);
}
cout << endl;
cout << "\n\n\t\t\t";
cout << "- GAME OVER -";
cout << "\n";
}
void clearBoard(int arr[][10], int row, int columns) {
for (size_t i = 0; i < row; i++) {
for (size_t j = 0; j < columns; j++) {
arr[i][j] = 0; // Itterate and Fill with 0
}
}
}
// Check Functions Increase count
void check_6 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) + 1] == flaggedBomb || board[row][toNum(column) + 1] == unopenedBomb){
count += 1;
}
}
void check_4 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) - 1] == flaggedBomb || board[row][toNum(column) - 1] == unopenedBomb){
count += 1;
}
}
void check_7 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) - 11] == flaggedBomb || board[row][toNum(column) - 11] == unopenedBomb){
count += 1;
}
}
void check_8 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) - 10] == flaggedBomb || board[row][toNum(column) - 10] == unopenedBomb){
count += 1;
}
}
void check_9 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) - 9] == flaggedBomb || board[row][toNum(column) - 9] == unopenedBomb){
count += 1;
}
}
void check_1 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) + 9] == flaggedBomb || board[row][toNum(column) + 9] == unopenedBomb){
count += 1;
}
}
void check_2 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) + 10] == flaggedBomb || board[row][toNum(column) + 10] == unopenedBomb){
count += 1;
}
}
void check_3 (int board[][10], int row, char column, int &count) {
int unopenedBomb = 1;
int flaggedBomb = 21;
if(board[row][toNum(column) + 11] == flaggedBomb || board[row][toNum(column) + 11] == unopenedBomb){
count += 1;
}
}