org.pesullivan.game.mill.engine
Class MillBoard

java.lang.Object
  extended by org.pesullivan.game.mill.engine.MillBoard
All Implemented Interfaces:
Board

public final class MillBoard
extends java.lang.Object
implements Board

A Nine Men's Morris implementation of Board.


Field Summary
static int AXIS_A
          Identifies axis A of a coordinate system that locates spots on the board.
static int AXIS_B
          Identifies axis B of a coordinate system that locates spots on the board.
static int MAX_SPOT_COORDINATE
          Bounds of a square cartesian coordinate system that locates spots on the board.
static int N_MAX_MEN_OUT
          Maximum number of men that can be taken from one side.
static int N_MEN_PER_SIDE
          Number of men per side.
static int N_ROWS
          Number of Mill board row lines.
static int N_SPOTS
          Number of Mill board row intersecions.
static int N_SPOTS_PER_ROW
          Number of spots in a mill row.
static int NO_SIDE
          Value signifying the absence of a player side.
static int NO_SPOT
          Value signifying the absence of a playing spot.
static int ROW_SPOT_0
          Identifies the first of 3 spots in a row.
static int ROW_SPOT_1
          Identifies the middle of 3 spots in a row.
static int ROW_SPOT_2
          Identifies the last of 3 spots in a row.
 
Method Summary
 Board copy()
          Return a copy (ignoring move iterator state).
 int getActiveSide()
          Get an index identifying the active side (whose turn it is).
 int getAlteredLevelOfPlay(int level)
          Return the number of turns the Game engine should look ahead.
 int getEmptyNeighborCount(int spot)
           
 Move getFirstMove()
          Reset the board's move iterator and return the first move.
 int getGoodEnoughScore(int plannedScore, int levelOfPlay)
          Return a score which is good enough that a player need look no farther to see how much better it gets.
 Move getLastMoveExecuted()
          Return the last move that was executed.
 int getMenLeft()
          Return the number of men left to be placed by the active side.
 int getMenLeft(int side)
          Return the number of men left to be placed by the given side.
 int getMenOut(int side)
          Return the number of men taken from the given side.
static MillMove getMove(java.lang.String positionText)
          Return the move described by positionText.
 Board getNewBoard()
          Return a new board ready for a new game.
 Move getNextMove()
          Return the next move based on the board's move iterator.
static java.lang.String getPositionText(int spot)
          Return text notation for the given move spot.
 int getProgress()
          Return a measure of progress towards the game conclusion.
 int getRepetitionPenalty(int nRepeats)
          Get a score penalty for repeating a position so many times.
 int getScore()
          Express the position's merits as a scalar relative score.
static int getSpot(int coordinateA, int coordinateB)
          Return the intersection spot index at coordinate a,b.
static int getSpot(java.lang.String positionText)
          Get the spot indicated by the given textPosition.
static int getSpotCoordinate(int spot, int axis)
          Return an a or b cartesian coordinate for the given board spot.
static int getSpotCoordinate(int row, int spot, int axis)
          Return an a or b cartesian coordinate for the given row spot.
 int getSpotSide(int spot)
          Get the side of the piece currently occupying the given spot.
 int getSpotSide(java.lang.String positionText)
          Get the side of the piece currently occupying the given spot.
 Move getUndefinedMove()
          Return a new move which has not yet been defined.
 void go(Move mv)
          Execute the given move.
 boolean isFlying()
          Return true if the current move is a flying move.
 boolean isGameOver()
          Return true if the active side has no move.
 boolean isPlacing()
          Return true if the current move is a placing move.
 boolean isSame(Board board)
          Compare board states (ignoring move iterator state).
 boolean isSliding()
          Return true if the current move is a sliding move.
 boolean isTaking()
          Return true if the current move is a taking move.
 boolean isTakingAny()
          Return true if the player can take even from a mill.
 boolean isTranslating()
          Return true if the current move is a sliding or flying move.
 boolean isValidMove(MillMove move)
          Return true if the given move is valid given the board state.
 boolean isWon()
          Return true if either side has won.
 boolean isWon(int score)
          Return true if the given score means that either side won.
 void setFrom(Board board)
          Set the board state to that of board, (ignoring move iterator state).
 void setFromJSON(org.json.simple.JSONObject jsonBoard)
          Set this board based on the parameter.
 org.json.simple.JSONObject toJSONObject()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_SPOT

public static final int NO_SPOT
Value signifying the absence of a playing spot. A playing spot is one of 24 positions on the board onto which pieces are placed.

See Also:
Constant Field Values

NO_SIDE

public static final int NO_SIDE
Value signifying the absence of a player side. There are two players so player sides can be either 0 or 1.

See Also:
Constant Field Values

MAX_SPOT_COORDINATE

public static final int MAX_SPOT_COORDINATE
Bounds of a square cartesian coordinate system that locates spots on the board. The 24 playing spots occupy a subset of the 6X6 matrix.

See Also:
Constant Field Values

AXIS_A

public static final int AXIS_A
Identifies axis A of a coordinate system that locates spots on the board.

See Also:
Constant Field Values

AXIS_B

public static final int AXIS_B
Identifies axis B of a coordinate system that locates spots on the board.

See Also:
Constant Field Values

ROW_SPOT_0

public static final int ROW_SPOT_0
Identifies the first of 3 spots in a row.

See Also:
Constant Field Values

ROW_SPOT_1

public static final int ROW_SPOT_1
Identifies the middle of 3 spots in a row.

See Also:
Constant Field Values

ROW_SPOT_2

public static final int ROW_SPOT_2
Identifies the last of 3 spots in a row.

See Also:
Constant Field Values

N_ROWS

public static final int N_ROWS
Number of Mill board row lines.

See Also:
Constant Field Values

N_SPOTS

public static final int N_SPOTS
Number of Mill board row intersecions.

See Also:
Constant Field Values

N_SPOTS_PER_ROW

public static final int N_SPOTS_PER_ROW
Number of spots in a mill row.

See Also:
Constant Field Values

N_MEN_PER_SIDE

public static final int N_MEN_PER_SIDE
Number of men per side. This number is in the name Nine Men's Morris.

See Also:
Constant Field Values

N_MAX_MEN_OUT

public static final int N_MAX_MEN_OUT
Maximum number of men that can be taken from one side. When this many men are taken the game is over.

See Also:
Constant Field Values
Method Detail

setFromJSON

public void setFromJSON(org.json.simple.JSONObject jsonBoard)
Description copied from interface: Board
Set this board based on the parameter.

Specified by:
setFromJSON in interface Board

toJSONObject

public org.json.simple.JSONObject toJSONObject()
Specified by:
toJSONObject in interface Board
Returns:
this board as a JSONObject.

getNewBoard

public Board getNewBoard()
Description copied from interface: Board
Return a new board ready for a new game.

Specified by:
getNewBoard in interface Board

getUndefinedMove

public Move getUndefinedMove()
Description copied from interface: Board
Return a new move which has not yet been defined.

Specified by:
getUndefinedMove in interface Board

copy

public Board copy()
Description copied from interface: Board
Return a copy (ignoring move iterator state).

Specified by:
copy in interface Board

setFrom

public void setFrom(Board board)
Description copied from interface: Board
Set the board state to that of board, (ignoring move iterator state).

Specified by:
setFrom in interface Board
Parameters:
board - the original board to copy.

isSame

public boolean isSame(Board board)
Description copied from interface: Board
Compare board states (ignoring move iterator state).

Specified by:
isSame in interface Board
Returns:
true if the board states are identical.

getActiveSide

public int getActiveSide()
Description copied from interface: Board
Get an index identifying the active side (whose turn it is).

Specified by:
getActiveSide in interface Board
Returns:
index 0 or 1.

