Design an online chess game platform

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-12-13

Problem Statement

Design and implement the core components of an online chess platform that allows players to play real-time matches, maintain game states, validate moves, and track outcomes. The design should focus on object modeling, move validation, and extensibility (timers, spectators, leaderboards).


Functional Requirements (FR)


User Management


•Players can register/login.
•Each player has a profile (username, rating, win/loss record).


Game Management


•A player can create a match or join an open match.
•The system matches two players into a game.
•Support for live games with timers.
•Support multiple concurrent games.


Gameplay


•Represent an 8×8 board with pieces in standard positions.
•Each piece (Pawn, Rook, Knight, Bishop, Queen, King) has unique movement rules.
•Validate moves according to chess rules:
•Legal movement patterns.
•No moving through other pieces (except Knight).
•No self-check moves.
•Castling, pawn promotion, en passant.
•Detect game states:
•Check, Checkmate, Stalemate, Draw (repetition, 50-move rule, insufficient material).


Game Flow


•Enforce turn-based play.
•Timer countdown for each player (optional Blitz/Classic modes).
•Game ends with win/loss/draw.


Persistence & Replay


•Save game history for later replay.
•Store player ratings and update after each match.


Non-Functional Requirements (NFR)


•Concurrency → Multiple games must run independently.
•Consistency → The board state must always remain valid.
•Extensibility → Should allow new features (spectators, AI opponents, tournaments).
•Scalability → Handle thousands of players in real-time.


Core Entities & Attributes


Player


•playerId, username, rating, winCount, lossCount, drawCount


Game


•gameId, playerWhite, playerBlack, board, currentTurn, status (ONGOING/COMPLETED), moveHistory[]


Board


•8x8 grid of Square objects, each holding a Piece or empty.


Piece (Abstract)


•type (Pawn/Rook/etc.), color, currentPosition
•isValidMove(source, destination, board)


Move


•moveId, gameId, player, piece, fromSquare, toSquare, isCapture, isCheck, isCheckmate


Timer (Optional)


•timeRemainingWhite, timeRemainingBlack, mode (blitz/classic).


APIs / Operations


User APIs


•registerPlayer(username, password)
•getPlayerStats(playerId)


Game APIs


•createGame(playerId, mode)
•joinGame(gameId, playerId)
•makeMove(gameId, playerId, from, to)
•getGameState(gameId)
•resignGame(gameId, playerId)


Replay & Stats APIs


•getMoveHistory(gameId)
•getLeaderboard()


LLD Design Goals


•Class Diagram → Player, Game, Board, Square, Piece (with subclasses), Move, GameManager.
•Design Patterns:
•Factory Pattern → For creating chess pieces.
•Strategy Pattern → For different movement rules of each piece.
•Observer Pattern → To notify players/spectators when a move is made.
•State Pattern → For game state transitions (Ongoing → Check → Checkmate/Draw).
•Concurrency Handling → Locking on board state while a move is processed.


Solution

Comments