Implemented sgf file saving, loading, and tracking during play
This commit is contained in:
parent
da935d8e46
commit
00448c967f
34
lib/goban.py
34
lib/goban.py
|
@ -33,10 +33,14 @@ class Goban:
|
||||||
self.winner = Goban.EMPTY
|
self.winner = Goban.EMPTY
|
||||||
|
|
||||||
self.file_name = file_name
|
self.file_name = file_name
|
||||||
|
|
||||||
|
# Track our game in an easily saveable format!
|
||||||
self.sgf_game = None
|
self.sgf_game = None
|
||||||
|
|
||||||
if self.file_name is not None:
|
if self.file_name is not None:
|
||||||
self.load_sgf(file_name)
|
self.load_sgf(file_name)
|
||||||
|
else:
|
||||||
|
self.sgf_game = sgf.Sgf_game(self.board_size)
|
||||||
|
|
||||||
|
|
||||||
def load_sgf(self, file_name):
|
def load_sgf(self, file_name):
|
||||||
|
@ -56,11 +60,29 @@ class Goban:
|
||||||
for point in white:
|
for point in white:
|
||||||
self.board[self._real_pos(point)] = Goban.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 is not None:
|
if color is not None:
|
||||||
self.play_move(color, pos)
|
if pos == 'pass':
|
||||||
|
self.pass_move(color)
|
||||||
|
else:
|
||||||
|
self.play_move(pos, color)
|
||||||
|
|
||||||
|
|
||||||
|
def save_sgf(self, file_name=None):
|
||||||
|
'''Saves the current game as an SGF file. If file_name is None, we use the previously specified filename.
|
||||||
|
If there is no previously specified filename, we raise an exception.'''
|
||||||
|
|
||||||
|
if self.file_name is None and file_name is not None:
|
||||||
|
self.file_name = file_name
|
||||||
|
|
||||||
|
if file_name is None:
|
||||||
|
file_name = self.file_name
|
||||||
|
if file_name is None:
|
||||||
|
return # fixme - this should be an exception instead
|
||||||
|
|
||||||
|
with open(file_name, 'w') as fn:
|
||||||
|
fn.write(self.sgf_game.serialise())
|
||||||
|
|
||||||
|
|
||||||
def set_hover(self, pos):
|
def set_hover(self, pos):
|
||||||
|
@ -88,6 +110,7 @@ class Goban:
|
||||||
self.hover = None
|
self.hover = None
|
||||||
self.elapsed_time = 0
|
self.elapsed_time = 0
|
||||||
self.winner = Goban.EMPTY
|
self.winner = Goban.EMPTY
|
||||||
|
self.sgf_game = sgf.Sgf_game(self.board_size)
|
||||||
|
|
||||||
|
|
||||||
def set_board_size(self, new_size):
|
def set_board_size(self, new_size):
|
||||||
|
@ -134,6 +157,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()
|
||||||
|
node.set_move(color, pos)
|
||||||
|
|
||||||
self.to_move = self._other_color(color)
|
self.to_move = self._other_color(color)
|
||||||
self.clear_hover()
|
self.clear_hover()
|
||||||
|
|
||||||
|
@ -155,6 +181,9 @@ class Goban:
|
||||||
if self.last_move is not None:
|
if self.last_move is not None:
|
||||||
self._changed.append(self.last_move)
|
self._changed.append(self.last_move)
|
||||||
|
|
||||||
|
node = self.sgf_game.extend_main_sequence()
|
||||||
|
node.set_move(color, 'pass')
|
||||||
|
|
||||||
if self.passed_last:
|
if self.passed_last:
|
||||||
self.to_move = Goban.EMPTY
|
self.to_move = Goban.EMPTY
|
||||||
self.winner = Goban.SCORING
|
self.winner = Goban.SCORING
|
||||||
|
@ -429,7 +458,6 @@ class Goban:
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _real_pos(self, pos):
|
def _real_pos(self, pos):
|
||||||
x,y = pos
|
x,y = pos
|
||||||
return x * self.board_size + y
|
return x * self.board_size + y
|
||||||
|
|
27
pygo.py
27
pygo.py
|
@ -54,11 +54,34 @@ class Pygo():
|
||||||
|
|
||||||
|
|
||||||
def on_game_save(self, widget):
|
def on_game_save(self, widget):
|
||||||
print 'stub: Pygo.on_game_save()'
|
if self.games.get_current_page() == -1:
|
||||||
|
return
|
||||||
|
|
||||||
|
game = self.games.get_nth_page(self.games.get_current_page())
|
||||||
|
if game.goban.file_name is None:
|
||||||
|
self.on_game_save_as(None)
|
||||||
|
else:
|
||||||
|
game.goban.save_sgf()
|
||||||
|
|
||||||
|
|
||||||
def on_game_save_as(self, widget):
|
def on_game_save_as(self, widget):
|
||||||
print 'stub: Pygo.on_game_save_as()'
|
if self.games.get_current_page() == -1:
|
||||||
|
return
|
||||||
|
|
||||||
|
game = self.games.get_nth_page(self.games.get_current_page())
|
||||||
|
|
||||||
|
dialog = gtk.FileChooserDialog(title=None, action=gtk.FILE_CHOOSER_ACTION_SAVE,
|
||||||
|
buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
|
||||||
|
|
||||||
|
if game.goban.file_name is not None:
|
||||||
|
dialog.set_current_name(game.goban.file_name)
|
||||||
|
|
||||||
|
# fixme - when we already have a filename, this throws an error...
|
||||||
|
resp = dialog.run()
|
||||||
|
if resp == gtk.RESPONSE_OK:
|
||||||
|
game.goban.save_sgf(dialog.get_filename())
|
||||||
|
|
||||||
|
dialog.destroy()
|
||||||
|
|
||||||
|
|
||||||
def on_game_close(self, widget):
|
def on_game_close(self, widget):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user