def test_suicide_rule1(self): BoardState.BOARD_SIZE = 9 board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'B', ' ', 'W', ' ', ' ', ' ', ' '], [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] state = BoardState(board) threw_correct = False try: state.move('B', Position(3, 3)) except Exception as e: if isinstance(e, SuicideException): threw_correct = True assert threw_correct
def test_capture2(self): BoardState.BOARD_SIZE = 9 board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', 'B', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'B', 'B', 'W', ' ', ' ', ' ', ' '], [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] tar_board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', 'W', ' ', ' ', ' ', ' '], [' ', 'W', ' ', ' ', 'W', ' ', ' ', ' ', ' '], [' ', ' ', 'W', 'W', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] state = BoardState(board) state.move('W', Position(2, 4)) assert BoardState.same_boards(state.board, tar_board)
def game_to_XYs_with_history(sgf_path): """ returns 8 planes: player, opponent, empty, 5 history planes :param sgf_path: :return: """ parser = SGFParser(sgf_path) Xs = [] Ys = [] moves = [m for m in parser.moves if m is not None] "spots_last_played[r][c] = turn num in which this spot was last played or None if it wasn't played yet" spots_last_played = [[None for c in range(BoardState.BOARD_SIZE)] for r in range(BoardState.BOARD_SIZE)] for move_i, (state, move) in enumerate( zip(BoardState.get_all_intermittent_states_from_moves(moves), moves)): # X, Y = board_state_to_X_Y(state, move) Y = get_board_Y_repr(state, move) three_planes = get_3_plane_X_representation(state, move.player) history_planes = get_history_planes(spots_last_played, current_turn=move_i) X = concat_planes(three_planes['player'], three_planes['opponent'], three_planes['empty'], *history_planes) Xs.append(X) Ys.append(Y) spots_last_played[move.pos.row][move.pos.col] = move_i move_i += 1 return Xs, Ys
def __init__(self): self.BOARD_SIZE = BoardState.BOARD_SIZE self.SQUARE_SIZE = 50 self.STONE_RADIUS = 20 self.state = BoardState() self.PADDING = 100 self.turn = 'W' self.canvas_width = self.SQUARE_SIZE * self.BOARD_SIZE + self.PADDING * 2 self.canvas_height = self.SQUARE_SIZE * self.BOARD_SIZE + self.PADDING * 2 self.top = Tk() self.canvas = Canvas(self.top, width=self.canvas_width, height=self.canvas_height) self.canvas.pack() self.canvas.bind('<Button-1>', self.mouse_click)
def run(): parser = SGFParser( 'data/13/sources/go13/2015-03-06T16:25:13.507Z_k5m7o9gtv63k.sgf') state = BoardState.init_from_moves(parser.moves) gui = GUI() gui.state = state gui.start()
def mouse_click(self, event): click_row = round((event.y - self.PADDING) / self.SQUARE_SIZE) click_col = round((event.x - self.PADDING) / self.SQUARE_SIZE) try: self.state.move(self.turn, Position(click_row, click_col)) self.turn = BoardState.other_player(self.turn) self.draw_state() except InvalidMoveException as e: print(e)
def test_liberties1(self): BoardState.BOARD_SIZE = 9 board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] state = BoardState(board) group, captured = state.get_group_and_is_captured(Position(4, 4)) assert len(group) == 1 and list(group)[0] == Position( 4, 4) and not captured
def test_liberties3(self): BoardState.BOARD_SIZE = 9 board = [ ['B', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['W', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] state = BoardState(board) group, captured = state.get_group_and_is_captured(Position(0, 0)) assert len(group) == 1 and captured
def sameboards_test(self): BoardState.BOARD_SIZE = 9 board1 = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'B', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] board2 = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'B', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] board3 = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'B', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', 'W', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] assert BoardState.same_boards( board1, board2) and not BoardState.same_boards(board1, board3)
def test_game1(self): BoardState.BOARD_SIZE = 13 init_state = BoardState() parser = SGFParser('tests_data/game1.sgf') moves = parser.moves good_end_board = [ [' ', 'W', 'B', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'W', 'W', 'B', ' ', ' ', 'B', 'B', ' ', ' ', ' ', 'W', ' '], [' ', ' ', 'W', 'B', 'B', 'B', 'W', 'B', ' ', ' ', 'B', 'B', 'B'], [' ', ' ', ' ', 'W', 'W', 'B', 'W', 'W', 'B', 'B', 'B', 'W', ' '], [' ', ' ', ' ', 'W', ' ', 'B', 'W', 'W', 'W', 'W', 'B', 'W', ' '], [' ', ' ', ' ', ' ', ' ', 'B', 'B', 'W', 'W', 'B', 'W', 'W', ' '], [' ', ' ', ' ', 'W', 'B', 'B', 'W', 'W', ' ', ' ', 'B', 'W', ' '], [' ', 'B', ' ', 'W', ' ', ' ', 'B', 'W', 'W', 'W', ' ', ' ', ' '], ['W', 'W', ' ', ' ', 'B', ' ', ' ', 'W', 'W', 'B', 'W', 'W', ' '], ['B', 'W', 'W', 'W', 'W', 'W', 'B', 'W', 'B', 'B', 'B', 'W', ' '], ['B', 'B', 'B', 'B', 'W', 'B', 'B', 'B', ' ', 'B', ' ', 'B', ' '], [' ', ' ', 'B', ' ', 'B', 'W', ' ', ' ', ' ', ' ', 'B', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] ] for move in moves: init_state.move(move.player, move.pos) assert BoardState.same_boards(init_state.board, good_end_board)
def game_to_XYs_3_planes(sgf_path): parser = SGFParser(sgf_path) Xs = [] Ys = [] moves = [m for m in parser.moves if m is not None] for state, move in zip( BoardState.get_all_intermittent_states_from_moves(moves), moves): # X, Y = board_state_to_X_Y(state, move) Y = get_board_Y_repr(state, move) three_planes = get_3_plane_X_representation(state, move.player) X = concat_planes(three_planes['player'], three_planes['opponent'], three_planes['empty']) Xs.append(X) Ys.append(Y) return Xs, Ys
def test_ko_rule1(self): BoardState.BOARD_SIZE = 9 board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['B', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['W', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] ko_board = [ [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['W', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', 'W', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['W', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ] state = BoardState(board) state.num_turns = 10 state.move('B', Position(3, 0)) state.move('W', Position(4, 0)) assert BoardState.same_boards(state.board, ko_board) threw_correct = False try: state.move('B', Position(5, 0)) except Exception as e: if isinstance(e, KoException): threw_correct = True assert threw_correct
def get_3_plane_X_representation(board_state, turn): """ plane 1 - 1 if player's stone plane 2 - 1 if opponent's stone plane 3 - 1 if empty spot """ plane_player = np.array([[ 1 if board_state.board[r][c] == turn else 0 for c in range(BoardState.BOARD_SIZE) ] for r in range(BoardState.BOARD_SIZE)]) plane_opponent = np.array([[ 1 if board_state.board[r][c] == BoardState.other_player(turn) else 0 for c in range(BoardState.BOARD_SIZE) ] for r in range(BoardState.BOARD_SIZE)]) plane_empty = np.array([[ 1 if board_state.board[r][c] == ' ' else 0 for c in range(BoardState.BOARD_SIZE) ] for r in range(BoardState.BOARD_SIZE)]) # return concat_planes(plane_player, plane_opponent, plane_empty) return { 'player': plane_player, 'opponent': plane_opponent, 'empty': plane_empty }
import sys project_root = '.' sys.path.append(project_root) from go_logic.GoLogic import BoardState from go_logic.sgf_parser import SGFParser from os import path, listdir import time if __name__ == "__main__": BoardState.BOARD_SIZE = 13 games_dir = path.join(project_root, 'tests_data/1000_games/') t1 = time.time() games_sgfs = listdir(games_dir) print('parsing') games_moves = [SGFParser(path.join(games_dir, game_sgf)).moves for game_sgf in games_sgfs] print('done parsing') for game_i, game_moves in enumerate(games_moves): print(game_i) init_state = BoardState() for move in game_moves: if move: init_state.move(move.player, move.pos) print(init_state) print(time.time() - t1)
class GUI: def __init__(self): self.BOARD_SIZE = BoardState.BOARD_SIZE self.SQUARE_SIZE = 50 self.STONE_RADIUS = 20 self.state = BoardState() self.PADDING = 100 self.turn = 'W' self.canvas_width = self.SQUARE_SIZE * self.BOARD_SIZE + self.PADDING * 2 self.canvas_height = self.SQUARE_SIZE * self.BOARD_SIZE + self.PADDING * 2 self.top = Tk() self.canvas = Canvas(self.top, width=self.canvas_width, height=self.canvas_height) self.canvas.pack() self.canvas.bind('<Button-1>', self.mouse_click) def draw_state(self): def circle(canvas, x, y, r, color): id = canvas.create_oval(x - r, y - r, x + r, y + r, fill=color) return id self.canvas.delete('all') # draw grid board_size = self.SQUARE_SIZE * self.BOARD_SIZE + self.PADDING * 2 for i in range(self.BOARD_SIZE): self.canvas.create_line(i * self.SQUARE_SIZE + self.PADDING, self.PADDING, i * self.SQUARE_SIZE + self.PADDING, board_size - self.PADDING - self.SQUARE_SIZE, fill='#000000', width=3) self.canvas.create_line(self.PADDING, i * self.SQUARE_SIZE + self.PADDING, board_size - self.PADDING - self.SQUARE_SIZE, i * self.SQUARE_SIZE + self.PADDING, fill='#000000', width=3) # draw stones for r in range(self.BOARD_SIZE): for c in range(self.BOARD_SIZE): if self.state.board[r][c] != ' ': color = '#000000' if self.state.board[r][ c] == 'B' else '#FFFFFF' x = self.PADDING + c * self.SQUARE_SIZE y = self.PADDING + r * self.SQUARE_SIZE circle(self.canvas, x, y, self.STONE_RADIUS, color) def start(self): self.draw_state() self.top.mainloop() def mouse_click(self, event): click_row = round((event.y - self.PADDING) / self.SQUARE_SIZE) click_col = round((event.x - self.PADDING) / self.SQUARE_SIZE) try: self.state.move(self.turn, Position(click_row, click_col)) self.turn = BoardState.other_player(self.turn) self.draw_state() except InvalidMoveException as e: print(e)