Minesweeper Game (text based, no graphics) - help

Im trying to make a minesweeper game. It will not have any graphics, but be entirely text-based.

Right now, Im just trying to form the grid.

Here is what I did so far:

The program will output a grid (ex: 5x5). Since Im just working on getting the grid to work for now, I want everything (mines and numbers) to be visible.

The mines are represented by -1.

I made a 2 dimensional array and looped through it using a nested loop. I used a random number generator to set some elements to -1. All other numbers I set to -2.

I then made a second nested loop that checks the surrounding squares for each element. The number of -1's adjacent to the square, that is the number assigned to that particuar square (just like a normal minesweeper game).

Problem is, sometimes it works and sometimes it doesn't. Sometimes the number in a square doesn't match the number of mines adjacent to it. (Keep in mind that corner-mines are considered adjacent). I noticed this error happens for only 1 or 2 square and always in the first two columns (from the left) in the grid. Can someone look through the code and let me know what Im doing wrong?


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

using namespace std;

int main()
{

    srand(time(0));

    int ROWS = 5;
    int COLUMNS = 5;

    int grid[ROWS][COLUMNS];
    int mines;
    
    // Assigned -1's randomly to some squares
// This will print out a grid with -1's and -2's
// -1's are mines and -2's are non-mines. 
// In the next print, the -2's should be changed to correct positive numbers. 
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            int randN = (rand()%10)+1;
            if(randN==1 || randN==2 || randN==3)
            {
                grid[i][j] = -1; // mines are -1's
            }
            else grid[i][j] = -2; // non-mines are represented by -2's

            cout << setw(4) << grid[i][j] << setw(4);
        }
        cout << endl;
    }

    // this second nested loop checks surrounding squares for each elements. 
   // If it gets to a square that itself is a mine, it skips it (using continue statement)
    // However many mines are adjacent to the square, that is the number assigned 
   //to that square. 

  // By the end of this loop, a grid should be printed out with -1's
 // and the correct positive numbers. 
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            mines = 0; // mines set to 0
            if(grid[i][j] == -1 || grid [i][j] >= 0)
                continue;
            else if(grid[i][j] == -2)
            {
                // checks squares surrounding top left corner for mines
                if(i == 0 && j == 0)
                {
                   if(grid[i][j+1] == -1)
                        mines++;
                   if(grid[i+1][j] == -1)
                        mines++;
                   if(grid[i+1][j+1] == -1)
                        mines++;
                }
    
                // top right corner
                if(i == 0 && j == COLUMNS-1)
                {
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                }

                // bottom left corner
                if(i == ROWS-1 && j == 0)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                }
                
                // bottom right corner
                if(i == ROWS-1 && j == COLUMNS-1)
                {
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                }
                
                // top edge
                if( i == 0 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i+1][j+1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                }
                
                // bottom edge
                if(i == ROWS-1 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                }

                // left edge
                if(j == 0 && i != 0 && i != ROWS-1)
                {
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i][j+1] == 1)
                        mines++;
                    if(grid[i+1][j+1] == 1)
                        mines++;
                    if(grid[i-1][j+1] == 1)
                        mines++;
                }

                // right edge
                if(j == COLUMNS-1 && i != 0 && i != ROWS-1)
                {
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                }

                
                // all other possible squares to be checked
                if(i !=0 && i!= ROWS-1 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i+1][j+1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;

                }

                grid[i][j] = mines; // assign number of mines adjecent to that square

            }
        }
        cout << endl;
    }
        
            
    // print out grid
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            cout << setw(3) << grid[i][j] << setw(3);
        }

        cout << endl;

    }
    return 0;
}
Last edited on
No takers?

Last edited on
Nevermind, figured it out.


The problem was lines 136-141
1
2
3
4
5
6
                    if(grid[i][j+1] == 1)
                        mines++;
                    if(grid[i+1][j+1] == 1)
                        mines++;
                    if(grid[i-1][j+1] == 1)
                        mines++;


I was checking for positive 1's instead of -1's.

-------

I may come back to this thread for help If I get stuck somewhere.
Last edited on
I've improved by code a bit but I need some help.

Here's the thing: Whatever coordinates the user enter, that is the square which is 'uncovered' along with all values equal to it. Thats fine.

The problem is, this change to the grid does not get carried over when the user enters in a second coordinate.

Run my code and you will see what Im talking about.

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

using namespace std;

