Play a Go move with captures and the suicide rule

Why it matters Self-play RL on Go needs an exact rules engine, and capture order is the classic bug that corrupts training games.

A Go environment must turn a move into the next board: place the stone, remove opponent groups that lost their last liberty, and reject suicide.

Write play_move(board, r, c, color). board is a list of equal-length strings ("." empty, "B", "W"); color is "B" or "W".
- If (r, c) is occupied, return None.
- Place the stone. For each orthogonal neighbor holding an opponent stone, if that opponent group now has zero liberties, remove the whole group.
- After captures, if the placed stone's own group has zero liberties, the move is suicide: return None.
- Otherwise return (new board as a list of strings, number of opponent stones captured).
Ignore ko.

Signaturedef play_move(board, r, c, color)

Example

play_move([".B.", "BW.", ".B."], 1, 2, "B") gives ([".B.", "B.B", ".B."], 1).

Understand the idea

Placing a Go stone can do two opposite things at once: it can take away the last liberty of an enemy group (a capture) and it can leave your own stone with no liberties (suicide). The rules settle this by order: first remove every enemy group that now has no liberties, then check your own group. A stone that looks surrounded is legal if it captures, because the capture opens up space.

Example: white at the center is surrounded by black on three sides with one empty point on the right. Black plays that point. White's group now has zero liberties, so it is removed (1 capture), and black's new stone has liberties, so the move stands.

Rule: place, capture adjacent enemy groups with 0 liberties, then reject if own group has 0 liberties.

Rules engines like this sit under every Go self-play pipeline that generates training data for policy and value networks.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exampleplay_move(['.B.', 'BW.', '.B.'], 1, 2, 'B')
  • Sample 2: capture into eyeplay_move(['.BW', 'BW.', 'W..'], 0, 0, 'W')