Question

In: Electrical Engineering

In this project we will implement the Minesweeper game. Minesweeper is played on a rectangle grid....

In this project we will implement the Minesweeper game. Minesweeper is played on a rectangle grid. When the game starts, a number of bombs are hidden on random positions on the field. In every round, the player "touches" a cell of the field. If the cell contains a bomb, it explodes, the game ends, and the player loses. Otherwise, the cell is uncovered to show the number of bombs in the vicinity, that is, the number of neighboring cells that contain bombs. (Each cell has eight neighbors, including the diagonal neighbors. If the number is zero, the cell is displayed as a blank.) When the user believes she knows the position of a bomb, she can flag that cell with a marker. It is allowed to change one's mind later and uncover a flagged cell. If all cells are either uncovered or flagged, and the number of flags is equal to the number of bombs, then the game ends and the player wins.

The display should be performed in the Terminal. The C code must enforce the rules and prevent the players to choose forbidden options.

Do c programming coding based on this qusetion..

Solutions

Expert Solution

// A C++ Program to Implement and Play Minesweeper

#include<bits/stdc++.h>

using namespace std;

#define BEGINNER 0

#define INTERMEDIATE 1

#define ADVANCED 2

#define MAXSIDE 25

#define MAXMINES 99

#define MOVESIZE 526 // (25 * 25 - 99)

int SIDE ; // side length of the board

int MINES ; // number of mines on the board

// A Utility Function to check whether given cell (row, col)

// is a valid cell or not

bool isValid(int row, int col)

{

    // Returns true if row number and column number

    // is in range

    return (row >= 0) && (row < SIDE) &&

           (col >= 0) && (col < SIDE);

}

// A Utility Function to check whether given cell (row, col)

// has a mine or not.

bool isMine (int row, int col, char board[][MAXSIDE])

{

    if (board[row][col] == '*')

        return (true);

    else

        return (false);

}

// A Function to get the user's move

void makeMove(int *x, int *y)

{

    // Take the input move

    printf("Enter your move, (row, column) -> ");

    scanf("%d %d", x, y);

    return;

}

// A Function to print the current gameplay board

void printBoard(char myBoard[][MAXSIDE])

{

    int i, j;

    printf ("    ");

    for (i=0; i<SIDE; i++)

        printf ("%d ", i);

    printf ("\n\n");

    for (i=0; i<SIDE; i++)

    {

        printf ("%d   ", i);

        for (j=0; j<SIDE; j++)

            printf ("%c ", myBoard[i][j]);

        printf ("\n");

    }

    return;

}

// A Function to count the number of

// mines in the adjacent cells

int countAdjacentMines(int row, int col, int mines[][2],

                      char realBoard[][MAXSIDE])

{

    int i;

    int count = 0;

    /*

        Count all the mines in the 8 adjacent

        cells

            N.W   N   N.E

              \   |   /

               \ | /

            W----Cell----E

                 / | \

               /   | \

            S.W    S   S.E

        Cell-->Current Cell (row, col)

        N --> North        (row-1, col)

        S --> South        (row+1, col)

        E --> East         (row, col+1)

        W --> West            (row, col-1)

        N.E--> North-East   (row-1, col+1)

        N.W--> North-West   (row-1, col-1)

        S.E--> South-East   (row+1, col+1)

        S.W--> South-West   (row+1, col-1)

    */

    //----------- 1st Neighbour (North) ------------

        // Only process this cell if this is a valid one

        if (isValid (row-1, col) == true)

        {

               if (isMine (row-1, col, realBoard) == true)

               count++;

        }

    //----------- 2nd Neighbour (South) ------------

        // Only process this cell if this is a valid one

        if (isValid (row+1, col) == true)

        {

               if (isMine (row+1, col, realBoard) == true)

               count++;

        }

    //----------- 3rd Neighbour (East) ------------

        // Only process this cell if this is a valid one

        if (isValid (row, col+1) == true)

        {

            if (isMine (row, col+1, realBoard) == true)

               count++;

        }

    //----------- 4th Neighbour (West) ------------

        // Only process this cell if this is a valid one

        if (isValid (row, col-1) == true)

        {

               if (isMine (row, col-1, realBoard) == true)

               count++;

        }

    //----------- 5th Neighbour (North-East) ------------

        // Only process this cell if this is a valid one

        if (isValid (row-1, col+1) == true)

        {

            if (isMine (row-1, col+1, realBoard) == true)

               count++;

        }

     //----------- 6th Neighbour (North-West) ------------

        // Only process this cell if this is a valid one

        if (isValid (row-1, col-1) == true)

        {

             if (isMine (row-1, col-1, realBoard) == true)

               count++;

        }

     //----------- 7th Neighbour (South-East) ------------

        // Only process this cell if this is a valid one

        if (isValid (row+1, col+1) == true)

        {

               if (isMine (row+1, col+1, realBoard) == true)

               count++;

        }

    //----------- 8th Neighbour (South-West) ------------

        // Only process this cell if this is a valid one

        if (isValid (row+1, col-1) == true)

        {

            if (isMine (row+1, col-1, realBoard) == true)

               count++;

        }

    return (count);

}

