Class LadderGameController

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

public class LadderGameController extends GameController
LadderGameController.

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

Responsibilities include:

  • Initializing the LadderBoardGame with a given Board and list of Players.
  • Handling player turns, including dice rolling (with animation) and executing moves on the game board.
  • Updating the LadderGameView in response to game state changes, such as player movement, tile actions, round progression, current player changes, and game completion.
  • Processing user interactions from the view, like clicking the "roll dice", "restart game", or "quit game" buttons.
  • Managing the display of player information (e.g., current tile) and game log entries.
See Also:
  • Constructor Details

    • LadderGameController

      public LadderGameController(LadderGameView ladderGameView, Board board, List<Player> players)
      Constructs a new LadderGameController.
      Parameters:
      ladderGameView - The LadderGameView associated with this controller.
      board - The Board (specifically a LadderGameBoard) for the game.
      players - The list of Players participating in the game.
  • Method Details

    • initializeBoardGame

      public void initializeBoardGame(Board board, List<Player> players)
      Initializes the GameController.boardGame instance as a LadderBoardGame. It creates a new game with the provided board and players, and sets the number of dice to 2. This controller is also added as an observer to the newly created game model. All players are initially placed on the starting tile (tile 0).
      Specified by:
      initializeBoardGame in class GameController
      Parameters:
      board - The Board for the game.
      players - The list of Players.
    • performPlayerTurn

      protected void performPlayerTurn()
      Executes a single turn for the current player in the LadderBoardGame. This involves:
      1. Disabling the roll dice button in the view.
      2. Simulating a dice roll in the LadderBoardGame.
      3. Retrieving the individual dice values.
      4. Triggering an animation for the dice roll in the LadderGameView.
      5. Upon completion of the animation, performing the player's turn in the game model using the total dice roll.
      6. Re-enabling the roll dice button.
      Specified by:
      performPlayerTurn in class GameController
    • 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.
      Specified by:
      performPlayerTurnForAllPlayers in class GameController
    • restartGame

      protected void restartGame()
      Restarts the current game. This involves:
      1. Creating a new list of LadderGamePlayers 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 LadderGameView to reflect the new game state.
      Specified by:
      restartGame in class GameController
    • onPlayerMoved

      public void onPlayerMoved(Player player, int diceRoll, int newTileId)
      Handles the playerMoved event from the LadderBoardGame model. Updates the game log in the view with the player's move, updates the player's displayed tile number, and triggers the visual movement of the player's token on the LadderGameStackPane.
      Parameters:
      player - The Player who moved.
      diceRoll - The result of the dice roll that caused the move.
      newTileId - The ID of the tile the player moved to.
    • onRoundNumberIncremented

      public void onRoundNumberIncremented(int roundNumber)
      Handles the roundNumberIncremented event from the LadderBoardGame 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 LadderBoardGame model. If "roll for all players" is not selected, 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.
    • onTileActionPerformed

      public void onTileActionPerformed(Player player, TileAction tileAction)
      Handles the tileActionPerformed event from the LadderBoardGame model. This occurs when a player lands on a tile with a special action (e.g., ladder, slide). Updates the game log with the action, updates the player's displayed tile number to the destination of the action, and triggers the visual movement of the player's token on the LadderGameStackPane to the action's destination tile.
      Parameters:
      player - The Player who activated the tile action.
      tileAction - The TileAction that was performed.
    • onGameFinished

      public void onGameFinished(Player winner)
      Handles the gameFinished event from the LadderBoardGame model. Navigates to the GameFinishedView, passing the winner and other ranked players.
      Specified by:
      onGameFinished in interface BoardGameObserver
      Specified by:
      onGameFinished in class GameController
      Parameters:
      winner - The Player who won the game.
    • onButtonClicked

      public void onButtonClicked(String buttonId)
      Handles button click events from the LadderGameView. 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 LadderGameController.
      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.