diff --git a/src/go.py b/src/go.py --- a/src/go.py +++ b/src/go.py @@ -1,21 +1,22 @@ -class Go: - EMPTY=0 - BLACK=1 - WHITE=-1 +EMPTY=0 +BLACK=1 +WHITE=-1 - ## Initializes self.board to a list[r][c]=Go.EMPTY. + +class Go: + ## Initializes self.board to a list[r][c]=EMPTY. def __init__(self,boardSize=19): self.boardSize=boardSize - self.board=[[Go.EMPTY]*self.boardSize for x in range(boardSize)] + self.board=[[EMPTY]*self.boardSize for x in range(boardSize)] ## Executes a move. # # Doesn't check for kos. Suicide not allowed. # - # @param color Go.BLACK or Go.WHITE + # @param color BLACK or WHITE # @return True on success, False on failure (illegal move) def move(self,color,row,col): - if self.board[row][col]!=Go.EMPTY: return False + if self.board[row][col]!=EMPTY: return False self.board[row][col]=color @@ -27,7 +28,7 @@ # check for suicide self.temp=[[False]*self.boardSize for x in self.board] if not self._floodFill(color,row,col): - self.board[row][col]=Go.EMPTY + self.board[row][col]=EMPTY return False return True @@ -39,7 +40,7 @@ def _floodFill(self,color,row,col): if col<0 or col>=self.boardSize or row<0 or row>=self.boardSize: return False # out of range if self.temp[row][col]: return False # already visited - if self.board[row][col]==Go.EMPTY: return True # found a liberty + if self.board[row][col]==EMPTY: return True # found a liberty if self.board[row][col]!=color: return False # opponent's stone self.temp[row][col]=True # set visited return self._floodFill(color,row,col-1) or self._floodFill(color,row,col+1) or self._floodFill(color,row-1,col) or self._floodFill(color,row+1,col) # check neighbors @@ -48,4 +49,9 @@ def _remove(self): for r in range(self.boardSize): for c in range(self.boardSize): - if self.temp[r][c]: self.board[r][c]=Go.EMPTY + if self.temp[r][c]: self.board[r][c]=EMPTY + + +def exportBoard(board): + substitutions={EMPTY:".", BLACK:"X", WHITE:"O"} + return "\n".join("".join(substitutions.get(x,"?") for x in row) for row in board)