Class MineSweeperBoard

java.lang.Object
  extended byMineSweeperBoardBase
      extended byMineSweeperBoard

public class MineSweeperBoard
extends MineSweeperBoardBase

A MineSweeperBoard holds a representation of the contents of the playing field for a Mine Sweeper game. The playing field is represented using a two-dimensional array of integer values. The integer value stored in each cell of the array indicates the icon which will appear in the corresponding cell of the graphical user interface for the game. Your job is to implement all the methods in this class skeleton and write a set of test cases for this class.

Version:
(place the date here)
Author:
your-pid (and partner's, if in lab)

Field Summary
 
Fields inherited from class MineSweeperBoardBase
COVERED_CELL, FLAG, FLAGGED_MINE, INVALID_CELL, MINE, UNCOVERED_MINE
 
Constructor Summary
MineSweeperBoard(int rows, int cols, int numMines)
          Construct a new MineSweeperBoard with the given dimensions.
 
Method Summary
 void flagCell(int row, int col)
          Place or remove a flag from the specified cell.
 boolean gameLost()
          Determine if the player has lost the current game.
 boolean gameWon()
          Determine if the player has won the current game.
 int getCell(int row, int col)
          Get the contents of the specified cell on this MineSweeperBoard.
 int getColumns()
          Get the number of columns in this MineSweeperBoard.
 int getRows()
          Get the number of rows in this MineSweeperBoard.
 int numAdjMines(int row, int col)
          Count the number of mines that appear in cells that are adjacent to the specified cell.
 void revealBoard()
          Uncover all of the cells on the board.
protected  void setCell(int row, int col, int value)
          Set the contents of the specified cell on this MineSweeperBoard.
 void uncoverCell(int row, int col)
          Uncover the specified cell.
 
Methods inherited from class MineSweeperBoardBase
clearBoard, equals, loadBoardState, placeMines, printBoard
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MineSweeperBoard

public MineSweeperBoard(int rows,
                        int cols,
                        int numMines)
Construct a new MineSweeperBoard with the given dimensions.

Parameters:
rows - the number of rows on the board
cols - the number of columns on the board
numMines - the number of mines to place on the board
Method Detail

flagCell

public void flagCell(int row,
                     int col)
Place or remove a flag from the specified cell. If the cell currently covered then place a flag on the cell. If the cell currently contains a flag, remove that flag but do not uncover the cell. If the cell has already been uncovered or is invalid, no change is made to the board.

Specified by:
flagCell in class MineSweeperBoardBase
Parameters:
row - the row of the cell to be flagged/unflagged.
col - the column of the cell to be flagged/unflagged.

gameLost

public boolean gameLost()
Determine if the player has lost the current game. The game is lost if the player has uncovered a mine. Note that this is not the opposite of gameWon(). Most of the time, the current game is neither lost nor won--when one of these two methods returns true, the game is over.

Specified by:
gameLost in class MineSweeperBoardBase
Returns:
true if the current game has been lost and false otherwise.

gameWon

public boolean gameWon()
Determine if the player has won the current game. The game is won when three conditions are met:
  1. Flags have been placed on all of the mines.
  2. No flags have been placed incorrectly.
  3. All non-flagged cells have been uncovered.
Note that this is not the opposite of gameLost(). Most of the time, the current game is neither lost nor won--when one of these two methods returns true, the game is over.

Specified by:
gameWon in class MineSweeperBoardBase
Returns:
true if the current game has been won and false otherwise.

getCell

public int getCell(int row,
                   int col)
Get the contents of the specified cell on this MineSweeperBoard. The value returned from this method must be one of the defined constants (COVERED_CELL, MINE, FLAG, FLAGGED_MINE, UNCOVERED_MINE) or a non-negative integer representing the number of mines adjacent to the cell.

Specified by:
getCell in class MineSweeperBoardBase
Parameters:
row - the row containing the cell (starting at zero)
col - the column containing the cell (starting at zero)
Returns:
the value contained in the cell specified by row and col, or INVALID_CELL if the specified cell does not exist

getColumns

public int getColumns()
Get the number of columns in this MineSweeperBoard.

Specified by:
getColumns in class MineSweeperBoardBase
Returns:
the number of columns in this MineSweeperBoard

getRows

public int getRows()
Get the number of rows in this MineSweeperBoard.

Specified by:
getRows in class MineSweeperBoardBase
Returns:
the number of rows in this MineSweeperBoard

numAdjMines

public int numAdjMines(int row,
                       int col)
Count the number of mines that appear in cells that are adjacent to the specified cell.

Specified by:
numAdjMines in class MineSweeperBoardBase
Parameters:
row - the row of the cell.
col - the column of the cell.
Returns:
the number of mines adjacent to the specified cell..

revealBoard

public void revealBoard()
Uncover all of the cells on the board.

Specified by:
revealBoard in class MineSweeperBoardBase

setCell

protected void setCell(int row,
                       int col,
                       int value)
Set the contents of the specified cell on this MineSweeperBoard. The value passed in should be one of the defined constants (COVERED_CELL, MINE, FLAG, FLAGGED_MINE, UNCOVERED_MINE) or a non-negative integer representing the number of mines adjacent to the cell. This method is for internal use by the MineSweeperBoardBase and its subclasses only.

Specified by:
setCell in class MineSweeperBoardBase
Parameters:
row - the row containing the cell
col - the column containing the cell
Returns:
the value to place in the cell

uncoverCell

public void uncoverCell(int row,
                        int col)
Uncover the specified cell. If the cell already contains a flag it should not be uncovered. If there is not a mine under the specified cell then the value in that cell is changed to the number of mines that appear in adjacent cells. If there is a mine under the specified cell the game is over and the player has lost. If the specified cell is already uncovered or is invalid, no change is made to the board.

Specified by:
uncoverCell in class MineSweeperBoardBase
Parameters:
row - the row of the cell to be uncovered.
col - the column of the cell to be uncovered.