From 08e89fd3f6b9828e38ca21cdc0968fd09f5a460d Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Mon, 23 Apr 2012 21:19:56 -0400 Subject: [PATCH] Fixed sgf loading --- lib/goban.py | 41 ++++++++++++++++++++++++++++++++++------- widgets/gogame.py | 8 ++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/goban.py b/lib/goban.py index 2a95037..90d9ac0 100644 --- a/lib/goban.py +++ b/lib/goban.py @@ -56,9 +56,9 @@ class Goban: if root.has_setup_stones(): black, white, empty = root.get_setup_stones() 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: - 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(): color, pos = node.get_move() @@ -66,7 +66,11 @@ class Goban: if pos == 'pass': self.pass_move(color) 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): @@ -132,7 +136,7 @@ class Goban: # function and by _delete_group_r(). # This list will *also* contain the last move and any # 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: color = self.to_move @@ -157,8 +161,9 @@ class Goban: self.last_move = rpos self.passed_last = False - node = self.sgf_game.extend_main_sequence() - node.set_move(color, pos) + if add_sgf: + node = self.sgf_game.extend_main_sequence() + node.set_move(color, self._pos_to_sgf(pos)) self.to_move = self._other_color(color) self.clear_hover() @@ -175,6 +180,10 @@ class Goban: if color is None: color = self.to_move + # If the game is over, fail silently + if color is None: + return + self._changed = [] if self.ko is not None: self._changed.append(self.ko) @@ -182,7 +191,7 @@ class Goban: self._changed.append(self.last_move) node = self.sgf_game.extend_main_sequence() - node.set_move(color, 'pass') + node.set_move(color, None) if self.passed_last: self.to_move = Goban.EMPTY @@ -203,6 +212,10 @@ class Goban: if color is None: color = self.to_move + # If the game is over, fail silently + if color is None: + return + self._changed = [] if self.ko is not None: self._changed.append(self.ko) @@ -486,3 +499,17 @@ class Goban: neighbors.append(pos + 1) 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) diff --git a/widgets/gogame.py b/widgets/gogame.py index 31e6506..c49c425 100644 --- a/widgets/gogame.py +++ b/widgets/gogame.py @@ -242,15 +242,15 @@ def _build_img_res(): base = _load_png('go_corner.png') 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['dr'] = base.copy().rotate_simple(180) + ret['dl'] = base.copy().rotate_simple(90) base = _load_png('go_edge.png') 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['d'] = base.copy().rotate_simple(180) + ret['l'] = base.copy().rotate_simple(90) for d in ('m', 'h', 'w', 'b', 'wH', 'bH', 'ws', 'bs'): ret[d] = _load_png('go_' + d + '.png')