Question: Structs are changed to classes with their own . h and . cpp files. Many of the functions now become methods in these classes. The

Structs are changed to classes with their own .h and .cpp files. Many of the functions now become methods in these classes.
The player will be able to choose difficulty (percentage of spaces with mines) and size of the board. I have taken care of most of this in the source.cpp file, but some work still needs to be done by you in the GameBoard class.
Struct Position
This struct is set up to keep track of slots on the board. several operators have been overloaded for you to make the use of the struct simpler. You do not have to code any of these. You are welcome!
The code:
struct Position
{
int row{0};
int column{0};
Position& operator++();
friend bool operator<(const Position& srcL, const int& srcR);
friend bool operator>(const Position& srcL, const int& srcR);
friend bool operator>=(const Position& srcL, const int& srcR);
friend bool operator==(const Position& srcL, const Position& srcR);
friend bool operator!=(const Position& srcL, const Position& srcR);
friend Position operator+(const Position& srcL, const Position& srcR);
};
Class GameBoard
public:
You will need to cast between these enums and ints. Do not hard code values in your code, use the enums!
enum class Difficulty { easy =25, medium =30, hard =40};
enum class Size { small =10, medium =15, large =25};
The default constructor is boardSetup from the previous assignment with the modification for adding difficulty and size. You will need to ask for Difficulty and Size and use a couple switches to set the class members. Difficulty is used in the Bernoulli distribution. You will need to divide by 100. Also, after the board is set up, you will need to loop over it one time to set the numNeighbors member for each slot. This will be done using the countMines method.
GameBoard();
makeMove is essentially changeGameState from the previous assignment. There are several modifications made now that gameSlot is a class with public methods that must be used.
void makeMove();
gameOver is essentially isGameDone from the previous assignment. With some minor modifications because gameSlot is a class now.
bool gameOver();
returns true if all empty slots are revealed and no mines are revealed.
bool wonGame();
simply calls the reveal method for each gameSlot in the board vector.
void revealMines();
getters, with the only difference between the two being that the return value for modifySlot allows for modification of that slot in the board vector. Otherwise they are the same as the boardIndex function from the level 2 assignment.
gameSlot getSlot(Position slotNum) const;
gameSlot& modifySlot(Position slotNum);
you have to write this one, and it replaces the displayGameState function from the previous assignment. The switch that displays the actual slot will be moved to the gameSlot << override.
std::ostream& operator<<(std::ostream& o, const GameBoard& src);
static Size dimension;//static for Position++
private:
This hasnt really changed, but the operator overloads for the Position struct can greatly simplify the if statement. I used !=,,< and the isMined() method of the gameSlot class once each. I used a total of three &&s. I also created an instance of the Position struct each loop out of the loop counters like so:
Position neighborNum{ slotNum + Position{neighborRow, neighborColumn}};
int countMines(Position slot);
Difficulty level{ Difficulty::easy };
std::vector board;
Class gameSlot
public:
This doesnt do anything.
gameSlot(){}
sets the piece member to gamePieces::hiddenMine if mined is true.
gameSlot(bool mined);
sets the numNeighbors member to the passed in value.
void setNumNeighbors(int num);
inverts the flagged member
void flipFlag();
if the piece is a variant of hidden switches to the revealed version.
void reveal();
returns true if the slot has been revealed
bool isRevealed();
returns true if the slot is hiddenMine or revealedMine
bool isMined();
returns true if slot is revealedMine
bool exploded();
Is essentially the code from the displayGameState function that deals with outputting the slot.
std::ostream& operator<<(std::ostream& o, const gameSlot& src);
private:
the enum is now completely used internally, which is ideal.
enum class gamePieces { hiddenEmpty, revealedEmpty, hiddenMine, revealedMine };
gamePieces piece{ gamePieces::hiddenEmpty };
bool flagged{ false };
int numNeighbors{0};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!