← Questions

Reversi move simulation with captures in 8 directions

0
AmazonCodingLast asked Sep 2026

Implement one move of the board game Reversi (also sold as Othello).

You get an n x n board as a list of lists of characters. Each cell is 'B' (black), 'W' (white) or '.' (empty). You also get row, col and player ('B' or 'W').

Write make_move(board, row, col, player) that returns the board after player tries to place a piece at (row, col).

Rules

  • Look from (row, col) in each of the 8 directions (horizontal, vertical, diagonal).
  • A direction captures when it holds one or more consecutive opponent pieces followed directly by a player piece. An empty cell or the board edge before the closing piece means no capture in that direction.
  • If at least one direction captures, place the piece and flip every captured opponent piece in every capturing direction to player.
  • If the cell is occupied, off the board, or no direction captures, the move is illegal: return the board unchanged.
  • Do not modify the input board; return a new one.

Constraints

  • 1 <= n <= 100
  • player is 'B' or 'W'.

Example 1

board  = [".WWB",
          "WW..",
          "B.B.",
          "...."]
row = 0, col = 0, player = 'B'

Output:

["BBBB",
 "BB..",
 "B.B.",
 "...."]

The run to the right closes on (0, 3), the run down closes on (2, 0), and the diagonal closes on (2, 2), so all four white pieces flip.

Example 2

board  = [".WW",
          "...",
          "..."]
row = 0, col = 0, player = 'B'

Output: the same board. The white run reaches the edge without a black piece to close it, so the move is illegal.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding