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.
-1.6 to move a token from the yard to relative track position 0.0 through 51 represent the common track.52 through 57 represent the player's private home lane.58 means that the token has finished.58.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.
playerIds takes the first turn.6 gives the same player another turn.LudoGame()
Creates an empty Ludo game manager.
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.
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.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 -1TRACK for positions 0 through 51HOME for positions 52 through 57FINISHED for position 58Token entries are returned in player order. Within each player, tokens are ordered by increasing token number.
2 ≤ playerIds.size() ≤ 41 ≤ gameId.length() ≤ 501 ≤ playerIds.get(i).length() ≤ 50gameId supplied to createGame is unique.gameId passed to another method identifies an existing game.playerId passed to playTurn belongs to the specified game.1 ≤ diceValue ≤ 60 ≤ tokenNumber ≤ 4100,000 method calls are made across all games.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"].
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"].
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.