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.
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.
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, returnif m[x][y] == 0
fill(m, x, y+1)
fill(m, x, y-1)
fill(m, x+1, y)
....
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".
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;
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 << " ";
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.