def init_neighbor_table(dim): rows, cols = dim new_table = {} for r in range(1, rows + 1): for c in range(1, cols + 1): p = Point(row=r, col=c) full_neighbors = p.neighbors() true_neighbors = [ n for n in full_neighbors if 1 <= n.row <= rows and 1 <= n.col <= cols] new_table[p] = true_neighbors neighbor_tables[dim] = new_table
def init_neighbor_table(dim): global neighbor_tables rows, cols = dim new_table = {} for row in range(1, rows + 1): for col in range(1, cols + 1): pt = Point(row=row, col=col) full_neighbors = pt.neighbors() new_table[pt] = [ n for n in full_neighbors if 1 <= n.row <= rows and 1 <= n.col <= cols ] neighbor_tables[dim] = new_table
def is_point_an_eye(board :Board, point: Point, color): # 目は空の点 if board.get(point) is not None: return False # 隣接するすべての点には味方の石が含まれている必要がある for neighbor in point.neighbors(): if board.is_on_grid(neighbor): neighbor_color = board.get(neighbor) if neighbor_color != color: return False # 点が盤の中央にある場合、4つの角のうち3つの角を支配する必要がある # 辺ではすべての角を支配する必要がある friendly_corners = 0 off_board_corners = 0 corners = [ Point(point.row - 1, point.col - 1), Point(point.row - 1, point.col + 1), Point(point.row + 1, point.col - 1), Point(point.row + 1, point.col + 1), ] for corner in corners: if board.is_on_grid(corner): corner_color = board.get(corner) if corner_color == color: friendly_corners += 1 else: off_board_corners += 1 if off_board_corners > 0: # 点が角または辺にある return off_board_corners + friendly_corners == 4 # 点は中央にある return friendly_corners >= 3
def encode(self, game_state): board_tensor = np.zeros( (self.num_planes, self.board_height, self.board_width)) for r in range(self.board_height): for c in range(self.board_width): point = Point(row=r + 1, col=c + 1) go_string = game_state.board.get_go_string(point) if go_string and go_string.color == game_state.next_player: board_tensor[offset("stone_color")][r][c] = 1 elif go_string and go_string.color == game_state.next_player.other: board_tensor[offset("stone_color") + 1][r][c] = 1 else: board_tensor[offset("stone_color") + 2][r][c] = 1 board_tensor[offset("ones")] = self.ones() board_tensor[offset("zeros")] = self.zeros() if not is_point_an_eye(game_state.board, point, game_state.next_player): board_tensor[offset("sensibleness")][r][c] = 1 ##ages = min(game_state.board.move_ages.get(r, c, step?), 8) ages = int(min(game_state.board.move_ages.get(r, c), 8)) # Nail insert int(...) if ages > 0: #print(ages) board_tensor[offset("turns_since") + ages][r][c] = 1 if game_state.board.get_go_string(point): liberties = min( game_state.board.get_go_string(point).num_liberties, 8) board_tensor[offset("liberties") + liberties][r][c] = 1 move = Move(point) if game_state.is_valid_move(move): new_state = game_state.apply_move(move) liberties = min( new_state.board.get_go_string(point).num_liberties, 8) board_tensor[offset("liberties_after") + liberties][r][c] = 1 adjacent_strings = [ game_state.board.get_go_string(nb) for nb in point.neighbors() ] capture_count = 0 for go_string in adjacent_strings: other_player = game_state.next_player.other if go_string and go_string.num_liberties == 1 and go_string.color == other_player: capture_count += len(go_string.stones) capture_count = min(capture_count, 8) board_tensor[offset("capture_size") + capture_count][r][c] = 1 if go_string and go_string.num_liberties == 1: go_string = game_state.board.get_go_string(point) if go_string: num_atari_stones = min(len(go_string.stones), 8) board_tensor[offset("self_atari_size") + num_atari_stones][r][c] = 1 if is_ladder_capture(game_state, point): board_tensor[offset("ladder_capture")][r][c] = 1 if is_ladder_escape(game_state, point): board_tensor[offset("ladder_escape")][r][c] = 1 if self.use_player_plane: if game_state.next_player == Player.black: board_tensor[offset("ones")] = self.ones() else: board_tensor[offset("zeros")] = self.zeros() return board_tensor