bluegammon.logic
Class BoardState

java.lang.Object
  extended bybluegammon.logic.Board
      extended bybluegammon.logic.BoardState

public class BoardState
extends Board

Singleton class holding game logic and state of a game. There can only be one state per device, thus the singleton pattern.

Extends the Board class with player-, turn-, dice- and rule-logic. The BoardState is detached from gui. State reports when pieces are moved etc can be retreived by implementing the BoardStateListener interface and registering this instance in the BoardState.

Author:
Peter Andersson
See Also:
BoardMediator

Field Summary
static int PM_DEST
          Possible move board destination index
static int PM_DICE
          Possible move dice value index
static int PM_SOUR
          Possible move board source index
 
Fields inherited from class bluegammon.logic.Board
MAX_POS, POS_BOARD, POS_GUARD, POS_OUT
 
Method Summary
 boolean areAllPiecesHome(boolean white)
          Checks if there are any pieces outside home - in that case pieces must not be moved off the boards.
 void commitMove(boolean white, int diceValue)
          Commits a move, step (3) of performing a move.
 void consumeDice(int diceIndex)
          Consumes a dice value, called when a player moves a piece.
 int countPossibleMoves(boolean white)
          Returns number of possible moves for current game state.
 int countUndoableMoves()
          Returns number of possible undoable moves
 Player getCurrentPlayer()
          Returns current player.
 int getDiceValue(int diceIndex)
          Returns value of specified dice
static BoardState getInstance()
           
 int[][] getPossibleMoves(boolean white)
          Calculates possible moves and returns array, composed as int[possibleMoveIndex][PM_SOUR | PM_DEST | PM_DICE] where PM_SOUR denotes the source index, PM_DEST denotes the destination index, and PM_DICE denotes the dice value index that is used for the move.
 Player getWaitingPlayer()
          Returns waiting player.
 boolean isCurrentPlayerWhite()
          Returns the color of current player.
 boolean isGameFinished()
          Returns whether the game is finished or not
 boolean isNoneBefore(boolean white, int index)
          Returns true if there are no pieces of specified color before specified index.
 int loadState(java.io.DataInputStream dis)
          Loads state from specified input stream.
 void makeMove(int index, int dest, boolean white)
          Moves a piece of specified color, from specified index to specified destination on board.
 void newDiceValues(int dice1, int dice2)
          Throws dices, and puts new values in the dice value vector.
 int saveState(java.io.DataOutputStream dos)
          Saves current state to specified outputstream.
 void setCurrentPlayer(boolean white)
          Sets the turn to the player having specified color without notifying listener about the change.
 void setGameFinished(boolean b)
          Marks this game as finished
 void setGameListener(BoardStateListener listener)
          Registers a boardstate-listener that will receive events upon state changes.
 void setPlayers(Player player1, Player player2)
          Defines the two backgammon players.
 void setTurn(boolean whiteTurn)
          Sets the turn to the player having specified color and notifies listener about the change.
 int undoLastMove(boolean white)
          Undoes the last move.
 
Methods inherited from class bluegammon.logic.Board
addPiece, calculatePiecesLeft, calculatePoints, countPieces, countPieces, getOpponentIndex, getPlayerIndex, isWhite, loadBoard, removePiece, saveBoard, setPieces, setStartPositions
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PM_SOUR

public static final int PM_SOUR
Possible move board source index

See Also:
Constant Field Values

PM_DEST

public static final int PM_DEST
Possible move board destination index

See Also:
Constant Field Values

PM_DICE

public static final int PM_DICE
Possible move dice value index

See Also:
Constant Field Values
Method Detail

setGameListener

public void setGameListener(BoardStateListener listener)
Registers a boardstate-listener that will receive events upon state changes.

Parameters:
listener - The listener.

setPlayers

public void setPlayers(Player player1,
                       Player player2)
Defines the two backgammon players.

Parameters:
player1 - Player one.
player2 - Player two.

isGameFinished

public boolean isGameFinished()
Returns whether the game is finished or not

Returns:
true if finished, false otherwise

setGameFinished

public void setGameFinished(boolean b)
Marks this game as finished

Parameters:
b - true for finished, false otherwise

isCurrentPlayerWhite

public boolean isCurrentPlayerWhite()
Returns the color of current player.

Returns:
true for white player, false for black player

getCurrentPlayer

public Player getCurrentPlayer()
Returns current player.

Returns:
current player.

getWaitingPlayer

public Player getWaitingPlayer()
Returns waiting player.

Returns:
noncurrent player.

setCurrentPlayer

public void setCurrentPlayer(boolean white)
Sets the turn to the player having specified color without notifying listener about the change.

Parameters:
white - True for white player, false for black player

setTurn

public void setTurn(boolean whiteTurn)
Sets the turn to the player having specified color and notifies listener about the change.

Parameters:
whiteTurn - True for white player, false for black player

makeMove

public void makeMove(int index,
                     int dest,
                     boolean white)
Moves a piece of specified color, from specified index to specified destination on board. Handles specific backgammon rules, i.e. if white moves to an index where one black piece resides, whereas the black piece is put under guard. Step (1) of performing a move.

Parameters:
index - The source index to move from
dest - The destination index to move to
white - The color of the piece to move

consumeDice

public void consumeDice(int diceIndex)
Consumes a dice value, called when a player moves a piece. Populates undoable moves vector and consumes the dice value used for move. Step (2) of performing a move.

Parameters:
diceIndex - The diceindex that was used for the move.

commitMove

public void commitMove(boolean white,
                       int diceValue)
Commits a move, step (3) of performing a move. Updates undoable move count and checks if game is ended because of this move.

Parameters:
white - The color of the pieces whose moves are to be commited.
diceValue - The dicevalue that was used for the move.

undoLastMove

public int undoLastMove(boolean white)
Undoes the last move. Does nothing if there are no moves to undo.

Parameters:
white - The color of the player that undoes
Returns:
the source index of the undoed move.

getPossibleMoves

public int[][] getPossibleMoves(boolean white)
Calculates possible moves and returns array, composed as int[possibleMoveIndex][PM_SOUR | PM_DEST | PM_DICE] where PM_SOUR denotes the source index, PM_DEST denotes the destination index, and PM_DICE denotes the dice value index that is used for the move.

The array is sorted depending on player color, allowing consistent movement of source and destination cursors by simply increasing or decreasing the move index.

To get number of possible moves, i.e. the length of the array, see countPossibleMoves()

This method caches results of possible moves calculation for performance.

Parameters:
white - The color of the player
Returns:
An array with possible moves, containing source indices, destination indices, and the dice index used for specific move.

countPossibleMoves

public int countPossibleMoves(boolean white)
Returns number of possible moves for current game state. See getPossibleMoves.

This method caches results of possible moves calculation for performance.

Parameters:
white - The color of the player
Returns:
Number of possible moves.

newDiceValues

public void newDiceValues(int dice1,
                          int dice2)
Throws dices, and puts new values in the dice value vector. If both dices are of same value, the dice value vector is expanded to contain four values. The dice value vector is sorted ascending.

Parameters:
dice1 - value of dice one
dice2 - value of dice two

getDiceValue

public int getDiceValue(int diceIndex)
Returns value of specified dice

Parameters:
diceIndex - the dice, 0 or 1
Returns:
a value between 1 and 6

countUndoableMoves

public int countUndoableMoves()
Returns number of possible undoable moves

Returns:
number of possible undoable moves

isNoneBefore

public boolean isNoneBefore(boolean white,
                            int index)
Returns true if there are no pieces of specified color before specified index.

Parameters:
white - True for white, false for black
index - The index
Returns:
true if no pieces before, false otherwise

areAllPiecesHome

public boolean areAllPiecesHome(boolean white)
Checks if there are any pieces outside home - in that case pieces must not be moved off the boards.

Parameters:
white - true to check if all white is home, false to check if all black is home.
Returns:
true if no pieces outside home, false otherwise.

loadState

public int loadState(java.io.DataInputStream dis)
              throws java.io.IOException
Loads state from specified input stream. See saveState for format.

Parameters:
dis - the input stream
Returns:
size of data in bytes
Throws:
java.io.IOException

saveState

public int saveState(java.io.DataOutputStream dos)
              throws java.io.IOException
Saves current state to specified outputstream. Format: boolean m_chooseTurns boolean m_diceThrown boolean whiteTurn; int m_undoableMoveCount int[][] m_undoableMoves[5][4] int m_diceVals int[] m_dice[4] Board this

Parameters:
dos - the output stream
Returns:
size of data in bytes
Throws:
java.io.IOException

getInstance

public static BoardState getInstance()