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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGameController
(GameView gameView, Board board, List<Player> players) Constructs a GameController. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Disables the roll dice button in the game view.protected void
Enables the roll dice button in the game view.protected Board
getBoard()
Gets the game board.Gets the list of players in the game.protected int
Gets the current round number of the game.protected void
Handles the action triggered by the roll dice button.abstract void
initializeBoardGame
(Board board, List<Player> players) Initializes the specificBoardGame
instance for the game.void
Initializes theGameView
with the initial game state.protected void
navigateToGameFinished
(Map<String, Object> params) Navigates to the game finished view.abstract void
onButtonClicked
(String buttonId) Handles button click events without parameters from theGameView
.abstract void
onButtonClickedWithParams
(String buttonId, Map<String, Object> params) Handles button click events with parameters from theGameView
.abstract void
onCurrentPlayerChanged
(Player player) Called when the current player in theBoardGame
model changes.abstract void
onGameFinished
(Player winner) Called when theBoardGame
model signals that the game has finished.abstract void
onRoundNumberIncremented
(int roundNumber) Called when the round number in theBoardGame
model is incremented.protected abstract void
Executes a single turn for the current player.protected abstract void
Executes a full turn for all players in the game sequentially.protected void
quitGame()
Executes the quit game action, if one has been set.protected abstract void
Restarts the current game.void
setOnNavigateToGameFinished
(Consumer<Map<String, Object>> onNavigateToGameFinished) Sets theConsumer
action to be executed when the game is finished.void
setOnQuitGame
(Runnable onQuitGame) Sets theRunnable
action to be executed when the game is quit.
-
Field Details
-
gameView
-
logger
protected final org.slf4j.Logger logger -
boardGame
-
gameFinishedParams
-
onQuitGame
Runnable action to execute when the game is quit.
-
-
Constructor Details
-
Method Details
-
initializeBoardGame
Initializes the specificBoardGame
instance for the game. This method must be implemented by subclasses to create and configure the appropriate game model. -
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 theGameView
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
Gets the list of players in the game.- Returns:
- A list of
Player
s.
-
getBoard
Gets the game board.- Returns:
- The
Board
object.
-
setOnQuitGame
Sets theRunnable
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. -
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
Handles button click events without parameters from theGameView
. Subclasses must implement this to define actions for specific button IDs.- Specified by:
onButtonClicked
in interfaceButtonClickObserver
- Parameters:
buttonId
- The ID of the button that was clicked.
-
onButtonClickedWithParams
Handles button click events with parameters from theGameView
. Subclasses must implement this to define actions for specific button IDs that require parameters.- Specified by:
onButtonClickedWithParams
in interfaceButtonClickObserver
- 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 theBoardGame
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 interfaceBoardGameObserver
- Parameters:
roundNumber
- The new round number.
-
onCurrentPlayerChanged
Called when the current player in theBoardGame
model changes. Subclasses must implement this to update the view or perform other actions related to the change of turn.- Specified by:
onCurrentPlayerChanged
in interfaceBoardGameObserver
- Parameters:
player
- The new currentPlayer
.
-
onGameFinished
Called when theBoardGame
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 interfaceBoardGameObserver
- Parameters:
winner
- ThePlayer
who won the game.
-