Class LudoGameController

java.lang.Object
edu.ntnu.idi.idatt.controller.common.GameController
edu.ntnu.idi.idatt.controller.ludo.LudoGameController
All Implemented Interfaces:
BoardGameObserver, ButtonClickObserver

public class LudoGameController extends GameController
LudoGameController.

This class extends GameController to manage the specific logic and interactions for a LudoBoardGame. It acts as a controller in the MVC pattern, mediating between the LudoGameView (the view) and the LudoBoardGame (the model).

Responsibilities include:

  • Initializing the LudoBoardGame with a given Board and list of LudoPlayers.
  • Handling player turns, which involves triggering a dice roll in the model, animating the roll in the view, and then executing the turn in the model.
  • Updating the LudoGameView in response to game state changes from the model. This includes:
    • Token releases: Moving a token from the start area to the track.
    • Token movements: Animating token movement on the board.
    • Token captures: Moving a captured token back to its start area.
    • Token finishes: Indicating a token has reached its goal.
    • Turn skips: Logging when a player cannot make a move.
    • Updating player information (e.g., number of finished tokens).
    • Round progression and current player changes.
    • Game completion announcements.
  • Processing user interactions from the view, such as clicking "roll dice", "restart game", or "quit game" buttons.
  • Managing automatic turns for bot players if "roll for all players" is not selected.
See Also:
  • Constructor Details

    • LudoGameController

      public LudoGameController(LudoGameView ludoGameView, Board board, List<Player> players)
      Constructs a new LudoGameController.
      Parameters:
      ludoGameView - The LudoGameView associated with this controller.
      board - The Board (specifically a LudoGameBoard) for the game.
      players - The list of Players (expected to be LudoPlayer instances) participating in the game.
  • Method Details

    • initializeBoardGame

      public void initializeBoardGame(Board board, List<Player> players)
      Initializes the GameController.boardGame instance as a LudoBoardGame. It creates a new game with the provided board and players, and sets the number of dice to 1 (standard for Ludo). This controller is also added as an observer to the newly created game model.
      Specified by:
      initializeBoardGame in class GameController
      Parameters:
      board - The Board for the Ludo game.
      players - The list of Players for the Ludo game.
    • performPlayerTurnForAllPlayers

      protected void performPlayerTurnForAllPlayers()
      Performs turns for all players sequentially until it is the first player's turn again. This is typically used when the "roll for all players" option is active in the view.
      Specified by:
      performPlayerTurnForAllPlayers in class GameController
    • performPlayerTurn

      protected void performPlayerTurn()
      Executes a single turn for the current player in the LudoBoardGame. This involves:
      1. Disabling the roll dice button in the view.
      2. Simulating a dice roll in the LudoBoardGame.
      3. Triggering an animation for the dice roll in the LudoGameView.
      4. Upon completion of the animation, performing the player's turn in the game model using the dice roll.
      5. Re-enabling the roll dice button in the view.
      Specified by:
      performPlayerTurn in class GameController
    • onTokenReleased

      public void onTokenReleased(Player player, int tileId, int tokenId)
      Handles the tokenReleased event from the LudoBoardGame model. Adds an entry to the game log in the view, triggers the visual release of the token on the LudoGameStackPane, and updates the player's displayed information.
      Parameters:
      player - The Player whose token was released.
      tileId - The ID of the tile onto which the token was released.
      tokenId - The ID (index) of the released token within the player's list of tokens.
    • restartGame

      protected void restartGame()
      Restarts the current Ludo game. This involves:
      1. Creating a new list of LudoPlayers based on the existing players (preserving their names, colors, token types, and bot status).
      2. Re-initializing the GameController.boardGame with the same board but the new list of player instances.
      3. Re-initializing the LudoGameView to reflect the new game state.
      Specified by:
      restartGame in class GameController
    • onTokenMoved

      public void onTokenMoved(Player player, LudoToken token, int diceRoll, int oldTileId, int newTileId)
      Handles the tokenMoved event from the LudoBoardGame model. Adds an entry to the game log, triggers the visual movement of the token on the LudoGameStackPane, and updates the player's displayed information.
      Parameters:
      player - The Player whose token moved.
      token - The LudoToken that moved.
      diceRoll - The result of the dice roll that caused the move.
      oldTileId - The ID of the tile the token moved from.
      newTileId - The ID of the tile the token moved to.
    • onRoundNumberIncremented

      public void onRoundNumberIncremented(int roundNumber)
      Handles the roundNumberIncremented event from the LudoBoardGame model. Updates the displayed round number in the view's player information panel and adds a new round entry to the game log.
      Specified by:
      onRoundNumberIncremented in interface BoardGameObserver
      Specified by:
      onRoundNumberIncremented in class GameController
      Parameters:
      roundNumber - The new round number.
    • onCurrentPlayerChanged

      public void onCurrentPlayerChanged(Player player)
      Handles the currentPlayerChanged event from the LudoBoardGame model. If "roll for all players" is not selected in the view, it sets the focus on the current player in the view's player information panel. If the new current player is a bot and "roll for all" is not active, it automatically performs the bot's turn. If "roll for all players" is selected, focus is removed from any specific player.
      Specified by:
      onCurrentPlayerChanged in interface BoardGameObserver
      Specified by:
      onCurrentPlayerChanged in class GameController
      Parameters:
      player - The new current Player.
    • onGameFinished

      public void onGameFinished(Player winner)
      Handles the gameFinished event from the LudoBoardGame model. Adds an entry to the game log in the view announcing the winner.
      Specified by:
      onGameFinished in interface BoardGameObserver
      Specified by:
      onGameFinished in class GameController
      Parameters:
      winner - The Player who won the Ludo game.
    • onTokenCaptured

      public void onTokenCaptured(Player player, LudoToken token, int oldTileId)
      Handles the tokenCaptured event from the LudoBoardGame model. Adds an entry to the game log, triggers the visual movement of the captured token back to its start area on the LudoGameStackPane, and updates the affected player's displayed information.
      Parameters:
      player - The Player whose token was captured.
      token - The LudoToken that was captured.
    • onTokenFinished

      public void onTokenFinished(Player player, LudoToken token)
      Handles the tokenFinished event from the LudoBoardGame model. Adds an entry to the game log and updates the player's displayed information (count of finished tokens).
      Parameters:
      player - The Player whose token reached the finish.
      token - The LudoToken that finished.
    • onTurnSkipped

      public void onTurnSkipped(Player player, int diceRoll)
      Handles the turnSkipped event from the LudoBoardGame model. Adds an entry to the game log indicating that the player rolled but had no valid moves, thus skipping their turn.
      Parameters:
      player - The Player whose turn was skipped.
      diceRoll - The dice roll that resulted in the skipped turn.
    • onButtonClicked

      public void onButtonClicked(String buttonId)
      Handles button click events from the LudoGameView. Delegates actions based on the button ID:
      Specified by:
      onButtonClicked in interface ButtonClickObserver
      Specified by:
      onButtonClicked in class GameController
      Parameters:
      buttonId - The ID of the button that was clicked.
    • onButtonClickedWithParams

      public void onButtonClickedWithParams(String buttonId, Map<String,Object> params)
      Handles button click events that may include parameters. This method is not currently used in LudoGameController.
      Specified by:
      onButtonClickedWithParams in interface ButtonClickObserver
      Specified by:
      onButtonClickedWithParams in class GameController
      Parameters:
      buttonId - The ID of the clicked button.
      params - A map of parameters associated with the button click.