Problem 2538. Find the Next Legal Move in Reversi

Reversi, also known as Othello, is a game in which reversible white/black chips are placed on a grid. The goal is to have the most pieces once the board is full (i.e. there are no more legal moves). A move, to be legal, must flip at least one opponent's chip by flanking it. "Flanking" occurs by surrounding a line of opposing chips with two of your own. It can occur on straight lines and diagonals.

If we represent a small 4x4 board this way, with 0 for empty squares, 1 for black chips, and 2 for white chips,

 [ 0 0 0 0
   0 1 2 0
   0 2 1 0 
   0 0 0 0 ]

then black has 4 legal moves shown by b.

 [ 0 0 b 0
   0 1 2 b
   b 2 1 0 
   0 b 0 0 ]

Any of these moves will flank (surround) a white chip and cause it to be flipped.

Your Cody challenge is to take the given board (always square) and side (1 or 2) and locate all the legal moves. Return all legal moves as a vector of column-wise indices into the matrix. If there are no legal moves, return empty.

Example.

For the board shown above,

 side = 1 (black)
 moves = [3 8 9 14]

Note: See also Problem 2565, Determine the Result of a Move in Reversi.

Solution Stats

39.34% Correct | 60.66% Incorrect
Last Solution submitted on Dec 13, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers20

Suggested Problems

More from this Author50

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!