Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <cstdlib> #include <ctime> using namespace std; #de

ID: 3832115 • Letter: #

Question

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

#define GAMESIZE 5

enum type
{
STAR=42,
ATTHERATE=64
};

//Method to initialize the mine
void initialize(type **&gameArray, int size)
{
gameArray = new type*[size];
for(int i = 0; i < size; ++i){
gameArray[i] = new type[size];
for(int j = 0; j < size; ++j){
gameArray[i][j] =(type)(42);
}
}
}

//Method to assign mine
bool assignMine(type **&gameArray, int size)
{
   bool status=false;

   while(!status)
   {
   int x=rand() % size;
   int y=rand() % size;
   if(gameArray[x][y]==(type)(42))
   {
       status=true;
       gameArray[x][y]=(type)(64);
   }

   }
   return true;
}

//Method to display the grid
void display(type **gameArray, int size)
{
   cout<<" ";
   for(int i = 0; i < size; ++i)
       cout<<i<<" ";

   cout<<" +-----------+ ";

for(int i = 0; i < size; ++i)
{
   cout<<i<<"|";
for(int j = 0; j < size; ++j)
   {
cout << (char)gameArray[i][j] << " ";
}
   cout<<"|";
cout << endl;
}
}

//Method to display the student details
//This method is leave incomplete fill with your data
void displayStudent()
{
   cout<<"Computer Science and engineering"<<endl;
   cout<<"CSE 1030 -Computer Science I"<<endl;
   cout<<"Fill the rest deatils here"<<endl;
}

int main()
{
   int num;
   displayStudent();
   cout<<"Enter number of mines to place on board (5-10): ";
   cin>>num;
   cout<<endl;
   srand(time(NULL));
   type **gameArray = NULL;
   initialize(gameArray, GAMESIZE);
   for(int i=0;i<num;i++)
   {
       assignMine(gameArray, GAMESIZE);
   }
   cout<<" GAME DETAILS: FILL Suitably"<<endl;
   cout<<" Initializing board...assigning mines...now let's begin... ";
   display(gameArray, GAMESIZE);
   return 0;
}

