Minesweeper Program?

So I'm currently working on a Minesweeper program, and I feel like I'm getting pretty close...the one problem I'm having is figuring out how to display the amount of "gophers" that are around the spot that the player selected. It has to do with the reveal function interacting with both arrays, but I'm not sure how to get it.

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
  /* 
 * Assignment 09
 * 04/12/18
 * Section 02
 */

#include <iostream>
#include <time.h>

using namespace std;


//-----Global Variables-----//
const int ROWS = 9;
const int COLS = 9;
bool game_over = false;
char field[ROWS][COLS];
char secret[ROWS][COLS];

//-----Function Prototypes-----//
void rules();
void random_gopher();
void create_field(char arr[][COLS]);
void print_field(char arr[][COLS]);
void choose();
void reveal(int, int);




//-----MAIN FUNCTION-----//
int main()
{
	rules();
	
	create_field(field);

	do
	{
		cout << endl;
		print_field(field);
		choose();
	}

	while (game_over != true);


	return 0;
}



//-----Other Functions-----//
void rules()
{
	cout << "Welcome to gopher hunt!\n\nTo play, enter an X value to pick a row and a Y value to pick a column.\n\n";
}

void create_field(char field[ROWS][COLS])
{
	cout << "Generating field...\n\n";
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			field[i][j] = '+';
		}
	}
}

void print_field(char field[ROWS][COLS])
{
	for (int a = 0; a < ROWS; a++)
	{
		cout << a << " ";
	}

	cout << COLS;
	cout << endl;

	for (int i = 0; i < ROWS; i++)
	{
		cout << i + 1 << " ";

		for (int j = 0; j < COLS; j++)
		{
			cout << field[i][j] << " ";
		}

		cout << endl;
	}
}

void choose()
{
	int horizontal, vertical;

	do
	{
		cout << endl;
		cin >> horizontal;
		cin >> vertical;
	}

	while (horizontal < 1 || horizontal > 9 || vertical < 1 || vertical > 9);

	if (secret[horizontal][vertical] == 'G')
	{
		game_over = true;

		for (int i = 0; i < ROWS; i++)
		{
			for (int j = 0; j < COLS; j++)
				if (secret[horizontal][vertical] == 'G')
				{
					field[horizontal][vertical] = 'G';
				}
		}
	}
	else
	{
		reveal(horizontal, vertical);
	}
}

void reveal(int horizontal, int vertical);
{

}

void random_gopher()
{

	srand(time(NULL));

	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			secret[i][j] = 'S';
		}
	}

	for (int a = 0; a < 10; a++)
	{
		int b = rand() % ROWS;
		int c = rand() % COLS;

		if (secret[b][c] == 'G')
		{
			a--;
			continue;
		}

		secret[b][c] = 'G';
	}
}
Bump...I imagine it has to do with taking the cell they selected and adding/subtracting 1 in both the horizontal options and the vertical. I just can't figure out how to lay that out in code.
I think you need a "neighbor count" grid that shows how many mines are next to each square, e.g. (where * are mines)

1  1  1  0  0  1  1
 
2  *  2  0  0  1  *

2  *  2  0  0  1  1

1  1  1  0  1  1  1

0  0  0  0  1  *  1

If the user picks a bomb, game over; if he picks a place with a non-zero value, only show that one value. But if he picks a zero you need to "floodfill" the zeroes up to the non-zeroes. E.g., Suppose the above map is totally covered and the user picks one of the zeroes, then it would look like this (where X's represent square that are still covered).

X  X  1        1  X
 
X  X  2        1  X

X  X  2        1  X

1  1  1     1  1  X

            1  X  X

You can use a recursive function for the floodfill.
1
2
3
4
5
6
7
fill( m, x, y):
    if x or y is out of range, return
    if m[x][y] == 0
        fill(m, x, y+1)
        fill(m, x, y-1)
        fill(m, x+1, y)
        ....

closed account (E0p9LyTq)
Don't make duplicate threads, that makes it easier for people to ignore you.

http://www.cplusplus.com/forum/beginner/235164/
Oh, I'm sorry, I had completely forgotten I had posted this one on here already! :/
tqb,

I did look into your solution, but when I turned in my first iteration of the project, I was instructed to not use recursive functions, as we have not learned them in class yet. We're supposed to be able to do this without having that.

I cannot update my code on this post, unfortunately, because it is too long. But I did add this to try and get the numbers to show up with how many gophers are around. The only problem is, it's not reading it properly, and when I put in 9,9 which should've said that there was 1 gopher next to it, I got the letter "Q".

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
int reveal(int horizontal, int vertical)
{
	int revealed_number = 0;
	for (int i = 0; i < ROWS; i++)
	{
		for (int j = 0; j < COLS; j++)
		{
			if (secret[horizontal + 1][vertical] == 'G')
			{
				revealed_number += 1;
			}

			if (secret[horizontal - 1][vertical] == 'G')
			{
				revealed_number += 1;
			}

			if (secret[horizontal][vertical + 1] == 'G')
			{
				revealed_number += 1;
			}

			if (secret[horizontal][vertical - 1] == 'G')
			{
				revealed_number += 1;
			}
		}
	}

	switch (revealed_number)
	{
	case 0:
		revealed_number = '0';
		break;
	case 1:
		revealed_number = '1';
		break;
	case 2:
		revealed_number = '2';
		break;
	case 3:
		revealed_number = '3';
		break;
	}

	return revealed_number;
}
TPB,

I thought I'd let you know, I've updated my code since then, and I got the go-ahead to use Recursion from my professor. I want to use the method you mentioned, but I'm unsure what the variable "m" is that you mentioned. Here is my code at this point. I want the recursion to happen during the reveal, of course, but I don't know how to get it to show the other zeroes since the other zeroes aren't actually there yet.

/*
* Assignment 09
* 04/12/18
* Section 02
*/

#include <iostream>
#include <time.h>

using namespace std;

//-----Global Variables-----//
const int ROWS = 9;
const int COLS = 9;
bool game_over = false;
char field[ROWS][COLS];
char secret[ROWS][COLS];
int win_count = 0;

//-----Function Prototypes-----//
void rules();
void random_gopher();
void create_gopher();
void create_field(char arr[][COLS]);
void print_field(char arr[][COLS]);
//void print_gopher(char arr[][COLS]);
void choose();
int reveal(int, int);




//-----MAIN FUNCTION-----//
int main()
{
rules();

create_field(field);

create_gopher();

do
{
cout << endl;
print_field(field);
//print_gopher(secret);
choose();
win_count += 1;
}

while (game_over != true && win_count < 71);

if (game_over == true)
{
cout << "You chose a gopher! Game over!" << endl;
}

if (win_count > 71)
{
cout << "Congrats, you win!" << endl;
}

cin.ignore();
cout << "Press enter to exit." << endl;
cin.ignore();

return 0;
}



//-----Other Functions-----//
void rules()
{
cout << "Welcome to gopher hunt!\n\nTo play, enter an X value to pick a row and a Y value to pick a column.\n\n";
}

void create_field(char field[ROWS][COLS])
{
cout << "Generating field...\n\n";
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
field[i][j] = '+';
}
}
}

