TensorTonic
Study Plans
Study Plan
Score a Terminal Position with Tromp-Taylor Rules
Reinforcement LearningData Processing
Medium

Score a terminal Go board using Tromp-Taylor area scoring. The board uses 0 for empty, 1 for Black, and -1 for White. A game is terminal after at least two consecutive passes or at least twice the board area in played actions.

SB=∣B∣+∑R:boundary⁡(R)={1}∣R∣S_B=|B|+\sum_{R:\operatorname{boundary}(R)=\{1\}}|R|SB​=∣B∣+R:boundary(R)={1}∑​∣R∣ SW=∣W∣+∑R:boundary⁡(R)={−1}∣R∣+kS_W=|W|+\sum_{R:\operatorname{boundary}(R)=\{-1\}}|R|+kSW​=∣W∣+R:boundary(R)={−1}∑​∣R∣+k

Here, B and W are the sets of Black and White stones, R is a horizontally or vertically connected empty region, and vertical bars denote the number of intersections. The boundary of R is the set of adjacent stone colours, k is komi, and the two equations give Black's and White's scores. Regions touching both colours or neither colour are neutral.

Return a four-item tuple containing the terminal flag, Black score, White score, and winner. Scores are 64-bit floating-point values; winner is 1 for Black, -1 for White, or 0 for a tie. For an unfinished game, return (False, None, None, None).

Loading visualization...

Examples

Input: board = [[1,1,1],[1,0,1],[1,1,1]], komi = 0.5, consecutive_passes = 2, move_count = 12

Output: (True, 9.0, 0.5, 1)

Explanation: Black owns eight stones and the enclosed center, while White receives only komi.

Input: board = [[1,0],[0,-1]], komi = 0.5, consecutive_passes = 1, move_count = 3

Output: (False, None, None, None)

Input: board = [[1,0,-1],[1,0,-1],[1,0,-1]], komi = 0, consecutive_passes = 2, move_count = 10

Output: (True, 3.0, 3.0, 0)

Hint 1

np.count_nonzero can count stones of one colour.

Hint 2

Use a set to collect the stone colours bordering an empty region.

Hint 3

A boundary set with one colour identifies owned territory.

Requirements

  • Use NumPy.
  • Add komi to White's score.
  • Preserve the input board.

Constraints

  • The board is a nonempty square nested Python list containing -1, 0, or 1.
  • Komi is a finite number.
  • Consecutive passes and move count are nonnegative integers; move count includes passes.
Try Similar Problems
Find a Go Group and Its LibertiesEasyApply a Go Action with Captures and RepetitionHardBuild the Legal Go Action MaskHardApply a Dihedral Go SymmetryMediumEncode AlphaGo Zero Board HistoryMediumSample a Recent Self-Play Replay BatchEasyCanonicalize Pairwise Preference DataMediumTurn Root Visits into a Search PolicyMediumExperience Replay BufferEasyTile and DiffMediumBox Encode and DecodeMediumDrop DuplicatesMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Score a Terminal Position with Tromp-Taylor Rules

Reinforcement LearningData Processing
Medium

Score a terminal Go board using Tromp-Taylor area scoring. The board uses 0 for empty, 1 for Black, and -1 for White. A game is terminal after at least two consecutive passes or at least twice the board area in played actions.

SB=∣B∣+∑R:boundary⁡(R)={1}∣R∣S_B=|B|+\sum_{R:\operatorname{boundary}(R)=\{1\}}|R|SB​=∣B∣+R:boundary(R)={1}∑​∣R∣ SW=∣W∣+∑R:boundary⁡(R)={−1}∣R∣+kS_W=|W|+\sum_{R:\operatorname{boundary}(R)=\{-1\}}|R|+kSW​=∣W∣+R:boundary(R)={−1}∑​∣R∣+k

Here, B and W are the sets of Black and White stones, R is a horizontally or vertically connected empty region, and vertical bars denote the number of intersections. The boundary of R is the set of adjacent stone colours, k is komi, and the two equations give Black's and White's scores. Regions touching both colours or neither colour are neutral.

Return a four-item tuple containing the terminal flag, Black score, White score, and winner. Scores are 64-bit floating-point values; winner is 1 for Black, -1 for White, or 0 for a tie. For an unfinished game, return (False, None, None, None).

Loading visualization...

Examples

Input: board = [[1,1,1],[1,0,1],[1,1,1]], komi = 0.5, consecutive_passes = 2, move_count = 12

Output: (True, 9.0, 0.5, 1)

Explanation: Black owns eight stones and the enclosed center, while White receives only komi.

Input: board = [[1,0],[0,-1]], komi = 0.5, consecutive_passes = 1, move_count = 3

Output: (False, None, None, None)

Input: board = [[1,0,-1],[1,0,-1],[1,0,-1]], komi = 0, consecutive_passes = 2, move_count = 10

Output: (True, 3.0, 3.0, 0)

Hint 1

np.count_nonzero can count stones of one colour.

Hint 2

Use a set to collect the stone colours bordering an empty region.

Hint 3

A boundary set with one colour identifies owned territory.

Requirements

  • Use NumPy.
  • Add komi to White's score.
  • Preserve the input board.

Constraints

  • The board is a nonempty square nested Python list containing -1, 0, or 1.
  • Komi is a finite number.
  • Consecutive passes and move count are nonnegative integers; move count includes passes.
Try Similar Problems
Find a Go Group and Its LibertiesEasyApply a Go Action with Captures and RepetitionHardBuild the Legal Go Action MaskHardApply a Dihedral Go SymmetryMediumEncode AlphaGo Zero Board HistoryMediumSample a Recent Self-Play Replay BatchEasyCanonicalize Pairwise Preference DataMediumTurn Root Visits into a Search PolicyMediumExperience Replay BufferEasyTile and DiffMediumBox Encode and DecodeMediumDrop DuplicatesMedium

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.