diff --git a/GTP/game.py b/GTP/game.py index 7f1b4c6..76b0750 100644 --- a/GTP/game.py +++ b/GTP/game.py @@ -8,19 +8,18 @@ import utils import copy - ''' (1, 1) is considered as the upper left corner of the board, (size, 1) is the lower left ''' -DELTA = [[1,0], [-1,0], [0, -1], [0, 1]] +DELTA = [[1, 0], [-1, 0], [0, -1], [0, 1]] + class Executor: def __init__(self, **kwargs): self.game = kwargs['game'] - def _bfs(self, vertex, color, block, status, alive_break): block.append(vertex) status[self.game._flatten(vertex)] = True @@ -30,7 +29,7 @@ class Executor: if self.game.board[self.game._flatten(n)] == color: self._bfs(n, color, block, status, alive_break) - def _find_block(self, vertex, alive_break = False): + def _find_block(self, vertex, alive_break=False): block = [] status = [False] * (self.game.size * self.game.size) color = self.game.board[self.game._flatten(vertex)] @@ -39,34 +38,32 @@ class Executor: for b in block: for n in self._neighbor(b): if self.game.board[self.game._flatten(n)] == utils.EMPTY: - return False,block - return True,block - + return False, block + return True, block def _is_qi(self, color, vertex): nei = self._neighbor(vertex) for n in nei: if self.game.board[self.game._flatten(n)] == utils.EMPTY: return True - + self.game.board[self.game._flatten(vertex)] = color for n in nei: if self.game.board[self.game._flatten(n)] == utils.another_color(color): - can_kill,block = self._find_block(n) - if can_kill: + can_kill, block = self._find_block(n) + if can_kill: self.game.board[self.game._flatten(vertex)] = utils.EMPTY return True ### can not suicide - can_kill,block = self._find_block(vertex) - if can_kill: + can_kill, block = self._find_block(vertex) + if can_kill: self.game.board[self.game._flatten(vertex)] = utils.EMPTY return False self.game.board[self.game._flatten(vertex)] = utils.EMPTY return True - def _check_global_isomorphous(self, color, vertex): ##backup _board = copy.copy(self.game.board) @@ -80,16 +77,14 @@ class Executor: self.game.board = _board return res - def _in_board(self, vertex): - x, y = vertex + x, y = vertex if x < 1 or x > self.game.size: return False if y < 1 or y > self.game.size: return False return True - def _neighbor(self, vertex): - x,y = vertex + x, y = vertex nei = [] for d in DELTA: _x = x + d[0] @@ -102,11 +97,10 @@ class Executor: nei = self._neighbor(vertex) for n in nei: if self.game.board[self.game._flatten(n)] == utils.another_color(color): - can_kill, block = self._find_block(n, alive_break = True) + can_kill, block = self._find_block(n, alive_break=True) if can_kill: for b in block: self.game.board[self.game._flatten(b)] = utils.EMPTY - def is_valid(self, color, vertex): ### in board @@ -121,7 +115,6 @@ class Executor: if not self._is_qi(color, vertex): return False - if self._check_global_isomorphous(color, vertex): return False @@ -131,7 +124,7 @@ class Executor: if not self.is_valid(color, vertex): return False self.game.board[self.game._flatten(vertex)] = color - self._process_board(color,vertex) + self._process_board(color, vertex) self.game.history.append(copy.copy(self.game.board)) return True @@ -142,13 +135,12 @@ class Game: self.komi = 6.5 self.board = [utils.EMPTY] * (self.size * self.size) self.strategy = None - self.executor = Executor(game = self) + self.executor = Executor(game=self) self.history = [] def _flatten(self, vertex): - x,y = vertex - return (y - 1) * self.size + (x-1) - + x, y = vertex + return (y - 1) * self.size + (x - 1) def clear(self): self.board = [utils.EMPTY] * (self.size * self.size) @@ -160,7 +152,6 @@ class Game: def set_komi(self, k): self.komi = k - def check_valid(self, vertex): return True @@ -169,35 +160,28 @@ class Game: return True res = self.executor.do_move(color, vertex) return res - def gen_move(self, color): - #move = self.strategy.gen_move(color) - #return move + # move = self.strategy.gen_move(color) + # return move return utils.PASS - def status2symbol(self, s): - pool = { utils.WHITE:'#', utils.EMPTY:'.', utils.BLACK:'*', utils.FILL:'F', utils.UNKNOWN:'?'} + pool = {utils.WHITE: '#', utils.EMPTY: '.', utils.BLACK: '*', utils.FILL: 'F', utils.UNKNOWN: '?'} return pool[s] - def show_board(self): row = [i for i in range(1, 20)] col = ' abcdefghijklmnopqrstuvwxyz' for i in range(self.size): - print(row[i], end = ' ') + print(row[i]) if row[i] < 10: - print(' ', end = '') + print(' ') for j in range(self.size): - print(self.status2symbol(self.board[self._flatten((j+1,i+1))]), end=' ') + print(self.status2symbol(self.board[self._flatten((j + 1, i + 1))])) print('\n') - print(' ', end = '') + print(' ') for j in range(self.size + 1): - print(col[j], end = ' ') + print(col[j]) print('\n') - - - -