antichess
Class Board<M extends Move>

java.lang.Object
  extended by antichess.Board<M>
Direct Known Subclasses:
ChessBoard

public abstract class Board<M extends Move>
extends Object

A Board represents an arbitrary 2-dimensional game board that is somehow populated by pieces that are allowed to move in specific waves.

Board defines some game-over conditions for all Boards built off of this one: NOTOVER with the value 0, DRAW with value -1, OUTOFTIME with value -2, and RESIGN with a value of -3. Subclasses of this Board should not set a different end condition that matches any of these integer value.

Specification Fields

Field Summary
static int DRAW
          End-game condition that signifies a draw
static int FORCEDEND
          End-game condition variable to represent a forced end
protected  int gameOverReason
           
protected  MoveHistory<M> moveStack
           
static int NOTOVER
          Default Board termination condition, indicating that the game is not over
protected  List<BoardObserver> observers
           
static int OUTOFTIME
          End-game condition that signifies that the game has run out of time for some player
protected  Player player
           
protected  Player winner
           
 
Constructor Summary
Board()
          Initializes some variables for the Board assuming it is being created for a new game, including observers.
 
Method Summary
 void addObserver(BoardObserver bo)
          Adds an observer to the list of BoardObservers
abstract  List<M> allLegalMoves()
           
abstract  Board<M> clone()
          Creates a clone of this Board
protected  void copyThis(Board<M> b)
          Inheritable helper method for copying various data members from this Board to an empty Board.
 void doMove(M m)
           
abstract  void doMove(M m, String timestamp)
           
 void endGame(int gameOverReason, Player winner)
          Terminates the current game, specifying the cause of game termination and the winner.
abstract  int getColumns()
           
 int getGameOverReason()
           
 MoveHistory<M> getMoveHistory()
           
abstract  Piece getPieceAt(int row, int column)
           
abstract  List<Piece> getPieces()
           
abstract  List<Piece> getPieces(Player player)
           
 Player getPlayer()
           
abstract  int getRows()
           
 Player getWinner()
           
 boolean hasALegalMove()
           
 boolean isGameOver()
           
abstract  boolean isMoveLegal(M m)
           
abstract  List<M> legalMoves(Piece p)
           
 void notifyObservers()
          Updates all observers in the list of observers that the Board has been changed.
protected  M popMove()
          Pop a move off the internal move stack and return it
protected  void pushMove(M m, String timestamp)
          Pushes the given move onto the internal piece stack
 void removeObserver(BoardObserver bo)
          Removes an observer from the list of BoardObservers.
abstract  void undoLastMove()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NOTOVER

public static final int NOTOVER
Default Board termination condition, indicating that the game is not over

See Also:
Constant Field Values

DRAW

public static final int DRAW
End-game condition that signifies a draw

See Also:
Constant Field Values

OUTOFTIME

public static final int OUTOFTIME
End-game condition that signifies that the game has run out of time for some player

See Also:
Constant Field Values

FORCEDEND

public static final int FORCEDEND
End-game condition variable to represent a forced end

See Also:
Constant Field Values

player

protected Player player

winner

protected Player winner

gameOverReason

protected int gameOverReason

observers

protected List<BoardObserver> observers

moveStack

protected MoveHistory<M extends Move> moveStack
Constructor Detail

Board

public Board()
Initializes some variables for the Board assuming it is being created for a new game, including observers.

Method Detail

clone

public abstract Board<M> clone()
Creates a clone of this Board

Overrides:
clone in class Object

copyThis

protected void copyThis(Board<M> b)
Inheritable helper method for copying various data members from this Board to an empty Board.

Parameters:
b - the empty board into which this board should be copied.

getRows

public abstract int getRows()
Returns:
the number of rows on the board

getColumns

public abstract int getColumns()
Returns:
the number of columns on the board

getPlayer

public Player getPlayer()
Returns:
this.player

getPieceAt

public abstract Piece getPieceAt(int row,
                                 int column)
Returns:
a Piece p in this.pieces with p.row == row and p.column == column or null if no such piece exists

getPieces

public abstract List<Piece> getPieces()
Returns:
a list of this.pieces in an unspecified order

getPieces

public abstract List<Piece> getPieces(Player player)
Returns:
an interator over {p in this.pieces | p.player == player} in an unspecified order

hasALegalMove

public boolean hasALegalMove()
Returns:
true iff the current player has a legal move to make

allLegalMoves

public abstract List<M> allLegalMoves()
Returns:
a list of all legal moves for the current player from the current position

legalMoves

public abstract List<M> legalMoves(Piece p)
Returns:
a list of all legal moves for the specified Piece
Requires:
p.player == this.player

isMoveLegal

public abstract boolean isMoveLegal(M m)
Returns:
true iff the given move is an allowed move for the current player

doMove

public void doMove(M m)
            throws IllegalMoveException
Parameters:
m - the Move that describes how to modify the Board
Throws:
IllegalMoveException - if the move is illegal
Effects:
the current board to rearrange the state of the Pieces according to the given Move
Modifies:
this

getMoveHistory

public MoveHistory<M> getMoveHistory()
Returns:
the move history for this board

pushMove

protected void pushMove(M m,
                        String timestamp)
Pushes the given move onto the internal piece stack

Requires:
m != null
Effects:
adds the given move to the this.moveStack
Modifies:
this

popMove

protected M popMove()
Pop a move off the internal move stack and return it

Effects:
removes the last move from this.moveStack and returns it
Modifies:
this

doMove

public abstract void doMove(M m,
                            String timestamp)
                     throws IllegalMoveException
Parameters:
m - the Move that describes how to modify the Board
timestamp - the time that Move m was made
Throws:
IllegalMoveException - if the move is illegal
Effects:
modifies the current board to rearrange the state of the Pieces according to the given Move, and if that move has an associated time (such as in timed games), logs that time.
Modifies:
this

undoLastMove

public abstract void undoLastMove()
Effects:
undoes the last Move performed on the Board, restoring it to its previous condition before that Move was executed.
Modifies:
this

isGameOver

public boolean isGameOver()
Returns:
true if the game is over; false otherwise

getWinner

public Player getWinner()
Returns:
the Player that has won the game

getGameOverReason

public int getGameOverReason()
Returns:
the termination condition for this game

endGame

public void endGame(int gameOverReason,
                    Player winner)
Terminates the current game, specifying the cause of game termination and the winner.

Parameters:
gameOverReason - the game termination condition. This value should match the value of one of the end- game conditions available for a particular game.
winner - the winner of the game
Effects:
sets the end-game variables on the board to those values specified.
Modifies:
this

addObserver

public void addObserver(BoardObserver bo)
Adds an observer to the list of BoardObservers

Parameters:
bo - the BoardObserver to add to this Board
Effects:
adds bo to the list of observers
Modifies:
this.observers

removeObserver

public void removeObserver(BoardObserver bo)
Removes an observer from the list of BoardObservers.

Parameters:
bo - the BoardObserver to remove from this Board
Effects:
removes bo from the list of observers if bo is present in the list of observers; otherwise nothing
Modifies:
this.observers

notifyObservers

public void notifyObservers()
Updates all observers in the list of observers that the Board has been changed.