394. Design Ludo Board Game
Asked in
Design Ludo Board Game

Design a Ludo board game for two to four players. Players take turns moving their four tokens according to a supplied dice value.

A token must leave the yard, travel around the common track, enter its home lane and reach the finish position. The first player to finish all four tokens wins the game.

Board and Token Positions

  • Every token begins in the yard at position -1.
  • A player must use a dice value of 6 to move a token from the yard to relative track position 0.
  • Relative positions 0 through 51 represent the common track.
  • Relative positions 52 through 57 represent the player's private home lane.
  • Position 58 means that the token has finished.
  • A token on the board advances by exactly the supplied dice value.
  • A move is invalid when it would place a token beyond position 58.
  • A finished token cannot be moved again.

Common Track

Players use the order supplied when the game is created. A player at index i has the common-track entry offset 13 * i.

For a token at relative position p, where 0 ≤ p ≤ 51, its absolute common-track position is:

(13 * i + p) % 52

Absolute positions 0, 8, 13, 21, 26, 34, 39 and 47 are safe positions.

Capturing Tokens

  • When a token lands on a non-safe common-track position, every opposing token on that position is returned to its yard.
  • Opposing tokens may share a safe position without being captured.
  • Tokens cannot be captured inside a home lane or after finishing.
  • Multiple tokens belonging to the same player may share a position.
  • Tokens do not block other tokens from passing through a position.

Turns

  • The first player in playerIds takes the first turn.
  • After a valid move or pass, the turn moves to the next player in the supplied order.
  • A dice value of 6 gives the same player another turn.
  • There is no penalty for receiving three consecutive sixes.
  • An invalid action does not change any token or advance the turn.
  • Because dice results are supplied as input, the game does not generate random values.

Class

LudoGame()

Creates an empty Ludo game manager.

Method Signatures

Create a Game

void createGame(String gameId, List<String> playerIds)

Creates a game with the given identifier and players. Every player receives four tokens numbered from 1 through 4. The order in playerIds determines the turn order and each player's common-track entry position.

Play a Turn

String playTurn(String gameId, String playerId, int diceValue, int tokenNumber)

Plays one turn using the supplied dice value. A tokenNumber from 1 through 4 selects a token to move. A tokenNumber of 0 requests a pass.

A pass is valid only when none of the player's tokens can move using the supplied dice value.

The method returns one of these deterministic status strings:

  • "MOVED": The selected token was moved.
  • "MOVED_AND_CAPTURED": The token moved and captured at least one opposing token.
  • "TOKEN_FINISHED": The token reached position 58, but the player has not yet won.
  • "PLAYER_WON": The move finished the player's fourth token.
  • "PASSED": The requested pass was valid.
  • "INVALID_MOVE": The player is not the current player, the selected token cannot make the requested move or a pass was requested when another legal move exists.
  • "GAME_OVER": The game already has a winner.

Get the Game State

List<String> getGameState(String gameId)

Returns the complete game state as comma-separated strings. The first entry is "turn,playerId", or "turn,NONE" after the game ends. The second entry is "winner,playerId", or "winner,NONE" when there is no winner.

Each remaining entry uses this format:

"playerId,tokenNumber,location,position"

The location is one of the following values:

  • YARD for position -1
  • TRACK for positions 0 through 51
  • HOME for positions 52 through 57
  • FINISHED for position 58

Token entries are returned in player order. Within each player, tokens are ordered by increasing token number.

Constraints

  • 2 ≤ playerIds.size() ≤ 4
  • 1 ≤ gameId.length() ≤ 50
  • 1 ≤ playerIds.get(i).length() ≤ 50
  • Game and player identifiers contain only lowercase English letters, digits and hyphens.
  • Every gameId supplied to createGame is unique.
  • All player identifiers within the same game are different.
  • Every gameId passed to another method identifies an existing game.
  • Every playerId passed to playTurn belongs to the specified game.
  • 1 ≤ diceValue ≤ 6
  • 0 ≤ tokenNumber ≤ 4
  • At most 100,000 method calls are made across all games.
  • No parameter value is null.

Examples

Example 1

LudoGame()

createGame(gameId = "game-1", playerIds = ["red", "blue"])

playTurn(gameId = "game-1", playerId = "red", diceValue = 6, tokenNumber = 1)

Returns "MOVED". Red's first token enters the track, and red receives another turn.

playTurn(gameId = "game-1", playerId = "red", diceValue = 3, tokenNumber = 1)

Returns "MOVED". The token advances to relative position 3.

playTurn(gameId = "game-1", playerId = "blue", diceValue = 4, tokenNumber = 0)

Returns "PASSED" because none of blue's tokens can leave the yard with a dice value of 4.

getGameState(gameId = "game-1")

Returns ["turn,red", "winner,NONE", "red,1,TRACK,3", "red,2,YARD,-1", "red,3,YARD,-1", "red,4,YARD,-1", "blue,1,YARD,-1", "blue,2,YARD,-1", "blue,3,YARD,-1", "blue,4,YARD,-1"].

Example 2

LudoGame()

createGame(gameId = "capture-game", playerIds = ["alpha", "beta"])

playTurn(gameId = "capture-game", playerId = "alpha", diceValue = 6, tokenNumber = 1)

Returns "MOVED".

playTurn(gameId = "capture-game", playerId = "alpha", diceValue = 6, tokenNumber = 1)

Returns "MOVED".

playTurn(gameId = "capture-game", playerId = "alpha", diceValue = 6, tokenNumber = 1)

Returns "MOVED".

playTurn(gameId = "capture-game", playerId = "alpha", diceValue = 2, tokenNumber = 1)

Returns "MOVED". Alpha's token reaches relative position 14.

playTurn(gameId = "capture-game", playerId = "beta", diceValue = 6, tokenNumber = 1)

Returns "MOVED".

playTurn(gameId = "capture-game", playerId = "beta", diceValue = 1, tokenNumber = 1)

Returns "MOVED_AND_CAPTURED". Beta's relative position 1 and alpha's relative position 14 both map to absolute position 14, which is not safe.

getGameState(gameId = "capture-game")

Returns ["turn,alpha", "winner,NONE", "alpha,1,YARD,-1", "alpha,2,YARD,-1", "alpha,3,YARD,-1", "alpha,4,YARD,-1", "beta,1,TRACK,1", "beta,2,YARD,-1", "beta,3,YARD,-1", "beta,4,YARD,-1"].

Example 3

LudoGame()

createGame(gameId = "pass-game", playerIds = ["sun", "moon"])

playTurn(gameId = "pass-game", playerId = "sun", diceValue = 3, tokenNumber = 1)

Returns "INVALID_MOVE" because a token cannot leave the yard without a dice value of 6. The turn remains with sun.

playTurn(gameId = "pass-game", playerId = "sun", diceValue = 3, tokenNumber = 0)

Returns "PASSED" because sun has no legal move. The turn advances to moon.



Please use Laptop/Desktop or any other large screen to add/edit code.