|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.jscience.computing.game.AbstractGame
public abstract class AbstractGame
This class implements the generic behaviour of a game to ease GamePlay
implementations. The following functions are abstract and need to be
implemented to represent the domain knowlege:
protected GameMove[] listLegalMoves (); protected boolean pushMove (GameMove); protected boolean popMove ();If the game does not support undoing of moves, the method
popMove() may always
simply return false.public int nextPlayer () public int[] getWinner ()The function
getResult(int
playerRole) is implemented in a way that it returns 1 if game is
won, -1 if game is lost and 0 if no winners are present, which is
appropriate for many games, but may have to be overwritten. Finally, the
method clone() must be overwritten in case the extending class
has non-primitive members to provide a deep copy. It is probably a good
idea to also implement a useful toString() method, so that the
game can be visualized properly.GameMove object that represents a move that can be applied to
a game to alter its status. GamePlay is designed for game
that have a finite rather easily determinable list of legal moves. Also,
this implementation assumes that the next player in a game is well defined
at any given state.
GamePlay,
GameMove,
Serialized Form| Constructor Summary | |
|---|---|
AbstractGame(java.lang.String name,
int numberOfPlayers)
Creates a new AbstractGame object. |
|
| Method Summary | |
|---|---|
protected void |
clearRedoList()
may be called by the subclass if certain events alter the game status so that the given redo moves cannot be applied anymore |
java.lang.Object |
clone()
Any inheriting class with non-primitive members must overwrite this clone() method to provide a full deep copy of the object, which is essential for spawnChild() to work
correctly. |
boolean |
gameOver()
This convenience function gameOver() simply checks whether there are any legal moves left; consequently, unless this function is overridden, listLegalMoves() must not check for gameOver(). |
java.lang.String |
getGameName()
DOCUMENT ME! |
GameMove |
getLastMove()
getLastMove() is a convenience function which returns the last element of the GameMove[] from getMoveHistory() in case the array has any elements in it; null is returned otherwise. |
int |
getLastPlayer()
getLastPlayer() is a convenience function which simply looks up the last move and then returns the playerRole that played it |
GameMove[] |
getLegalMoves()
Instead of overwriting this (final) function, implement listLegalMoves() instead. |
GameMove[] |
getLegalMoves(int playerRole)
getLegalMoves(playerRole) returns the subset of getLegalMoves() where player == move.getPlayer(). |
GameMove[] |
getMoveHistory()
DOCUMENT ME! |
int |
getNumberOfRedoMoves()
DOCUMENT ME! |
GameMove[] |
getRedoList()
DOCUMENT ME! |
double |
getResult(int playerRole)
a default implementation for convenience which may suit most games that do not involve betting or any form of measuring how 'big' the win was. |
boolean |
isLegalMove(GameMove move)
this implementation checks whether the move is contained in the array returned by getLegalMoves(); thus this method relies on GameMove.equals() being implemented properly for the move in question |
boolean |
isWinner(int gameRole)
isWinner() checks whether the given gameRole is in the array retruned by getWinner(). |
protected abstract GameMove[] |
listLegalMoves()
listLegalMoves() returns the legal moves for this game. |
static AbstractGame |
loadFromFile(java.lang.String fileLocation)
just a convenience function |
boolean |
makeMove(GameMove move)
DOCUMENT ME! |
int |
numberOfMoves()
DOCUMENT ME! |
int |
numberOfPlayers()
DOCUMENT ME! |
protected abstract boolean |
popMove()
popMove undoes the last move by altering the game board to the stage before the last move happened. |
protected abstract boolean |
pushMove(GameMove move)
pushMove takes a GameMove and alters the game according to to the move. |
boolean |
redoMove()
DOCUMENT ME! |
protected void |
resetLegalMoves()
forces a recalculation of getLegalMoves() with listLegalMoves(); needed if the extending class alters the game status without calling makeMove(), redoMove() or undoLastMove() and the move history and redo list is to be maintained |
protected void |
resetLists()
resetList() forces a recalculation of getLegalMoves() and also resets the move list and the redo list. |
void |
saveToFile(java.lang.String fileLocation)
just a convenience function |
GamePlay |
spawnChild(GameMove move)
spawnChild() creates a clone of the current game and advances the game by the given GameMove. |
java.lang.String |
toString()
overwritten to provide useful information about the game |
boolean |
undoLastMove()
DOCUMENT ME! |
boolean |
undoMoves(int n)
calls undoLastMove() either n times or as many times as there are numberOfMoves(), whichever is smaller |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface org.jscience.computing.game.GamePlay |
|---|
getWinner, nextPlayer |
| Constructor Detail |
|---|
public AbstractGame(java.lang.String name,
int numberOfPlayers)
name - DOCUMENT ME!numberOfPlayers - DOCUMENT ME!| Method Detail |
|---|
protected abstract GameMove[] listLegalMoves()
protected abstract boolean pushMove(GameMove move)
move - DOCUMENT ME!
protected abstract boolean popMove()
public java.lang.String getGameName()
getGameName in interface GamePlay
public boolean isWinner(int gameRole)
throws GameRuntimeException
GameRuntimeExceptionGamePlay.getWinner()
public int getLastPlayer()
throws GameRuntimeException
GameRuntimeException - if no move has been made, yet.public boolean gameOver()
public GameMove getLastMove()
public final GameMove[] getLegalMoves()
getLegalMoves in interface GamePlayGamePlay.isLegalMove(GameMove)protected final void resetLegalMoves()
protected final void clearRedoList()
protected void resetLists()
public boolean isLegalMove(GameMove move)
isLegalMove in interface GamePlaymove - DOCUMENT ME!
GamePlay.getLegalMoves()public final GameMove[] getLegalMoves(int playerRole)
playerRole - DOCUMENT ME!
public boolean makeMove(GameMove move)
makeMove in interface GamePlaymove - DOCUMENT ME!
public boolean redoMove()
redoMove in interface GamePlaypublic final int getNumberOfRedoMoves()
public boolean undoMoves(int n)
n - DOCUMENT ME!
public boolean undoLastMove()
undoLastMove in interface GamePlaypublic final int numberOfMoves()
public int numberOfPlayers()
numberOfPlayers in interface GamePlayAutoPlay,
Player
public double getResult(int playerRole)
throws GameRuntimeException
getResult in interface GamePlayplayerRole - DOCUMENT ME!
GameRuntimeException - if the game is still in progresspublic final GameMove[] getMoveHistory()
getMoveHistory in interface GamePlaypublic final GameMove[] getRedoList()
getRedoList in interface GamePlay
public GamePlay spawnChild(GameMove move)
throws GameRuntimeException
clone() method to ensure a deep copy if the
subclass has non-primitive members.
spawnChild in interface GamePlaymove - DOCUMENT ME!
GameRuntimeException - if the given move is not legal or cannot be
performed because the game is not cloneableGameMove,
clone()
public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
spawnChild() to work
correctly. Ay overwriting of this method should still begin with a call
to super.clone(), though. Also, note that this clone()
method does not clone the embedded GameMove objects, i.e. those are
expected not to change (except for their heuristics, which shouldn't
affect game functionality) - or the subclass will have to take care of
this.
clone in interface GamePlayclone in class java.lang.Objectjava.lang.CloneNotSupportedExceptionspawnChild(GameMove)public void saveToFile(java.lang.String fileLocation)
fileLocation - DOCUMENT ME!
GameRuntimeException - DOCUMENT ME!public static AbstractGame loadFromFile(java.lang.String fileLocation)
fileLocation - DOCUMENT ME!
GameRuntimeException - DOCUMENT ME!public java.lang.String toString()
toString in class java.lang.Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||