Minesweeper - checking adjacent cells for bombs

Hi, i'm trying to create a minesweeper board in visual studio. So far I've managed to set up two boards using arrays(a visible play board for the user and a hidden board containing the bombs).

I've managed to randomly input the bombs on to the hidden board and now i need to create a loop that checks each cell in the hidden board to see how many mines are in the adjacent cells. After checking each cell the loop then needs to place a number on the hidden board representing the total adjacent mines to that cell.

The problem i'm having atm is after creating the loop not all the bomb counts are as they should be e.g. some cells show a number 4 despite only being next to 1 bomb. If anyone could tell me what I've done wrong or point me in the right direction it would be greatly appreciated.

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
#include <Windows.h>
#include <iostream>
#include <conio.h>
#include <ctime>
#include <string>
#include <stdlib.h>

using namespace std;

void displayBoard();
void hiddenBoard();
void placeMines();
//void mineCheck();
//int countMines(int x,int y);

int show[8][8];
int hide[8][8];


int main()
{
displayBoard();
hiddenBoard();
placeMines();
	
		cout << "display board \n";		
		for (int i = 0;i < 8;i++)//display array
		{
			for (int j = 0;j < 8;j++)
			{
				cout << "[" << show[i][j] << "]";
			}
			cout << "\n";
		}

		for (int x = 0;x<8;x++) //count mines around hidden array

		{
			for (int y = 0;y < 8;y++)
			{
				int DIMX = 8;
				int DIMY = 8;
				int num =0;

				if (hide[x][y] == 9)
				{

				}
				else {

					if (x > 0 && y > 0) { // top left
						if (hide[x - 1][y - 1] == 0) {
							num++;
						}
					}
					if (y > 0) { // top
						if (hide[x][y - 1] == 0) {
							num++;
						}
					}
					if (x < DIMX - 1 && y > 0) { // top  right
						if (hide[x + 1][y - 1] == 0) {
							num++;
						}
					}
					if (x > 0) { // left
						if (hide[x - 1][y] == 0) {
							num++;
						}
					}
					if (x < DIMX - 1) { // right
						if (hide[x + 1][y] == 0) {
							num++;
						}
					}
					if (x > 0 && y < DIMY - 1) { // bottom left
						if (hide[x - 1][y + 1] == 0) {
							num++;
						}
					}
					if (y < DIMY - 1) { // bottom
						if (hide[x][y + 1] == 0) {
							num++;
						}
					}
					if (x < DIMX - 1 && y < DIMY - 1) { // bottom right
						if (hide[x + 1][y + 1] == 0) {
							num++;
						}
					}
					if (num != 0)     //if array location
					{
						hide[x][y] = num;
					}
					else
					{
						hide[x][y] = 0;
						
					}
				}
			}

		}
	
		cout << "hidden board \n";
		for (int i = 0;i < 8;i++)		//display array
			{
				for (int j = 0;j < 8;j++)
				{
					cout << "[" << hide[i][j] << "]";
				}
				cout << "\n";
			}

	_getch();
}
void displayBoard()
{
	for (int i = 0;i<8;i++) //create array
		for (int j = 0;j<8;j++)
		{
			show[i][j] = 0;
		}
}
void hiddenBoard()
{
	for (int i = 0;i<8;i++) //create array
		for (int j = 0;j<8;j++)
		{
			hide[i][j] = 0;
		}
}
void placeMines()
{
int mines = 0, X, Y;
	do
		{
			do
				{
					srand(time(0)); //make mine locations random every game
					X = rand() % 8; // get a random row
					Y = rand() % 8; // get a random column
				} 
			while (hide[Y][X] != 0); // do again if a mine is located there already
				hide[Y][X] = 9; // place a mine at location
				mines++; // increase mine count
		}
	while (mines < 8); // total mine count
}



/*void mineCheck()
{
	int x, y;

	if (hide[x -1][y] == char(255))
	{
		hide[x][y] == 's';
	}


}*/
I think that the problem is that you are comparing to 0 instead of 9.

1
2
3
4
5
if (x > 0 && y > 0) { // top left
	if (hide[x - 1][y - 1] == 0) {
		num++;            ^
	}                    should be 9  
}


You should also not call srand(time(0)) inside the a loop like that. That reseeds the random generator each time and since time(0) returns the number of seconds it means you will get the same X and Y values during the whole second. That is why it takes 8 seconds before the program prints anything. What you should do instead is call it once, at the beginning of main().
Last edited on
Yeah just needed to change the 0's to 9', works fine now. Thanks for explaining srand as well. i had no idea why it was taking so long to load.
Registered users can post here. Sign in or register to post.