void print_field(char field[ROWS][COLS])
{
for (int a = 0; a < ROWS; a++)
{
cout << a << " ";
}

cout << COLS;
cout << endl;

for (int i = 0; i < ROWS; i++)
{
cout << i + 1 << " ";

for (int j = 0; j < COLS; j++)
{
cout << field[i][j] << " ";
}

cout << endl;
}
}

/*void print_gopher(char secret[ROWS][COLS])
{
for (int a = 0; a < ROWS; a++)
{
cout << a << " ";
}

cout << COLS;
cout << endl;

for (int i = 0; i < ROWS; i++)
{
cout << i + 1 << " ";

for (int j = 0; j < COLS; j++)
{
cout << secret[i][j] << " ";
}

cout << endl;
}
}*/

void choose()
{
int horizontal = 0, vertical = 0;
cout << endl;
cin >> horizontal;
cin >> vertical;

horizontal -= 1;
vertical -= 1;

if (horizontal > 8 || horizontal < 0 || vertical > 8 || vertical < 0)
{
cout << "Illegal move. Try again." << endl;
}

if (secret[horizontal][vertical] == 'G')
{
game_over = true;
}

else
{
field[horizontal][vertical] = reveal(horizontal, vertical);
}
}

int reveal(int horizontal, int vertical)
{
int revealed_number = 0;

if (secret[horizontal + 1][vertical] == 'G')
{
revealed_number += 1;
}

if (secret[horizontal - 1][vertical] == 'G')
{
revealed_number += 1;
}

if (secret[horizontal][vertical + 1] == 'G')
{
revealed_number += 1;
}

if (secret[horizontal][vertical - 1] == 'G')
{
revealed_number += 1;
}

if (revealed_number == 0)
{
secret[horizontal + 1][vertical] == 0;
}



switch (revealed_number)
{
case 0:
revealed_number = '0';
break;
case 1:
revealed_number = '1';
break;
case 2:
revealed_number = '2';
break;
case 3:
revealed_number = '3';
break;
}

return revealed_number;
}



/*void random_gopher()
{

srand(time(NULL));

for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
secret[i][j] = 'S';
}
}

for (int a = 0; a < 10; a++)
{
int b = rand() % ROWS;
int c = rand() % COLS;

if (secret[b][c] == 'G')
{
a--;
continue;
}

secret[b][c] = 'G';
}3

}*/

void create_gopher()
{
secret[2][3] = 'G';
secret[5][6] = 'G';
secret[4][1] = 'G';
secret[8][9] = 'G';
secret[3][6] = 'G';
secret[7][4] = 'G';
secret[9][9] = 'G';
secret[5][8] = 'G';
secret[2][9] = 'G';
secret[1][7] = 'G';
}
I'm unsure what the variable "m" is that you mentioned

The "m" stood for the 2D array that holds the counts of the number of adjacent gophers. Maybe it should be called "adjacent". You should initialize it after you place the gophers.

If the player picks a gopher, game over.
If he picks a 0 (0 gophers nearby) then that square and any connected 0 squares are uncovered up to and including bordering non-zero squares. That's where you could use recursion.
If he picks a non-zero then just uncover that one square.
You need a way to keep track of which squares are uncovered.
Topic archived. No new replies allowed.