bluegammon.logic
Class BoardMediator

java.lang.Object
  extended bybluegammon.logic.BoardMediator
All Implemented Interfaces:
BoardStateListener

public class BoardMediator
extends java.lang.Object
implements BoardStateListener

The BoardMediator class is active during a backgammon game. It takes care of coordination between BoardState and BoardCanvas i.e. between the gui and the logic. BoardCanvas is detached from any backgammon-logic - this connection is made by this class by implementing BoardStateListener to receive events upon board state changes. The BoardMediator then calls the BoardCanvas to update the gui when state changes.

In order to save the game at any time, but still have animations, the BoardState differs from the BoardCanvas: when the state is changed (e.g. a move is made), the BoardState is updated directly. The BoardCanvas has intermittent states; e.g. during an animation of a piece movement, one piece is "missing" from BoardCanvas board until the piece is put down.

This class is a simple coordinater, thus it does not have any state. The BoardMediator is accessed statically.

Author:
Peter Andersson
See Also:
BoardCanvas, BoardState

Method Summary
static void commitTurn()
          Called from interaction, commits players' moves.
static int countPossibleMoves()
          Returns number of possible moves based on current board state for player with current turn.
static int countStatePieces(int index)
          Returns number of pieces on specified index in current state.
static int countUndoableMoves()
          Returns number of possible undoable moves
static void exitGame(int reason)
          Called from interaction, exits current game.
static void forceGameFinished()
          Forces the game state to be finished.
 void gameFinished(boolean whiteWinner, int loserPiecesLeft, int points)
          Called from BoardState when a game is finished
static BoardCanvas getCanvas()
          Returns the boardcanvas singleton instance.
static Player getCurrentPlayer()
          Returns the player whos holding the turn, i.e. can make moves.
static int getDiceValue(int index)
          Returns value of specified dice
static LocalPlayer getLocalPlayer()
          Returns the one LocalPlayer.
static Player getOpponentPlayer()
          Returns opponent player.
static int[][] getPossibleMoves()
          Returns possible moves based on current board state.
static void init(Player p1, Player p2, boolean resumed)
          Initiates a new or resumed game with specified players.
static boolean isCurrentPlayerWhite()
          Returns the color of current player.
static boolean isGameFinished()
          Returns whether the game is finished or not
static boolean isRemoteTurn()
          Returns whether the current turn belongs to a remote player or not.
static void loadGame(java.io.InputStream is, boolean flipColors)
          Loads a saved game from specified input stream.
static void lostRemoteConnection(java.io.IOException e)
          Called from IO framework remote connection is lost in a remote game.
static void makePlayerMove(int possibleMoveIndex)
          Called from interaction, moves a player piece.
static int newDiceValue()
          Returns a new dice value, a random integer between 1 and 6.
static void newTurn(boolean whiteTurn)
          Called from interaction and logic, changes turn.
 void pieceMoved(boolean white, int from, int to)
          Called from BoardState whenever a piece is moved.
static int saveGame(java.io.OutputStream os)
          Saves current game to specified output stream.
static void showMessage(char[] mess)
          Called when remote player sent a message
static void shutdown()
          Shuts the BoardMediator down.
static void startup()
          Starts the BoardMediator.
 void turnChange(boolean whiteTurn)
          Called from BoardState when turn changes.
 void undoAdded(int undos, int diceValue)
          Called from BoardState when a possible undo has been added
static void undoLastMove()
          Called from interaction, undoes the last move.
 void undoPerformed(int undos, int diceValue)
          Called from BoardState when an undo is performed.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

startup

public static void startup()
Starts the BoardMediator.


init

public static void init(Player p1,
                        Player p2,
                        boolean resumed)
Initiates a new or resumed game with specified players.

Parameters:
p1 - Player 1.
p2 - Player 2.
resumed - True if resumed, false if new game.

shutdown

public static void shutdown()
Shuts the BoardMediator down.


makePlayerMove

public static void makePlayerMove(int possibleMoveIndex)
Called from interaction, moves a player piece.

Parameters:
possibleMoveIndex - How to move according to current BoardState

undoLastMove

public static void undoLastMove()
Called from interaction, undoes the last move. Does nothing if there are no moves to undo.


commitTurn

public static void commitTurn()
Called from interaction, commits players' moves.


newTurn

public static void newTurn(boolean whiteTurn)
Called from interaction and logic, changes turn.

Parameters:
whiteTurn - true if turn changes to white player, false otherwise.

exitGame

public static void exitGame(int reason)
Called from interaction, exits current game.

Parameters:
reason - An integer denoting the reason for quitting, one of PlayerListener.LOCAL_QUIT, PlayerListener.REMOTE_QUIT, PlayerListener.LOCAL_GIVE_UP, PlayerListener.REMOTE_GIVE_UP

lostRemoteConnection

public static void lostRemoteConnection(java.io.IOException e)
Called from IO framework remote connection is lost in a remote game.

Parameters:
e - The exception that was the reason of losing connection.

showMessage

public static void showMessage(char[] mess)
Called when remote player sent a message

Parameters:
mess - the message.

getCurrentPlayer

public static Player getCurrentPlayer()
Returns the player whos holding the turn, i.e. can make moves. This method returns null if current player has not yet been chosen, meaning that the first dice roll is still ongoing.

Returns:
The current player or null.

getOpponentPlayer

public static Player getOpponentPlayer()
Returns opponent player. In case of local game, this method returns the player currently waiting for his or her turn. In a remote game, it returns the remote player.

Returns:
The opponent.

getLocalPlayer

public static LocalPlayer getLocalPlayer()
Returns the one LocalPlayer. If the game is local, arbitrary LocalPlayer is returned. This method only makes sense in remote games.

Returns:
The LocalPlayer in the game.

isRemoteTurn

public static boolean isRemoteTurn()
Returns whether the current turn belongs to a remote player or not.

Returns:
true if current turn is a remote player, false otherwise.

getPossibleMoves

public static int[][] getPossibleMoves()
Returns possible moves based on current board state.

Returns:
Array with possible moves.
See Also:
BoardState.getPossibleMoves(boolean)

countPossibleMoves

public static int countPossibleMoves()
Returns number of possible moves based on current board state for player with current turn. If current turn is not yet resolved, zero is returned.

Returns:
number of possible moves.

isGameFinished

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

Returns:
true if finished, false otherwise

forceGameFinished

public static void forceGameFinished()
Forces the game state to be finished.


countUndoableMoves

public static int countUndoableMoves()
Returns number of possible undoable moves

Returns:
number of possible undoable moves

newDiceValue

public static int newDiceValue()
Returns a new dice value, a random integer between 1 and 6.

Returns:
A new dice value between 1 and 6.

getDiceValue

public static int getDiceValue(int index)
Returns value of specified dice

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

countStatePieces

public static int countStatePieces(int index)
Returns number of pieces on specified index in current state. If there are white pieces, number of white pieces are returned. If there are black pieces, number of black pieces are returned. Otherwise zero is returned.

Parameters:
index - The board index
Returns:
Number of pieces on index.

getCanvas

public static BoardCanvas getCanvas()
Returns the boardcanvas singleton instance.

Returns:
The boardcanvas singleton instance.

loadGame

public static void loadGame(java.io.InputStream is,
                            boolean flipColors)
                     throws java.io.IOException
Loads a saved game from specified input stream. If the game is a remote game, the colors of the players are set as specified in the data stream. If the data is loaded from a remote device (i.e. the opponent), the player colors need to be flipped according to what is specified in saved data.

Parameters:
is - The input stream to load saved game data from.
flipColors - Flag indicating if the colors of the players should be flipped according to what is specified in the saved game data.
Throws:
java.io.IOException - If error occurs when loading data.

saveGame

public static int saveGame(java.io.OutputStream os)
                    throws java.io.IOException
Saves current game to specified output stream.

Parameters:
os - The output stream to write saved game data to.
Returns:
Number of bytes written to output stream.
Throws:
java.io.IOException - If error occurs when writing saved game data.

isCurrentPlayerWhite

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

Returns:
true for white player, false for black player

turnChange

public void turnChange(boolean whiteTurn)
Description copied from interface: BoardStateListener
Called from BoardState when turn changes.

Specified by:
turnChange in interface BoardStateListener
Parameters:
whiteTurn - true if white turn, false if black.

pieceMoved

public void pieceMoved(boolean white,
                       int from,
                       int to)
Description copied from interface: BoardStateListener
Called from BoardState whenever a piece is moved.

Specified by:
pieceMoved in interface BoardStateListener
Parameters:
white - true if white piece, false otherwise
from - piece source index
to - piece destination index

undoAdded

public void undoAdded(int undos,
                      int diceValue)
Description copied from interface: BoardStateListener
Called from BoardState when a possible undo has been added

Specified by:
undoAdded in interface BoardStateListener
Parameters:
undos - Number of current undos
diceValue - The dice value that can be used for undo (1-6)

undoPerformed

public void undoPerformed(int undos,
                          int diceValue)
Description copied from interface: BoardStateListener
Called from BoardState when an undo is performed.

Specified by:
undoPerformed in interface BoardStateListener
Parameters:
undos - Number of current undos
diceValue - The dice value that was used for undo (1-6)

gameFinished

public void gameFinished(boolean whiteWinner,
                         int loserPiecesLeft,
                         int points)
Description copied from interface: BoardStateListener
Called from BoardState when a game is finished

Specified by:
gameFinished in interface BoardStateListener
Parameters:
whiteWinner - True if white player wins, false if black player wins
loserPiecesLeft - Total number of loser pieces left
points - Point per piece