- 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
players
list, thegameBoard
status, thecurrentTurn
andcurrentRuleSet
; - 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 void
addBuildActionListener(BuildActionListener buildActionListener)
Adds a new BuildActionListener to the corresponding listvoid
addEndGameListener(EndGameListener endGameListener)
Adds a new EndGameListener to the corresponding listvoid
addEndTurnListener(EndTurnListener endTurnListener)
Adds a new EndTurnListener to the corresponding listvoid
addMoveActionListener(MoveActionListener moveActionListener)
Adds a new MoveActionListener to the corresponding listvoid
addPlayerLostListener(PlayerLostListener playerLostListener)
Adds a new PlayerLostListener to the corresponding listjava.util.List<Cell>
buildBoardData()
Clones the game's gameBoard as a list of cellsGameData
buildGameData()
Creates a GameData object using this object's informationvoid
endTurn()
Ends the turn upon a requestvoid
generateNextTurn()
Generates the next game turnjava.util.List<Cell>
getBuildableCells(Worker worker)
Provides the cell the given worker can build onRuleSetContext
getCurrentRuleSet()
currentRuleSet getterTurn
getCurrentTurn()
currentTurn getterGameBoard
getGameBoard()
gameBoard getterjava.util.List<Player>
getPlayers()
players getterjava.util.List<Cell>
getWalkableCells(Worker worker)
Provides the cells the given worker can walk toPlayer
getWinner()
winner getterboolean
hasFirstPlayerLost()
Checks if the current player can start their turnPlayer
nextPlayer()
Calculates player playing nextvoid
restoreState()
Restores the game to a previously saved stateGame
saveStateToVariable()
Creates a clone of this objectvoid
setCellsReferences(Player player)
Sets the position references for the Player's workers and the gameBoard cellsvoid
setCurrentTurn(Turn currentTurn)
currentTurn settervoid
validateBuildAction(BuildAction buildAction)
Checks if a building action is valid and eventually applies itvoid
validateMoveAction(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:
hasFirstPlayerLost
in 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:
buildGameData
in interfaceGameInterface
- Returns:
- this object's data class
-
buildBoardData
Clones the game's gameBoard as a list of cells- Specified by:
buildBoardData
in 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:
restoreState
in interfaceGameInterface
- See Also:
Lobby.reloadMatch(boolean)
-
addMoveActionListener
Adds a new MoveActionListener to the corresponding list- Specified by:
addMoveActionListener
in interfaceGameInterface
- Parameters:
moveActionListener
- the listener to add
-
addEndTurnListener
Adds a new EndTurnListener to the corresponding list- Specified by:
addEndTurnListener
in interfaceGameInterface
- Parameters:
endTurnListener
- the listener to add
-
addBuildActionListener
Adds a new BuildActionListener to the corresponding list- Specified by:
addBuildActionListener
in interfaceGameInterface
- Parameters:
buildActionListener
- the listener to add
-
addEndGameListener
Adds a new EndGameListener to the corresponding list- Specified by:
addEndGameListener
in interfaceGameInterface
- Parameters:
endGameListener
- the listener to add
-
addPlayerLostListener
Adds a new PlayerLostListener to the corresponding list- Specified by:
addPlayerLostListener
in interfaceGameInterface
- Parameters:
playerLostListener
- the listener to add
-