Design a scalable multiplayer Ludo game

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-04-10

Problem Statement

Design a scalable multiplayer Ludo game that allows users to play online with friends or random opponents. Focus is on object-oriented design, modularity, game state management, and concurrency handling.


✅ Functional Requirements


1.Game Setup
•Support 2-4 players per game.
•Players can create a game room or join an existing room.
•Assign colors (tokens) to players: Red, Green, Blue, Yellow.
2.Gameplay Rules
•Turn-based movement of tokens based on dice roll (1-6).
•Tokens move from home → main path → finish area.
•Capture rules: landing on opponent’s token sends it back to home.
•Dice rolling must be fair and random.
3.Game State Management
•Maintain positions of all tokens, current turn, and dice state.
•Validate moves according to game rules.
•Handle player disconnects and allow reconnections.
4.Multiplayer & Scalability
•Support multiple concurrent game rooms.
•Notify players in real-time about moves (via WebSockets or similar).
•Ensure consistency of game state across clients.
5.Optional Features
•Chat between players.
•Leaderboard and statistics.
•AI players for single-player mode.


🔐 Constraints & Assumptions


•Focus on LLD, game state, and multiplayer handling, not graphics/UI.
•Assume network layer provides reliable messaging between server and clients.
•Game must handle concurrent moves and multiple game rooms safely.


🧱 Key Components & Classes


1.Player Class
1 2class Player { 3private: 4 string playerId; 5 string name; 6 string color; 7 vector<Token*> tokens; 8public: 9 Token* getToken(int tokenId); 10};
1.Token Class
1 2class Token { 3private: 4 int tokenId; 5 int position; // position on board, home, or finish 6public: 7 void move(int steps); 8};
1.Dice Class
1 2class Dice { 3public: 4 int roll(); // returns random number 1-6 5};
1.GameBoard Class
1 2class GameBoard { 3private: 4 int totalCells; 5 map<string, vector<Token*>> playerTokens; // playerId -> tokens 6public: 7 bool validateMove(Token* token, int steps); 8 void updateTokenPosition(Token* token, int steps); 9};
1.GameRoom Class
1 2class GameRoom { 3private: 4 string roomId; 5 vector<Player*> players; 6 GameBoard* board; 7 Dice* dice; 8 int currentTurnIndex; 9public: 10 void addPlayer(Player* player); 11 void startGame(); 12 void playTurn(Player* player); 13 void handleDisconnect(Player* player); 14};
1.GameServer Class
•Manages multiple game rooms concurrently.
•Ensures real-time notifications of moves to all players in a room.
•Handles thread-safe game state updates.


🔄 Example Workflow


1.Player creates a GameRoom → other players join.
2.Game starts → Dice.roll() determines steps.
3.Player chooses a token → GameBoard.validateMove() checks legality → updateTokenPosition().
4.GameServer notifies all players in real-time.
5.Turns rotate → game continues until a player wins.
6.Handle disconnects → allow rejoining and restore game state.


🧪 Expected Deliverables


•Class diagram showing Player, Token, Dice, GameBoard, GameRoom, and GameServer.
•Implementation of game setup, token movement, turn handling, and multiplayer notifications.
•Test cases:
•Single game with 4 players
•Token capture and movement rules
•Player disconnect/reconnect handling
•Multiple concurrent game rooms


💡 Discussion Points


•Concurrency: synchronizing moves in multiplayer environment.
•Scalability: supporting thousands of concurrent game rooms.
•Real-time notifications: WebSockets, server push, or message queues.
•Extensibility for AI players, tournaments, or leaderboards.


Solution

Comments