KatInterview

Build a Rule-Based Board Game State Manager

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designimmutabilitystrategy-pattern

Last updated

Design an immutable, thread-safe game engine with pluggable validation rules for a turn-based strategy game.

Build a Rule-Based Board Game State Manager

You're building the core state-management system for a turn-based strategy board game with pieces that have different movement and interaction rules. The system must allow new rules to be added without changing the core engine code.

Core Requirements

Your design must support:

  1. Immutable state: Each action creates a new state rather than modifying the current one
  2. Pluggable validation: Movement and capture rules should be separate components that can be swapped or extended
  3. Full history tracking: Store every action so any prior state can be reconstructed
  4. Thread-safe reads: Multiple clients may query the current state concurrently

Your Task

Design the class structure showing how these components interact:

  • State representation: How the board, pieces, and turn information are stored
  • Action objects: How moves or commands are represented
  • Validation layer: How rules are checked before applying an action
  • Engine coordination: How the engine orchestrates validation, state transitions, and history

Example Skeleton

class Action:
    # represents a player's move
    pass

class GameState:
    # immutable snapshot of board and pieces
    pass

class Engine:
    def execute(self, action: Action) -> GameState:
        # validate, apply, record history
        pass

Show how you would structure the validation layer so that adding a new rule (for example, "pieces cannot move onto water terrain unless they have a swimming ability") requires only writing a new validator class, not editing the engine itself.

Include consideration for:

  • How state immutability helps with concurrency
  • Where game-specific rules live versus generic orchestration logic
  • How history replay would work

Hints

Hint 1

Separate 'what happened' (state) from 'is this allowed' (validation) from 'make it happen' (orchestration)

Hint 2

Each validator can be a small object with one check method; compose them into a validation pipeline

Hint 3

Store actions in order; replay by starting from initial state and applying each action sequentially