Design an Online Tic-Tac-Toe Game

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-06-06

Problem Statement

Design a scalable, real-time Online Tic-Tac-Toe Game that allows users to:


•Play in real-time against friends or random players
•Handle matchmaking, move updates, and game result evaluation
•Support both private (invite-based) and public (random) matches
•Optionally persist match history, game states, and support replays


🎯 Functional Requirements:

1.User Registration & Login
•Authentication using email, phone, or social logins
2.Matchmaking
•Random pairing with online users
•Private game invites via unique game codes
3.Game Mechanics
•3x3 board with turn-based play
•Validate moves and detect win/draw conditions
•Notify both players in real-time of opponent's moves
4.Real-Time Communication
•Use WebSockets or polling to sync moves
5.Game History (Optional)
•Store previous matches, winners, move history
6.Game Termination
•Detect disconnects, resignations, and inactivity

⚙️ Non-Functional Requirements:

•Low latency for real-time gameplay (<200ms)
•Scalability to support thousands of concurrent players
•High availability and graceful failure handling
•Secure communication and move validation

🧱 Core Components:

ComponentDescription

Frontend

UI for board, moves, matchmaking

Game Service

Handles game creation, move validation, state updates

Matchmaking Service

Pairs players randomly or via invite

WebSocket Server

Real-time communication between clients

State Store

Stores current board state per match (Redis/in-memory)

Persistence Layer

Stores match history, players, results (DB)

Notification Service

Sends move updates, game results

Auth Service

Manages user sessions and authentication


🔁 Game Flow:

1.User logs in
2.Chooses to create/join a match
3.Matchmaking service pairs two users
4.Game service initializes game board
5.Players take turns; each move is validated and updated
6.Game ends with win/loss/draw or disconnect
7.Final result is stored and optionally shown in history

🧮 Move Validation Example:

1json 2CopyEdit 3POST /move 4{ 5 "match_id": "abc123", 6 "user_id": "user42", 7 "position": [1, 2] // row, col 8}

Backend:

•Checks if it's the user’s turn
•Checks if the cell is empty
•Updates board and checks win/draw
•Sends real-time update via WebSocket to both users

🗃️ Game State Schema (Redis or in-memory):

1json 2CopyEdit 3{ 4 "match_id": "abc123", 5 "players": ["user1", "user2"], 6 "board": [ 7 ["X", "O", ""], 8 ["", "X", ""], 9 ["", "", "O"] 10 ], 11 "next_turn": "user1", 12 "status": "IN_PROGRESS" 13}

🧰 Tech Stack Suggestions:

LayerTools

Frontend

React / Flutter / Android iOS

Backend API

Node.js / Django / Spring Boot

Real-time Comm

WebSockets (Socket.io / SignalR)

Data Store

Redis (game state), PostgreSQL/MongoDB (user & history)

Auth

Firebase Auth / OAuth / JWT

Deployment

Docker + Kubernetes + Load Balancer


🔐 Security & Fair Play:

•Validate moves server-side
•Prevent replay attacks with move signatures
•Rate-limit move requests
•Ensure correct turn order and reject illegal moves


Solution

Comments