org.pesullivan.game.game42
Class Game

java.lang.Object
  extended by org.pesullivan.game.game42.Game
Direct Known Subclasses:
MillGame

public class Game
extends java.lang.Object

Supplies all business logic required by any deterministic 2 player strategy game that implements Board. Only one instance of Game is required for the duration of a game, and its methods deal with all positions that need to be considered. Optimal moves are planned by looking ahead a given number of turns depending on the level of play selected. A history of each board position is maintained so that users can undo and redo moves.

See Also:
Board

Constructor Summary
Game(Board board)
          Construct a Game object using Board to supply all game specific middle tier behavior.
 
Method Summary
 void addPlan(Plan plan)
           
 boolean canRedo()
          Return true if undo() was called without a matching redo().
 boolean canUndo()
          Return true if this is not the first turn.
 boolean cpuWon()
          Return true if the CPU has won the game.
 boolean cpuWon(int score)
          Return true if the given score relative to the active side, is a winning score for the CPU.
 boolean getAbortPlanningThreadPending()
          If true is returned this means that any thread running the expensive getTurnMoves() will abort.
 Board getBoard()
          Return a readonly board that holds the current position.
 Board getBoard(int nBoardInHistory)
          Return a readonly board at the given historical position.
 int getCPUSide()
          Get the side of the board taken by the CPU.
 int getLevelOfPlay()
          Get the 1 based level of difficulty of play.
 int getNCurrentBoardInHistory()
          Return the 0 based index of the current board in the history of positions.
 int getPredictedScoreFromCache()
          Return the score predicted by any cached Plan that may exist for the current position and side.
 java.util.List<Move> getTurnMoves()
          Get optimal moves for the current turn.
 Plan getTurnPlan()
          Get optimal plan for the current turn.
 void go(Move move)
          Execute the given move.
 boolean isCPUsMove()
          Return true if it's the CPU's move.
 void newGame()
          Begin a new game.
 void redo()
          Reverse the last undo().
 void setAbortPlanningThreadPending(boolean abortPlanningThreadPending)
          Set a flag that the expensive getTurnMoves() inspects periodically to decide whether it needs to abort.
 void setCPUSide(int side)
          Set the side of the board taken by the CPU.
 void setFromJSONString(java.lang.String strJSON)
           
 void setLevelOfPlay(int level)
          Set the the level of difficulty of play.
 java.lang.String toJSONString()
           
 void undo()
          Return to the position prior to the last move.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Game

public Game(Board board)
Construct a Game object using Board to supply all game specific middle tier behavior.

Parameters:
board - holds the initial starting position of the Game.
Method Detail

getNCurrentBoardInHistory

public int getNCurrentBoardInHistory()
Return the 0 based index of the current board in the history of positions.


setAbortPlanningThreadPending

public void setAbortPlanningThreadPending(boolean abortPlanningThreadPending)
Set a flag that the expensive getTurnMoves() inspects periodically to decide whether it needs to abort.


getAbortPlanningThreadPending

public boolean getAbortPlanningThreadPending()
If true is returned this means that any thread running the expensive getTurnMoves() will abort.


getBoard

public Board getBoard()
Return a readonly board that holds the current position.


getBoard

public Board getBoard(int nBoardInHistory)
Return a readonly board at the given historical position. This history records the board state after each move.

Parameters:
nBoardInHistory - 0 based board history index.

getLevelOfPlay

public int getLevelOfPlay()
Get the 1 based level of difficulty of play. In general a level of difficulty of 1 means that the game should look ahead 0 turns, 2 means look ahead 1 turn and so on.


setLevelOfPlay

public void setLevelOfPlay(int level)
Set the the level of difficulty of play. This method influences the number of turns getTurnMoves() should look ahead. In general a level of difficulty of 1 means that the game should look ahead 0 turns, 2 means look ahead 1 turn and so on. The plan cache is cleared as a result of this call since the cache was populated using an obsolete levelOfPlay.

Parameters:
level - 1 based level of difficulty.

setCPUSide

public void setCPUSide(int side)
Set the side of the board taken by the CPU.

Parameters:
side - 0 or 1.

getCPUSide

public int getCPUSide()
Get the side of the board taken by the CPU.


isCPUsMove

public boolean isCPUsMove()
Return true if it's the CPU's move.


getTurnPlan

public Plan getTurnPlan()
Get optimal plan for the current turn. Game determines the number of turns to look ahead by calling Board.getAlteredLevelOfPlay(int level), passing the level obtained from Game getLevelOfPlay(). This method delegates to class Turn to do the work of planning optimal moves. Class Turn is protected within this package. After calculating the moves, the resulting Plan is added to a cache for possible reuse. The Plan object should be returned to remote clients which may then add the plan to their own local copy of the game object. The plan object contains information which will speed up the process of calculating subsequent moves.

Returns:
optimal Plans to complete the turn, or null if it was aborted.
See Also:
Plan

getTurnMoves

public java.util.List<Move> getTurnMoves()
Get optimal moves for the current turn. Game determines the number of turns to look ahead by calling Board.getAlteredLevelOfPlay(int level), passing the level obtained from Game getLevelOfPlay(). This method delegates to class Turn to do the work of planning optimal moves. Class Turn is protected within this package. After calculating the moves, the resulting Plan is added to a cache for possible reuse.

Returns:
optimal Moves to complete the turn, or null if it was aborted.
See Also:
Move

newGame

public void newGame()
Begin a new game. The game is returned to the board position initially passed to Game(Board board), and the undo/redo cache is cleared.


getPredictedScoreFromCache

public int getPredictedScoreFromCache()
Return the score predicted by any cached Plan that may exist for the current position and side. Optimal plans are cached during the execution of getTurnMoves().

Returns:
the predicted score, or the current score if there is no cached plan.

canUndo

public boolean canUndo()
Return true if this is not the first turn.


canRedo

public boolean canRedo()
Return true if undo() was called without a matching redo().


undo

public void undo()
Return to the position prior to the last move.


redo

public void redo()
Reverse the last undo().


go

public void go(Move move)
Execute the given move. After executing the move add the resulting board to the undo/redo cache and clear all subsequent boards in the cache.

Parameters:
move - the move to execute.

cpuWon

public boolean cpuWon()
Return true if the CPU has won the game. Use this method to identify the winner when you know the game has been won.

See Also:
Board.isWon(int score)

cpuWon

public boolean cpuWon(int score)
Return true if the given score relative to the active side, is a winning score for the CPU. Use this method to identify the winner predicted by score.

See Also:
getPredictedScoreFromCache(), Board.isWon(int score)

setFromJSONString

public void setFromJSONString(java.lang.String strJSON)

toJSONString

public java.lang.String toJSONString()

addPlan

public void addPlan(Plan plan)