Class Controller

java.lang.Object
it.polimi.ingsw.Controller.Controller

public class Controller extends Object
This is the Controller of the whole game.
The Controller should be the only entity able to modify the model.
  • Field Details

    • history

      private final List<PlayerAction> history
    • modelWrapper

      private final ModelWrapper modelWrapper
    • unsafeReferences

      private boolean unsafeReferences
  • Constructor Details

    • Controller

      private Controller(ModelWrapper modelWrapper)
      Subscribes a new Controller object to a ModelWrapper instance, allowing the creation of a Controller to Model connection.
      Parameters:
      modelWrapper - an instance of ModelWrapper
    • Controller

      Controller(ModelWrapper modelWrapper, List<PlayerAction> history)
      Generates a new instance of the Controller. This is the debug method to call to create a game, since the internal attributes are set to the parameters.
      Note: this method will not protect model references after editing actions to the model. Note: this method should be called ONLY by test code.
      Parameters:
      modelWrapper - an instance of ModelWrapper
      history - an instance to a list of PlayerAction, used by the controller to check the flow of the game
  • Method Details

    • createGame

      public static Controller createGame(GameMode gameMode, OptionalValue<Lobby> lobby, String... players) throws InputValidationException
      Generates a new instance. This is the static method to call for general purpose creation of a game.
      Parameters:
      gameMode - the game mode the players are going to use
      lobby - in case a server is used, insert the Lobby object wrapped in an OptionalValue to let it receive ModelUpdateEvent and GameOverEvent
      players - a list of minimum 2 and maximum 4 strings containing the nicknames of the players. In the case of 4 players: players at index 0 and 2 go together against players at index 1 and 3
      Returns:
      the generated controller
      Throws:
      InputValidationException - if the supplied players are less than 2 or more than 4
    • executeAction

      public void executeAction(PlayerAction action) throws InputValidationException
      An execution request handler. Actions are passed in, validated and (if possible) executed.
      Warning: this request is not thread safe, that job is delegated to the caller to handle.

      Note: if this Controller was generated using the debug constructor, then references to the model, once modified, are going to be kept unsafe, generally decreasing the security of the editing mechanism.

      Parameters:
      action - the action to be validated and executed.
      Throws:
      InputValidationException - thrown when validation fails, carries information about the error. If thrown, the model is guaranteed to not have been modified.
    • getHistory

      private List<PlayerAction> getHistory()
      Returns:
      an immutable copy of the list of player actions.
      Note: the single actions are immutable by default, so do not get cloned