I have a grid, and now I need to put bombs in some places randomly, I've tried looking at other peoples version, but I can't figure it out. I would like the grid to be empty and when choosing coordinates if the square is empty then I want it to show an O and if it's a bomb to reveal an X, any help would be great.
you *draw* a grid but you don't seem to logically *store* a grid.
I think youll want a construct that represents the board, maybe
bool board[8][8];
and then set say 10 of them to true at random.
then when you print the board refer to this structure on whether to print an X or not when you draw the board. (this is typical -- you often in games keep the drawing of something and the data for it totally separated).
#include <random>
int main()
{
constint boardsize = 8; //constants belong in a single place, with a name,
//not in code as 8 100 times.
constint nummines = 10;
//declare your board structure.
bool board[boardsize][boardsize]; //this should be vectors if you know them
int r,c; //rows and columns
for(r = 0; r< boardsize; r++) //explicit assignment, there are shortcuts you could take
for(c = 0; c< boardsize; c++)
board[r][c] = false;
int count = 0;
std::default_random_engine generator; //set up random numbers
std::uniform_int_distribution<int> distribution(0,boardsize-1)
while (count < nummines)
{
r = distribution(generator); //get a random row and column
c = distribution(generator);
if(!board[r][c]) //just in case we get duplicates at random
{
board[r][c] = true;
count ++;
}
}
}
see if you can get that working. as nummines approaches the total number of locations in the board, the loop may become a problem. for only 10 out of 64, its fine. if it were 60 out of 64, it would sit there getting repeats for a long time potentially, see? If that is an issue we can talk about a better way.