Minesweeper program not working, and I'm unsure why

I'm trying to make a minesweeper program, but with gophers instead, and I'm struggling to get it to work. It was working before I added the reveal function, and selecting a gopher would make you lose. Now, losing seems to be completely random, and the reveal function that's supposed to show the number of gophers around you just returns random letters and symbols, most often "Q." Can anyone analyze my code and maybe help me figure out where I went wrong? Thanks!

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
  /* 
 * 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);

	cout << "You chose a gopher! Game over!" << 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 < 1 || vertical > 8 || vertical < 1)
	{
		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;
	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;
}



/*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';
}
Why did you ignore my answer to your last thread (same question as this) ???
closed account (E0p9LyTq)
Don't make duplicate threads, especially when you already got replies to the first one.

www.cplusplus.com/forum/beginner/235068/
Oh, I'm sorry! I had completely forgotten I had posted that one on here already. I'll take this one down, thank you guys. :)

EDIT: I can't figure out how to take it down
Last edited on
closed account (E0p9LyTq)
Delete it, that should be an option available to you.

Better is check-marking the topic as solved.

Look to the left of the page. There should be a box with your user name and a "my topics" link inside.

Click that and you see all the topics you started and/or the topics you made replies to if you didn't start it.
Topic archived. No new replies allowed.