Class GameController

java.lang.Object
edu.ntnu.idi.idatt.controller.common.GameController
All Implemented Interfaces:
BoardGameObserver, ButtonClickObserver
Direct Known Subclasses:
LadderGameController, LudoGameController

public abstract class GameController extends Object implements ButtonClickObserver, BoardGameObserver
GameController.

Abstract controller for managing game logic and interactions between the model (BoardGame) and the view (GameView).

It handles game initialization, player turns, and game state updates. It also acts as an observer for both button clicks from the view and game events from the model.

See Also:
  • Field Details

    • gameView

      protected final GameView gameView
    • logger

      protected final org.slf4j.Logger logger
    • boardGame

      protected BoardGame boardGame
    • gameFinishedParams

      protected Map<String,Object> gameFinishedParams
    • onQuitGame

      protected Runnable onQuitGame
      Runnable action to execute when the game is quit.
    • onNavigateToGameFinished

      protected Consumer<Map<String,Object>> onNavigateToGameFinished
      Consumer action to execute when the game is finished.
  • Constructor Details

    • GameController

      public GameController(GameView gameView, Board board, List<Player> players)
      Constructs a GameController.
      Parameters:
      gameView - The GameView associated with this controller.
      board - The Board for the game.
      players - A list of Players participating in the game.
  • Method Details

    • initializeBoardGame

      public abstract void initializeBoardGame(Board board, List<Player> players)
      Initializes the specific BoardGame instance for the game. This method must be implemented by subclasses to create and configure the appropriate game model.
      Parameters:
      board - The Board for the game.
      players - A list of Players participating in the game.
    • performPlayerTurnForAllPlayers

      protected abstract void performPlayerTurnForAllPlayers()
      Executes a full turn for all players in the game sequentially. Subclasses must implement the logic for how multiple player turns are handled, for example, in a "roll for all" scenario.
    • performPlayerTurn

      protected abstract void performPlayerTurn()
      Executes a single turn for the current player. Subclasses must implement the specific actions that constitute a player's turn (e.g., rolling dice, moving pieces, checking for game events).
    • restartGame

      protected abstract void restartGame()
      Restarts the current game. Subclasses must implement the logic to reset the game state to its initial configuration, allowing players to start a new game with the same settings.
    • initializeGameView

      public void initializeGameView()
      Initializes the GameView with the initial game state. This typically involves setting up the display of players, round number, and the game board.
    • getRoundNumber

      protected int getRoundNumber()
      Gets the current round number of the game.
      Returns:
      The current round number.
    • getPlayers

      protected List<Player> getPlayers()
      Gets the list of players in the game.
      Returns:
      A list of Players.
    • getBoard

      protected Board getBoard()
      Gets the game board.
      Returns:
      The Board object.
    • setOnQuitGame

      public void setOnQuitGame(Runnable onQuitGame)
      Sets the Runnable action to be executed when the game is quit.
      Parameters:
      onQuitGame - The action to perform on quitting the game.
    • quitGame

      protected void quitGame()
      Executes the quit game action, if one has been set.
    • setOnNavigateToGameFinished

      public void setOnNavigateToGameFinished(Consumer<Map<String,Object>> onNavigateToGameFinished)
      Sets the Consumer action to be executed when the game is finished.
      Parameters:
      onNavigateToGameFinished - The action to perform on navigating to the game finished view.
    • handleRollDiceButtonAction

      protected void handleRollDiceButtonAction()
      Handles the action triggered by the roll dice button. Disables the button, then either performs a turn for all players or a single player based on the view's selection.
    • enableRollDiceButton

      protected void enableRollDiceButton()
      Enables the roll dice button in the game view.
    • disableRollDiceButton

      protected void disableRollDiceButton()
      Disables the roll dice button in the game view.
    • onButtonClicked

      public abstract void onButtonClicked(String buttonId)
      Handles button click events without parameters from the GameView. Subclasses must implement this to define actions for specific button IDs.
      Specified by:
      onButtonClicked in interface ButtonClickObserver
      Parameters:
      buttonId - The ID of the button that was clicked.
    • onButtonClickedWithParams

      public abstract void onButtonClickedWithParams(String buttonId, Map<String,Object> params)
      Handles button click events with parameters from the GameView. Subclasses must implement this to define actions for specific button IDs that require parameters.
      Specified by:
      onButtonClickedWithParams in interface ButtonClickObserver
      Parameters:
      buttonId - The ID of the button that was clicked.
      params - A map of parameters associated with the button click.
    • onRoundNumberIncremented

      public abstract void onRoundNumberIncremented(int roundNumber)
      Called when the round number in the BoardGame model is incremented. Subclasses must implement this to update the view or perform other actions in response to a new round starting.
      Specified by:
      onRoundNumberIncremented in interface BoardGameObserver
      Parameters:
      roundNumber - The new round number.
    • onCurrentPlayerChanged

      public abstract void onCurrentPlayerChanged(Player player)
      Called when the current player in the BoardGame model changes. Subclasses must implement this to update the view or perform other actions related to the change of turn.
      Specified by:
      onCurrentPlayerChanged in interface BoardGameObserver
      Parameters:
      player - The new current Player.
    • onGameFinished

      public abstract void onGameFinished(Player winner)
      Called when the BoardGame model signals that the game has finished. Subclasses must implement this to handle the end of the game, such as displaying a winner message or offering options to restart or quit.
      Specified by:
      onGameFinished in interface BoardGameObserver
      Parameters:
      winner - The Player who won the game.