diff --git a/lib/goban.py b/lib/goban.py index c890d62..e529fdb 100644 --- a/lib/goban.py +++ b/lib/goban.py @@ -1,17 +1,17 @@ -import gomill.sgf +from gomill import sgf class Goban: """Represents the go board. Handles stone placement, captures, etc""" # enum values for the board array - EMPTY=0 - WHITE=1 - BLACK=2 - SCORE_BLACK=3 - SCORE_WHITE=4 - SCORE_DAME=5 - SCORING=6 + EMPTY='.' + WHITE='w' + BLACK='b' + SCORE_BLACK='B' + SCORE_WHITE='W' + SCORE_DAME='d' + SCORING='s' def __init__(self, board_size=19, file_name=None): @@ -20,12 +20,6 @@ class Goban: num_points = board_size * board_size self.board = [Goban.EMPTY] * num_points - self.file_name = file_name - self.sgf_game = None - - if self.file_name is not None: - self.load_sgf(file_name) - self.def_draw_codes = self._make_default_draw_codes() self.to_move = Goban.BLACK @@ -38,22 +32,35 @@ class Goban: self.elapsed_time = 0 self.winner = Goban.EMPTY + self.file_name = file_name + self.sgf_game = None - def load_sgf(self): + if self.file_name is not None: + self.load_sgf(file_name) + + + def load_sgf(self, file_name): try: - with open(self.file_name, 'r') as fn: + with open(file_name, 'r') as fn: self.sgf_game = sgf.Sgf_game.from_string(fn.read()) except IOError: # fixme - this should be convertable into a dialog box... perhaps it should throw an exception of its own print 'There was a problem loading the SGF file.' + # Do initial layout + root = self.sgf_game.get_root() + if root.has_setup_stones(): + black, white, empty = root.get_setup_stones() + for point in black: + self.board[self._real_pos(point)] = Goban.BLACK + for point in white: + self.board[self._real_pos(point)] = Goban.WHITE + + for node in self.sgf_game.get_main_sequence(): color, pos = node.get_move() - if color == 'b': - color = Goban.BLACK - elif color == 'w': - color = Goban.WHITE - self.play_move(color, pos) + if color is not None: + self.play_move(color, pos) def set_hover(self, pos): diff --git a/pygo.py b/pygo.py index d61948e..7b31221 100755 --- a/pygo.py +++ b/pygo.py @@ -68,15 +68,32 @@ class Pygo(): def on_local_new(self, widget): game = gogame.GoGame(goban.Goban()) - self.games.append_page(game, gtk.Label('Local Game')) - self.games.set_tab_reorderable(game, True) - game.show_all() - game.winner_box.hide() + self._add_game(game, 'Local Game') def on_local_load_sgf(self, widget): - print 'stub: Pygo.on_local_load_sgf()' + dialog = gtk.FileChooserDialog(title='Choose SGF File', action=gtk.FILE_CHOOSER_ACTION_OPEN, + buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) + dialog.set_default_response(gtk.RESPONSE_OK) + + # fixme - add filters and such? + + resp = dialog.run() + + if resp == gtk.RESPONSE_OK: + game = gogame.GoGame(goban.Goban(file_name=dialog.get_filename())) + self._add_game(game, 'Loaded SGF') # fixme - put something more interesting in the label + + dialog.destroy() + + + def _add_game(self, game, label): + self.games.append_page(game, gtk.Label(label)) + self.games.set_tab_reorderable(game, True) + game.show_all() + game.winner_box.hide() + def on_net_direct(self, widget): print 'stub: Pygo.on_net_direct()' diff --git a/ui/default.glade b/ui/default.glade index 41f599b..4e728e3 100644 --- a/ui/default.glade +++ b/ui/default.glade @@ -101,11 +101,24 @@ - + True _Load SGF True - + + + + + + True + gtk-open + 1 + 0.5 + 0.5 + 0 + 0 + +