Fully implemented loading SGF files, although we can only look at the main sequence for now

This commit is contained in:
Anna Rose 2012-04-21 18:25:04 -04:00
parent 692dc294d6
commit 40d9c6c08f
3 changed files with 65 additions and 28 deletions

View File

@ -1,17 +1,17 @@
import gomill.sgf from gomill import sgf
class Goban: class Goban:
"""Represents the go board. Handles stone placement, captures, etc""" """Represents the go board. Handles stone placement, captures, etc"""
# enum values for the board array # enum values for the board array
EMPTY=0 EMPTY='.'
WHITE=1 WHITE='w'
BLACK=2 BLACK='b'
SCORE_BLACK=3 SCORE_BLACK='B'
SCORE_WHITE=4 SCORE_WHITE='W'
SCORE_DAME=5 SCORE_DAME='d'
SCORING=6 SCORING='s'
def __init__(self, board_size=19, file_name=None): def __init__(self, board_size=19, file_name=None):
@ -20,12 +20,6 @@ class Goban:
num_points = board_size * board_size num_points = board_size * board_size
self.board = [Goban.EMPTY] * num_points 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.def_draw_codes = self._make_default_draw_codes()
self.to_move = Goban.BLACK self.to_move = Goban.BLACK
@ -38,22 +32,35 @@ class Goban:
self.elapsed_time = 0 self.elapsed_time = 0
self.winner = Goban.EMPTY 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: 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()) self.sgf_game = sgf.Sgf_game.from_string(fn.read())
except IOError: except IOError:
# fixme - this should be convertable into a dialog box... perhaps it should throw an exception of its own # 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.' 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(): for node in self.sgf_game.get_main_sequence():
color, pos = node.get_move() color, pos = node.get_move()
if color == 'b': if color is not None:
color = Goban.BLACK self.play_move(color, pos)
elif color == 'w':
color = Goban.WHITE
self.play_move(color, pos)
def set_hover(self, pos): def set_hover(self, pos):

27
pygo.py
View File

@ -68,14 +68,31 @@ class Pygo():
def on_local_new(self, widget): def on_local_new(self, widget):
game = gogame.GoGame(goban.Goban()) game = gogame.GoGame(goban.Goban())
self.games.append_page(game, gtk.Label('Local Game')) self._add_game(game, 'Local Game')
self.games.set_tab_reorderable(game, True)
game.show_all()
game.winner_box.hide()
def on_local_load_sgf(self, widget): 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): def on_net_direct(self, widget):

View File

@ -101,11 +101,24 @@
</child> </child>
<child> <child>
<widget class="GtkMenuItem" id="load_sgf"> <widget class="GtkImageMenuItem" id="load_sgf">
<property name="visible">True</property> <property name="visible">True</property>
<property name="label" translatable="yes">_Load SGF</property> <property name="label" translatable="yes">_Load SGF</property>
<property name="use_underline">True</property> <property name="use_underline">True</property>
<signal name="activate" handler="on_local_load_sgf" last_modification_time="Sat, 21 Apr 2012 08:07:07 GMT"/> <signal name="activate" handler="on_local_load_sgf" last_modification_time="Sat, 21 Apr 2012 21:10:09 GMT"/>
<accelerator key="O" modifiers="GDK_CONTROL_MASK" signal="activate"/>
<child internal-child="image">
<widget class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="stock">gtk-open</property>
<property name="icon_size">1</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
</widget>
</child>
</widget> </widget>
</child> </child>
</widget> </widget>