Clear area in Mine Sweeper

I'm working on improving a 'Mine Sweeper' program I wrote, but am having trouble clearing the whole area that is surrounded by mines. In my original game, I just clear a 3x3 area around the selected field, but I would prefer a complete reveal. I'm trying to figure it out using a test program. I can't get it to fully work.

Could someone show me where I'm making my mistake?
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
// Mine Sweeper Testing.cpp : main project file.

#include <iostream>
#include <string>
#include <Windows.h>

using std::string;
using std::cout;
using std::cin;
using std::endl;


void clear(int Grid[][10], int col, int row);
void Show_Grid(int Grid[][10]);
void gotoXY(int x, int y);


int main()
{
	gotoXY(30, 5);
	cout << " Mine Sweeper Testing";
	int Grid[10][10] = {
		{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 1, 0, 1, 1, 1, 1 },
		{ 1, 0, 1, 1, 0, 1, 1, 0, 0, 1 },
		{ 1, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
		{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
	};
	// A '1' = mine present, '0' = clear
	gotoXY(0, 6);
	Show_Grid(Grid);
	clear(Grid, 1,1);
	gotoXY(0, 18);
	Show_Grid(Grid);
	gotoXY(25, 25);
	return 0;
}

void Show_Grid(int Grid[][10])
{
	int x, y;
	
	for (y = 0; y < 10; y++)
	{
		for (x = 0; x < 10; x++)
		{
			if (Grid[y][x] == -1)
				cout << "0 ";
			else
				cout << "? ";
		}
		cout << endl;
	}

}

void clear(int Grid[][10], int r, int c)
{
	int found = 0, x, y;
	int sqr[10][10];

	for (x = 0; x < 10; x++)
	{
		for (y = 0; y < 10; y++)
		{
			sqr[x][y] = Grid[x][y];
		}

	}
	sqr[r][c] = -1;
	do
	{

		found = 0;
		if (sqr[r - 1][c] == 0)
		{
			found++;
			sqr[r - 1][c] = -1;
		}
		if (sqr[r][c - 1] == 0)
		{
			found++;
			sqr[r][c - 1] = -1;
		}
		if (sqr[r][c + 1] == 0)
		{
			found++;
			sqr[r][c + 1] = -1;
		}
		if (sqr[r + 1][c] == 0)
		{
			found++;
			sqr[r + 1][c] = -1;
		}


		if (sqr[r - 1][c] == -1)
		{
			if(r-1> 0)
				r--;
		}
		else if (sqr[r + 1][c] == -1)
		{
			if (r + 1 < 9)
				r++;
		}
		else if (sqr[r][c - 1] == -1)
		{
			if (c - 1 >  0)
				c--;
		}
		else if (sqr[r][c + 1] == -1)
		{
			if (c + 1 < 9)
				c++;
		}
		Sleep(1000);// This section just to be able to view what the r & c values are 
		gotoXY(1, 1);
		cout << "r = "<< r;
		gotoXY(1, 2);
		cout << "c = " << c;
	} while (found);

	for (x = 0; x < 10; x++) // Transfer values to Grid array
	{
		for (y = 0; y < 10; y++)
		{
			if (sqr[x][y] == -1)
				Grid[x][y] = -1;
		}
	}
}

void gotoXY(int x, int y)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD CursorPosition;
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
void clear(int Grid[][10], int r, int c)
{
    if (Grid[r][c])
        return;

    Grid[r][c] = -1;

    clear(Grid, r - 1, c);
    clear(Grid, r + 1, c);
    clear(Grid, r, c - 1);
    clear(Grid, r, c + 1);
}


A recursive solution is fairly simple.

See: http://en.wikipedia.org/wiki/Flood_fill
@cire

Thanks. This will make my 'Mine sweeper' game a lot easier to play. I don't fully understand how that function works quite yet, but I will study on it. After I do understand it a bit more, then maybe a recursive function will be simple to understand and use, but, alas, not yet. Again, thanks.
Topic archived. No new replies allowed.