1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// This is the specification for the minesweeper class
const int SIZE = 3;
class minesweeper
{
public:
// Constructors
minesweeper();
minesweeper(int mines);
void resetGame();
// PRE: None
// POST: The game is reset using the existing number of mines
// All cells are cleared and the mines are placed in random cells
// Adjacency values are calculated for all cells
int flagCell(int x, int y);
// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
// POST: Cell x,y is flagged as a mine.
// Function return value is the result of checkEndOfGame.
void unflagCell(int x, int y);
// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
// POST: Cell x,y no longer flagged as a mine.
int uncoverCell(int x, int y);
// PRE: x,y must be a valid row,column pair (between 1 and 3 inclusive)
// POST: Cell x,y is uncovered.
// Function return value is the result of checkEndOfGame.
void displayBoard();
// PRE: None
// POST: The board is displayed
void revealBoard();
// PRE: None
// POST: The board is displayed with all cells uncovered.
// The state of the board and the game are unchanged
private:
struct cell
{
bool mine; // Is there a mine in this cell
bool covered; // Is this cell still covered
int minesAdj; // How many mines are adjacent to this cell
bool flagged; // Is this cell marked as a mine
};
cell board[SIZE][SIZE]; // The board
int mineCount; // The number of mines
// Private member functions used within the class
int checkEndOfGame();
// POST: This is used by uncoverCell and flagCell to check
// for a win or loss after uncovering or flagging a cell.
// Function return values are: 0 = keep playing, 1 = loss, 2 = win
void clearAround(int i, int j);
// POST: All cells adjacent to cell i,j that have an
// adjacency value of zero are marked as uncovered
// This is used by uncoverCell to speed up play.
};
|