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.moveCount = 0
def move(self, color, y, x):
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._floodFill(-color, x+i, y+j):
self._remove()
self.temp = [[False]*19 for i in range(19)]
if not self._floodFill(color, x, y):
self.board[x][y] = EMPTY
return False
self.moveCount += 1
return True
def _floodFill(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._floodFill(color, x-1, y) or \
self._floodFill(color, x+1, y) or \
self._floodFill(color, x, y-1) or \
self._floodFill(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