Bird
0
0
LLDsystem_design~7 mins

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

Choose your learning style9 modes available
Problem Statement
When designing a game, mixing responsibilities like managing the game state, player actions, and board layout in one place causes confusion and bugs. Without clear separation, the code becomes hard to maintain, test, and extend for new game rules or features.
Solution
Separate the game into three classes: Board to manage the game layout and state, Player to represent each participant and their actions, and Game to control the overall flow and rules. This clear division lets each class focus on one job, making the system easier to understand and modify.
Architecture
┌─────────┐       ┌─────────┐       ┌─────────┐
│  Player │──────▶│   Game  │──────▶│  Board  │
└─────────┘       └─────────┘       └─────────┘
      ▲                                │
      │                                │
      └────────────────────────────────┘

This diagram shows Player interacting with Game, which manages the Board. The Board holds the game state, while Game controls the rules and flow.

Trade-offs
✓ Pros
Clear separation of concerns improves code readability and maintainability.
Easier to add new features like different game rules or player types.
Simplifies testing each part independently.
✗ Cons
Requires more initial design effort to define clear interfaces.
May introduce slight overhead in communication between classes.
Beginners might find multiple classes harder to grasp at first.
Use when building any turn-based or board game with multiple players and a defined game state, especially if the game rules might evolve or the codebase will grow.
Avoid if the game is extremely simple (e.g., single-player with no complex state) or a quick prototype where speed of coding is more important than structure.
Real World Examples
Google
Google's online chess game uses separate classes for Board, Player, and Game to manage moves, enforce rules, and track game state cleanly.
Amazon
Amazon's game development teams use this pattern to build scalable multiplayer games with clear role separation.
Discord
Discord bots that run games like tic-tac-toe use distinct classes to handle player input, game logic, and board state.
Code Example
The before code mixes board state and player turns inside one Game class, making it hard to extend. The after code splits responsibilities: Board manages the grid, Player holds player info, and Game controls turns and rules. This separation improves clarity and flexibility.
LLD
### Before: Monolithic Game class without separation
class Game:
    def __init__(self):
        self.board = [['' for _ in range(3)] for _ in range(3)]
        self.players = ['X', 'O']
        self.current_player = 0

    def make_move(self, row, col):
        if self.board[row][col] == '':
            self.board[row][col] = self.players[self.current_player]
            self.current_player = 1 - self.current_player

### After: Separate Board, Player, and Game classes
class Board:
    def __init__(self, size=3):
        self.size = size
        self.grid = [['' for _ in range(size)] for _ in range(size)]

    def place_mark(self, row, col, mark):
        if self.grid[row][col] == '':
            self.grid[row][col] = mark
            return True
        return False

class Player:
    def __init__(self, name, mark):
        self.name = name
        self.mark = mark

class Game:
    def __init__(self, player1, player2, board):
        self.board = board
        self.players = [player1, player2]
        self.current_index = 0

    def make_move(self, row, col):
        player = self.players[self.current_index]
        if self.board.place_mark(row, col, player.mark):
            self.current_index = 1 - self.current_index
            return True
        return False
OutputSuccess
Alternatives
Monolithic Game Class
Combines board, player, and game logic into one class without separation.
Use when: Choose when building very simple games or quick prototypes where design overhead is not justified.
Entity-Component-System (ECS)
Uses entities with components and systems to manage game state and behavior instead of fixed classes.
Use when: Choose for complex games requiring high flexibility and performance, like real-time strategy or action games.
Summary
Separating Board, Player, and Game classes prevents mixing responsibilities and reduces bugs.
Each class focuses on a single role: Board for state, Player for participants, Game for rules and flow.
This design improves code clarity, extensibility, and testing.