Problem statement
Design the object model and core APIs for a Memory (Concentration) card-matching game: a grid of face-down cards hides matching pairs; on each turn a player flips two cards, keeps a matched pair and flips again, and the player with the most pairs wins.
Operating context. An even number of cards is laid in a grid, and each card has exactly one matching partner. One or more players take turns. On a turn a player reveals two face-down cards: a match is claimed and grants another turn, a mismatch is shown briefly and then both cards flip back and the turn passes. The game ends when every pair is claimed, and the highest pair count wins. Single in-process session, one game at a time.
Out of scope. Rendering and flip animation, timed or speed modes beyond a rule seam, networked multiplayer, hardware shuffle RNG, and high-score persistence.
What to produce. The class model (board, card, game, player, turn, matcher, shuffler), the public API each exposes, and the reveal state machine (waiting for the first card, waiting for the second, resolving). Be explicit about how the match/mismatch and extra-turn rules stay total, how the shuffler is injected so a layout is reproducible in tests, how the matching predicate is pluggable for variants (match by suit, by number, triples), and how a card's identity stays hidden while face-down.
Functional requirements
- Deal a shuffled board of face-down cards so that every card has exactly one matching partner.
- Reveal a chosen face-down card: hold it on the first reveal and resolve the pair on the second.
- Claim a matched pair for the current player and grant another turn; on a mismatch flip both back and pass the turn.
- Track each player's claimed-pair count and detect game end when no unclaimed cards remain.
- Reject revealing an already-claimed card or a card already face-up this turn.
Non-functional requirements
- Match resolution is O(1) — comparing the two held cards, not scanning the board.
- The turn's reveal state machine (waiting-first, waiting-second, resolving) is total and cannot skip a step.
- The shuffler is injected so a test can deal a known, reproducible layout.
- The matching predicate is pluggable so variants (match by suit, by value, triples) swap without editing turn logic.
- The player set is a collection so single-player solitaire and N-player modes share one turn engine.
- Board and card face state are encapsulated; a card's face value is not exposed while it is face-down.
Topics
- System Design LLD
- Oop Solid
- Patterns State
- Patterns Strategy
- Domain Card-Games