KatInterview

Run-of-K Grid Game Simulator for Many Players

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designsimulationgriddesign

Last updated

Simulate an N x N, M-player connect-K game with turn order, move validation, win detection from the last move, board snapshots and undo.

Run-of-K Grid Game Simulator

You are simulating a turn-based game on a square board with n rows and n columns. There are m players, numbered 1 to m. Players take turns in the fixed cycle 1, 2, ..., m, 1, 2, ..., and player 1 goes first. A player wins by owning at least k consecutive cells in a straight line: horizontal, vertical, diagonal or anti-diagonal.

Instead of building a class, you write one function that receives the game settings and a list of operations, runs them in order on a fresh game, and returns the result of each operation.

Inputs

  • n: the board side length.
  • m: the number of players.
  • k: the length of the run needed to win.
  • operations: a list of strings. Each is "move", "getBoardState" or "undo".
  • arguments: a list of the same length as operations. Entry i holds the arguments for operations[i]:
    • for "move" it is [row, col, player_id] (rows and columns are 0-indexed);
    • for "getBoardState" and "undo" it is an empty list.

Operations

The board starts empty, with every cell 0. A cell holding player p's mark stores the value p. Each operation appends one entry to the output list.

move: player player_id tries to mark cell (row, col). Check the conditions in this order:

  1. If the game is already finished, the result is -2.
  2. If (row, col) is outside the board, the result is -1.
  3. If the cell is already occupied, the result is -1.
  4. If player_id is not the player whose turn it is, the result is -1.

A rejected move changes nothing. Otherwise the mark is placed and the turn passes to the next player in the cycle.

  • If the placed mark is part of a straight run of at least k cells owned by the same player, that player wins. The game becomes finished and the result is 1.
  • Otherwise the result is 0. If the board is now completely full, the game also becomes finished (a draw).

A game is finished when someone has won or the board is full. While it is finished, every move returns -2, even one that would otherwise be invalid.

getBoardState: the result is a snapshot of the board as a list of n rows, each a list of n integers. It is a copy, so later moves do not change it.

undo: reverts the most recent successfully placed mark.

  • If no mark is currently on the board from valid moves (nothing to undo), the result is -1 and nothing changes.
  • Otherwise the cell is cleared, the turn returns to the player who made that move, the game is no longer finished, and the result is 0. Undo can be repeated to step back through earlier moves.

Any other operation name produces -1.

Output

Return a list with one result per operation, in order. Each result is an integer (1, 0, -1, -2) or, for getBoardState, a 2D list of integers.

Constraints

  • n, m and k are positive integers.
  • k may be larger than n. In that case nobody can win, and the game ends only when the board is full.
  • player_id, row and col in a move can be any integers, including values outside the valid ranges.
  • len(operations) == len(arguments).
  • A run counts only if its cells are adjacent along one of the four line directions and all belong to the same player. Longer runs than k also count as a win.

Examples

Example 1

Input: simulate_connect_k_game(3, 2, 3, ['move', 'move', 'move', 'move', 'move'], [[0, 0, 1], [1, 1, 2], [0, 1, 1], [2, 2, 2], [0, 2, 1]])

Output: [0, 0, 0, 0, 1]

Example 2

Input: simulate_connect_k_game(1, 2, 1, ['move', 'getBoardState', 'undo', 'getBoardState', 'move'], [[0, 0, 1], [], [], [], [1, 1, 1]])

Output: [1, [[1]], 0, [[0]], -1]

Example 3

Input: simulate_connect_k_game(1, 1, 1, ['move', 'move', 'undo', 'move'], [[0, 0, 1], [0, 0, 1], [], [0, 0, 1]])

Output: [1, -2, 0, 1]

Hints

Hint 1

Keep the board, a stack of placed moves, whose turn it is, and a finished flag. Process the operations one at a time against this state.

Hint 2

A new winning run must pass through the cell just placed, since the game stops at the first win. From that cell, count same-player neighbours in both opposite directions along each of the 4 line orientations, and compare 1 + both counts with k.

Hint 3

For undo, pop the last move, clear its cell, set the turn back to that move's player and clear the finished flag. Remember that a full board also finishes the game, and that a winning move does not change the order of validation checks.