// Prototypes
void Print_Grid (vector<vector<int> >&, vector<vector<string> >&, int, int);
void Play_Game(vector<vector<int> > &, vector<vector<string> > &, int, int);

int main()
{
    cout << "Enter # of rows and columns: ";
    int gridSize;
    cin >> gridSize;
    int ROWS = gridSize;
    int COLUMNS = gridSize;

    // int grid
    vector<vector<int> > grid(ROWS, vector<int>(COLUMNS, -2));

    // cover grid
    vector<vector<string> > cgrid(ROWS, vector<string>(COLUMNS, "*"));

    Print_Grid(grid, cgrid, ROWS, COLUMNS);
    cout << "\n";

    Play_Game(grid, cgrid, ROWS, COLUMNS);


    return 0;
}

void Print_Grid(vector<vector<int> > &grid, vector<vector<string> > &cgrid, int ROWS, int COLUMNS)
{
    srand(time(0));

    int mines;

    // Assigns -1's randomly to some squares
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            int randN = (rand()%10)+1;
            if(randN==1 || randN==2 || randN==3)
            {
                grid[i][j] = -1; // mines are -1's
            }
        }
    }

    // this second nested loop checks surrounding squares for each elements.
    // However many mines are adjacent to the square,
    // that is the number assigned to that square.
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            mines = 0;
            if(grid[i][j] != -2)
                continue;
            else if(grid[i][j] == -2)
            {
                // checks squares surrounding top left corner
                if(i == 0 && j == 0)
                {
                   if(grid[i][j+1] == -1)
                        mines++;
                   if(grid[i+1][j] == -1)
                        mines++;
                   if(grid[i+1][j+1] == -1)
                        mines++;
                }

                // top right corner
                if(i == 0 && j == COLUMNS-1)
                {
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                }

                // bottom left corner
                if(i == ROWS-1 && j == 0)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                }

                // bottom right corner
                if(i == ROWS-1 && j == COLUMNS-1)
                {
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                }

                // top edge
                if( i == 0 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i+1][j+1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                }

                // bottom edge
                if(i == ROWS-1 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                }

                // left edge
                if(j == 0 && i != 0 && i != ROWS-1)
                {
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i+1][j+1] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                }

                // right edge
                if(j == COLUMNS-1 && i != 0 && i != ROWS-1)
                {
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                }


                // all other possible squares to be checked
                if(i !=0 && i!= ROWS-1 && j != 0 && j != COLUMNS-1)
                {
                    if(grid[i][j+1] == -1)
                        mines++;
                    if(grid[i][j-1] == -1)
                        mines++;
                    if(grid[i+1][j] == -1)
                        mines++;
                    if(grid[i-1][j] == -1)
                        mines++;
                    if(grid[i+1][j+1] == -1)
                        mines++;
                    if(grid[i+1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j-1] == -1)
                        mines++;
                    if(grid[i-1][j+1] == -1)
                        mines++;
                }
                grid[i][j] = mines; // assign number of mines adjecent to that square
            }
        }

    }

    // print out int grid
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            cout << setw(3) << grid[i][j] << setw(3);
        }

        cout << endl;
    }

    cout << "\n\nCOVER GRID\n\n";

    //Print cover grid
    for(int i=0; i<ROWS; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            cout << setw(3) << cgrid[i][j] << setw(3);
        }

        cout << endl;
    }

}

void Play_Game(vector<vector<int> > &grid, vector<vector<string> > &cgrid, int ROWS, int COLUMNS)
{
    // Bool variables used to break out of the nested loops
    bool flag1 = true;
    bool flag2 = true;
    bool flag3 = true;

    int row, column; // coordinares entered by user stored in these two variables

    while (flag1==true)
    {
        cout << "Enter row and column # (separated by space): ";
        cin >> row >> column;
        cout << "\n";

        for(int i=0; i<ROWS; i++)
        {
            for(int j=0; j<COLUMNS; j++)
            {
                if(grid[row][column] == grid[i][j] && grid[i][j])
                    cout << setw(3) << grid[i][j] << setw(3);
                else cout << setw(3) << cgrid[i][j] << setw(3);

                if(grid[row][column] == -1)
                    flag3 = false;

            }

            if(flag3 == false)
                flag2 = false;

            cout << endl;

        }

        if(flag2 == false)
            flag1 = false;

        cout << "\n";
    }

    cout << "\nYOU LOSE!" << endl;


If you've understood the problem im talking about, please guide me to a potential solution. How can I save the changes made to the grid made by the user's entered coordinates?
Topic archived. No new replies allowed.