TensorTonic
Problems
Build the Legal Go Action Mask
Reinforcement LearningData Processing
Hard

Determine which Go actions are legal. The board uses 0 for empty, 1 for Black, and -1 for White. On a board with side length n, intersection actions are numbered in row-major order from 0 to n squared minus one; action n squared means pass.

A group contains same-colour stones connected horizontally or vertically. Its liberties are adjacent empty intersections. A placement must be on an empty point. Adjacent opposing groups with no liberties are captured; the played stone's group must have a liberty after those captures. The resulting board must not match any supplied seen position, which is the positional-superko rule.

Pass is legal in every nonterminal state. A state is terminal after at least two consecutive passes or at least twice the board area in played actions.

Return a NumPy Boolean array of length n squared plus one, with one flag per intersection followed by pass. Every flag must be false for a terminal state.

Loading visualization...

Examples

Input: board = [[0,0],[0,0]], player = 1, seen_positions = [[[0,0],[0,0]]], consecutive_passes = 0, move_count = 0

Output: [True, True, True, True, True]

Explanation: Every empty intersection and the final pass action are legal.

Input: board = [[-1,0,-1],[0,-1,0],[-1,0,-1]], player = 1, seen_positions = [[[-1,0,-1],[0,-1,0],[-1,0,-1]]], consecutive_passes = 0, move_count = 4

Output: [False, False, False, False, False, False, False, False, False, True]

Input: board = [[1,0],[0,-1]], player = 1, seen_positions = [[[1,0],[0,-1]]], consecutive_passes = 2, move_count = 4

Output: [False, False, False, False, False]

Hint 1

divmod(action, board_size) gives the row and column.

Hint 2

A group-and-liberties helper can serve both capture and suicide checks.

Hint 3

np.array_equal compares a candidate board with one seen position.

Requirements

  • Use NumPy and return a Boolean array.
  • Preserve the board and all seen positions.

Constraints

  • The board and every seen position are nonempty square nested Python lists with the same shape and values -1, 0, or 1.
  • Player is 1 for Black or -1 for White.
  • Seen positions is a nonempty list ending with the current board.
  • Consecutive passes and move count are nonnegative integers; move count includes passes.
Try Similar Problems
Apply a Go Action with Captures and RepetitionHardScore a Terminal Position with Tromp-Taylor RulesMediumFind a Go Group and Its LibertiesEasyApply a Dihedral Go SymmetryMediumEncode AlphaGo Zero Board HistoryMediumSample a Recent Self-Play Replay BatchEasyCanonicalize Pairwise Preference DataMediumApply One Policy-Value Head Training StepHardExperience Replay BufferEasyApply Custom TransformsHardGRPO Group-Relative AdvantageHardBoolean MaskingHard

Accepts: array

Accepts: number

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Build the Legal Go Action Mask

Build the Legal Go Action Mask

Reinforcement LearningData Processing
Hard

Determine which Go actions are legal. The board uses 0 for empty, 1 for Black, and -1 for White. On a board with side length n, intersection actions are numbered in row-major order from 0 to n squared minus one; action n squared means pass.

A group contains same-colour stones connected horizontally or vertically. Its liberties are adjacent empty intersections. A placement must be on an empty point. Adjacent opposing groups with no liberties are captured; the played stone's group must have a liberty after those captures. The resulting board must not match any supplied seen position, which is the positional-superko rule.

Pass is legal in every nonterminal state. A state is terminal after at least two consecutive passes or at least twice the board area in played actions.

Return a NumPy Boolean array of length n squared plus one, with one flag per intersection followed by pass. Every flag must be false for a terminal state.

Loading visualization...

Examples

Input: board = [[0,0],[0,0]], player = 1, seen_positions = [[[0,0],[0,0]]], consecutive_passes = 0, move_count = 0

Output: [True, True, True, True, True]

Explanation: Every empty intersection and the final pass action are legal.

Input: board = [[-1,0,-1],[0,-1,0],[-1,0,-1]], player = 1, seen_positions = [[[-1,0,-1],[0,-1,0],[-1,0,-1]]], consecutive_passes = 0, move_count = 4

Output: [False, False, False, False, False, False, False, False, False, True]

Input: board = [[1,0],[0,-1]], player = 1, seen_positions = [[[1,0],[0,-1]]], consecutive_passes = 2, move_count = 4

Output: [False, False, False, False, False]

Hint 1

divmod(action, board_size) gives the row and column.

Hint 2

A group-and-liberties helper can serve both capture and suicide checks.

Hint 3

np.array_equal compares a candidate board with one seen position.

Requirements

  • Use NumPy and return a Boolean array.
  • Preserve the board and all seen positions.

Constraints

  • The board and every seen position are nonempty square nested Python lists with the same shape and values -1, 0, or 1.
  • Player is 1 for Black or -1 for White.
  • Seen positions is a nonempty list ending with the current board.
  • Consecutive passes and move count are nonnegative integers; move count includes passes.
Try Similar Problems
Apply a Go Action with Captures and RepetitionHardScore a Terminal Position with Tromp-Taylor RulesMediumFind a Go Group and Its LibertiesEasyApply a Dihedral Go SymmetryMediumEncode AlphaGo Zero Board HistoryMediumSample a Recent Self-Play Replay BatchEasyCanonicalize Pairwise Preference DataMediumApply One Policy-Value Head Training StepHardExperience Replay BufferEasyApply Custom TransformsHardGRPO Group-Relative AdvantageHardBoolean MaskingHard

Accepts: array

Accepts: number

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1