Question: Program Design First, you will be implementing a class, Minesweeper to represent the game. Below we list the required public methods for the class. These

Program Design

First, you will be implementing a class, Minesweeper to represent the game. Below we list the required public methods for the class. These methods allow us to provide you with the user interface mentioned above.

Probably the biggest decision you will make initially is how to represent the information about each tile. Don't obsess over this. Just pick something that looks like it should work and implement it. Make a tile class/struct? Fine. Use some other encoding? Fine, too. I thought about defining such a class, chose not to, and will likely go back and reimplement to see how much cleaner it will look with one. (I'm pretty sure it will look a little better, but not dramatically.)

A border. Just as with Conway's Game of Life, it is possible that a border will simplify your code. In my implementation, I did not use a border and I only see a few lines of code that could have been avoided if I had had one.

So, to help you along, I am going to specify a set of methods that must be public. You may have additional public or private methods, but these are the methods our driver program will require, and they see a reasonable set for you to provide.

1)Minesweeper : The constructor to initialize the game.

2)display: To display the board. Take a boolean argument specifying whether to display the bombs or not. We won't want to for normal turns, but will want to at the end.

3)done: Returns true if the game is over, false otherwise.

4)validRow: Takes a row number. Returns true if the row number is valid, and false otherwise.

5)validCol: Takes a column number. Returns true if the column number is valid, and false otherwise.

6)isVisible: Takes a row number and a column number. Returns true if the corresponding cell is visible and false otherwise.

7)play: Takes a row number and a column number, changing the state of the board as appropriate for this move. Returns false if the move results in an explosion.

Randomness

It would be boring to play the game with the exact same bombs every time so you will want to generater bombs randomly. Perhaps the easiest way to generate a random number is with rand() from the header file.

By default, every time you run your program you will see the same sequence of random numbers. You can change the sequence by "seeding" the random generator. This is done with the function srand(int). to provide a "seed" for rand(). If you are using srand, you are going to only call it once in your program. A common mistake is to call it in a loop.

One common hack is to call srand(time(NULL)) to provide the seed. That uses the current time. Note that the function time requires that you include .

Other Things

The option for a player to "flag" a square that he is sure is a bomb, will make it more pleasant to play the game. I have not assumed this ability in the interface that I provided. Feel free to add a method and modify the interface to use it, but best to first get the rest working.

Driver Program / User Interface

int main() { Minesweeper sweeper; // Continue until the only invisible cells are bombs while (!sweeper.done()) { sweeper.display(false); // display the board without bombs int row_sel = -1, col_sel = -1; while (row_sel == -1) { // Get a valid move int r, c; cout << "row? "; cin >> r; if (!sweeper.validRow(r)) { cout << "Row out of bounds "; continue; } cout << "col? "; cin >> c; if (!sweeper.validCol(c)) { cout << "Column out of bounds "; continue; } if (sweeper.isVisible(r,c)) { cout << "Square already visible "; continue; } row_sel = r; col_sel = c; } // Set selected square to be visible. May effect other cells. if (!sweeper.play(row_sel,col_sel)) { cout << "BOOM!!! "; sweeper.display(true); // We're done! Should consider ending more "cleanly", // eg. Ask user to play another game. exit(0); } } // Ah! All invisible cells are bombs, so you won! cout << "You won!!!! "; sweeper.display(true); // Final board with bombs shown } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Your question involves designing a basic Minesweeper game class in a programming context providing a clear outline for defining methods and implementing the behavior of the game Below Ill break down h... View full answer

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 Databases Questions!