PROGRAM DESCRIPTION: For this C++ program, you will edit your Homework 5 programming assignment to add the functionality to play a simplified version of the classic Minesweeper game that will display to the screen. Unless so indicated, all of the requirements in Homework 5 hold in Homework 6. Note that there are some changes to this assignment! You will take your solution from Homework 5 and edit it as follows: You shall organize your program into three files prgma. h will hold the include directives for the necessary libraries, any global constants, any enumerated data types, any type definitions, any structure definitions, and the list of function prototypes (i.e., function declarations) main.cpp I hold the local include directive for the header file as well as the main function for the program func .cpp ill hold the local include directive for the header file as well as all function definitions (not including main, of course) used in the program 2. Define a structure that contains row. column coordinate of the game board Specifically, this structure will contain the following two members: (1) an integer member that holds the row position on the board, and (2) an integer member that holds the column position on the board 3. Update the function to display the game board by adding another parameter used to indicate whether or not to "reveal" the solution with all mines visible, or to display the current, active board with mines hidden (showing the initial value instead). For squares containing a mine (i.e., G'), if the boolean to reveal the board is set to false, you are to display the character instead of so as not to prematurely reveal the location of any of the mines. f the boolean is set to rue, you will display either an or an e' for squares containing a mine, as appropriate. All other squares should be displayed as is (regarding the characters they represent based on their enum value. You will call this function every time to display the updated board after each valid user selection 4. Add a boolean value-returning function to check the status of the coordinates selected by the user so as to return a boolean to indicate whether or not a mine as hit (i e., if the user selected a square containing a mine, this function would return true; otherwise, it would return false). At a minimum, you will pass the game board as well as the structure variable containing the row-column coordinate described in #2 above to this function. You must use this structure ariable to check the rows and columns of the board. If the square containing a mine is selected, you will set the value of the square to the enum representing that the user selected a square containing a mine (i.e., the x If a previously

Explanation / Answer

Prgm.h

#include <iostream.h>

using namespace std;

char displayArray [5][5], mineArray [5][5], playAgain;

int numMines, xPosNum, yPosNum, count = 0;

string mines, xPos, yPos;

void assignArray();

void mineCount();

void showArray(char anArray[5][5]);

void playGame();

void adjacentCheck();

int checkGame();

Main.cpp

#include <iostream>

#include <sstream>

#include <cstdlib>

#include <ctime>

#include <string>

#include <stdlib.h>

using namespace std;

int main()

{

    //Main program running

    do

    {

        system("cls");

        //User inputs the number of mines he wants

        do

        {

            cout << "Please enter a valid number of mines (min = 5, max = 10): ";

            cin >> mines;

            stringstream(mines) >> numMines;

        }while(numMines < 5 || numMines > 10);

        //Code to clear the screen

        system("cls");

        assignArray();

        showArray(displayArray);

        playGame();

        cout << "Play again? (Y/N): ";

        cin >> playAgain;

        }while(playAgain == 'Y' || playAgain == 'y');

    return 0;

}

Func.cpp:

//Function to randomly assign mines and symbols to the arrays

void assignArray()

{

    srand((unsigned int)time(0));

     

    //Assigning symbols to the arrays

    for(int row = 0; row < 5; row ++){

        for(int col = 0; col < 5; col ++){

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

            mineArray [row][col] = '0';

        }

    }

    //Randomly assigning mines

    for(int i = 0; i < numMines; i++){

        int row = (rand() % 4);

        int col = (rand() % 4);

        while(mineArray[row][col] == '@'){

            row = (rand() % 4);

            col = (rand() % 4);

        }

        mineArray[row][col] = '@';

    }

    //Counting the mines for adjacent tiles

    mineCount();

}

//Function to count mines for surrounding tiles

void mineCount()

{

    for(int row = 0; row < 5; row ++){

        for(int col = 0; col < 5; col++){

            if(mineArray [row][col] == '@'){

                for(int x = row - 1; x <= row + 1; x++){

                    for(int y = col - 1; y <= col + 1; y++){

                        if(x >= 0 && x <= 4){

                            if(y >= 0 && y <= 4){

                                if(mineArray[x][y] != '@'){

                                    mineArray[x][y] += 1;

                                }

                            }

                        }

                    }

                }

            }

        }

    }

}

//Function for displaying arrays

void showArray(char anArray[5][5])

{

    for(int row = 0; row < 5; row++){

        for(int col = 0; col < 5; col++){

            cout << anArray [row][col] << " ";

        }

        cout << endl;

    }

}

//Function for playing the game

void playGame()

{

    //Code for selecting a valid tile

    for(int i = 0; i <= 25; i++){

        do

        {

            cout << "Please enter a valid row position number (1 - 5): ";

            cin >> xPos;

            stringstream(xPos) >> xPosNum;

        }while(xPosNum < 1 || xPosNum > 5);

        do

        {

            cout << "Please enter a valid column position number (1 - 5): ";

            cin >> yPos;

            stringstream(yPos) >> yPosNum;

        }while(yPosNum < 1 || yPosNum > 5);

        //Code for checking if the tile has already been chosen

        while(displayArray[xPosNum - 1][yPosNum - 1] == mineArray[xPosNum - 1][yPosNum - 1]){

            cout << "That position has already been revealed." << endl << endl;

            cout << "Please enter another row position number: ";

            cin >> xPos;

            stringstream(xPos) >> xPosNum;

            cout << "Please enter another column position number: ";

            cin >> yPos;

            stringstream(yPos) >> yPosNum;

        }

        //Code for checking for a loss or victory

        if(mineArray[xPosNum - 1][yPosNum - 1] == '@'){

            cout << " Opps! You stepped on a mine!" << endl;

            cout << "Game Over!" << endl << endl;

            showArray(mineArray);

            return;

        }

        else{

            system("cls");

            displayArray[xPosNum - 1][yPosNum - 1] = mineArray[xPosNum - 1][yPosNum - 1];

            adjacentCheck();

            showArray(displayArray);

            count = checkGame();

            if(count == (25 - numMines)){

                cout << " Congrats! You've cleared all the mines!" << endl;

                return;

            }

        }

    }

}

//Function for revealing numbers surrounding a '0' tile

void adjacentCheck()

{

    if(mineArray[xPosNum - 1][yPosNum - 1] == '0'){

        for(int x = xPosNum - 2; x != xPosNum + 1; x++){

            for(int y = yPosNum - 2; y != yPosNum + 1; y++){

                if(x >= 0 && x <= 4){

                    if(y >= 0 && y <= 4){

                        displayArray[x][y] = mineArray[x][y];                      

                        if(mineArray[x][y] == '0'){

                            for(int x2 = x - 1; x2 != x + 2; x2++){

                                for(int y2 = y - 1; y2 != y + 2; y2++){

                                    if(x2 >= 0 && x2 <= 4){

                                        if(y2 >= 0 && y2 <= 4){

                                            if(mineArray[x2][y2] != '@'){

                                            displayArray[x2][y2] = mineArray[x2][y2];

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

    }

}

//Function to check for a victory

int checkGame()

{

    int count = 0;

    for(int x = 0; x < 5; x++){

        for(int y = 0; y < 5; y++){

            if(displayArray[x][y] == mineArray[x][y]){

                count += 1;

            }

        }

    }

    return count;

}

#include <iostream.h>