Bird
0
0
LLDsystem_design~7 mins

Move validation in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
Without proper move validation, a game system can accept illegal moves, causing inconsistent game states and unfair advantages. This leads to confusion, bugs, and a poor user experience as players see impossible or invalid game progressions.
Solution
Move validation checks each player's move against the game's rules before accepting it. It ensures only legal moves update the game state, preventing errors and cheating. This validation can be centralized in a dedicated component that verifies moves and rejects invalid ones.
Architecture
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Player Client │──────▶│ Move Validator│──────▶│ Game State DB │
└───────────────┘       └───────────────┘       └───────────────┘
         │                      │                      ▲
         │                      │                      │
         └──────────────────────┴──────────────────────┘

This diagram shows the flow where the player sends a move to the Move Validator, which checks the move and updates the Game State database only if the move is valid.

Trade-offs
✓ Pros
Prevents illegal moves, maintaining consistent and fair game state.
Centralizes rule enforcement, simplifying debugging and updates.
Improves user experience by rejecting invalid actions immediately.
✗ Cons
Adds processing overhead for each move, potentially increasing latency.
Requires comprehensive rule implementation, which can be complex for some games.
If centralized, can become a bottleneck or single point of failure.
Use when game rules are complex enough that client-side validation is insufficient or untrustworthy, especially in multiplayer or competitive games with thousands of moves per second.
Avoid if the game is very simple with trivial moves or single-player where trust in the client is sufficient and performance is critical.
Real World Examples
Chess.com
Validates each chess move server-side to prevent illegal moves and cheating in online matches.
Blizzard Entertainment
In games like Hearthstone, validates card plays and moves to enforce game rules and prevent exploits.
Epic Games
In Fortnite, validates player actions to prevent cheating and maintain fair gameplay.
Code Example
The before code applies moves directly without checking if they are legal, risking invalid game states. The after code introduces a MoveValidator that checks each move against game rules before applying it, ensuring only valid moves update the game state.
LLD
### Before: No validation
class Game:
    def __init__(self):
        self.state = {}

    def make_move(self, move):
        # Directly apply move without checks
        self.state.update(move)


### After: With move validation
class MoveValidator:
    def __init__(self, rules):
        self.rules = rules

    def is_valid(self, move, state):
        # Check move against rules and current state
        return all(rule(move, state) for rule in self.rules)

class Game:
    def __init__(self, validator):
        self.state = {}
        self.validator = validator

    def make_move(self, move):
        if self.validator.is_valid(move, self.state):
            self.state.update(move)
            return True
        else:
            return False
OutputSuccess
Alternatives
Client-side validation
Validation happens on the player's device before sending moves to the server.
Use when: Use when trust in client is high and performance is critical, but risk of cheating is low.
Hybrid validation
Initial quick validation on client, followed by authoritative validation on server.
Use when: Use when balancing user experience with security and fairness.
Summary
Move validation prevents illegal or cheating moves from corrupting the game state.
It works by checking each move against game rules before applying it.
Proper validation improves fairness and user experience in multiplayer games.