// A Recursive Fucntion to play the Minesweeper Game

bool playMinesweeperUtil(char myBoard[][MAXSIDE], char realBoard[][MAXSIDE],

            int mines[][2], int row, int col, int *movesLeft)

{

    // Base Case of Recursion

    if (myBoard[row][col] != '-')

        return (false);

    int i, j;

    // You opened a mine

    // You are going to lose

    if (realBoard[row][col] == '*')

    {

        myBoard[row][col]='*';

        for (i=0; i<MINES; i++)

            myBoard[mines[i][0]][mines[i][1]]='*';

        printBoard (myBoard);

        printf ("\nYou lost!\n");

        return (true) ;

    }

    else

     {

        // Calculate the number of adjacent mines and put it

        // on the board

        int count = countAdjacentMines(row, col, mines, realBoard);

        (*movesLeft)--;

        myBoard[row][col] = count + '0';

        if (!count)

        {

            /*

            Recur for all 8 adjacent cells

                N.W   N   N.E

                  \   |   /

                      \ | /

                W----Cell----E

                     / | \

                   /   | \

                S.W    S   S.E

            Cell-->Current Cell (row, col)

            N --> North        (row-1, col)

            S --> South        (row+1, col)

            E --> East         (row, col+1)

            W --> West            (row, col-1)

            N.E--> North-East   (row-1, col+1)

            N.W--> North-West   (row-1, col-1)

            S.E--> South-East   (row+1, col+1)

            S.W--> South-West   (row+1, col-1)

            */

                //----------- 1st Neighbour (North) ------------

            // Only process this cell if this is a valid one

            if (isValid (row-1, col) == true)

            {

                   if (isMine (row-1, col, realBoard) == false)

                   playMinesweeperUtil(myBoard, realBoard, mines, row-1, col, movesLeft);

            }

            //----------- 2nd Neighbour (South) ------------

            // Only process this cell if this is a valid one

            if (isValid (row+1, col) == true)

            {

                   if (isMine (row+1, col, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row+1, col, movesLeft);

            }

            //----------- 3rd Neighbour (East) ------------

            // Only process this cell if this is a valid one

            if (isValid (row, col+1) == true)

            {

                if (isMine (row, col+1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row, col+1, movesLeft);

            }

            //----------- 4th Neighbour (West) ------------

            // Only process this cell if this is a valid one

            if (isValid (row, col-1) == true)

            {

                   if (isMine (row, col-1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row, col-1, movesLeft);

            }

            //----------- 5th Neighbour (North-East) ------------

            // Only process this cell if this is a valid one

            if (isValid (row-1, col+1) == true)

            {

                if (isMine (row-1, col+1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row-1, col+1, movesLeft);

            }

             //----------- 6th Neighbour (North-West) ------------

            // Only process this cell if this is a valid one

            if (isValid (row-1, col-1) == true)

            {

                 if (isMine (row-1, col-1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row-1, col-1, movesLeft);

            }

             //----------- 7th Neighbour (South-East) ------------

            // Only process this cell if this is a valid one

            if (isValid (row+1, col+1) == true)

            {

                 if (isMine (row+1, col+1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row+1, col+1, movesLeft);

            }

            //----------- 8th Neighbour (South-West) ------------

            // Only process this cell if this is a valid one

            if (isValid (row+1, col-1) == true)

            {

                if (isMine (row+1, col-1, realBoard) == false)

                    playMinesweeperUtil(myBoard, realBoard, mines, row+1, col-1, movesLeft);

            }

        }

        return (false);

    }

}

// A Function to place the mines randomly

// on the board

void placeMines(int mines[][2], char realBoard[][MAXSIDE])

{

    bool mark[MAXSIDE*MAXSIDE];

    memset (mark, false, sizeof (mark));

    // Continue until all random mines have been created.

    for (int i=0; i<MINES; )

     {

        int random = rand() % (SIDE*SIDE);

        int x = random / SIDE;

        int y = random % SIDE;

        // Add the mine if no mine is placed at this

        // position on the board

        if (mark[random] == false)

        {

            // Row Index of the Mine

            mines[i][0]= x;

            // Column Index of the Mine

            mines[i][1] = y;

            // Place the mine

            realBoard[mines[i][0]][mines[i][1]] = '*';

            mark[random] = true;

            i++;

        }

    }

    return;

}

// A Function to initialise the game

void initialise(char realBoard[][MAXSIDE], char myBoard[][MAXSIDE])

{

    // Initiate the random number generator so that

    // the same configuration doesn't arises

    srand(time (NULL));

    // Assign all the cells as mine-free

    for (int i=0; i<SIDE; i++)

    {

        for (int j=0; j<SIDE; j++)

        {

            myBoard[i][j] = realBoard[i][j] = '-';

        }

    }

    return;

}

// A Function to cheat by revealing where the mines are

// placed.

void cheatMinesweeper (char realBoard[][MAXSIDE])

{

    printf ("The mines locations are-\n");

    printBoard (realBoard);

    return;

}

// A function to replace the mine from (row, col) and put

// it to a vacant space

void replaceMine (int row, int col, char board[][MAXSIDE])

{

    for (int i=0; i<SIDE; i++)

    {

        for (int j=0; j<SIDE; j++)

            {

                // Find the first location in the board

                // which is not having a mine and put a mine

                // there.

                if (board[i][j] != '*')

                {

                    board[i][j] = '*';

                    board[row][col] = '-';

                    return;

                }

            }

    }

    return;

}

// A Function to play Minesweeper game

void playMinesweeper ()

{

    // Initially the game is not over

    bool gameOver = false;

    // Actual Board and My Board

    char realBoard[MAXSIDE][MAXSIDE], myBoard[MAXSIDE][MAXSIDE];

    int movesLeft = SIDE * SIDE - MINES, x, y;

    int mines[MAXMINES][2]; // stores (x,y) coordinates of all mines.

      initialise (realBoard, myBoard);

    // Place the Mines randomly

    placeMines (mines, realBoard);

     /*

     If you want to cheat and know

     where mines are before playing the game

     then uncomment this part

     cheatMinesweeper(realBoard);

     */

    // You are in the game until you have not opened a mine

    // So keep playing

    int currentMoveIndex = 0;

    while (gameOver == false)

     {

        printf ("Current Status of Board : \n");

        printBoard (myBoard);

        makeMove (&x, &y);

        // This is to guarantee that the first move is

        // always safe

        // If it is the first move of the game

        if (currentMoveIndex == 0)

        {

            // If the first move itself is a mine

            // then we remove the mine from that location

            if (isMine (x, y, realBoard) == true)

                replaceMine (x, y, realBoard);

        }

        currentMoveIndex ++;

        gameOver = playMinesweeperUtil (myBoard, realBoard, mines, x, y, &movesLeft);

        if ((gameOver == false) && (movesLeft == 0))

         {

            printf ("\nYou won !\n");

            gameOver = true;

         }

    }

    return;

}

// A Function to choose the difficulty level

// of the game

void chooseDifficultyLevel ()

{

    /*

    --> BEGINNER = 9 * 9 Cells and 10 Mines

    --> INTERMEDIATE = 16 * 16 Cells and 40 Mines

    --> ADVANCED = 24 * 24 Cells and 99 Mines

    */

    int level;

    printf ("Enter the Difficulty Level\n");

    printf ("Press 0 for BEGINNER (9 * 9 Cells and 10 Mines)\n");

    printf ("Press 1 for INTERMEDIATE (16 * 16 Cells and 40 Mines)\n");

    printf ("Press 2 for ADVANCED (24 * 24 Cells and 99 Mines)\n");

    scanf ("%d", &level);

    if (level == BEGINNER)

    {

        SIDE = 9;

        MINES = 10;

    }

    if (level == INTERMEDIATE)

    {

        SIDE = 16;

        MINES = 40;

    }

    if (level == ADVANCED)

    {

        SIDE = 24;

        MINES = 99;

    }

    return;

}

// Driver Program to test above functions

int main()

{

    /* Choose a level between

    --> BEGINNER = 9 * 9 Cells and 10 Mines

    --> INTERMEDIATE = 16 * 16 Cells and 40 Mines

    --> ADVANCED = 24 * 24 Cells and 99 Mines

    */

    chooseDifficultyLevel ();

    playMinesweeper ();

    return (0);

}


Related Solutions

Minesweeper. Minesweeper is a 1960s era video game played on an m-by-n grid of cells. The...
Minesweeper. Minesweeper is a 1960s era video game played on an m-by-n grid of cells. The goal is to deduce which cells contain hidden mines using clues about the number of mines in neighboring cells. Write a program Minesweeper.java that takes three integer command-line arguments m, n, and k and prints an m-by-n grid of cells with k mines, using asterisks for mines and integers for the neighboring mine counts (with two space characters between each cell). To do so,...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played...
Design and implement an Android application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then seek for the user’s selection (using your choice of an object...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.
Monshimout is a game from the Cheyenne people and played by women. It could be played...
Monshimout is a game from the Cheyenne people and played by women. It could be played by two or more players, and if played by more than two, then the players divided into two equal teams. Game equipment consisted of five plum stones and a basket made of woven grass or willow twigs. The basket measured 3-4 inches deep, 8 inches across at the top, and almost 1/2 inch thick. The plum stones were left plain on one side, but...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
Programming Project #1 – Day Planner In this project we will develop classes to implement a...
Programming Project #1 – Day Planner In this project we will develop classes to implement a Day Planner program. Be sure to develop the code in a step by step manner, finish phase 1 before moving on to phase 2. Phase 1 The class Appointment is essentially a record; an object built from this class will represent an Appointment in a Day Planner . It will contain 5 fields: month (3 character String), day (int), hour (int), minute (int), and...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
What would be the equilibrium of the classic game “Chicken” if it was played as a...
What would be the equilibrium of the classic game “Chicken” if it was played as a Stackleberg game in which one player gets to decide first?
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with...
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with a group of 3 persons. Each person chooses one number and it is assigned to either 'snap', 'crackle', or 'pop'. Then the number of rounds is decided (e.g. 12). The players play the game for the predetermined number of rounds (here 12). In each round: ● if the number of the round is a multiple of the snap number, the person who chose the...
I have a JavaFx game that you have a rectangle and you move it with the...
I have a JavaFx game that you have a rectangle and you move it with the arrow keys to shoot circles that I call enemies. I want to add some collision to the rectangle so that when it hits the circles the game ends and I also want to add a counter so that each time I shoot an enemy it adds 2 points and when you die it prints out how many points you get. import javafx.animation.AnimationTimer; import javafx.application.Application;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT