bluegammon.logic
Class Board

java.lang.Object
  extended bybluegammon.logic.Board
Direct Known Subclasses:
BoardState

public class Board
extends java.lang.Object

Representing a game state and basic logic for modifying and querying. The methods are based upon a structure consisting of board index, white positions, and black positions. The indices and the white positions are identical, the black positions are inversed indices.

If a white piece is moved two steps from black home, it is moved from index 0 to index 2, from white position 0 to white position 2. If a black piece is moved two steps from white home, it is moved from index 23 to index 21, from black position 0 to black position 2.

From position's point of view, position of pieces are increased. From index' point of view, indices for white are increased, and indices for black are decreased as the game continues.


 index wPos bPos                 index wPos bPos
                        24
     0    0   23 -B---     -W--- 23    23     0
     1    1   22 -L-H-     -H-H- 22    22     1
     2    2   21 -A-O-     -I-O- 21    21     2
     3    3   20 -C-M-     -T-M- 20    20     3
     4    4   19 -K-E-     -E-E- 19    19     4
     5    5   18 -----     ----- 18    18     5 
                 =======25======
     6    6   17 -----     ----- 17    17     6     
     7    7   16 -----     ----- 16    16     7
     8    8   15 -----     ----- 15    15     8
     9    9   14 -----     ----- 14    14     9
     10   10  13 -----     ----- 13    13     10
     11   11  12 -----     ----- 12    12     11
 

This class is used by BoardState and BoardCanvas

Author:
Peter Andersson

Field Summary
static int MAX_POS
          Maximum index plus one
static int POS_BOARD
          Maximum index that is on the diagonals of a backgammon board
static int POS_GUARD
          Index of a piece that is on guard
static int POS_OUT
          Index of a piece that is out
 
Constructor Summary
Board()
           
 
Method Summary
 void addPiece(boolean white, int index)
          Adds a piece on specified index
 int calculatePiecesLeft(boolean white)
          Returns number of pieces still on board.
 int calculatePoints(boolean loserWhite)
          Return point per piece in current state.
 int countPieces(boolean white, int index)
          Returns number of pieces on specified index, for specified color.
 int countPieces(int index)
          Returns number of pieces on specified index.
 int getOpponentIndex(boolean white, int pos, int step)
          Returns board index of opponent
 int getPlayerIndex(boolean white, int pos, int step)
          Returns board index of player
 boolean isWhite(int index)
          Returns if there are any white pieces on specified index
 int loadBoard(java.io.DataInputStream dis)
          Reads the board status from specified data output stream.
 void removePiece(boolean white, int index)
          Removes a piece from specified index
 int saveBoard(java.io.DataOutputStream dos)
          Saves the board status to specified data output stream.
 void setPieces(boolean white, int index, int pieces)
          Sets number of pieces in specified index
 void setStartPositions()
          Initializes the board to start positions
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POS_BOARD

public static final int POS_BOARD
Maximum index that is on the diagonals of a backgammon board

See Also:
Constant Field Values

POS_OUT

public static final int POS_OUT
Index of a piece that is out

See Also:
Constant Field Values

POS_GUARD

public static final int POS_GUARD
Index of a piece that is on guard

See Also:
Constant Field Values

MAX_POS

public static final int MAX_POS
Maximum index plus one

See Also:
Constant Field Values
Constructor Detail

Board

public Board()
Method Detail

setStartPositions

public void setStartPositions()
Initializes the board to start positions


isWhite

public boolean isWhite(int index)
Returns if there are any white pieces on specified index

Parameters:
index - the board index
Returns:
true if there are any white pieces on the specified index

countPieces

public int countPieces(int index)
Returns number of pieces on specified index. 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.

countPieces

public int countPieces(boolean white,
                       int index)
Returns number of pieces on specified index, for specified color.

Parameters:
white - If white or black pieces are to be counted
index - The index
Returns:
Number of pieces on specified index of specified color

setPieces

public void setPieces(boolean white,
                      int index,
                      int pieces)
Sets number of pieces in specified index

Parameters:
white - Piece color
index - The board index
pieces - Number of pieces to set

addPiece

public void addPiece(boolean white,
                     int index)
Adds a piece on specified index

Parameters:
white - Piece color
index - The board index to add a piece to

removePiece

public void removePiece(boolean white,
                        int index)
Removes a piece from specified index

Parameters:
white - Piece color
index - The board index to remove a piece from

getPlayerIndex

public int getPlayerIndex(boolean white,
                          int pos,
                          int step)
Returns board index of player

Parameters:
white - Player color
pos - The position (0-23) for the piece
step - How many steps from specified position
Returns:
The index

getOpponentIndex

public int getOpponentIndex(boolean white,
                            int pos,
                            int step)
Returns board index of opponent

Parameters:
white - Opponent color
pos - The position (0-23) for the piece
step - How many steps from specified position
Returns:
The index

calculatePiecesLeft

public int calculatePiecesLeft(boolean white)
Returns number of pieces still on board.

Parameters:
white - True for checking white pieces, false for black
Returns:
Number of pieces on board, not being out.

calculatePoints

public int calculatePoints(boolean loserWhite)
Return point per piece in current state.

Parameters:
loserWhite - true for white loser, false for black
Returns:
Number of points

saveBoard

public int saveBoard(java.io.DataOutputStream dos)
              throws java.io.IOException
Saves the board status to specified data output stream. Format: int[] BLACK[MAX_POS]; int[] WHITE[MAX_POS];

Parameters:
dos - Output stream to save to
Returns:
size of data in bytes
Throws:
java.io.IOException

loadBoard

public int loadBoard(java.io.DataInputStream dis)
              throws java.io.IOException
Reads the board status from specified data output stream. See saveBoard for format.

Parameters:
dis - Input stream to read from
Returns:
size of data in bytes
Throws:
java.io.IOException