C++ minesweeper coding... posted below at the bottom is my code which meets all
ID: 3831743 • Letter: C
Question
C++ minesweeper coding...
posted below at the bottom is my code which meets all of homework 5 requrements. i'm needing to edit this code to meet all of the parameters posted below. any help would be greatly appreciated.
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., ‘@’), 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. If the boolean is set to true, you will display either an ‘X’ or an ‘@’ 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., squares containing the enum value representing the '*'), you will check all of the immediately adjacent squares on the board to see If they contain a mine and then you will write to the square the enum value representing the number of immediately adjacent squares containing mines. Note that immediately adjacent means the up-to-8 squares surrounding the aforementioned selected square. Squares on the edge of the board will not have eight immediately adjacent squares, so be sure to handle these cases.
5. Inside the main function, you will add a loop to play the game until either the game is won (user successfully selected 10 squares without hitting a mine) or lost (user selected a square containing a mine prior to selecting 10 squares). For each turn, the user will enter a set of coordinates corresponding to the position (i.e., row and column) on the board of the square the user wants to select. If the user enters an invalid coordinate position, you will indicate that an invalid position was entered and prompt the user to enter the coordinates again without incrementing the number of turns taken. You may assume that the user enters the position coordinates correctly as integers, though one of both values may not be in the valid range defined. Following each turn, you will display an updated version of the board. Ultimately, the game will be over if the user selects a square containing a mine (i.e., did not win the game) or the user exhausts all 10 turns without selecting a square containing a mine (i.e., won the game). At the end of the game, you will display a meaningful message about whether the user won or lost the game as well as the final updated board with the position of all the mines revealed.
6. Instead of a traditional two-dimensional array as was done in Homework 5, you shall declare and create a two-dimensional dynamic array in main to represent the 5-by-5 board for the enum type you declared to represent the various values that a square can assume. Since the board array is not global, you will pass the board array to the needed functions as a pointer (actually, as a pointer to a pointer). You will need to make sure to return the memory for the twodimensional array back to the freestore when your program is done using it (at the end). You may assume that all input by the user is of the correct data type, though perhaps out of range. You should contact your instructor if there is any question about what is being asked for.
BONUS OPPORTUNITY: Up to 15 Points For students who have completed all requirements for this program, you may add the following option to gain additional bonus points to your program score: • You may add the option to "save" the game (perhaps using a sentinel value instead of the coordinates) so that it can be resumed at a later time. This would entail saving the game board (with all statuses indicated) and the number of turns the user has completed. This information may be written to a file in any format you choose. Then when the program is run again, you can check If the user wants to resume the previous game and, if so, open the file (if it exists) and load the data into memory to continue the game. Please note that unless you have completed all other requirements for this program no credit will be given for trying this extra credit. In other words, make sure your program is complete before attempting this extra credit.
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 5;//size of the board
enum board {SPACE = '*', MN = '@', SPOT = 'X', POS = '#'};
void pboard(board me[][SIZE], int size, int& mini);
/*
Function: pboard
Parameters:an array that displays the palying board, int = a place on the board,
another int shows number of mines
Return: no return
Description: displays a proportional 5x5 board with * in the spaces
*/
void header(); //displays header
void setup(int& mini); //displays rules of the game, passes and int for number of mines
int main()
{
int mini;//number mines used
int count=0;//break loop
board me[SIZE][SIZE] = {SPACE, MN, SPOT, POS};//Array for the board with specific enums
header();//header function call
cout << endl;
cout << " Welcome to Minesweeper!" << endl << endl;
while(count < 1)//prompt user for mines between 5 and 10
{
cout << "Enter the numbers of mines you'd like between 5 and 10. ";
cin >> mini;
cout << endl << endl;
if((mini < 5) || (mini > 10))//set limit on number of mines
{
cout << "Invalid input. Please try again. ";
}
else
{
count++;
}
}
setup(mini);//instructions function call
pboard(me, SIZE, mini);//function call for board
return 0;
}
void header()//displays header
{
cout << "CSCE 1030 ||" << endl;
return;
}
void setup(int& mini)//diplays rules with the user input of mines
{
cout << "This computer program will randomly assign " << mini <<" mines to the ";
cout << "5 by 5 board. Your objective will be to select ten squares ";
cout << "on the board that do not contain mines using the given in- ";
cout << "formation from the adjacent squares. The game is over when ";
cout << "you either select 10 squares without hitting a mine or you ";
cout << "select a square containing a mine. ";
cout << endl << endl;//makes things look pretty
cout << "Initializing board...assigning mines...now let's begin...good luck!" << endl << endl;
return;
}
void pboard(board me[][SIZE], int size, int& mini) //define board function
{
int count = 0;
string row[SIZE] = {"0|","1|","2|","3|","4|"};//board colums
srand(time(NULL));//random character generator
for (int i = 0; i < SIZE; i++)//place * across entire board
{
for(size = 0; size < SIZE; size++)
{
me[i][size] = SPACE;
}
}
int i;//used for random num
while(count < mini)//distrubutes mines randomly
{
i = (rand() % SIZE + 0);
size = (rand() % SIZE + 0);
if(me[i][size] != MN)//prevents multiple mines in same position through array
{
me[i][size] = MN;
count++;
}
else{}
}
cout << " 0 1 2 3 4" << endl;//makes board easy to read
cout << " +-----------+" << endl;//makes board look nice
for (int i = 0; i < SIZE; i++)//prints out the board
{
cout << row[i] << " ";
for(int size = 0; size < SIZE; size++)
{
cout << setw(1) << static_cast(me[i][size]) << " ";//character associated is printed out(enum values)
}
cout << "|" << endl;//makes board nice and proportional
}
cout << " +-----------+" << endl;//makes board look nice and proportional
return;
}
Explanation / Answer
#include
#include
#include
#include
#include
using namespace std;
const int SIZE = 5;//size of the board
enum board {SPACE = '*', MN = '@', SPOT = 'X', POS = '#'};
void pboard(board me[][SIZE], int size, int& mini);
/*
Function: pboard
Parameters:an array that displays the palying board, int = a place on the board,
another int shows number of mines
Return: no return
Description: displays a proportional 5x5 board with * in the spaces
*/
void header(); //displays header
void setup(int& mini); //displays rules of the game, passes and int for number of mines
int main()
{
int mini;//number mines used
int count=0;//break loop
board me[SIZE][SIZE] = {SPACE, MN, SPOT, POS};//Array for the board with specific enums
header();//header function call
cout << endl;
cout << " Welcome to Minesweeper!" << endl << endl;
while(count < 1)//prompt user for mines between 5 and 10
{
cout << "Enter the numbers of mines you'd like between 5 and 10. ";
cin >> mini;
cout << endl << endl;
if((mini < 5) || (mini > 10))//set limit on number of mines
{
cout << "Invalid input. Please try again. ";
}
else
{
count++;
}
}
setup(mini);//instructions function call
pboard(me, SIZE, mini);//function call for board
return 0;
}
void header()//displays header
{
cout << "CSCE 1030 ||" << endl;
return;
}
void setup(int& mini)//diplays rules with the user input of mines
{
cout << "This computer program will randomly assign " << mini <<" mines to the ";
cout << "5 by 5 board. Your objective will be to select ten squares ";
cout << "on the board that do not contain mines using the given in- ";
cout << "formation from the adjacent squares. The game is over when ";
cout << "you either select 10 squares without hitting a mine or you ";
cout << "select a square containing a mine. ";
cout << endl << endl;//makes things look pretty
cout << "Initializing board...assigning mines...now let's begin...good luck!" << endl << endl;
return;
}
void pboard(board me[][SIZE], int size, int& mini) //define board function
{
int count = 0;
string row[SIZE] = {"0|","1|","2|","3|","4|"};//board colums
srand(time(NULL));//random character generator
for (int i = 0; i < SIZE; i++)//place * across entire board
{
for(size = 0; size < SIZE; size++)
{
me[i][size] = SPACE;
}
}
int i;//used for random num
while(count < mini)//distrubutes mines randomly
{
i = (rand() % SIZE + 0);
size = (rand() % SIZE + 0);
if(me[i][size] != MN)//prevents multiple mines in same position through array
{
me[i][size] = MN;
count++;
}
else{}
}
cout << " 0 1 2 3 4" << endl;//makes board easy to read
cout << " +-----------+" << endl;//makes board look nice
for (int i = 0; i < SIZE; i++)//prints out the board
{
cout << row[i] << " ";
for(int size = 0; size < SIZE; size++)
{
cout << setw(1) << static_cast(me[i][size]) << " ";//character associated is printed out(enum values)
}
cout << "|" << endl;//makes board nice and proportional
}
cout << " +-----------+" << endl;//makes board look nice and proportional
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.