Simulate one disc-flipping board game move across all eight directions
AmazonSoftware Engineer · Online AssessmentLast updated
Simulate one move of a two-player disc-flipping board game: place a piece, flip every run of opponent pieces enclosed by the player's own pieces in all eight directions, and return the board unchanged when the move is illegal. It tests careful grid traversal, precise legality rules and avoiding partial updates.
You are simulating one move of a two-player board game played on an m x n grid. Each cell is 0 (empty), 1 (a piece of player 1) or 2 (a piece of player 2). Given the board and one move, in which player places a piece on cell (row, col), return the board after the move.
When a piece is placed, every straight run of the opponent's pieces that is enclosed between the new piece and another piece of the same player is flipped to the player's color. All eight directions must be checked: up, down, left, right and the four diagonals. If the move is illegal, return the original board unchanged.
Function Signature
def place_piece(board: list[list[int]], row: int, col: int, player: int) -> list[list[int]]:
Rules
- Let
opponent = 3 - player. In one direction, walk away from(row, col)one cell at a time. If the walk passes one or more consecutiveopponentcells and then reaches aplayercell, everyopponentcell passed in that direction is flipped toplayer. - If the first cell in a direction is not an
opponentcell, or if the walk reaches an empty cell or the edge of the board before reaching aplayercell, nothing flips in that direction. - All flips are decided from the board as it was before the move. A piece flipped in one direction never causes further flips.
- The move is legal only if
(row, col)is empty and at least one opponent piece flips in at least one direction. A legal move placesplayerat(row, col)and applies every flip. - If the move is illegal, return the board exactly as given: no piece is placed and nothing flips.
- The returned grid has the same dimensions as the input. You may modify
boardin place or build a new grid; only the returned grid is checked.
Constraints
1 <= m, n <= 100, wherem = len(board)andn = len(board[i])for every rowi- Every cell is
0,1or2. 0 <= row < mand0 <= col < nplayeris1or2.- The board may hold any arrangement of pieces; it does not have to be reachable in a real game.
Examples
Example 1
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 1, col = 3, player = 2
Output: [
[0, 0, 0, 0, 0],
[2, 2, 2, 2, 0],
[0, 0, 2, 1, 0],
[0, 2, 0, 0, 0]
]
To the left, the run (1, 2), (1, 1) of player 1 pieces ends at player 2's piece at (1, 0), so both flip. Down and to the left, (2, 2) is enclosed by player 2's piece at (3, 1) and flips. Straight down, (2, 3) is followed by the empty cell (3, 3), so it stays. Every other direction starts with an empty cell.
Example 2
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 3, col = 0, player = 1
Output: [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
To the right, player 2's piece at (3, 1) is followed by the empty cell (3, 2), so it is not enclosed. Up and up-right start with empty cells, and the other directions leave the board at once. Nothing flips, so the move is illegal and the board is returned unchanged.
Example 3
Input: board = [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
row = 1, col = 2, player = 2
Output: [
[0, 0, 0, 0, 0],
[2, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 2, 0, 0, 0]
]
Cell (1, 2) is already occupied, so the move is illegal, even though (1, 1) lies between it and player 2's piece at (1, 0).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.