Make update code on playing a stone much more efficient

This commit is contained in:
2012-04-18 11:33:20 -04:00
parent e9032003f7
commit becf13e4ab
3 changed files with 57 additions and 17 deletions

View File

@ -67,19 +67,33 @@ class Goban:
self.hover = None
# For performance reasons (to help with drawing code)
# any modified positions are returned in a list.
# This is self._changed, which is modified by this
# 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):
if color is None:
color = self.to_move
if self.to_move == Goban.EMPTY:
return
return None
rpos = self._real_pos(pos)
if not self._valid_move(rpos, color):
return
return None
self.board[rpos] = color
self._changed = []
self._changed.append(rpos)
if self.ko is not None:
self._changed.append(self.ko)
if self.last_move is not None:
self._changed.append(self.last_move)
self._capture(rpos)
self.last_move = rpos
self.passed_last = False
@ -87,6 +101,12 @@ class Goban:
self.to_move = self._other_color(color)
self.clear_hover()
# If there is a new ko, send that back too
if self.ko is not None:
self._changed.append(self.ko)
return self._changed
# fixme: need to handle post-game stuff here... scoring code
@ -94,6 +114,12 @@ class Goban:
if color is None:
color = self.to_move
self._changed = []
if self.ko is not None:
self._changed.append(self.ko)
if self.last_move is not None:
self._changed.append(self.last_move)
if self.passed_last:
self.to_move = Goban.EMPTY
else:
@ -103,17 +129,27 @@ class Goban:
self.last_move = None
self.ko = None
return self._changed
def resign(self, color=None):
if color is None:
color = self.to_move
self._changed = []
if self.ko is not None:
self._changed.append(self.ko)
if self.last_move is not None:
self._changed.append(self.last_move)
self.passed_last = False
self.last_move = None
self.ko = None
self.winner = self._other_color(color)
self.to_move = Goban.EMPTY
return self._changed
def _capture(self, pos, color=None):
"""Look for stones captured on the 4 sides of pos, remove them and increment
@ -232,12 +268,6 @@ class Goban:
return self._delete_group_r(pos, who)
if who == Goban.EMPTY:
return 0
self.board[x][y].state = Goban.EMPTY
def _delete_group_r(self, pos, who):
if not self._on_board(pos):
@ -247,6 +277,7 @@ class Goban:
return 0
self.board[pos] = Goban.EMPTY
self._changed.append(pos)
num_deleted = 1