getScore

public int getScore()
Description copied from interface: Board
Express the position's merits as a scalar relative score. A score of zero indicates that both sides have equal merit, while a score greater than zero indicates that the active side is winning. The score is calculated with respect to the current board state without looking ahead. You can improve Board's play by finding optimal weights (as with genetic algorithms) for getScore's criteria. Humans surpass computers in performance of this method.

Specified by:
getScore in interface Board
Returns:
the score from the perspective of the active side.

getGoodEnoughScore

public int getGoodEnoughScore(int plannedScore,
                              int levelOfPlay)
Description copied from interface: Board
Return a score which is good enough that a player need look no farther to see how much better it gets. The Game engine optimizes by pruning move search tree branches using this score. The returned score should be just above what is probable based on plannedScore for optimal performance. Towards the end game the returned score should be far higher than what is probable. levelOfPlay is optionally used to set expectations for what is good enough.

Specified by:
getGoodEnoughScore in interface Board
Parameters:
plannedScore - expectations based on a previous search.
levelOfPlay - level of play requested by the Game engine.
Returns:
the score from the perspective of the side with plannedScore.

getRepetitionPenalty

public int getRepetitionPenalty(int nRepeats)
Description copied from interface: Board
Get a score penalty for repeating a position so many times. The Game engine uses this penalty to encourage the choice of a different position than one that has already occurred nRepeats times. This encourages variety of play and draw avoidance at the expense of good judgment.

Specified by:
getRepetitionPenalty in interface Board
Parameters:
nRepeats - the number of times this position has already occurred during this game.
Returns:
a positive score penalty.

getFirstMove

public Move getFirstMove()
Description copied from interface: Board
Reset the board's move iterator and return the first move. The board's move iterator iterates all moves that are possible from the current position. Each move represents the smallest discrete decision a player can make, and each turn is comprised of 1 or more moves. Even a skipped turn is described by a non-null Move. For better performance return the most promising moves first.

Specified by:
getFirstMove in interface Board
Returns:
a readonly move or null if the game is over.

getNextMove

public Move getNextMove()
Description copied from interface: Board
Return the next move based on the board's move iterator.

Specified by:
getNextMove in interface Board
Returns:
a readonly move or null if the board's move iterator is done.

getLastMoveExecuted

public Move getLastMoveExecuted()
Description copied from interface: Board
Return the last move that was executed.

Specified by:
getLastMoveExecuted in interface Board
Returns:
a readonly move or null if it is a new game.

go

public void go(Move mv)
Description copied from interface: Board
Execute the given move. If the active side can't go again after the move is executed, the side is changed.

Specified by:
go in interface Board
See Also:
Board.getFirstMove(), Board.getNextMove()

getAlteredLevelOfPlay

public int getAlteredLevelOfPlay(int level)
Description copied from interface: Board
Return the number of turns the Game engine should look ahead. Each turn has one move and possibly several "go again" moves, so the number of moves looked ahead may exceed the level of play. Even though the game engine may request a certain level of play, getAlteredLevelOfPlay() may arbitrarily override it at various stages of the game for performance reasons.

Specified by:
getAlteredLevelOfPlay in interface Board
Parameters:
level - the level of play requested.
Returns:
an integer greater than 0.

isWon

public boolean isWon(int score)
Description copied from interface: Board
Return true if the given score means that either side won.

Specified by:
isWon in interface Board

getProgress

public int getProgress()
Description copied from interface: Board
Return a measure of progress towards the game conclusion. The Game engine uses this measure to encourage choice of a move which concludes the game sooner. Note that getGoodEnoughScore may subvert this method by pruning (and thus ignoring) moves which conclude the game sooner.

Specified by:
getProgress in interface Board
Returns:
a number that increases as the game progresses.

getEmptyNeighborCount

public int getEmptyNeighborCount(int spot)
Returns:
number of empty neighbours adjacent to the given spot.

