C++ - Minesweeper

Posted on by Kenny Cason
tags = [ c++, game, minesweeper ]

The other day I was tutoring a freshman programmer program his first minesweeper text game in C++. Here is the resulting code.

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
#include <iostream>
#include<cstdlib>
#include<ctime>

const int DIMX = 6;
const int DIMY = 6;
const int MINES = 6;
const int MINE = -1;
const char COVERED = 'X';
const char UNCOVERED = ' ';
const char FLAG = 'F';
int state[DIMX][DIMY];
char display[DIMX][DIMY];

void init(); // initialize game states
int countSorroundingMines(int x, int y); // helper function used by init
void reveal(int x, int y);
void player(); // handle player input
void setFlag();
void uncover();
void print(); // print the minefield
void cheat(); // print out the mines
bool isWin();
bool isLose();
bool playNewGame();

int main() {
    std::cout << "Welcome to MineSweeper!" << std::endl;
    init();
    bool playing = true;
    while(playing) {
        print();
        player();
        if(isWin()) {
            std::cout << "You Win!" << std::endl;
            playing = false;
        } else if(isLose()) {
            std::cout << "You Lose!" << std::endl;
            playing = false;
        }

        if(!playing) {
            playing = playNewGame();
            if(playing) {
                init();
            }
        }
    }
    std::cout << "Exiting Minesweeper!" << std::endl;
    return 0;
}

void init() {
    std::cout << "New Game!" << std::endl;
    // set display to "uncovered"
    for(int y = 0; y < DIMY; y++) {
        for(int x = 0; x < DIMX; x++) {
            display[x][y] = COVERED;
        }
    }
    // initialize mines
    srand(time(0));
    for(int i = 0; i < MINES; i++) {
        bool placed = false;
        while(!placed) {
            int x = rand() % DIMX;
            int y = rand() % DIMY;
            if(state[x][y] != MINE) { // check mine not set
                placed = true;
                state[x][y] = MINE;
            }
        }
    }
    // place hint numbers
    for(int y = 0; y < DIMY; y++) {
        for(int x = 0; x < DIMX; x++) {
            state[x][y] = countSorroundingMines(x, y);
        }
    }
}

bool playNewGame() {
    bool selected = false;
    while(!selected) {
        char choice;
        std::cout << "Play another game? Y or N" << std::endl;
        std::cin >> choice;
        if(choice == 'N' || choice == 'n') {
            return false;
            selected = true;
        } else if(choice == 'Y' || choice == 'y') {
            return true;
            selected = true;
        } else {
            std::cout << "That is not a valid choice!" << std::endl;
        }
    }
}

void reveal(int x, int y) {
    if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) { // check that x and y are valid
        if(display[x][y] == UNCOVERED) {
            return;
        }
        display[x][y] = UNCOVERED;

        if(state[x][y] == MINE || state[x][y] != 0) {
            return;
        }
        reveal(x - 1, y - 1);
        reveal(x, y - 1);
        reveal(x + 1, y - 1);
        reveal(x - 1, y);
        reveal(x + 1, y);
        reveal(x - 1, y + 1);
        reveal(x, y + 1);
        reveal(x + 1, y + 1);
    } else {
        return;
    }
}

/*
[x-1, y-1][x, y-1][x+1, y-1]
[x-1, y  ][x, y  ][x+1, y  ]
[x-1, y+1][x, y+1][x+1, y+1]
*/
int countSorroundingMines(int x, int y) {
    if(state[x][y] == MINE) {
        return MINE;
    }
    int num = 0;
    if(x > 0 && y > 0) { // top left
        if(state[x - 1][y - 1] == MINE) {
            num++;
        }
    }
    if(y > 0) { // top
        if(state[x][y - 1] == MINE) {
            num++;
        }
    }
    if(x < DIMX - 1  && y > 0) { // top  right
        if(state[x + 1][y - 1] == MINE) {
            num++;
        }
    }
    if(x > 0) { // left
        if(state[x - 1][y] == MINE) {
            num++;
        }
    }
    if(x < DIMX - 1) { // right
        if(state[x + 1][y] == MINE) {
            num++;
        }
    }
    if(x > 0 && y < DIMY - 1) { // bottom left
        if(state[x - 1][y + 1] == MINE) {
            num++;
        }
    }
    if(y < DIMY - 1) { // bottom
        if(state[x][y + 1] == MINE) {
            num++;
        }
    }
    if(x < DIMX - 1 && y < DIMY - 1) { // bottom right
        if(state[x + 1][y + 1] == MINE) {
            num++;
        }
    }
    return num;
}

bool isWin() {
    bool win = true;
    for(int y = 0; y < DIMY; y++) {
        for(int x = 0; x < DIMX; x++) {
            win &= ((display[x][y] == UNCOVERED && state[x][y] != MINE) ||
                    ((display[x][y] == COVERED || display[x][y] == FLAG) && state[x][y] == MINE));
        }
    }
    return win;
}

bool isLose() {
    for(int y = 0; y < DIMY; y++) {
        for(int x = 0; x < DIMX; x++) {
            if (display[x][y] == UNCOVERED && state[x][y] == MINE) {
                return true;
            }
        }
    }
    return false;
}

void player() {
    std::cout << "Commands: R => reveal square, F => set flag, C => cheat, N => New Game" << std::endl;
    bool selected = false;
    while(!selected) {
        char choice;
        std::cin >> choice;
        if(choice == 'R' || choice == 'r') {
            uncover();
            selected = true;
        } else if(choice == 'F' || choice == 'f') {
            setFlag();
            selected = true;
        } else if(choice == 'C' || choice == 'c') {
            cheat();
        } else if(choice == 'N' || choice == 'n') {
            init();
            selected = true;
        } else {
            std::cout << "Invalid Choice!" << std::endl;
        }
    }

}

void uncover() {
    bool selected = false;
    while(!selected) {
        int x;
        int y;
        std::cout << "Input X (1 - " << (DIMX) << ")" << std::endl;
        std::cin >> x;
        std::cout << "Input Y (1 - " << (DIMY) << ")" << std::endl;
        std::cin >> y;
        x--;
        y--;
        if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
            reveal(x, y); // call recursive reveal algorithm
            selected = true;
        } else {
            std::cout << "(X,Y) Values out of range!" << std::endl;
        }
    }
}

void setFlag() {
    bool selected = false;
    while(!selected) {
        int x;
        int y;
        std::cout << "Input X (1 - " << (DIMX) << ")" << std::endl;
        std::cin >> x;
        std::cout << "Input Y (1 - " << (DIMY) << ")" << std::endl;
        std::cin >> y;
        x--;
        y--;
        if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
            display[x][y] = FLAG;
            selected = true;
        } else {
            std::cout << "(X,Y) Values out of range!" << std::endl;
        }
    }
}

void cheat() {
    std::cout << "Cheating is bad!" << std::endl;
    std::cout << "   ";
    for(int x = 0; x < DIMX; x++) {
        std::cout << (x + 1) << " ";
    }
    std::cout << std::endl << "";
    for(int x = 0; x < DIMX; x++) {
        std::cout << "";
    }
    std::cout << std::endl;
    for(int y = 0; y < DIMY; y++) {
        std::cout << (y + 1) << "  ";
        for(int x = 0; x < DIMX; x++) {
            if(state[x][y] == MINE) {
                std::cout << "@ ";
            } else {
                if(state[x][y] == 0) {
                    std:: cout << "  ";
                } else {
                    std:: cout << state[x][y] << " ";
                }
            }
        }
        std::cout << std::endl;
    }
}

void print() {
    std::cout << "   ";
    for(int x = 0; x < DIMX; x++) {
        std::cout << (x + 1) << " ";
    }
    std::cout << std::endl << "";
    for(int x = 0; x < DIMX; x++) {
        std::cout << "";
    }
    std::cout << std::endl;
    for(int y = 0; y < DIMY; y++) {
        std::cout << (y + 1) << "  ";
        for(int x = 0; x < DIMX; x++) {
            if(display[x][y] == COVERED || display[x][y] == 'F') { // is it tile still uncovered
                std::cout << display[x][y] << " ";
            } else { // display the state
                if(state[x][y] == MINE) {
                    std::cout << "@ ";
                } else {
                    if(state[x][y] == 0) {
                        std:: cout << "  ";
                    } else {
                        std:: cout << state[x][y] << " ";
                    }
                }
            }
        }

        std::cout << std::endl;
    }
}

Sample Output:

comments powered by Disqus