#include <iostream> #include <cstdlib> #include <ctime> using namespace std; //e
ID: 3832128 • Letter: #
Question
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//enum data type
enum BOARD{ star = 1, rateSym = 2};
//define the board size
const int myBoardSize = 5;
//define the board size
//Method initMineBoard() to allocate space for myMineBoard
void initMineBoard(BOARD **&myMineBoard)
{
myMineBoard = new BOARD*[myBoardSize];
for(int i = 0; i < myBoardSize; ++i)
{
myMineBoard[i] = new BOARD[myBoardSize];
for(int j = 0; j < myBoardSize; ++j)
{
myMineBoard[i][j] =(BOARD)(1);
}
}
}
//Method to place mine
bool placeMine(BOARD **&myMineBoard, int d)
{
bool isPlaced;
isPlaced = false;
do
{
int x =rand() % d;
int y =rand() % d;
if(myMineBoard[x][y] == star)
{
isPlaced = true;
myMineBoard[x][y] = rateSym;
}
}while(!isPlaced);
return true;
}
//Method to printMineBoard the grid
void printMineBoard(BOARD **myMineBoard)
{
cout << " ";
for(int i = 0; i < myBoardSize; ++i)
cout << i <<" ";
cout << " +----------+ ";
for(int i = 0; i < myBoardSize; ++i)
{
cout << i << "|";
for(int j = 0; j < myBoardSize; ++j)
{
if(myMineBoard[i][j] == star)
{
cout << "* ";
}
else
{
cout << "@ ";
}
}
cout << "|";
cout << endl;
}
}
//main
int main()
{
int minesCount;
BOARD **myMineBoard ;
srand(time(0));
cout << "************************************" << endl;
cout << "This computer program will randomly assign 6 mines to the" << endl;
cout << "5 by 5 board. Your objective will be to select ten squares" << endl;
cout << "on the board that do not contain mines using the given" << endl;
cout << "information from the adjacent squares. The game is over" << endl;
cout << "when you either select 10 squares without hitting a mine or" << endl;
cout << "you select a square containing a mine. Fill your details:" << endl;
cout << "************************************" <<endl;
do
{
cout << "Enter mines count (5-10): ";
cin >> minesCount;
if(minesCount >= 5 && minesCount <= 10)
{
break;
}
}while(1);
cout<<endl;
//call initMineBoard()
initMineBoard(myMineBoard);
//Loop to place the mines
for(int i =0; i <minesCount; i++)
{
placeMine(myMineBoard,myBoardSize);
}
//Display the mine board
cout << "MINE BOARD:" << endl;
//Call printMineBoard()
printMineBoard(myMineBoard);
return 0;
}
THIS IS THE BEGINING OF MINESWEEPER GAME, I WANT YOU TO ADD ON THIS CODE THE FOLLOWIN STEPS IN ORDER TO COMPLETE THE GAME:
HERE ARE THE STEPS:
You shall organize your program into three files prgm.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 will hold the local include directive for the header file as well as the main function for the program func .cpp will hold the local include directive for the header file as well as a 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 you are to display the character instead of false so as not to prematurely reveal the location of any of the mines. If the boolean is set to true, you will display either an X' or an 'a' 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 was 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 variable 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 selected square is selected, you are to indicate that an invalid position was entered and return false. Note that invalid selections (such as this case) should not increment the counter for the number of turns. For other selections (i.e.,Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
enum BOARD{ star = 1, rateSym = 2};
const int myBoardSize = 5;
void initMineBoard(BOARD **&myMineBoard)
{
myMineBoard = new BOARD*[myBoardSize];
for(int i = 0; i < myBoardSize; ++i)
{
myMineBoard[i] = new BOARD[myBoardSize];
for(int j = 0; j < myBoardSize; ++j)
{
myMineBoard[i][j] =(BOARD)(1);
}
}
}
bool placeMine(BOARD **&myMineBoard, int d)
{
bool isPlaced;
isPlaced = false;
do
{
int x =rand() % d;
int y =rand() % d;
if(myMineBoard[x][y] == star)
{
isPlaced = true;
myMineBoard[x][y] = rateSym;
}
}while(!isPlaced);
return true;
}
void printMineBoard(BOARD **myMineBoard)
{
cout << " ";
for(int i = 0; i < myBoardSize; ++i)
cout << i <<" ";
cout << " +----------+ ";
for(int i = 0; i < myBoardSize; ++i)
{
cout << i << "|";
for(int j = 0; j < myBoardSize; ++j)
{
if(myMineBoard[i][j] == star)
{
cout << "* ";
}
else
{
cout << "@ ";
}
}
cout << "|";
cout << endl;
}
}
//main
int main()
{
int minesCount;
BOARD **myMineBoard ;
srand(time(0));
cout << "************************************" << endl;
cout << "This computer program will randomly assign 6 mines to the" << endl;
cout << "5 by 5 board. Your objective will be to select ten squares" << endl;
cout << "on the board that do not contain mines using the given" << endl;
cout << "information from the adjacent squares. The game is over" << endl;
cout << "when you either select 10 squares without hitting a mine or" << endl;
cout << "you select a square containing a mine. Fill your details:" << endl;
cout << "************************************" <<endl;
do
{
cout << "Enter mines count (5-10): ";
cin >> minesCount;
if(minesCount >= 5 && minesCount <= 10)
{
break;
}
}while(1);
cout<<endl;
//call initMineBoard()
initMineBoard(myMineBoard);
//Loop to place the mines
for(int i =0; i <minesCount; i++)
{
placeMine(myMineBoard,myBoardSize);
}
cout << "MINE BOARD:" << endl;
printMineBoard(myMineBoard);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.