KatInterview

Simulate 2048 Moves with Scripted Tile Spawns

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designsimulationmatrixstate-management

Last updated

Simulate a 4x4 sliding-tile merge game with scripted spawns, skipping spawns on no-op moves, then report the final board and whether any move remains.

2048 Replay Engine

You are building the deterministic core of a 2048-style game. Randomness is replaced by a pre-written script of spawns, so the same inputs always give the same result.

Implement a function that takes a starting 4x4 board and a list of moves. It applies the moves in order and returns the final board and whether the game is over.

Board and moves

  • The board is a 4x4 grid of non-negative integers. 0 means an empty cell.
  • Each move is one of "left", "right", "up", "down", matched case-insensitively ("LEFT" and "Left" are valid).
  • On a move, every tile slides as far as possible in that direction, stopping at the wall or at another tile.
  • Two adjacent tiles with equal value (after sliding) merge into one tile of double the value.
  • A tile may take part in at most one merge per move. A tile created by a merge cannot merge again in the same move. For example, a row [2,2,2,2] moved left becomes [4,4,0,0], and [4,4,8,0] becomes [8,8,0,0].
  • When three equal tiles are in a line, the pair closest to the wall being moved toward merges first. [2,2,2,0] moved left becomes [4,2,0,0], and moved right becomes [0,0,2,4].

Spawning

  • The move at index i is paired with spawn_cells[i] (a [row, col] pair) and spawn_tiles[i] (the value to place).
  • If move i changed the board, the tile spawn_tiles[i] is placed at spawn_cells[i] after the slide and merge.
  • If move i did not change the board, nothing spawns, and the spawn entry at index i is simply ignored. Spawn entries are always looked up by the move's index, not by how many moves have succeeded so far.
  • When a spawn happens, it must be valid. Otherwise raise ValueError if:
    • the value is not 2 or 4,
    • the position does not have exactly two elements,
    • the position is outside the 4x4 grid,
    • the target cell is not empty after the slide and merge.

Game over

After all moves, the game is over exactly when the board has no empty cell and no two orthogonally adjacent cells hold equal values. If moves is empty, evaluate this on the starting board.

Return value

Return a dictionary with two keys:

  • "board": the final board as a tuple of 4 tuples of 4 integers.
  • "game_over": a boolean.

The input board must not be modified.

Errors

Raise ValueError in each of these cases:

  • the board is not 4x4,
  • any board value is negative,
  • spawn_cells or spawn_tiles has fewer entries than moves (checked before processing any move),
  • a move string is not one of the four directions (case-insensitive),
  • a spawn is invalid, as described above.

Constraints

  • The board is 4x4.
  • 0 <= len(moves) <= 1000.
  • Board values are integers, and valid boards contain only non-negative values.
  • Spawn lists may be longer than moves. Extra entries are ignored.

Examples

Example 1

Input: simulate_2048([[2, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], ['left'], [[0, 2]], [2])

Output: {'board': [[4, 0, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'game_over': False}

Example 2

Input: simulate_2048([[2, 2, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], ['left'], [[1, 0]], [4])

Output: {'board': [[4, 4, 0, 0], [4, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'game_over': False}

Example 3

Input: simulate_2048([[2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], ['left'], [[0, 1]], [4])

Output: {'board': [[2, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'game_over': False}

Hints

Hint 1

Reduce every direction to one primitive that operates on a single line of 4 cells: drop zeros, merge equal neighbours from the front once, then pad with zeros.

Hint 2

For right and down, reverse the line, apply the same primitive, and reverse back. For up and down, extract each column into a list first and write the result back.

Hint 3

Take a copy of the grid before each move and compare afterwards to decide whether to spawn. Index the spawn data by the move's position in the list, and for the game-over check look at empties plus right and down neighbours.