Class LudoBoardGame

java.lang.Object
edu.ntnu.idi.idatt.model.game.BoardGame
edu.ntnu.idi.idatt.model.game.LudoBoardGame
All Implemented Interfaces:
Game, BoardGameSubject

public class LudoBoardGame extends BoardGame
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:
  • Constructor Details

    • LudoBoardGame

      public LudoBoardGame(Board board, List<Player> players, int diceCount)
      Constructs a new LudoBoardGame.
      Parameters:
      board - The Board (expected to be a LudoGameBoard) for the game.
      players - The list of Players (expected to be LudoPlayer 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 each LudoPlayer onto their respective starting tiles on the LudoGameBoard. The first player in the list is set as the current player.
      Specified by:
      initializeGame in interface Game
      Specified by:
      initializeGame in class BoardGame
    • getWinner

      public Player getWinner()
      Determines if there is a winner in the Ludo game. A player wins if all of their LudoTokens have reached the LudoToken.TokenStatus.FINISHED state.
      Returns:
      The winning Player, or null 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 (via getWinner()), it notifies observers by calling BoardGame.notifyGameFinished(Player).
      Overrides:
      checkWinCondition in class BoardGame
    • 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:
      1. If the player can move a token (i.e., has released tokens), moveToken(int) is called.
      2. If the player cannot move but rolled a 6, releaseToken() is called.
      3. Otherwise (cannot move and did not roll a 6), the turn is skipped, and observers are notified.
      After the action, it checks for finished tokens, checks the win condition, updates the current player, and handles round number incrementation.
      Parameters:
      diceRoll - The result of the dice roll for this turn.
    • moveToken

      public void moveToken(int diceRoll)
      Moves a LudoToken of the current player by the given dice roll. It selects the first available token with status LudoToken.TokenStatus.RELEASED, calculates its new tile using findNextTile(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.