Bird
0
0
LLDsystem_design~25 mins

Board, Player, Game classes in LLD - System Design Exercise

Choose your learning style9 modes available
Design: Turn-based Game System
Design classes for Board, Player, and Game to manage game logic and state. UI, network communication, and persistence are out of scope.
Functional Requirements
FR1: Support multiple players in a game
FR2: Manage the game board state
FR3: Allow players to take turns making moves
FR4: Track player scores and game progress
FR5: Detect game end conditions (win, draw, etc.)
Non-Functional Requirements
NFR1: Support up to 4 players per game
NFR2: Each move must be processed within 100ms
NFR3: System should be designed for easy extension to different game types
NFR4: Maintain game state consistency during concurrent player actions
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
Board class to represent game state
Player class to represent each participant
Game class to manage game flow and rules
Design Patterns
State pattern for managing game phases
Observer pattern for notifying players of state changes
Command pattern for encapsulating player moves
Reference Architecture
  +----------------+       +----------------+       +----------------+
  |    Player      |<----->|     Game       |<----->|     Board      |
  +----------------+       +----------------+       +----------------+
         ^                        ^                        ^
         |                        |                        |
  playerId, name           gameId, players          boardState, size
  score, status            currentTurn, status      update(), reset()
Components
Player
Class
Represents a player with identity, score, and status in the game
Board
Class
Maintains the current state of the game board and provides methods to update and query it
Game
Class
Controls game flow, manages players and board, enforces rules, and tracks game progress
Request Flow
1. 1. Game initializes Board and Player instances.
2. 2. Game sets the first player's turn.
3. 3. Player makes a move by invoking Game's move method.
4. 4. Game validates the move and updates Board state.
5. 5. Game updates Player scores and checks for end conditions.
6. 6. Game switches turn to the next Player.
7. 7. Repeat steps 3-6 until game ends.
Database Schema
Entities: - Player: player_id (PK), name, score, status - Game: game_id (PK), current_turn (FK to Player), status - Board: board_id (PK), game_id (FK to Game), state (serialized board data) Relationships: - Game has many Players - Game has one Board - Player participates in many Games (if multiplayer across sessions)
Scaling Discussion
Bottlenecks
Handling concurrent moves from multiple players
Maintaining consistent game state under concurrent access
Extending to support multiple simultaneous games
Solutions
Use locking or transactional mechanisms to serialize moves per game instance
Implement optimistic concurrency control with versioning for board state
Design Game instances to be independent and scalable horizontally
Interview Tips
Time: Spend 10 minutes clarifying requirements and scope, 20 minutes designing classes and interactions, 10 minutes discussing scaling and improvements, 5 minutes for questions.
Explain clear separation of concerns between Board, Player, and Game classes
Discuss how turns and moves are managed to ensure fairness and consistency
Highlight extensibility for different game types and rules
Address concurrency and state consistency challenges
Mention design patterns that improve maintainability and flexibility