- 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 SummaryModifier 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- 
GameDefault 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 empty
- players- the list of players
 
 
- 
- 
Method Details- 
getCurrentTurncurrentTurn getter- Returns:
- the current turn
 
- 
setCurrentTurncurrentTurn setter- Parameters:
- currentTurn- the current turn to set
 
- 
getPlayersplayers getter- Returns:
- a list containing the players currently playing
 
- 
getGameBoardgameBoard getter- Returns:
- the gameBoard
 
- 
getCurrentRuleSetcurrentRuleSet getter- Returns:
- the current RuleSetContext
 
- 
getWalkableCellsProvides 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
 
- 
getBuildableCellsProvides 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
 
- 
getWinnerwinner getter- Returns:
- the Player who has been declared winner
 
- 
setCellsReferencesSets the position references for the Player's workers and the gameBoard cells- Parameters:
- player- the player to set the references to
 
- 
validateMoveActionChecks 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
 
- 
validateBuildActionChecks 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
 
- 
endTurnEnds 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
 
- 
generateNextTurnpublic 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.
- 
hasFirstPlayerLostpublic boolean hasFirstPlayerLost()Checks if the current player can start their turn- Specified by:
- hasFirstPlayerLostin interface- GameInterface
- Returns:
- true if the player can start the turn, false otherwise
 
- 
nextPlayerCalculates 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
 
- 
buildGameDataCreates a GameData object using this object's information- Specified by:
- buildGameDatain interface- GameInterface
- Returns:
- this object's data class
 
- 
buildBoardDataClones the game's gameBoard as a list of cells- Specified by:
- buildBoardDatain interface- GameInterface
- Returns:
- a clone of the gameBoard
 
- 
saveStateToVariableCreates a clone of this object- Returns:
- a clone of this
 
- 
restoreStatepublic void restoreState()Restores the game to a previously saved stateTo work, the file containing the saved game must be already set - Specified by:
- restoreStatein interface- GameInterface
- See Also:
- Lobby.reloadMatch(boolean)
 
- 
addMoveActionListenerAdds a new MoveActionListener to the corresponding list- Specified by:
- addMoveActionListenerin interface- GameInterface
- Parameters:
- moveActionListener- the listener to add
 
- 
addEndTurnListenerAdds a new EndTurnListener to the corresponding list- Specified by:
- addEndTurnListenerin interface- GameInterface
- Parameters:
- endTurnListener- the listener to add
 
- 
addBuildActionListenerAdds a new BuildActionListener to the corresponding list- Specified by:
- addBuildActionListenerin interface- GameInterface
- Parameters:
- buildActionListener- the listener to add
 
- 
addEndGameListenerAdds a new EndGameListener to the corresponding list- Specified by:
- addEndGameListenerin interface- GameInterface
- Parameters:
- endGameListener- the listener to add
 
- 
addPlayerLostListenerAdds a new PlayerLostListener to the corresponding list- Specified by:
- addPlayerLostListenerin interface- GameInterface
- Parameters:
- playerLostListener- the listener to add
 
 
-