0
0
LLDsystem_design~7 mins

Piece movement rules (polymorphism) in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When implementing a board game like chess, hardcoding movement rules for each piece in a single class leads to complex, tangled code that is hard to maintain and extend. Adding new piece types or changing rules requires modifying existing code, increasing the risk of bugs and slowing development.
Solution
Use polymorphism by defining a base Piece class with a method for movement rules, then create subclasses for each piece type that override this method with their specific movement logic. This separates concerns, making the code easier to read, maintain, and extend without changing existing classes.
Architecture
Piece
───────────
Pawn
───────────

This diagram shows a base Piece class with a move method, and subclasses Pawn, Knight, and Bishop each implementing their own move method to define specific movement rules.

Trade-offs
✓ Pros
Separates movement logic for each piece, improving code clarity and maintainability.
Makes it easy to add new piece types without modifying existing code.
Supports polymorphic behavior, allowing uniform handling of different pieces.
✗ Cons
May introduce more classes, increasing the number of files and complexity in small projects.
Requires understanding of object-oriented principles, which might be challenging for beginners.
Overhead of dynamic dispatch can slightly impact performance in very tight loops.
Use when the system has multiple piece types with distinct movement rules and you expect to add or modify pieces over time, especially in games like chess or checkers with complex rules.
Avoid when the game has very few piece types with simple, similar movement rules or when performance is critical and the overhead of polymorphism is unacceptable.
Real World Examples
Chess.com
Uses polymorphism to implement different chess piece movement rules, allowing easy updates and extensions to game logic.
Lichess
Separates piece movement logic into classes to maintain clean code and support variants with different piece behaviors.
Ubisoft
In strategy games, uses polymorphism to handle unit movement rules, enabling diverse unit types with unique behaviors.
Code Example
The before code uses a single class with conditional statements to handle movement rules, making it hard to maintain and extend. The after code uses polymorphism by defining a base Piece class and subclasses for each piece type, each implementing its own move method. This design is cleaner, easier to extend, and follows object-oriented principles.
LLD
### Before: Without polymorphism (all logic in one class)
class Piece:
    def __init__(self, type):
        self.type = type

    def move(self, position):
        if self.type == 'pawn':
            # pawn movement logic
            return 'pawn moves'
        elif self.type == 'knight':
            # knight movement logic
            return 'knight moves'
        # more conditions for other pieces


### After: With polymorphism (each piece has its own class)
class Piece:
    def move(self, position):
        raise NotImplementedError()

class Pawn(Piece):
    def move(self, position):
        return 'pawn moves'

class Knight(Piece):
    def move(self, position):
        return 'knight moves'

# Usage
pieces = [Pawn(), Knight()]
for p in pieces:
    print(p.move(None))
OutputSuccess
Alternatives
Procedural conditional logic
Implements all movement rules in a single function using conditionals to check piece type.
Use when: Choose when the number of piece types is very small and unlikely to change, and simplicity is preferred over extensibility.
Data-driven movement rules
Stores movement rules in data structures (e.g., arrays or config files) instead of code, interpreted at runtime.
Use when: Choose when you want to allow non-developers to modify rules without code changes or support many variants dynamically.
Summary
Hardcoding piece movement rules in one place leads to tangled, hard-to-maintain code.
Polymorphism lets each piece type define its own movement logic in separate classes.
This approach improves code clarity, extensibility, and supports adding new pieces easily.