initial player

This commit is contained in:
Sander Land committed 2020-12-27 18:35:31 +01:00
1 parent 4d8956c214
commit b6b1fe2d5b
3 files changed
+31 -10

No files matched your search

+1 -1
View File
@@ -329,7 +329,7 @@ class KataGoEngine:
"includeMovesOwnership": ownership and not next_move, "includeMovesOwnership": ownership and not next_move,
"includePolicy": not next_move, "includePolicy": not next_move,
"initialStones": [[m.player, m.gtp()] for m in initial_stones], "initialStones": [[m.player, m.gtp()] for m in initial_stones],
"initialPlayer": analysis_node.root.next_player, "initialPlayer": analysis_node.initial_player,
"moves": [[m.player, m.gtp()] for m in moves], "moves": [[m.player, m.gtp()] for m in moves],
"overrideSettings": {**settings, **(extra_settings or {})}, "overrideSettings": {**settings, **(extra_settings or {})},
} }
+19 -9
View File
@@ -300,23 +300,33 @@ class SGFNode:
return self.__class__(parent=self, move=move) return self.__class__(parent=self, move=move)
@property @property
def next_player(self): def initial_player(self): # player for first node
"""Returns player to move""" root = self.root
if "PL" in self.properties: # explicit if "PL" in root.properties: # explicit
return "B" if self.get_property("PL").upper().strip() == "B" else "W" return "B" if self.get_property("PL").upper().strip() == "B" else "W"
elif self.children: # child exist, use it if not placement elif root.children: # child exist, use it if not placement
for child in self.children: for child in root.children:
for color in "BW": for color in "BW":
if color in child.properties: if color in child.properties:
return color return color
# b move or setup with only black moves like root handicap # b move or setup with only black moves like handicap
if "B" in self.properties or ( if "AB" in self.properties and "AW" not in self.properties:
"AB" in self.properties and "W" not in self.properties and "AW" not in self.properties
):
return "W" return "W"
else: else:
return "B" return "B"
@property
def next_player(self):
"""Returns player to move"""
if self.is_root:
return self.initial_player
elif "B" in self.properties:
return "W"
elif "W" in self.properties:
return "B"
else: # only placements, find a parent node with a real move. TODO: better placement support
return self.parent.next_player
@property @property
def player(self): def player(self):
"""Returns player that moved last. nb root is considered white played if no handicap stones are placed""" """Returns player that moved last. nb root is considered white played if no handicap stones are placed"""
+11
View File
@@ -157,11 +157,22 @@ def test_foxwq():
def test_next_player(): def test_next_player():
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb])" input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb])"
assert "B" == SGF.parse_sgf(input_sgf).next_player assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[B])" input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[B])"
assert "B" == SGF.parse_sgf(input_sgf).next_player assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[W])" input_sgf = "(;GM[1]FF[4]AB[aa]AW[bb]PL[W])"
assert "W" == SGF.parse_sgf(input_sgf).next_player assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa])" input_sgf = "(;GM[1]FF[4]AB[aa])"
assert "W" == SGF.parse_sgf(input_sgf).next_player assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa]PL[B])" input_sgf = "(;GM[1]FF[4]AB[aa]PL[B])"
assert "B" == SGF.parse_sgf(input_sgf).next_player assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa];B[dd])" # branch exists
assert "B" == SGF.parse_sgf(input_sgf).next_player
assert "B" == SGF.parse_sgf(input_sgf).initial_player
input_sgf = "(;GM[1]FF[4]AB[aa];W[dd])" # branch exists
assert "W" == SGF.parse_sgf(input_sgf).next_player
assert "W" == SGF.parse_sgf(input_sgf).initial_player