Class LudoGameController
java.lang.Object
edu.ntnu.idi.idatt.controller.common.GameController
edu.ntnu.idi.idatt.controller.ludo.LudoGameController
- All Implemented Interfaces:
BoardGameObserver
,ButtonClickObserver
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 givenBoard
and list ofLudoPlayer
s. - 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:
-
Field Summary
Fields inherited from class edu.ntnu.idi.idatt.controller.common.GameController
boardGame, gameFinishedParams, gameView, logger, onNavigateToGameFinished, onQuitGame
-
Constructor Summary
ConstructorsConstructorDescriptionLudoGameController
(LudoGameView ludoGameView, Board board, List<Player> players) Constructs a newLudoGameController
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
initializeBoardGame
(Board board, List<Player> players) Initializes theGameController.boardGame
instance as aLudoBoardGame
.void
onButtonClicked
(String buttonId) Handles button click events from theLudoGameView
.void
onButtonClickedWithParams
(String buttonId, Map<String, Object> params) Handles button click events that may include parameters.void
onCurrentPlayerChanged
(Player player) Handles thecurrentPlayerChanged
event from theLudoBoardGame
model.void
onGameFinished
(Player winner) Handles thegameFinished
event from theLudoBoardGame
model.void
onRoundNumberIncremented
(int roundNumber) Handles theroundNumberIncremented
event from theLudoBoardGame
model.void
onTokenCaptured
(Player player, LudoToken token, int oldTileId) Handles thetokenCaptured
event from theLudoBoardGame
model.void
onTokenFinished
(Player player, LudoToken token) Handles thetokenFinished
event from theLudoBoardGame
model.void
onTokenMoved
(Player player, LudoToken token, int diceRoll, int oldTileId, int newTileId) Handles thetokenMoved
event from theLudoBoardGame
model.void
onTokenReleased
(Player player, int tileId, int tokenId) Handles thetokenReleased
event from theLudoBoardGame
model.void
onTurnSkipped
(Player player, int diceRoll) Handles theturnSkipped
event from theLudoBoardGame
model.protected void
Executes a single turn for the current player in theLudoBoardGame
.protected void
Performs turns for all players sequentially until it is the first player's turn again.protected void
Restarts the current Ludo game.Methods inherited from class edu.ntnu.idi.idatt.controller.common.GameController
disableRollDiceButton, enableRollDiceButton, getBoard, getPlayers, getRoundNumber, handleRollDiceButtonAction, initializeGameView, navigateToGameFinished, quitGame, setOnNavigateToGameFinished, setOnQuitGame
-
Constructor Details
-
LudoGameController
Constructs a newLudoGameController
.- Parameters:
ludoGameView
- TheLudoGameView
associated with this controller.board
- TheBoard
(specifically aLudoGameBoard
) for the game.players
- The list ofPlayer
s (expected to beLudoPlayer
instances) participating in the game.
-
-
Method Details
-
initializeBoardGame
Initializes theGameController.boardGame
instance as aLudoBoardGame
. 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 classGameController
- Parameters:
board
- TheBoard
for the Ludo game.players
- The list ofPlayer
s 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 classGameController
-
performPlayerTurn
protected void performPlayerTurn()Executes a single turn for the current player in theLudoBoardGame
. This involves:- Disabling the roll dice button in the view.
- Simulating a dice roll in the
LudoBoardGame
. - Triggering an animation for the dice roll in the
LudoGameView
. - Upon completion of the animation, performing the player's turn in the game model using the dice roll.
- Re-enabling the roll dice button in the view.
- Specified by:
performPlayerTurn
in classGameController
-
onTokenReleased
Handles thetokenReleased
event from theLudoBoardGame
model. Adds an entry to the game log in the view, triggers the visual release of the token on theLudoGameStackPane
, and updates the player's displayed information.- Parameters:
player
- ThePlayer
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:- Creating a new list of
LudoPlayer
s based on the existing players (preserving their names, colors, token types, and bot status). - Re-initializing the
GameController.boardGame
with the same board but the new list of player instances. - Re-initializing the
LudoGameView
to reflect the new game state.
- Specified by:
restartGame
in classGameController
- Creating a new list of
-
onTokenMoved
public void onTokenMoved(Player player, LudoToken token, int diceRoll, int oldTileId, int newTileId) Handles thetokenMoved
event from theLudoBoardGame
model. Adds an entry to the game log, triggers the visual movement of the token on theLudoGameStackPane
, and updates the player's displayed information. -
onRoundNumberIncremented
public void onRoundNumberIncremented(int roundNumber) Handles theroundNumberIncremented
event from theLudoBoardGame
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 interfaceBoardGameObserver
- Specified by:
onRoundNumberIncremented
in classGameController
- Parameters:
roundNumber
- The new round number.
-
onCurrentPlayerChanged
Handles thecurrentPlayerChanged
event from theLudoBoardGame
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 interfaceBoardGameObserver
- Specified by:
onCurrentPlayerChanged
in classGameController
- Parameters:
player
- The new currentPlayer
.
-
onGameFinished
Handles thegameFinished
event from theLudoBoardGame
model. Adds an entry to the game log in the view announcing the winner.- Specified by:
onGameFinished
in interfaceBoardGameObserver
- Specified by:
onGameFinished
in classGameController
- Parameters:
winner
- ThePlayer
who won the Ludo game.
-
onTokenCaptured
Handles thetokenCaptured
event from theLudoBoardGame
model. Adds an entry to the game log, triggers the visual movement of the captured token back to its start area on theLudoGameStackPane
, and updates the affected player's displayed information. -
onTokenFinished
Handles thetokenFinished
event from theLudoBoardGame
model. Adds an entry to the game log and updates the player's displayed information (count of finished tokens). -
onTurnSkipped
Handles theturnSkipped
event from theLudoBoardGame
model. Adds an entry to the game log indicating that the player rolled but had no valid moves, thus skipping their turn.- Parameters:
player
- ThePlayer
whose turn was skipped.diceRoll
- The dice roll that resulted in the skipped turn.
-
onButtonClicked
Handles button click events from theLudoGameView
. Delegates actions based on the button ID:- "roll_dice": Calls
GameController.handleRollDiceButtonAction()
. - "restart_game": Calls
restartGame()
. - "quit_game": Calls
GameController.quitGame()
.
- Specified by:
onButtonClicked
in interfaceButtonClickObserver
- Specified by:
onButtonClicked
in classGameController
- Parameters:
buttonId
- The ID of the button that was clicked.
- "roll_dice": Calls
-
onButtonClickedWithParams
Handles button click events that may include parameters. This method is not currently used inLudoGameController
.- Specified by:
onButtonClickedWithParams
in interfaceButtonClickObserver
- Specified by:
onButtonClickedWithParams
in classGameController
- Parameters:
buttonId
- The ID of the clicked button.params
- A map of parameters associated with the button click.
-