|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.pesullivan.game.game42.Game
public class Game
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.
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 |
---|
public Game(Board board)
Board
to supply all game specific middle tier behavior.
board
- holds the initial starting position of the Game.Method Detail |
---|
public int getNCurrentBoardInHistory()
public void setAbortPlanningThreadPending(boolean abortPlanningThreadPending)
getTurnMoves()
inspects periodically
to decide whether it needs to abort.
public boolean getAbortPlanningThreadPending()
true
is returned this means that any thread running
the expensive getTurnMoves()
will abort.
public Board getBoard()
public Board getBoard(int nBoardInHistory)
nBoardInHistory
- 0 based board history index.public int getLevelOfPlay()
public void setLevelOfPlay(int level)
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.
level
- 1 based level of difficulty.public void setCPUSide(int side)
side
- 0 or 1.public int getCPUSide()
public boolean isCPUsMove()
public Plan getTurnPlan()
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.
Plan
s to complete the turn, or null if it was aborted.Plan
public java.util.List<Move> getTurnMoves()
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.
Move
s to complete the turn, or null if it was aborted.Move
public void newGame()
Game(Board board)
,
and the undo/redo cache is cleared.
public int getPredictedScoreFromCache()
Plan
that may exist
for the current position and side. Optimal plans are cached
during the execution of getTurnMoves()
.
public boolean canUndo()
public boolean canRedo()
undo()
was called
without a matching redo()
.
public void undo()
public void redo()
undo()
.
public void go(Move move)
move
- the move to execute.public boolean cpuWon()
Board.isWon(int score)
public boolean cpuWon(int score)
getPredictedScoreFromCache()
,
Board.isWon(int score)
public void setFromJSONString(java.lang.String strJSON)
public java.lang.String toJSONString()
public void addPlan(Plan plan)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |