Fixed sgf loading

This commit is contained in:
Anna Rose 2012-04-23 21:19:56 -04:00
parent 00448c967f
commit 08e89fd3f6
2 changed files with 38 additions and 11 deletions

View File

@ -56,9 +56,9 @@ class Goban:
if root.has_setup_stones(): if root.has_setup_stones():
black, white, empty = root.get_setup_stones() black, white, empty = root.get_setup_stones()
for point in black: for point in black:
self.board[self._real_pos(point)] = Goban.BLACK self.board[self._real_pos(self._sgf_to_move(point))] = Goban.BLACK
for point in white: for point in white:
self.board[self._real_pos(point)] = Goban.WHITE self.board[self._real_pos(self._sgf_to_move(point))] = Goban.WHITE
for node in self.sgf_game.get_main_sequence(): for node in self.sgf_game.get_main_sequence():
color, pos = node.get_move() color, pos = node.get_move()
@ -66,7 +66,11 @@ class Goban:
if pos == 'pass': if pos == 'pass':
self.pass_move(color) self.pass_move(color)
else: else:
self.play_move(pos, color) self.play_move(self._sgf_to_move(pos), color, add_sgf=False)
if self.sgf_game.get_winner() is not None:
self.winner = self.sgf_game.get_winner()
self.to_move = None
def save_sgf(self, file_name=None): def save_sgf(self, file_name=None):
@ -132,7 +136,7 @@ class Goban:
# function and by _delete_group_r(). # function and by _delete_group_r().
# This list will *also* contain the last move and any # This list will *also* contain the last move and any
# previous or new ko positions. # previous or new ko positions.
def play_move(self, pos, color=None): def play_move(self, pos, color=None, add_sgf=True):
if color is None: if color is None:
color = self.to_move color = self.to_move
@ -157,8 +161,9 @@ class Goban:
self.last_move = rpos self.last_move = rpos
self.passed_last = False self.passed_last = False
node = self.sgf_game.extend_main_sequence() if add_sgf:
node.set_move(color, pos) node = self.sgf_game.extend_main_sequence()
node.set_move(color, self._pos_to_sgf(pos))
self.to_move = self._other_color(color) self.to_move = self._other_color(color)
self.clear_hover() self.clear_hover()
@ -175,6 +180,10 @@ class Goban:
if color is None: if color is None:
color = self.to_move color = self.to_move
# If the game is over, fail silently
if color is None:
return
self._changed = [] self._changed = []
if self.ko is not None: if self.ko is not None:
self._changed.append(self.ko) self._changed.append(self.ko)
@ -182,7 +191,7 @@ class Goban:
self._changed.append(self.last_move) self._changed.append(self.last_move)
node = self.sgf_game.extend_main_sequence() node = self.sgf_game.extend_main_sequence()
node.set_move(color, 'pass') node.set_move(color, None)
if self.passed_last: if self.passed_last:
self.to_move = Goban.EMPTY self.to_move = Goban.EMPTY
@ -203,6 +212,10 @@ class Goban:
if color is None: if color is None:
color = self.to_move color = self.to_move
# If the game is over, fail silently
if color is None:
return
self._changed = [] self._changed = []
if self.ko is not None: if self.ko is not None:
self._changed.append(self.ko) self._changed.append(self.ko)
@ -486,3 +499,17 @@ class Goban:
neighbors.append(pos + 1) neighbors.append(pos + 1)
return neighbors return neighbors
# Convert an sgf vector to a move tuple
def _sgf_to_move(self, move):
x,y = move
new_x = self.board_size - 1 - x
return (new_x, y)
# Convert a 1-dimensional position to an sgf move
def _pos_to_sgf(self, pos):
x = self.board_size - 1 + (pos / self.board_size)
y = pos % self.board_size
return (x,y)

View File

@ -242,15 +242,15 @@ def _build_img_res():
base = _load_png('go_corner.png') base = _load_png('go_corner.png')
ret['ul'] = base ret['ul'] = base
ret['dl'] = base.copy().rotate_simple(90)
ret['dr'] = base.copy().rotate_simple(180)
ret['ur'] = base.copy().rotate_simple(270) ret['ur'] = base.copy().rotate_simple(270)
ret['dr'] = base.copy().rotate_simple(180)
ret['dl'] = base.copy().rotate_simple(90)
base = _load_png('go_edge.png') base = _load_png('go_edge.png')
ret['u'] = base ret['u'] = base
ret['l'] = base.copy().rotate_simple(90)
ret['d'] = base.copy().rotate_simple(180)
ret['r'] = base.copy().rotate_simple(270) ret['r'] = base.copy().rotate_simple(270)
ret['d'] = base.copy().rotate_simple(180)
ret['l'] = base.copy().rotate_simple(90)
for d in ('m', 'h', 'w', 'b', 'wH', 'bH', 'ws', 'bs'): for d in ('m', 'h', 'w', 'b', 'wH', 'bH', 'ws', 'bs'):
ret[d] = _load_png('go_' + d + '.png') ret[d] = _load_png('go_' + d + '.png')