-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathminesweeper.cpp
801 lines (763 loc) · 20 KB
/
minesweeper.cpp
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <string>
using namespace std;
int main ( );
void initialState ( char vis_table[], char real_table[], int n );
void printTable ( char vis_table[], int n );
void getneighbors ( int neighbors[], int pos, int n );
bool isRealNeighbor ( int index, int pos, int n );
void placeMines ( char real_table[], int n, int mines );
void calculateNumbers ( char real_table[], int n );
char countMines ( char real_table[], int neighbors[], int pos, int n );
void openSafe ( char real_table[], char vis_table[], int n, int pos, int &counter );
void play ( char real_table[], char vis_table[], int n, int mines );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main function for the MINESWEEPER program.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Local Parameters:
//
// Local, int MINES, the number of mines to use.
//
// Local, int N, the number of rows and columns in the grid.
//
// Local, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Local, char VIS_TABLE[N*N], contains a symbolic map of facts the
// user has uncovered:
// '-', a square which has not been explored;
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
{
int n = 8;
int mines = 10;
char real_table[n*n];
char vis_table[n*n];
//
// Initialize the random number generator.
//
srand ( time ( NULL) );
//
// Initialize both arrays with '-'.
//
initialState ( vis_table, real_table, n );
//
// In the real grid, replace N occurrences of '-' by '*'.
// This is where the mines are.
//
placeMines ( real_table, n, mines );
//
// Count the mines adjacent to each square.
//
calculateNumbers ( real_table, n );
//
// Start the game.
//
play ( real_table, vis_table, n, mines );
return 0;
}
//****************************************************************************80
void initialState ( char vis_table[], char real_table[], int n )
//****************************************************************************80
//
// Purpose:
//
// INITIALSTATE initializes all the elements of the two arrays to -'s
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Output, char VIS_TABLE[N*N], contains a symbolic map of facts the
// user has uncovered:
// '-', a square which has not been explored;
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Output, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
{
for ( int i = 0; i < n * n; i++ )
{
vis_table[i] = '-';
real_table[i] = '-';
}
return;
}
//****************************************************************************80
void printTable ( char vis_table[], int n )
//****************************************************************************80
//
// Purpose:
//
// PRINTTABLE prints the visible table.
//
// Discussion:
//
// This function is called after each move by the player, to show the
// current state of the game.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input, char VIS_TABLE[N*N], contains a symbolic map of facts the
// user has uncovered:
// '-', a square which has not been explored;
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
{
cout << "\n";
cout << " ";
for ( int i = 1; i <= n; i++ )
{
cout << " " << i;
}
cout << endl;
for ( int i = 0; i < n; i++ )
{
cout << setw(2) << i+1 << " ";
for ( int j = 0; j < n; j++ )
{
cout << vis_table[n*i + j] << " ";
}
cout << endl;
}
return;
}
//****************************************************************************80
void placeMines ( char real_table[], int n, int mines )
//****************************************************************************80
//
// Purpose:
//
// PLACEMINES places MINES mines randomly in the table.
//
// Discussion:
//
// On input, the N by N table contains only '-' values. On output,
// MINES entries of the table, chosen at random, now contain the value '*'.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input/output, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
// Input, int MINES, the number of mines to use.
//
{
int mine;
int num_mines = 0;
while ( num_mines < mines )
{
mine = rand ( ) % ( n * n );
if ( real_table[mine] != '*' )
{
real_table[mine] = '*';
num_mines++;
}
}
return;
}
//****************************************************************************80
void calculateNumbers ( char real_table[], int n )
//****************************************************************************80
//
// Purpose:
//
// CALCULATENUMBERS calculates the number of mines adjacent to each square.
//
// Discussion:
//
// This function is used to update the REAL_TABLE array once the position
// of the mines has been randomly assigned.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input/output, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
{
int counter;
int neighbors[8];
//
// For each square that is not mined, look at the eight neighbors,
// count how many of them are mines, and store that value in the map.
//
for ( int i = 0; i < n*n; i++ )
{
if ( real_table[i] != '*' )
{
getneighbors ( neighbors, i, n );
real_table[i] = countMines ( real_table, neighbors, i, n );
}
}
return;
}
//****************************************************************************80
void getneighbors ( int neighbors[], int pos, int n )
//****************************************************************************80
//
// Purpose:
//
// GETNEIGHBORS returns the indices of the 8 neighbors of a square.
//
// Discussion:
//
// This function stores the position in the real_table of the 8 possible
// neighbors for each square (at position pos) I am using a one dimmensional
// so I have to take the number of squares per line (which is n) into account.
//
// Here is a diagram of the indices of the neighbors of the square at POS:
//
// ... |... |... |... | ...
// -+-------+-------+-------+-
// ... |POS-N-1|POS-N |POS-N+1| ...
// -+-------+-------+-------+-
// ... |POS-1 |POS |POS+1 | ...
// -+-------+-------+-------+-
// ... |POS+N-1|POS+N |POS+N+1| ...
// -+-------+-------+-------+-
// ... |... |... |... | ...
//
// If POS is near the top, bottom, left or right of the grid, then some of
// these neighbors don't actually exist. But we leave that problem to the
// ISREALNEIGHBOR function.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Output, int NEIGHBORS[8], the indices of the neighbors in the
// northwest, north, northeast, west, east, southwest, south and southeast.
//
// Input, int POS, the index of the square being considered.
//
// Input, int N, the number of rows and columns in the grid.
//
{
neighbors[0] = ( pos - n ) - 1;
neighbors[1] = pos - n ;
neighbors[2] = ( pos - n ) + 1;
neighbors[3] = pos - 1;
neighbors[4] = pos + 1;
neighbors[5] = ( pos + n ) - 1;
neighbors[6] = ( pos + n );
neighbors[7] = ( pos + n ) + 1;
return;
}
//****************************************************************************80
bool isRealNeighbor ( int index, int pos, int n )
//****************************************************************************80
//
// Purpose:
//
// ISREALNEIGHBOR checks which neighbors are "real neighbors".
//
// Discussion:
//
// On an N by N grid, the square at location POS may have up to eight
// neighbors, in the northwest, north, northeast, west, east, southwest,
// south and southeast directions.
//
// However, some of these neighbors will not actually exist if
// * POS is in the first row of the grid;
// * POS is in the last row of the grid;
// * POS is in the first column of the grid;
// * POS is in the last column of the grid.
//
// This function checks whether a neighbor in the INDEX direction
// does not exist because of the placement of POS.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input, int INDEX, indicates the neighbor to be checked.
//
// Input, int POS, the index of the square.
//
// Input, int N, the number of rows and columns in the grid.
//
{
//
// Northwest, north, northeast neighbors don't exist
// if POS < N.
//
if ( pos < n )
{
if ( index == 0 || index == 1 || index == 2 )
{
return false;
}
}
//
// Southwest, south, and southeast neighbors don't exist
// if N * ( N - 1 ) <= POS.
//
if ( n * ( n - 1 ) <= pos )
{
if ( index == 5 || index == 6 || index == 7 )
{
return false;
}
}
//
// Northwest, west, and southwest neighbors don't exist
// if POS is divisible by N.
//
if ( pos % n == 0 )
{
if ( index == 0 || index == 3 || index == 5 )
{
return false;
}
}
//
// Northeast, east, and southeast neighbors don't exist
// if POS + 1 is divisible by N.
//
if ( ( pos + 1 ) % n == 0 )
{
if ( index == 2 || index == 4 || index == 7 )
{
return false;
}
}
//
// Apparently, the neighbor in the INDEX direction is legal.
//
return true;
}
//****************************************************************************80
char countMines ( char real_table[], int neighbors[], int pos, int n )
//****************************************************************************80
//
// Purpose:
//
// COUNTMINES computes the number of mines that are neighbors to a square.
//
// Discussion:
//
// This function takes a square at position POS and its 8 neighbors and
// counts how many of the neighbors are mines.
//
// It returns the character ' ' if there are no neighboring mines,
// otherwise a digit between 1 to 8 corresponding to the number of mines.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input/output, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int NEIGHBORS[8], the indices of the neighbors in the
// northwest, north, northeast, west, east, southwest, south and southeast.
//
// Input, int POS, the index of the square being examined.
//
// Input, int N, the number of rows and columns in the grid.
//
{
int mines = 0;
char value;
//
// Count the neighbor mines.
//
for ( int i = 0; i < 8; i++ )
{
if ( isRealNeighbor( i, pos, n ) )
{
if ( real_table[neighbors[i]] == '*' )
{
mines++;
}
}
}
//
// Form the character that symbolizes the neighbor mine count.
//
if ( mines == 0 )
{
value = ' ';
}
else
{
value = ( char ) ( ( ( int ) '0' ) + mines );
}
return value;
}
//****************************************************************************80
void play ( char real_table[], char vis_table[], int n, int mines )
//****************************************************************************80
//
// Purpose:
//
// PLAY controls the game and allows the player to make moves.
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input/output, char VIS_TABLE[N*N], contains a symbolic map of facts the
// user has uncovered:
// '-', a square which has not been explored;
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
// Input, int MINES, the number of mines to use.
//
// Local parameters:
//
// Local, int COL, the column selected by the user. This is a 1-based index.
//
// Local, int COUNTER, counts the number of safe squares which the player
// has cleared so far. The game is over if the player can clear all the
// safe squares.
//
// Local, char CURR_SYMBOL, the symbol of the square which is currently
// being cleared. It could be blank, or a number or a '*'.
//
// Local, int POS, the position in the 1D array corresponding to the
// user's chosen ROW and COL.
//
// Local, int ROW, the row selected by the user. This is a 1-based index.
//
// Local, string S, used to read and discard illegal input.
//
// Local, int SAFE, the total number of safe squares.
{
int col;
int counter = 0;
char curr_symbol;
int pos;
int row;
string s;
int safe = n*n - mines;
//
// For each iteration of this loop, the player chooses a square, and the program
// updates the game correspondingly.
//
do
{
//
// Display the table of data the user has uncovered.
//
printTable ( vis_table, n );
//
// Ask the player to specify the next square to clear.
//
cout << "Enter row and column: " << endl;
cin >> row
>> col;
//
// Deal with ^D or end of file.
//
if ( cin.eof ( ) )
{
cerr << "\n";
cerr << "PLAY:\n";
cerr << " Input terminated with end-of-file.\n";
cerr << " Game terminated.\n";
exit ( 1 );
}
//
// Deal with illegal input, such as "1,2" or "Quit".
// You have to clear the error, then read the line as a string so it's gone.
//
if ( cin.fail ( ) )
{
cin.clear ( );
cin >> s;
if ( s[0] == 'Q' )
{
cout << "\n";
cout << "QUIT the game.\n";
cout << "\n";
cout << "Here is the mine map:\n";
cout << "\n";
printTable ( real_table, n );
return;
}
if ( s[0] == 'M' )
{
cout << "\n";
cout << "Enter row and column for mine flag: " << endl;
cin >> row >> col;
pos = n * ( row - 1 ) + ( col - 1 );
if ( vis_table[pos] == '-' )
{
vis_table[pos] = '?';
}
cout << "\n";
printTable ( vis_table, n );
continue;
}
if ( s[0] == 'U' )
{
cout << "\n";
cout << "Enter row and column for mine flag: " << endl;
cin >> row >> col;
pos = n * ( row - 1 ) + ( col - 1 );
if ( vis_table[pos] == '?' )
{
vis_table[pos] = '-';
}
cout << "\n";
printTable ( vis_table, n );
continue;
}
cerr << "\n";
cerr << "PLAY - Warning!\n";
cerr << " That input was illegal.\n";
cerr << " Please enter a row number and a column number.\n";
continue;
}
if ( row < 1 || n < row || col < 1 || n < col )
{
cerr << "\n";
cerr << "Your row or column index was illegal.\n";
cerr << "Legal values are between 1 and " << n << ".\n";
continue;
}
//
// Calculate the position of the square in the 1D array.
//
pos = n * ( row - 1 ) + ( col - 1 );
//
// If this square was not cleared already...
//
if ( vis_table[pos] == '-' )
{
//
// ...then update this position in VIS_TABLE to equal the character which
// REAL_TABLE holds.
//
curr_symbol = real_table[pos];
vis_table[pos] = curr_symbol;
//
// If the square which the player cleared has no mines around it
// call a function which will automatically clear all its neighbors.
// The function also increments the counter for the safe square which were open.
//
if ( curr_symbol == ' ' )
{
openSafe ( real_table, vis_table, n, pos, counter );
}
//
// If the square was not a mine, we cleared another square,
// so increment the counter.
//
if ( curr_symbol != '*' )
{
counter++;
}
}
} while ( counter < safe && curr_symbol != '*' );
//
// If the number of squares cleared by the player is the same as
// the number of safe squares, then the player won the game.
//
if ( counter == safe )
{
printTable ( real_table, n );
cout << "\n";
cout << "You Win!" << endl;
}
//
// The player loses by choosing a mine.
// The position of all mines is shown.
//
if ( curr_symbol == '*' )
{
for ( int i = 0; i < n * n; i++ )
{
if ( real_table[i] == '*' )
{
vis_table[i] = '*';
}
}
printTable ( vis_table, n );
cout << "\n";
cout << "You Lose!" << endl;
}
return;
}
//****************************************************************************80
void openSafe ( char real_table[], char vis_table[], int n, int pos,
int &counter )
//****************************************************************************80
//
// Purpose:
//
// OPENSAFE is called whenever a square with no neighboring mines is cleared.
//
// Discussion:
//
// If there are no mines around the square at position POS then it is safe
// to clear all the squares around it. It is possible that some of the
// neighbors also don't have mines around them so their neighbors have to
// get cleared as well. This is why this function uses recursion (i.e it
// calls itself when a neighbors has 0 mines around it).
//
// Modified:
//
// 24 July 2011
//
// Author:
//
// Detelina Stoyanova
//
// Parameters:
//
// Input, char REAL_TABLE[N*N], contains a symbolic map with
// '*' for mines,
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Local, char VIS_TABLE[N*N], contains a symbolic map of facts the
// user has uncovered:
// '-', a square which has not been explored;
// ' ' for unmined squares with no mine neighbors,
// a digit between 1 and 8 for unmined squares with some mine neighbors.
//
// Input, int N, the number of rows and columns in the grid.
//
// Input, int POS, the index of the square being considered.
//
{
int neighbors[8];
//
// Index the 8 neighbors of this square.
//
getneighbors ( neighbors, pos, n );
//
// Consider each neighbor square.
//
for ( int i = 0; i < 8; i++ )
{
//
// If there really is a neighbor square in that direction, ...
//
if ( isRealNeighbor ( i, pos, n ) )
{
//
// ... and the neighbor square is not a mine, and has not been explored yet ...
//
if ( vis_table[neighbors[i]] == '-' )
{
//
// ...THEN make the neighbor square visible and add one more to the
// count of squares cleared.
//
vis_table[neighbors[i]] = real_table[neighbors[i]];
counter++;
//
// If the neighbor square has no mine neighbors, then we have to
// check the neighbors of this neighbor, now, too!
//
if ( vis_table[neighbors[i]] == ' ' )
{
openSafe ( real_table, vis_table, n, neighbors[i], counter );
}
}
}
}
return;
}