public class TurnBasedGame<T extends GameState>
extends java.lang.Object
Helper for turn based games that will handle the transaction logic to allow replay of game
states and handle serialization of game information so it can be saved and/or sent over to
other participants in the game. To fully implement this you need to implement
GameState
and Transaction
as well as the factory methods
GameStateFactory
and TransactionFactory
.
The core functionality of this class is to provide a way to store and maintain
Transaction
objects and a GameState
. As a user of this class you should call
addTransaction(Transaction)
when you have a valid transaction to modify current game
state with. You may use these multiple times. A transaction could be small (move single unit)
or could be big (these are the moves I have done over this turn) but should be considered an
important point where you can effectively pause/stop the game state.
To then apply/play the transactions you should call playNextTransaction()
. This will
return the played transaction so further processing (GUI updates maybe?) could be done.
You can always reset the game state using resetGameState()
to the initial game state
as supplied in the constructor. This will clone the game state object using
GameState.cloneState()
and set the current game state to this new clone (and discard
the other one).
Finally, you may save the state and the transactions using serialize()
which will
return JSON representation suitable for saving to persistent memory or transferred to
another player in a multi-player environment. To read a saved version you supply the
JSON representation as well as factory methods used to recreate the transactions and game
state using TurnBasedGame(JSONObject, GameStateFactory, TransactionFactory)
.
// Example code that creates a helper class with an initial game state
GameStateImplementation initialGameState = new GameStateImplementation(...);
TurnBasedGame<GameStateImplementation> myGame = new TurnBasedGame<>(initialGameState);*
// Example of adding a transaction
Transaction<GameStateImplementation> trans = new TransactionImplementation(...);
myGame.addTransaction(trans);
// Example of playing transaction
myGame.playNextTransaction();
// Example of replay transaction
myGame.resetGameState(); // <= will "undo" 'trans'
myGame.playNextTransaction(); // <= will replay 'trans' again
GameState
,
Transaction
,
GameStateFactory
,
TransactionFactory
Constructor and Description |
---|
TurnBasedGame(JSONObject json,
GameStateFactory<T> stateFactory,
TransactionFactory<T> transFactory)
Create turn based game from JSON
|
TurnBasedGame(T initialState)
Create game from initial state
|
Modifier and Type | Method and Description |
---|---|
void |
addTransaction(Transaction<T> trans)
Add transaction as unplayed transaction
|
T |
getCurrentState()
Get current game state
|
boolean |
hasUnplayedTransaction()
Check if there are any unplayed transactions
|
Transaction<T> |
playNextTransaction()
Play next unplayed transaction
|
void |
resetGameState()
Reset game state to initial state keeping all transactions but flagging them as unplayed
|
JSONObject |
serialize()
Serialize game and transaction to JSON
|
public TurnBasedGame(JSONObject json, GameStateFactory<T> stateFactory, TransactionFactory<T> transFactory) throws JSONException
json
- JSON of game created earlierstateFactory
- How to create game statetransFactory
- How to create transactionsJSONException
- Missing JSON fields and that alikepublic TurnBasedGame(T initialState)
initialState
- Initial game state at start of gamepublic void addTransaction(Transaction<T> trans)
trans
- Transaction that should occurpublic boolean hasUnplayedTransaction()
public Transaction<T> playNextTransaction()
public void resetGameState()
public T getCurrentState()
public JSONObject serialize() throws JSONException
JSONException
- on error