Minesweeper game project.

Hello,

I have just joined this community, wanting to be better in programming using c++. I'm so amazed by this site, as it helped me before..even expecting more help now!! :)


Fine, here's what I got..the following code I searched on the internet, about minesweeper..can't really recall whether I got from here or else where. I want to fully understand the logic. Win and Lose situations.
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


#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
 
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() {
    cout << "Welcome to MineSweeper!" << endl;

    init();
    bool playing = true;
    while(playing) {
        print();
        player();
        if(isWin()) {
            cout << "You Win!" <<endl;
            playing = false;
        } else if(isLose()) {
            cout << "You Lose!" << endl;
            playing = false;
        }
 
        if(!playing) {
            playing = playNewGame();
            if(playing) {
                init();
            }
        }
    }
    cout << "Exiting Minesweeper!" <<  endl;
    return 0;
}
 
void init() {
    cout << "New Game!" << 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;
        cout << "Play another game? Y or N" << endl;
        cin >> choice;
        if(choice == 'N' || choice == 'n') {
            return false;
            selected = true;
        } else if(choice == 'Y' || choice == 'y') {
            return true;
            selected = true;
        } else {
            cout << "That is not a valid choice!" << 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;
}



Later on, I found that using a mouse interface was a must. Obviously in that code..I use co-ordinates in order to reveal the unknown wanted points..which is abit lame. So I must put mouse to open the the wanted points.

If someone could guide me or give me a head start to add a mouse..I'll be greatful.

Lastly, I've searched for a stopwatch function to put into the code. You see, I should tell the user how many SECS he played, or even MINS but not nano secs, as I assume that's the measurement for the compiler.

Thanks in advance..
Last edited on
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
 
void player() {
    cout << "Commands: R => Show, F =>Flag, C =>Cheat, N => NewGame" << endl;
    bool selected = false;
    while(!selected) {
        char choice;
        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 {
            cout << "Invalid Choice!" << endl;
        }
    }
 
}
 
void uncover() {
    bool selected = false;
    while(!selected) {
        int x;
        int y;
        cout << "Input X (1 - " << (DIMX) << ")" << endl;
        cin >> x;
        cout << "Input Y (1 - " << (DIMY) << ")" << endl;
        cin >> y;
        x--;
        y--;
        if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
            reveal(x, y); // call recursive reveal algorithm
            selected = true;
        } else {
            cout << "(X,Y) Values out of range!" << endl;
        }
    }
}
 
void setFlag() {
    bool selected = false;
    while(!selected) {
        int x;
        int y;
        cout << "Input X (1 - " << (DIMX) << ")" << endl;
        cin >> x;
        cout << "Input Y (1 - " << (DIMY) << ")" << endl;
        cin >> y;
        x--;
        y--;
        if(x >= 0 && x < DIMX && y >= 0 && y < DIMY) {
            display[x][y] = FLAG;
            selected = true;
        } else {
            cout << "(X,Y) Values out of range!" << endl;
        }
    }
}
 
void cheat() {
    cout << "Only this once!!" << endl;
    cout << "   ";
    for(int x = 0; x < DIMX; x++) {
        cout << (x + 1) << " ";
    }
    cout << endl << "";
    for(int x = 0; x < DIMX; x++) {
        cout << "";
    }
    cout << endl;
    for(int y = 0; y < DIMY; y++) {
        cout << (y + 1) << "  ";
        for(int x = 0; x < DIMX; x++) {
            if(state[x][y] == MINE) {
                cout << "@ ";
            } else {
                if(state[x][y] == 0) {
                     cout << "  ";
                } else {
                     cout << state[x][y] << " ";
                }
            }
        }
        cout <<endl;
    }
}
 
void print() {
    cout << "   ";
    for(int x = 0; x < DIMX; x++) {
        cout << (x + 1) << " ";
    }
    cout << endl << "";
    for(int x = 0; x < DIMX; x++) {
        cout << "";
    }
    cout << endl;
    for(int y = 0; y < DIMY; y++) {
        cout << (y + 1) << "  ";
        for(int x = 0; x < DIMX; x++) {
            if(display[x][y] == COVERED || display[x][y] == 'F') { // is it tile still uncovered
                cout << display[x][y] << " ";
            } else { // display the state
                if(state[x][y] == MINE) {
                    cout << "@ ";
                } else {
                    if(state[x][y] == 0) {
                        cout << "  ";
                    } else {
                        cout << state[x][y] << " ";
                    }
                }
            }
        }
 
        cout <<endl;
    }
}


that's the rest...
Topic archived. No new replies allowed.