KatInterview

Object Model and Move Engine for a Sliding-Tile Merge Game

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designgame-logicsimulation

Last updated

Design the classes and core move algorithm for a configurable N x N sliding-tile merge puzzle, with testable randomness, win and loss detection, and room for undo.

You are asked to design the classes and the core move logic for a sliding-tile merge puzzle in the style of 2048, played on a square N x N board. Focus on the object model, who owns which state, and the move algorithm. Do not design a UI or rendering layer.

How the game works

The player picks one of four directions. Every tile slides as far as it can in that direction. When two neighboring tiles with equal values collide, they combine into a single tile holding their sum. After any move that changes the board, one new tile appears in a random empty cell.

Assumptions

  • The board is N x N, with N = 4 by default but configurable.
  • Tile values are powers of two. An empty cell is stored as 0.
  • A spawned tile is 2 with probability 0.9 and 4 with probability 0.1. Both numbers must be configurable.
  • A new game begins with two tiles already placed.
  • By default the player wins on creating a 2048 tile. Play may continue afterward.
  • Randomness must be injectable so that tests are reproducible.
  • This is a single local session. Networking and persistence are out of scope, but the design should not make them hard to add later.

Requirements

  1. Start-up: create a board with two random tiles.
  2. Moves: support UP, DOWN, LEFT, RIGHT.
  3. Slide and merge: tiles slide toward the move direction, and any given tile may take part in at most one merge per move.
  4. Scoring: each merge adds the value of the newly created tile to the score.
  5. Spawning: add a new tile only if the move changed the board.
  6. End states: detect when the player has won and when the game is over.
  7. Separation of concerns: keep presentation code completely out of the game logic.

What to discuss

Describe the classes, their responsibilities and their relationships, then give the move algorithm and its complexity. Also explain how your design supports each of the following without rewriting the merge logic:

  • undo
  • custom board sizes
  • deterministic tests
  • configurable spawn probabilities

State any clarifying questions you would ask before starting.

Hints

Hint 1

Reduce all four directions to one operation: take a single row, compress it toward index 0, and merge. Then think about how to turn UP, DOWN and RIGHT into that same operation by extracting lines in the right order.

Hint 2

To enforce one merge per tile per move, build the output list left to right and only merge the incoming value with the last output value if that last value has not itself been produced by a merge in this pass.

Hint 3

Game over is not the same as a full board. A full board can still have a legal move if any two adjacent cells are equal. Also, compare the board before and after to decide whether to spawn a tile or count the move as valid.

Hint 4

Inject a random source and a spawn policy into the game rather than calling a global random function, and keep the board state immutable or cheaply copyable so that undo becomes a stack of snapshots.