Module AM37

Class Game

java.lang.Object
it.polimi.ingsw.model.Game
All Implemented Interfaces:
GameInterface

public class Game
extends java.lang.Object
implements GameInterface
Provides methods to get and alter the state of the game.

In particular, a Game object:

  • Holds and changes the status of the game, composed by the players list, the gameBoard status, the currentTurn and currentRuleSet;
  • 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 Details

    • Game

      public Game​(GameBoard gameBoard, java.util.List<Player> players)
      Default constructor

      Creates 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

    • getCurrentTurn

      public Turn getCurrentTurn()
      currentTurn getter
      Returns:
      the current turn
    • setCurrentTurn

      public void setCurrentTurn​(Turn currentTurn)
      currentTurn setter
      Parameters:
      currentTurn - the current turn to set
    • getPlayers

      public java.util.List<Player> getPlayers()
      players getter
      Returns:
      a list containing the players currently playing
    • getGameBoard

      public GameBoard getGameBoard()
      gameBoard getter
      Returns:
      the gameBoard
    • getCurrentRuleSet

      public RuleSetContext getCurrentRuleSet()
      currentRuleSet getter
      Returns:
      the current RuleSetContext
    • getWalkableCells

      public java.util.List<Cell> getWalkableCells​(Worker worker)
      Provides the cells the given worker can walk to

      May 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

      public java.util.List<Cell> getBuildableCells​(Worker worker)
      Provides the cell the given worker can build on

      May 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

      public Player getWinner()
      winner getter
      Returns:
      the Player who has been declared winner
    • setCellsReferences

      public void setCellsReferences​(Player player)
      Sets the position references for the Player's workers and the gameBoard cells
      Parameters:
      player - the player to set the references to
    • validateMoveAction

      public void validateMoveAction​(MoveAction moveAction) throws IllegalActionException
      Checks if a movement action is valid and eventually applies it

      This 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

      public void validateBuildAction​(BuildAction buildAction) throws IllegalActionException
      Checks if a building action is valid and eventually applies it

      This 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

      public void endTurn() throws IllegalEndingTurnException
      Ends the turn upon a request

      The 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 turn

      Creates 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 interface GameInterface
      Returns:
      true if the player can start the turn, false otherwise
    • nextPlayer

      public Player nextPlayer()
      Calculates player playing next

      By 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

      public GameData buildGameData()
      Creates a GameData object using this object's information
      Specified by:
      buildGameData in interface GameInterface
      Returns:
      this object's data class
    • buildBoardData

      public java.util.List<Cell> buildBoardData()
      Clones the game's gameBoard as a list of cells
      Specified by:
      buildBoardData in interface GameInterface
      Returns:
      a clone of the gameBoard
    • saveStateToVariable

      public Game saveStateToVariable()
      Creates a clone of this object
      Returns:
      a clone of this
    • restoreState

      public void restoreState()
      Restores the game to a previously saved state

      To work, the file containing the saved game must be already set

      Specified by:
      restoreState in interface GameInterface
      See Also:
      Lobby.reloadMatch(boolean)
    • addMoveActionListener

      public void addMoveActionListener​(MoveActionListener moveActionListener)
      Adds a new MoveActionListener to the corresponding list
      Specified by:
      addMoveActionListener in interface GameInterface
      Parameters:
      moveActionListener - the listener to add
    • addEndTurnListener

      public void addEndTurnListener​(EndTurnListener endTurnListener)
      Adds a new EndTurnListener to the corresponding list
      Specified by:
      addEndTurnListener in interface GameInterface
      Parameters:
      endTurnListener - the listener to add
    • addBuildActionListener

      public void addBuildActionListener​(BuildActionListener buildActionListener)
      Adds a new BuildActionListener to the corresponding list
      Specified by:
      addBuildActionListener in interface GameInterface
      Parameters:
      buildActionListener - the listener to add
    • addEndGameListener

      public void addEndGameListener​(EndGameListener endGameListener)
      Adds a new EndGameListener to the corresponding list
      Specified by:
      addEndGameListener in interface GameInterface
      Parameters:
      endGameListener - the listener to add
    • addPlayerLostListener

      public void addPlayerLostListener​(PlayerLostListener playerLostListener)
      Adds a new PlayerLostListener to the corresponding list
      Specified by:
      addPlayerLostListener in interface GameInterface
      Parameters:
      playerLostListener - the listener to add