Hey, i am working on a project in which i am making a minesweeper game. The game part was written by my prof. all i had to do was make the class and header to run the background of the game. I have done so successfully, but i am having trouble with one part. When i try to place the mines into the board, it refuses to put the correct number of mines in. I dont know why. ere is the entire function that places the mines into the board:
void Minesweeper::init ()
{
srand((unsigned)time(0));
int minesLeft = 0, cellsLeft = 0, randNum = 0;
// sets minesLeft to the data member numMines
minesLeft = numMines;
//sets cellsLeft to the number of rows and cols in the board
//nrows and ncols are both data members
cellsLeft = nRows * nCols;
while (minesLeft > 0)
{
for (int r = 0; r < nRows; r++)//rows
{
for (int c = 0; c < nCols; c++)//cols
{
//places mines with a probibility of numMines / cellsLeft
randNum = (rand()%cellsLeft)+1;
if (randNum <= minesLeft)
{
mine [r][c] = true;
minesLeft --;
}
if (randNum > minesLeft)
{
mine [r][c] = false;
}
cellsLeft--;
mark [r][c] = NO_MARK;
}
}
}
}//Minesweeper::init
the problem that i have is that it never places the correct number of mines. here are some examples of what it does place.
No. of rows = 10
No. of columns = 10
Mo. of mines = 20
output of mine placement
_ _ _ _ _ M M _ _ M
_ _ M _ M _ _ _ _ M
_ _ _ _ _ _ _ _ _ M
_ _ _ _ _ _ _ M _ _
_ _ M _ _ _ _ _ _ _
_ _ _ _ M M _ _ M _
_ M _ _ M M _ _ _ _
_ _ _ _ _ _ _ _ _ _
M _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
~~~~~~~~~~~~~~~~
No. of rows = 10
No. of columns = 10
Mo. of mines = 20
output of mine placement
_ _ _ _ _ _ _ _ M _
M M _ _ _ M _ _ M _
_ M _ M _ _ _ _ _ _
_ M _ _ M _ M _ _ _
_ _ _ M _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ M _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ M _ M _ M _ _ _ _
~~~~~~~~~~~~~~~~
No. of rows = 10
No. of columns = 10
Mo. of mines = 20
output of mine placement
_ M _ _ _ _ _ _ _ _
_ _ M _ _ _ M _ _ _
_ _ _ _ _ _ _ _ _ _
M _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ M _ _ _ _ _ _ _ _
_ _ M _ _ _ M M _ _
_ _ M _ M _ _ M M _
_ _ M _ _ _ M _ _ M
_ M _ _ _ _ _ _ _ _
Thanks for any help.
Mattwaab;