Rock Paper Scissors
Hand Game
Design rock paper scissors as a small object model: rules as data, players that choose moves, and a best-of-N match that keeps score, so that a five-move variant needs no new code.
Overview
Two players each pick rock, paper or scissors at the same time. Rock beats scissors, scissors beats paper, paper beats rock, and the same pick is a draw. A match is best of N rounds. That is the whole game, which is why interviewers like it: there is nothing to explain, so all the time goes to your design.
The trap is to write the rules as if statements. It works for three moves and turns into a wall of conditions for five. The design below keeps the rules as a table, checks that the table is balanced, and lets a match run on any table. Players are objects that produce a move when asked; the computer's way of choosing is a strategy you can swap.
A strong answer names the three pieces in the first minute: a rules table, a player interface, a match that keeps score. Then it shows the variant with five moves as a new table and nothing else.
Scoping the problem
Before any code, ask these questions. Each one changes the design, and each comes with the answer this design assumes.
- Human against human, or against the computer? This decides whether a Player interface is needed and where the computer's choice lives. Assume both, behind one interface.
- How does a match end? Best of N needs an odd N and a wins-needed count, and draws do not count. Assume best of N, with N odd.
- Do both players move at the same time? In a local game the UI collects both moves first; over a network you need a commitment step. Assume both moves are collected before the round is judged.
- Will there be variants? This decides between rules as data and rules as code. Assume yes, so the rules are a table.
- Should the computer learn from the human? That needs a strategy interface with a history argument. Assume yes, with two strategies.
- Does the computer's randomness need to be reproducible in tests? Then the random source is injected. Assume yes, a seeded random.Random.
Functional requirements
- Two players play rounds; each round both moves are collected first, then judged as win, lose or draw for the first player
- A match is best of N with N odd; it ends when a player reaches N // 2 + 1 wins; draws add a round and no score
- Human players submit a move before the round; computer players ask a strategy, which sees the opponent's past moves
- Rules are a table of which move beats which; the classic table is provided and any balanced table is accepted
- The match refuses a round after it is over and refuses a move that is not in its rule set
Non-functional requirements
- A new variant is a new rules table; Match, Player, Round and the strategies do not change
- The rules constructor rejects a table that is unbalanced, self-beating or names unknown moves
- The computer's randomness is injected so tests are reproducible
- Rounds are immutable records; the score is derived from them rather than kept in a counter that can drift
Interview questions
- Why keep the rules in a table instead of a beats() method on the Move enum?
- Add rock paper scissors lizard Spock. What changes?
- Which computer strategy is best?
- Over a network, the second player could wait to see the first move. How do you make the moves simultaneous?
- Why does best_of have to be odd, and why do draws not count?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio