Class MineSweeperBoardBase

java.lang.Object
  extended byMineSweeperBoardBase
Direct Known Subclasses:
MineSweeperBoard

public abstract class MineSweeperBoardBase
extends Object

A MineSweeperBoard holds a representation of the contents of the playing field for a Mine Sweeper game. The playing field is represented using a 2 dimensional array of integer values. The integer value stored in each cell of the array indicates the icon which will appear in the cooresponding cell of the graphical user interface for the game.

This abstract base class defines the basic features of a MineSweeperBoard, which you must create.

Version:
2003.12.02
Author:
Stephen Edwards (based on Grant Braught's original)

Field Summary
static int COVERED_CELL
          A constant value representing a covered cell.
static int FLAG
          A constant value representing a cell which does not contain a mine but has had a flag placed on it.
static int FLAGGED_MINE
          A constant value representing a cell which contains a mine and has had a flag placed on it.
static int INVALID_CELL
          A constant value representing the contents of an invalid cell.
static int MINE
          A constant value representing a a cell that has not been uncovered yet but contains a mine.
static int UNCOVERED_MINE
          A constant value representing a cell containing a mine that has been uncovered.
 
Constructor Summary
MineSweeperBoardBase()
           
 
Method Summary
protected  void clearBoard()
          Cover all cells in the board.
 boolean equals(Object other)
          Check whether two boards have the same cell contents.
abstract  void flagCell(int row, int col)
          Place or remove a flag from the specified cell.
abstract  boolean gameLost()
          Determine if the player has lost the current game.
abstract  boolean gameWon()
          Determine if the player has won the current game.
abstract  int getCell(int row, int col)
          Get the contents of the specified cell on this MineSweeperBoard.
abstract  int getColumns()
          Get the number of columns in this MineSweeperBoard.
abstract  int getRows()
          Get the number of rows in this MineSweeperBoard.
 void loadBoardState(String[] rows)
          Reset the board using a given array of strings.
abstract  int numAdjMines(int row, int col)
          Count the number of mines that appear in cells that are adjacent to the specified cell.
protected  void placeMines(int numMines)
          Place a specified number of mines at random locations on this board.
 void printBoard(PrintWriter out)
          Print the contents of this MineSweeperBoard to the specified output stream.
abstract  void revealBoard()
          Uncover all of the cells on the board.
protected abstract  void setCell(int row, int col, int value)
          Set the contents of the specified cell on this MineSweeperBoard.
abstract  void uncoverCell(int row, int col)
          Uncover the specified cell.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COVERED_CELL

public static final int COVERED_CELL
A constant value representing a covered cell. A covered cell is any cell which does not contains a mine, has not been flagged and has not yet been uncovered.

See Also:
Constant Field Values

FLAG

public static final int FLAG
A constant value representing a cell which does not contain a mine but has had a flag placed on it.

See Also:
Constant Field Values

FLAGGED_MINE

public static final int FLAGGED_MINE
A constant value representing a cell which contains a mine and has had a flag placed on it.

See Also:
Constant Field Values

INVALID_CELL

public static final int INVALID_CELL
A constant value representing the contents of an invalid cell. This value is returned by the getCell method when an invalid cell is specified.

See Also:
Constant Field Values

MINE

public static final int MINE
A constant value representing a a cell that has not been uncovered yet but contains a mine.

See Also:
Constant Field Values

UNCOVERED_MINE

public static final int UNCOVERED_MINE
A constant value representing a cell containing a mine that has been uncovered.

See Also:
Constant Field Values
Constructor Detail

MineSweeperBoardBase

public MineSweeperBoardBase()
Method Detail

clearBoard

protected void clearBoard()
Cover all cells in the board. This method is usually used when initializing a newly created board to set all the cells to their initial "covered" state.


equals

public boolean equals(Object other)
Check whether two boards have the same cell contents.

Parameters:
other - the other object to compare with

flagCell

public abstract 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.

Parameters:
row - the row of the cell to be flagged/unflagged
col - the column of the cell to be flagged/unflagged

gameLost

public abstract boolean gameLost()
Determine if the player has lost the current game. The game is lost if the player has uncovered a mine.

Returns:
true if the current game has been lost and false otherwise

gameWon

public abstract 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.

Returns:
true if the current game has been won and false otherwise.

getCell

public abstract 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.

Parameters:
row - the row containing the cell.
col - the column containing the cell.
Returns:
the value contained in the cell specified by row and col, or INVALID_CELL if the specified cell does not exist.

getColumns

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

Returns:
the number of columns in this MineSweeperBoard.

getRows

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

Returns:
the number of rows in this MineSweeperBoard

loadBoardState

public void loadBoardState(String[] rows)
Reset the board using a given array of strings. Each string in the array represents one row on the board. The string's characters are interpreted, one character per board cell, using the same conventions as in printBoard() (e.g., the character 'O' represents an empty covered cell, '+' represents a covered mine, etc.).

Parameters:
rows - the array of strings to interpret; the array length must be the same as the number of rows in this board, and each string's length must be the same as the number of columns in this board.

numAdjMines

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

Parameters:
row - the row of the cell.
col - the column of the cell.
Returns:
the number of mines adjacent to the specified cell..

placeMines

protected void placeMines(int numMines)
Place a specified number of mines at random locations on this board.

Parameters:
numMines - the number of mines to place

printBoard

public void printBoard(PrintWriter out)
Print the contents of this MineSweeperBoard to the specified output stream. This method is never used by the MineSweeper game, and is only intended for testing purposes. The board is printed surrounded by dashes, with cells marked using the following conventions:
 O = covered cell
 F = flag
 M = flagged mine
 + = covered mine
 * = uncovered mine (about to explode!)
 1..9 or space = uncovered cell
 

Parameters:
out - the output stream to print the board description on

revealBoard

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


setCell

protected abstract 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.

Parameters:
row - the row containing the cell
col - the column containing the cell
Returns:
the value to place in the cell

uncoverCell

public abstract 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.

Parameters:
row - the row of the cell to be uncovered.
col - the column of the cell to be uncovered.