KatInterview

Apply a Single Capture Move on a Two-Colour Grid

Arrays & HashingAuto-graded · 29 test cases
MediumAuto-gradedArrays & Hashinggridsimulationmatrix

Last updated

Apply one move on a grid: validate the target cell, find enclosed opponent runs in eight directions, and flip them together, or return the grid unchanged if no capture is possible.

Capture Move on a Grid

A rectangular grid holds stones for two players. Each cell contains 0 (vacant), 1 (player 1's stone) or 2 (player 2's stone). One player now drops a stone on a chosen cell, and you must produce the grid that results.

Write a function that receives the grid, the target cell (row, col) and the acting player, and returns the grid after the move.

How a move works

Define enemy = 3 - player. From the target cell, look along each of the eight compass lines: horizontal, vertical and the four diagonals. For one line:

  1. Step away from the target one cell at a time.
  2. Skip over consecutive enemy stones. These form the run.
  3. If the run contains at least one stone and the cell right after it is on the grid and holds a player stone, the whole run is captured and becomes player.
  4. Otherwise nothing is captured along that line. This includes the case where the first neighbour is not an enemy stone, and the case where the run ends at a vacant cell or at the grid border.

Legality

  • The move is legal only if the target cell is vacant and at least one enemy stone is captured on at least one line.
  • For a legal move, put a player stone on the target and flip all captured stones.
  • For an illegal move, return the grid exactly as it was given. Nothing is placed and nothing flips.

Important detail

Every capture is determined using the grid as it stood before the move. A stone that gets flipped along one line never creates or extends a capture along another line.

You may either mutate the input or build a new grid. Only the returned grid is checked, and it must have the same dimensions as the input.

Constraints

  • 1 <= m, n <= 100, where m is the number of rows and n the length of every row
  • Each cell is 0, 1 or 2
  • 0 <= row < m and 0 <= col < n
  • player is 1 or 2
  • The arrangement of stones can be arbitrary; it need not come from a real game

Examples

Example 1

Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 1, 3, 2)

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

Example 2

Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 3, 0, 1)

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

Example 3

Input: apply_capture_move([[0, 0, 0, 0, 0], [2, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 2, 0, 0, 0]], 1, 2, 2)

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

Hints

Hint 1

Handle the easy rejection first: if the target cell is not vacant, nothing can happen.

Hint 2

For each of the 8 direction pairs (dr, dc) other than (0, 0), walk while you see enemy stones, remember them, and then check the cell where the walk stopped, including a bounds check before reading it.

Hint 3

Do not write to the grid while scanning. Gather all stones to flip first, return the original if there are none, and only then place the new stone and flip everything.