Package edu.ntnu.idi.idatt.model.game
Class LudoBoardGame
java.lang.Object
edu.ntnu.idi.idatt.model.game.BoardGame
edu.ntnu.idi.idatt.model.game.LudoBoardGame
- All Implemented Interfaces:
Game
,BoardGameSubject
LudoBoardGame.
This class extends BoardGame
to implement the specific rules and logic for a Ludo
game. It manages the game state, player turns, token movements, and win conditions for Ludo.
Key Ludo-specific functionalities include:
- Initializing player tokens to their starting positions.
- Determining the winner based on all tokens of a player reaching their finished state.
- Handling dice rolls (typically a single die for Ludo).
- Player turn logic: moving a token if possible, releasing a token from the start area on a roll of 6, or skipping the turn if no valid move can be made.
- Calculating the next tile for a token based on the dice roll, considering the Ludo track and finish paths.
- Releasing tokens from their start area to the main track.
- Moving tokens along the track and into their respective finish areas.
- Handling token captures: sending an opponent's token back to its start area if landed upon.
- Notifying observers (like
LudoGameController
) of game events such as token release, movement, capture, finish, and turn skips.
- See Also:
-
Field Summary
Fields inherited from class edu.ntnu.idi.idatt.model.game.BoardGame
board, currentPlayer, dice, logger, observers, players, roundNumber
-
Constructor Summary
ConstructorsConstructorDescriptionLudoBoardGame
(Board board, List<Player> players, int diceCount) Constructs a newLudoBoardGame
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Checks if the win condition for the Ludo game has been met.Determines if there is a winner in the Ludo game.void
Initializes the Ludo game.void
moveToken
(int diceRoll) Moves aLudoToken
of the current player by the given dice roll.void
performPlayerTurn
(int diceRoll) Performs a turn for the current player based on the given dice roll.int
rollDice()
Rolls the game's dice (typically one die for Ludo) and returns the total value.Methods inherited from class edu.ntnu.idi.idatt.model.game.BoardGame
addObserver, createDice, getBoard, getCurrentPlayer, getDice, getPlayers, getRoundNumber, handleRoundNumber, incrementRoundNumber, notifyCurrentPlayerChanged, notifyGameFinished, notifyRoundNumberIncremented, removeObserver, setBoard, setCurrentPlayer, setPlayers, updateCurrentPlayer
-
Constructor Details
-
LudoBoardGame
Constructs a newLudoBoardGame
.- Parameters:
board
- TheBoard
(expected to be aLudoGameBoard
) for the game.players
- The list ofPlayer
s (expected to beLudoPlayer
instances) participating in the game.diceCount
- The number of dice to use in the game (typically 1 for Ludo).
-
-
Method Details
-
initializeGame
public void initializeGame()Initializes the Ludo game. This involves placing all tokens of eachLudoPlayer
onto their respective starting tiles on theLudoGameBoard
. The first player in the list is set as the current player.- Specified by:
initializeGame
in interfaceGame
- Specified by:
initializeGame
in classBoardGame
-
getWinner
Determines if there is a winner in the Ludo game. A player wins if all of theirLudoToken
s have reached theLudoToken.TokenStatus.FINISHED
state.- Returns:
- The winning
Player
, ornull
if no player has won yet.
-
checkWinCondition
protected void checkWinCondition()Checks if the win condition for the Ludo game has been met. If a winner is found (viagetWinner()
), it notifies observers by callingBoardGame.notifyGameFinished(Player)
.- Overrides:
checkWinCondition
in classBoardGame
-
rollDice
public int rollDice()Rolls the game's dice (typically one die for Ludo) and returns the total value. Logs the dice roll result.- Returns:
- The total value rolled on the dice.
-
performPlayerTurn
public void performPlayerTurn(int diceRoll) Performs a turn for the current player based on the given dice roll. The logic is as follows:- If the player can move a token (i.e., has released tokens),
moveToken(int)
is called. - If the player cannot move but rolled a 6,
releaseToken()
is called. - Otherwise (cannot move and did not roll a 6), the turn is skipped, and observers are notified.
- Parameters:
diceRoll
- The result of the dice roll for this turn.
- If the player can move a token (i.e., has released tokens),
-
moveToken
public void moveToken(int diceRoll) Moves aLudoToken
of the current player by the given dice roll. It selects the first available token with statusLudoToken.TokenStatus.RELEASED
, calculates its new tile usingfindNextTile(LudoToken, int)
, updates the token's current tile, and notifies observers of the move. It then checks for and handles any token captures at the new tile.- Parameters:
diceRoll
- The number of steps to move the token, determined by the dice roll.
-