Design an online game of Tic-Tac-Toe
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-03-02
Problem Statement
Design and implement the low-level architecture of a multiplayer Tic-Tac-Toe game that allows two players to play online, with turn-based gameplay, move validation, and winner detection.
Functional Requirements (FR)
Game Setup
•Players can register/login.
•Players can create a new game or join an existing game.
•Support two-player mode (player vs player).
•Optionally support AI/bot mode for single-player.
Gameplay
•Represent the board as a 3x3 grid.
•Players take alternate turns placing X or O.
•Validate each move (cell must be empty, within board boundaries).
•Detect win conditions (row, column, diagonal).
•Detect draw conditions (board full, no winner).
•Declare result and end game.
Match Management
•Allow multiple matches across different users concurrently.
•Support rematch option.
•Persist game history (optional extension).
Notifications (optional)
•Notify opponent of turn.
•Notify both players of result.
Non-Functional Requirements (NFR)
•Concurrency → Multiple games should run simultaneously without interference.
•Low Latency → Moves should reflect instantly.
•Consistency → No invalid or overlapping moves should be allowed.
•Extensibility → Support larger boards (NxN) or different rule sets in future.
Core Entities & Attributes
Player
•playerId, name, symbol (X/O), score
Game
•gameId, players[2], board, currentTurn, status (WAITING, IN_PROGRESS, COMPLETED), winner
Board
•2D array (3x3) representing the state of the game
Move
•moveId, playerId, gameId, row, column, timestamp
APIs / Operations
Player APIs
•createPlayer(name)
•joinGame(gameId, playerId)
Game APIs
•createGame(playerId)
•makeMove(gameId, playerId, row, column)
•getGameState(gameId)
•checkWinner(gameId)
LLD Design Goals
•Class Diagram → Entities like Player, Game, Board, Move, GameManager.
•Design Patterns:
•Observer Pattern → for notifying players about moves/results.
•Factory Pattern → for creating games (AI vs Player, Player vs Player).
•Singleton → for GameManager managing all active games.
•Handle trade-offs:
•How to store active games (in-memory vs DB).
•How to scale for many concurrent matches.