getPositionText

public static java.lang.String getPositionText(int spot)
Return text notation for the given move spot.

See Also:
getMove(String positionText)

getMove

public static MillMove getMove(java.lang.String positionText)
Return the move described by positionText.

Parameters:
positionText - text notation describing a move. Position text is of the form {square}{point}[{square}{point}] where: {square} is X, Y or Z starting with the outermost square and {point} is 1,2...8 starting at the upper left and going clockwise.
Returns:
the move if found otherwise null.

isGameOver

public boolean isGameOver()
Return true if the active side has no move. This routine leaves the move iterator in an undefined state.


isWon

public boolean isWon()
Return true if either side has won.


getMenLeft

public int getMenLeft()
Return the number of men left to be placed by the active side.


getMenLeft

public int getMenLeft(int side)
Return the number of men left to be placed by the given side.


getSpotSide

public int getSpotSide(int spot)
Get the side of the piece currently occupying the given spot.

Parameters:
spot - 0 based index of the given board spot.
Returns:
side index (0 or 1) or NO_SIDE if the spot is unoccupied.
See Also:
N_SPOTS

getSpotSide

public int getSpotSide(java.lang.String positionText)
Get the side of the piece currently occupying the given spot.

Parameters:
positionText - text notation describing a spot.
Returns:
side index (0 or 1) or NO_SIDE if the spot is unoccupied.
See Also:
getMove(String positionText)

getSpot

public static int getSpot(java.lang.String positionText)
Get the spot indicated by the given textPosition.

Parameters:
positionText - text notation describing a spot.
Returns:
spot index or NO_SPOT if positionText is invalid.
See Also:
getMove(String positionText)

getSpotCoordinate

public static int getSpotCoordinate(int spot,
                                    int axis)
Return an a or b cartesian coordinate for the given board spot. Spots are formed by the intersection of two board rows and they occupy a grid bounded by (0,0) and (6,6). There are three spots per row. UI clients may use spot coordinates to assign locations to various board spots on screen. A,B coordinate mapping to the screen is left up to the UI client.

Parameters:
spot - 0 based index identifying a spot on the board.
axis - AXIS_A or AXIS_B.
Returns:
a scalar coordinate.
See Also:
N_SPOTS, AXIS_A, AXIS_B

getSpotCoordinate

public static int getSpotCoordinate(int row,
                                    int spot,
                                    int axis)
Return an a or b cartesian coordinate for the given row spot.

Parameters:
row - 0 based index identifying a row of 3 spots.
spot - one of ROW_SPOT_0, ROW_SPOT_1, ROW_SPOT_2.
axis - AXIS_A or AXIS_B.
Returns:
a scalar coordinate.
See Also:
getSpotCoordinate(int spot, int axis), N_ROWS, ROW_SPOT_0, ROW_SPOT_1, ROW_SPOT_2, AXIS_A, AXIS_B

getSpot

public static int getSpot(int coordinateA,
                          int coordinateB)
Return the intersection spot index at coordinate a,b.

Parameters:
coordinateA - coordinate on the board A axis.
coordinateB - coordinate on the board B axis.
Returns:
spot index or NO_SPOT if there is no spot at a,b.

isValidMove

public boolean isValidMove(MillMove move)
Return true if the given move is valid given the board state. This routine leaves the move iterator in an undefined state.


isTaking

public boolean isTaking()
Return true if the current move is a taking move.


isTakingAny

public boolean isTakingAny()
Return true if the player can take even from a mill.


isTranslating

public boolean isTranslating()
Return true if the current move is a sliding or flying move.


isPlacing

public boolean isPlacing()
Return true if the current move is a placing move.


isSliding

public boolean isSliding()
Return true if the current move is a sliding move.


isFlying

public boolean isFlying()
Return true if the current move is a flying move.


getMenOut

public int getMenOut(int side)
Return the number of men taken from the given side.