Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to end the game of minesweeper once an M move hits a bomb in this piece of code

+2 votes
asked May 19, 2020 by Leighton Baxter (140 points)
bool Found = false, Cheat = true;

int col, row;

int NumberofMinesToFind = 0; //should be NumberOfMines

int NumberOfFlags = 0;

int MaxFlags = 10; // Should be MORE THAN NumberOfMines +

char Move;

while ((NumberofMinesToFind > 0 || NumberOfFlags < MaxFlags ||Found == false))

{

if (Cheat = true)

{

DisplayBoard(HiddenBoard, Dimension);

}

cout << "What type of move do you want to make." << endl;

cout << "F - Place a flag on the board to indicate where you think a Mine might be" << endl;

cout << "M - To reveal what the cell contains" << endl;

cout << "An M move does not count as one of your Flags but an F move does" << endl;

cout << "Select move (F/M): ";

cin >> Move;

row = -1;

while (row < 1 || row > Dimension)

{

cout << "Enter row: ";

cin >> row;

if (row < 1 || row > Dimension)

{

cout << "row value entered is out of bounds" << endl;

}

}

row = row - 1;

col = -1;

while (col < 1 || col > Dimension)

{

cout << "Enter column: ";

cin >> col;

if (col < 1 || row > Dimension)

{

cout << "col value entered is out of bounds" << endl;

}

}

col = col - 1;

if (Move == 'M')

{

if (Found == false)

            {

               cout << "GAME OVER" << endl;

            }

}

if (Move == 'F')

{

Found = CheckBoard(row, col, GameBoard, HiddenBoard);

if (Found == true)

{

  NumberofMinesToFind--;

}  

NumberOfFlags++;

}

DisplayBoard(GameBoard, Dimension);

cout << "Number of flags placed so far " << NumberOfFlags << endl;

}

cout << "Game finished" << endl;

bool Won;

if (NumberofMinesToFind == 0)

{

cout << "You have won the Game" << endl;

Won = true;

}

else

    {

    cout << "You have lost the game" << endl;

Won = false;

}

UpdateRecords(Records, PlayerName, Dimension, Won);

}

1 Answer

+1 vote
answered May 29, 2020 by Peter Minarik (84,720 points)

You can add an extra condition in your main loop and that could control how you exit from the loop.

e.g.:

bool gameOver = false;
...
while (!gameOver && (NumberofMinesToFind > 0 || NumberOfFlags < MaxFlags || Found == false))
{
    ...

    if (Move == 'M')
    {
        if (Found == false)
        {
            cout << "GAME OVER" << endl;
            gameOver = true;
        }
    }
    ....
}

An alternative is that instead of setting the gameOver flag, don't use it at all, just call break to leave the loop. In this case, you have to find other means to know that the game is over and the loop didn't quit for other reasons.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...