KatInterview

Build a Game-Agnostic Two-Player Match Runner

System DesignModel answer
HardModel answerSystem Designobject-oriented designstrategy patternextensibility

Last updated

Design a modular Python framework that can run any two-player turn-based game by swapping rule and agent implementations without touching the core loop.

Background

Turn-based games share a common skeleton: players alternate, moves change some state, and the game eventually ends. Yet every game has unique rules. A well-designed framework captures the skeleton once and lets game-specific logic plug in cleanly.

Your task is to design that framework in Python.

What to Build

Create a match runner that can execute a complete game between two agents. The runner must remain completely ignorant of the rules of any particular game. Rules, players, and the runner itself are separate components that communicate through narrow interfaces.

The design must satisfy four properties:

  1. State lifecycle — the runner tracks state from the initial position through each move to a terminal condition (one player wins, or the game is drawn).
  2. Turn management — the runner asks the rules which player acts next, collects a move from that player's agent, confirms the move is legal, and advances the state.
  3. Plug-in extensibility — adding a new game type (e.g. Connect Four) or a new kind of agent (e.g. a Minimax AI) must require zero changes to the runner.
  4. Match observability — the system records every move and every intermediate state so a match can be replayed step-by-step or inspected for debugging.

Required Design

Provide Python abstract base classes (or Protocol types) that define the contracts between three components: the runner, a rules provider, and an agent. Then implement the runner itself and demonstrate the interfaces work by plugging in a Tic-Tac-Toe rules implementation and a random-move agent.

Constraints

  • The design must not assume any board shape, piece type, or fixed player count beyond "exactly two players."
  • Running multiple matches concurrently must be safe. Identify how your design achieves this, not just that it does.
  • If an agent submits an illegal move, the runner must handle it gracefully rather than crashing.

What a Good Answer Covers

  • The interface definitions and the reasoning behind each design choice
  • The complete runner loop
  • Thread-safety argument
  • A worked example (Tic-Tac-Toe rules + random agent) that exercises the interfaces
  • Trade-offs and known limitations

Hints

Hint 1

Start by identifying what the runner strictly needs to know at each step of the loop — you'll find it only ever needs to ask three questions: whose turn is it, what moves are available, and is the game over. Everything else belongs in the rules provider.

Hint 2

Immutable state objects are the simplest path to both thread safety and free replay support — if apply_move returns a new state rather than mutating the existing one, no two concurrent matches can interfere and your history list is already a replay log.

Hint 3

Model an illegal move as a forfeit rather than an exception by default: the offending player loses immediately. This keeps the runner loop clean and lets you test agent correctness separately from game correctness.