|
Holger's Java API |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.antelmann.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 overridden.
Finally, the method clone() must be overridden
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(String name,
int numberOfPlayers)
|
|
| Method Summary | |
|---|---|
boolean |
addObserver(GameObserver observer)
observer is called on makeMove(GameMove); cloned games will point to the same set of observers. |
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 |
Object |
clone()
Any inheriting class with non-primitive members must override 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(). |
String |
getGameName()
returns the name of the game for display in titles (as toString() may be used to display something else) |
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 overriding this (final) function, implement listLegalMoves() instead. |
GameMove[] |
getLegalMoves(int playerRole)
getLegalMoves(playerRole) returns the subset of getLegalMoves() where player == move.getPlayer(). |
GameMove[] |
getMoveHistory()
returns an array containing all moves that have been applied to the current game status; the last move that was applied is the last move in the array at index (getMoveHistory().length-1) |
int |
getNumberOfRedoMoves()
|
GameObserver[] |
getObservers()
|
GameMove[] |
getRedoList()
returns an array containing all moves that have been taken back and could be reapplied; the GameMove at the index 0 is the next object that can be reapplied. |
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(String fileLocation)
just a convenience function |
boolean |
makeMove(GameMove move)
If the move is successfully carried out, the function returns true, false otherwise. |
int |
numberOfMoves()
|
int |
numberOfPlayers()
numberOfPlayers() returns the number of game roles in the GamePlay. |
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()
returns true only if last move previously taken back is successfully reapplied. |
boolean |
removeObserver(GameObserver observer)
|
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(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. |
String |
toString()
overridden to provide useful information about the game |
boolean |
undoLastMove()
If the last move is successfully taken back, this method returns true, false otherwise. |
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 com.antelmann.game.GamePlay |
|---|
getWinner, nextPlayer |
| Constructor Detail |
|---|
public AbstractGame(String name,
int numberOfPlayers)
| Method Detail |
|---|
public boolean addObserver(GameObserver observer)
public boolean removeObserver(GameObserver observer)
public GameObserver[] getObservers()
protected abstract GameMove[] listLegalMoves()
protected abstract boolean pushMove(GameMove move)
protected abstract boolean popMove()
public String getGameName()
GamePlay
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 GamePlayGamePlay.getLegalMoves()public final GameMove[] getLegalMoves(int playerRole)
public boolean makeMove(GameMove move)
GamePlay
makeMove in interface GamePlaypublic boolean redoMove()
GamePlay
redoMove in interface GamePlaypublic final int getNumberOfRedoMoves()
public boolean undoMoves(int n)
public boolean undoLastMove()
GamePlay
undoLastMove in interface GamePlaypublic final int numberOfMoves()
public int numberOfPlayers()
GamePlay
numberOfPlayers in interface GamePlayAutoPlay,
Player
public double getResult(int playerRole)
throws GameRuntimeException
getResult in interface GamePlayGameRuntimeException - if the game is still in progresspublic final GameMove[] getMoveHistory()
GamePlay
getMoveHistory in interface GamePlaypublic final GameMove[] getRedoList()
GamePlay
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 GamePlayGameRuntimeException - if the given move is not legal or cannot be
performed because the game is not cloneableGameMove,
clone()
public Object clone()
throws CloneNotSupportedException
spawnChild() to work correctly. Ay overriding
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 ObjectCloneNotSupportedExceptionspawnChild(GameMove)public void saveToFile(String fileLocation)
public static AbstractGame loadFromFile(String fileLocation)
public String toString()
toString in class Object
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||