You are "generating" the mines every turn (and calling srand every time, too). |
No, it generates mines on the first turn only.
first_turn
assures that the code executes once. However, first_turn has the wrong sense (0-> first turn, 1-> other turns). And it should be a boolean. And that code should be inside an
if
instead of a
while
.
Also generateMines() ensures that the players first guess isn't on a mine. Why?
Also generateMines() will never place a mine in the last row or column.
I found it much easier on my head to replace array2 with an array of booleans that just says whether the corresponding square in array is exposed to the player:
the test in checkwin becomes:
if (exposed[y][x] && array[y][x] == '*') {
When printing the board I did:
1 2
|
char ch = (exposed[y][x] ? array[y][x] : ' ');
cout << "[" << ch << "]";
|
And right after calling reveal, I always expose the current square:
1 2
|
reveal(a, b);
exposed[a][b] = true; // always expose the one they picked.
|
I counted the mines differently: first I found where the mines are, then I incremented the squares around them. Note that it would be more efficient to increment the squares when you place the mines instead.
Here is how I did reveal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void
reveal(int y, int x)
{
if (x<0 || x>=9) return; // x out of bounds
if (y<0 || y>=9) return; // y out of bounds
if (exposed[y][x]) return; // already did this square
if (array[y][x] != '*') {
exposed[y][x] = true;
}
if (array[y][x] == '_') {
reveal(y - 1, x - 1);
reveal(y, x - 1);
reveal(y + 1, x - 1);
reveal(y - 1, x);
reveal(y + 1, x);
reveal(y - 1, x + 1);
reveal(y, x + 1);
reveal(y + 1, x + 1);
}
}
|
You might want to get reveal() working (or at least not crashing) first. Then you can work on the other problems.