|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbluegammon.logic.Board
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
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 |
public static final int POS_BOARD
public static final int POS_OUT
public static final int POS_GUARD
public static final int MAX_POS
Constructor Detail |
public Board()
Method Detail |
public void setStartPositions()
public boolean isWhite(int index)
index
- the board index
public int countPieces(int index)
index
- The board index
public int countPieces(boolean white, int index)
white
- If white or black pieces are to be countedindex
- The index
public void setPieces(boolean white, int index, int pieces)
white
- Piece colorindex
- The board indexpieces
- Number of pieces to setpublic void addPiece(boolean white, int index)
white
- Piece colorindex
- The board index to add a piece topublic void removePiece(boolean white, int index)
white
- Piece colorindex
- The board index to remove a piece frompublic int getPlayerIndex(boolean white, int pos, int step)
white
- Player colorpos
- The position (0-23) for the piecestep
- How many steps from specified position
public int getOpponentIndex(boolean white, int pos, int step)
white
- Opponent colorpos
- The position (0-23) for the piecestep
- How many steps from specified position
public int calculatePiecesLeft(boolean white)
white
- True for checking white pieces, false for black
public int calculatePoints(boolean loserWhite)
loserWhite
- true for white loser, false for black
public int saveBoard(java.io.DataOutputStream dos) throws java.io.IOException
dos
- Output stream to save to
java.io.IOException
public int loadBoard(java.io.DataInputStream dis) throws java.io.IOException
saveBoard
for format.
dis
- Input stream to read from
java.io.IOException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |