Implemented basic scoring code, so that scoring estimates are drawn at the end of a game
This commit is contained in:
63
lib/goban.py
63
lib/goban.py
@ -5,8 +5,11 @@ class Goban:
|
||||
EMPTY=0
|
||||
WHITE=1
|
||||
BLACK=2
|
||||
# GRAY_WHITE=3
|
||||
# GRAY_BLACK=4
|
||||
SCORE_BLACK=3
|
||||
SCORE_WHITE=4
|
||||
SCORE_DAME=5
|
||||
SCORING=6
|
||||
|
||||
|
||||
def __init__(self, board_size=19):
|
||||
# Build the board intersections
|
||||
@ -109,7 +112,6 @@ class Goban:
|
||||
|
||||
|
||||
|
||||
# fixme: need to handle post-game stuff here... scoring code
|
||||
def pass_move(self, color=None):
|
||||
if color is None:
|
||||
color = self.to_move
|
||||
@ -122,6 +124,9 @@ class Goban:
|
||||
|
||||
if self.passed_last:
|
||||
self.to_move = Goban.EMPTY
|
||||
self.winner = Goban.SCORING
|
||||
self.auto_score()
|
||||
return range(len(self.board))
|
||||
else:
|
||||
self.to_move = self._other_color(color)
|
||||
self.passed_last = True
|
||||
@ -294,8 +299,9 @@ class Goban:
|
||||
return None
|
||||
|
||||
point = self.board[pos]
|
||||
code = None
|
||||
|
||||
if point == Goban.EMPTY:
|
||||
if point == Goban.EMPTY or point == Goban.SCORE_DAME:
|
||||
code = self.def_draw_codes[pos]
|
||||
elif point == Goban.BLACK:
|
||||
code = 'b'
|
||||
@ -305,6 +311,10 @@ class Goban:
|
||||
code = 'w'
|
||||
if pos == self.last_move:
|
||||
code += 'Cb'
|
||||
elif point == Goban.SCORE_WHITE:
|
||||
code = self.def_draw_codes[pos] + 'ws'
|
||||
elif point == Goban.SCORE_BLACK:
|
||||
code = self.def_draw_codes[pos] + 'bs'
|
||||
|
||||
if pos == self.ko:
|
||||
code += 'S'
|
||||
@ -312,6 +322,51 @@ class Goban:
|
||||
return code
|
||||
|
||||
|
||||
def auto_score(self):
|
||||
'''
|
||||
Detects regions and assigns them to the appropriate player
|
||||
This may not always guess correctly, so we also have API that
|
||||
can manually change these things.
|
||||
|
||||
After calling this function, the entire board should be redrawn.
|
||||
'''
|
||||
for i in range(len(self.board)):
|
||||
if self.board[i] == Goban.EMPTY:
|
||||
bs = self.board_size * self.board_size
|
||||
checked = set()
|
||||
score = self._score_space(i, checked)
|
||||
|
||||
for c in checked:
|
||||
self.board[c] = score
|
||||
|
||||
|
||||
|
||||
def _score_space(self, pos, checked):
|
||||
if pos in checked:
|
||||
return None
|
||||
|
||||
if self.board[pos] == Goban.BLACK:
|
||||
return Goban.SCORE_BLACK
|
||||
elif self.board[pos] == Goban.WHITE:
|
||||
return Goban.SCORE_WHITE
|
||||
|
||||
checked.add(pos)
|
||||
|
||||
possible = set()
|
||||
for i in self._neighbors(pos):
|
||||
score = self._score_space(i, checked)
|
||||
possible.add(score)
|
||||
|
||||
if Goban.SCORE_DAME in possible or (Goban.SCORE_BLACK in possible and Goban.SCORE_WHITE in possible):
|
||||
return Goban.SCORE_DAME
|
||||
elif Goban.SCORE_BLACK in possible:
|
||||
return Goban.SCORE_BLACK
|
||||
elif Goban.SCORE_WHITE in possible:
|
||||
return Goban.SCORE_WHITE
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def _make_default_draw_codes(self):
|
||||
ret = []
|
||||
|
Reference in New Issue
Block a user