BLACK = 1
WHITE = -1
EMPTY = 0
class Go:
board = [[EMPTY]*19 for i in range(19)]
def __init__(self):
self.board = [[EMPTY]*19 for i in range(19)]
self.move_count = 0
self.temp = [[]]
def move(self, color, y, x):
"""Update the board with the specified move.
:param int color: BLACK or WHITE
:param int y: the row number
:param int x: the column number
:return: True on success, False on an invalid move"""
if self.board[x][y] != EMPTY:
return False
self.board[x][y] = color
for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
self.temp = [[False]*19 for i in range(19)]
if not self._flood_fill(-color, x + i, y + j):
self._remove()
self.temp = [[False]*19 for i in range(19)]
if not self._flood_fill(color, x, y):
self.board[x][y] = EMPTY
return False
self.move_count += 1
return True
def _flood_fill(self, color, x, y):
if x < 0 or x > 18 or y < 0 or y > 18:
return False
if self.temp[x][y]:
return False
if self.board[x][y] == EMPTY:
return True
if self.board[x][y] != color:
return False
self.temp[x][y] = True
return self._flood_fill(color, x-1, y) or \
self._flood_fill(color, x+1, y) or \
self._flood_fill(color, x, y-1) or \
self._flood_fill(color, x, y+1)
def _remove(self):
for i in range(19):
for j in range(19):
if self.temp[i][j]:
self.board[i][j] = EMPTY