- All Implemented Interfaces:
GameInterface
public class Game extends java.lang.Object implements GameInterface
In particular, a Game object:
- Holds and changes the status of the game, composed by the
playerslist, thegameBoardstatus, thecurrentTurnandcurrentRuleSet; - Checks the requests coming from the Controller and, if no errors are detected, performs the required
action (see
Action); - Notifies the Controller about the outcome of the operations, using the various listeners;
- Saves and restores a previous state from file, in case of a server failure (persistence)
This is perhaps the most dense class of the entire project, but after some discussion it has been decided not to "tear it apart".
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidaddBuildActionListener(BuildActionListener buildActionListener)Adds a new BuildActionListener to the corresponding listvoidaddEndGameListener(EndGameListener endGameListener)Adds a new EndGameListener to the corresponding listvoidaddEndTurnListener(EndTurnListener endTurnListener)Adds a new EndTurnListener to the corresponding listvoidaddMoveActionListener(MoveActionListener moveActionListener)Adds a new MoveActionListener to the corresponding listvoidaddPlayerLostListener(PlayerLostListener playerLostListener)Adds a new PlayerLostListener to the corresponding listjava.util.List<Cell>buildBoardData()Clones the game's gameBoard as a list of cellsGameDatabuildGameData()Creates a GameData object using this object's informationvoidendTurn()Ends the turn upon a requestvoidgenerateNextTurn()Generates the next game turnjava.util.List<Cell>getBuildableCells(Worker worker)Provides the cell the given worker can build onRuleSetContextgetCurrentRuleSet()currentRuleSet getterTurngetCurrentTurn()currentTurn getterGameBoardgetGameBoard()gameBoard getterjava.util.List<Player>getPlayers()players getterjava.util.List<Cell>getWalkableCells(Worker worker)Provides the cells the given worker can walk toPlayergetWinner()winner getterbooleanhasFirstPlayerLost()Checks if the current player can start their turnPlayernextPlayer()Calculates player playing nextvoidrestoreState()Restores the game to a previously saved stateGamesaveStateToVariable()Creates a clone of this objectvoidsetCellsReferences(Player player)Sets the position references for the Player's workers and the gameBoard cellsvoidsetCurrentTurn(Turn currentTurn)currentTurn settervoidvalidateBuildAction(BuildAction buildAction)Checks if a building action is valid and eventually applies itvoidvalidateMoveAction(MoveAction moveAction)Checks if a movement action is valid and eventually applies it
-
Constructor Details
-
Game
Default constructorCreates a new Game instance with the given parameters; the game board can be empty, since Workers can be added later; the Players list must not be empty and contain 2 or 3 elements (the official Santorini game allows 4 players, but since it messes up with some stuff we do not implement this mode).
- Parameters:
gameBoard- the game field; can be emptyplayers- the list of players
-
-
Method Details
-
getCurrentTurn
currentTurn getter- Returns:
- the current turn
-
setCurrentTurn
currentTurn setter- Parameters:
currentTurn- the current turn to set
-
getPlayers
players getter- Returns:
- a list containing the players currently playing
-
getGameBoard
gameBoard getter- Returns:
- the gameBoard
-
getCurrentRuleSet
currentRuleSet getter- Returns:
- the current
RuleSetContext
-
getWalkableCells
Provides the cells the given worker can walk toMay change during the turn due to other actions.
- Parameters:
worker- the worker to be moved- Returns:
- a list of cells the given worker can move to
-
getBuildableCells
Provides the cell the given worker can build onMay change during the turn due to other actions
- Parameters:
worker- the worker willing to build- Returns:
- a list of cells the given worker can build on
-
getWinner
winner getter- Returns:
- the Player who has been declared winner
-
setCellsReferences
Sets the position references for the Player's workers and the gameBoard cells- Parameters:
player- the player to set the references to
-
validateMoveAction
Checks if a movement action is valid and eventually applies itThis method makes a call to the current ruleSet context containing the current turn strategy to check if, following the current ruleSet, the provided moveAction is legal; this method DOES NOT check whether the action is performed by a current player's work or not: this kind of checks should be made in the controller section.
If the action is valid, it is also applied on the gameBoard, and a clone of the modified board is sent to all the players through observers. In case the player won, it is also notified via the observers. At the end of those operations, the game state is saved, to allow a possible undo.
If the action is not valid, an exception is thrown.- Parameters:
moveAction- the movement action to validate- Throws:
IllegalActionException- if the action is not valid
-
validateBuildAction
Checks if a building action is valid and eventually applies itThis method makes a call to the current ruleSet context containing the current turn strategy to check if, following the current ruleSet, the provided buildAction is legal; this method DOES NOT check whether the action is performed by a current player's work or not: this kind of checks should be made in the controller section.
If the action is valid, it is also applied on the gameBoard, and the client receives a list of updated cells, via listener. The game state is then saved, to allow a possible undo.
If the action is not valid, an exception is thrown.- Parameters:
buildAction- the movement action to validate- Throws:
IllegalActionException- if the action is not valid
-
endTurn
Ends the turn upon a requestThe condition for a player to end their turn may vary based on their chosen god, so this method calls the current ruleSet method to check if the player can end the turn. If all the conditions for the turn to end are verified, the next turn is generated (see generateNextTurn()); otherwise, an exception is thrown.
- Throws:
IllegalEndingTurnException- if the player cannot end their turn
-
generateNextTurn
public void generateNextTurn()Generates the next game turnCreates a new Turn object, applies the end turn effects for the current player, sets the next player's ruleSet strategy, then actually set the new turn
When the new turn begins, the listeners are notified; the lose conditions are then verified, before the player can make any actions; if verified, the player is removed. -
hasFirstPlayerLost
public boolean hasFirstPlayerLost()Checks if the current player can start their turn- Specified by:
hasFirstPlayerLostin interfaceGameInterface- Returns:
- true if the player can start the turn, false otherwise
-
nextPlayer
Calculates player playing nextBy default, the next player is calculated by getting the current player's index in the players list and adding 1 (modulus the number of players).
This formula stops working in case a player is removed from the game; in this case, we use the turn number to calculate the next player.- Returns:
- the next player
-
buildGameData
Creates a GameData object using this object's information- Specified by:
buildGameDatain interfaceGameInterface- Returns:
- this object's data class
-
buildBoardData
Clones the game's gameBoard as a list of cells- Specified by:
buildBoardDatain interfaceGameInterface- Returns:
- a clone of the gameBoard
-
saveStateToVariable
Creates a clone of this object- Returns:
- a clone of this
-
restoreState
public void restoreState()Restores the game to a previously saved stateTo work, the file containing the saved game must be already set
- Specified by:
restoreStatein interfaceGameInterface- See Also:
Lobby.reloadMatch(boolean)
-
addMoveActionListener
Adds a new MoveActionListener to the corresponding list- Specified by:
addMoveActionListenerin interfaceGameInterface- Parameters:
moveActionListener- the listener to add
-
addEndTurnListener
Adds a new EndTurnListener to the corresponding list- Specified by:
addEndTurnListenerin interfaceGameInterface- Parameters:
endTurnListener- the listener to add
-
addBuildActionListener
Adds a new BuildActionListener to the corresponding list- Specified by:
addBuildActionListenerin interfaceGameInterface- Parameters:
buildActionListener- the listener to add
-
addEndGameListener
Adds a new EndGameListener to the corresponding list- Specified by:
addEndGameListenerin interfaceGameInterface- Parameters:
endGameListener- the listener to add
-
addPlayerLostListener
Adds a new PlayerLostListener to the corresponding list- Specified by:
addPlayerLostListenerin interfaceGameInterface- Parameters:
playerLostListener- the listener to add
-