Cleaned up some of the sgf code and refactored it to make move navigation easier when we get to it

This commit is contained in:
Anna Rose 2012-04-25 12:15:41 -04:00
parent 8e573f1a86
commit 3625cb3804
2 changed files with 53 additions and 14 deletions

View File

@ -1,5 +1,6 @@
from gomill import sgf from gomill import sgf
# leftoffat: implementing navigation forwards and backward through the game.
class Goban: class Goban:
"""Represents the go board. Handles stone placement, captures, etc""" """Represents the go board. Handles stone placement, captures, etc"""
@ -52,6 +53,17 @@ class Goban:
print 'There was a problem loading the SGF file.' print 'There was a problem loading the SGF file.'
# Do initial layout # Do initial layout
self._place_initial_stones()
for node in self.sgf_game.get_main_sequence():
self._play_node(node)
if self.sgf_game.get_winner() is not None:
self.winner = self.sgf_game.get_winner()
self.to_move = None
def _place_initial_stones(self):
root = self.sgf_game.get_root() root = self.sgf_game.get_root()
if root.has_setup_stones(): if root.has_setup_stones():
black, white, empty = root.get_setup_stones() black, white, empty = root.get_setup_stones()
@ -59,18 +71,37 @@ class Goban:
self.board[self._real_pos(self._sgf_to_move(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(self._sgf_to_move(point))] = Goban.WHITE self.board[self._real_pos(self._sgf_to_move(point))] = Goban.WHITE
for point in empty:
self.board[self._real_pos(self._sgf_to_move(point))] = Goban.EMPTY
for node in self.sgf_game.get_main_sequence():
# Play a single node from an sgf tree. Useful for warking the tree.
def _play_node(self, node):
color, pos = node.get_move() color, pos = node.get_move()
if color is not None: if color is not None:
if pos == 'pass': if pos == 'pass':
self.pass_move(color) self.pass_move(color)
else: else:
self.play_move(self._sgf_to_move(pos), color, add_sgf=False) self.play_move(self._sgf_to_move(pos), color, add_sgf=False)
# fixme - add resignation detection
if self.sgf_game.get_winner() is not None:
self.winner = self.sgf_game.get_winner() # Play all moves up to and including node, which should be
self.to_move = None # an sgf.Tree_node
def _play_to_node(self, node):
self.soft_reset()
for n in self.sgf_game.get_main_sequence():
self._sgf_play_node(self, n)
if n == node:
return
# Play to the nth move
def _play_to_move_n(self, n):
self.soft_reset()
portion = self.sgf_game.get_main_sequence()[:n]
for node in portion:
self._play_node(node)
def save_sgf(self, file_name=None): def save_sgf(self, file_name=None):
@ -101,8 +132,9 @@ class Goban:
self.hover = rpos self.hover = rpos
def reset(self): def soft_reset(self):
"""Reset the board to a pre-game state""" """Reset the board to a pre-game state, but preserves move history.
In other words, goes back to before move #1"""
# Clear the board by setting it to the same size it currently is at # Clear the board by setting it to the same size it currently is at
self.set_board_size(self.board_size) self.set_board_size(self.board_size)
self.to_move = Goban.BLACK self.to_move = Goban.BLACK
@ -114,7 +146,12 @@ class Goban:
self.hover = None self.hover = None
self.elapsed_time = 0 self.elapsed_time = 0
self.winner = Goban.EMPTY self.winner = Goban.EMPTY
def reset(self):
'''Fully resets the game. The only thing preserved is the SGF filename, if it is set '''
self.sgf_game = sgf.Sgf_game(self.board_size) self.sgf_game = sgf.Sgf_game(self.board_size)
self.soft_reset()
def set_board_size(self, new_size): def set_board_size(self, new_size):
@ -351,7 +388,7 @@ class Goban:
def _delete_group_r(self, pos, who): def _delete_group_r(self, pos, who):
if not self._on_board(pos): if not self._on_board(pos):
return return 0
if self.board[pos] != who: if self.board[pos] != who:
return 0 return 0
@ -477,7 +514,7 @@ class Goban:
def _on_board(self, pos): def _on_board(self, pos):
return pos >= 0 or pos < self.board_size * self.board_size return pos >= 0 and pos < self.board_size * self.board_size
def _other_color(self, color): def _other_color(self, color):

View File

@ -169,6 +169,8 @@ class GoGame(gtk.HBox):
code = 'bH' code = 'bH'
elif self.goban.to_move == goban.Goban.WHITE: elif self.goban.to_move == goban.Goban.WHITE:
code = 'wH' code = 'wH'
else:
continue
base_img = GoGame.img_res[code] base_img = GoGame.img_res[code]
img = base_img.scale_simple(inc, inc, gtk.gdk.INTERP_BILINEAR) img = base_img.scale_simple(inc, inc, gtk.gdk.INTERP_BILINEAR)