def select_move(self, game_state): candidates = [] for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): candidate = Point(row=r, col=c) if game_state.is_valid_move(Move.play(candidate)) and \ not is_point_an_eye(game_state.board, candidate, game_state.next_player): candidates.append(candidate) if not candidates: return Move.pass_turn() return Move.play(random.choice(candidates))
def select_move(self, game_state): """Choose a random valid move that perserves our own eyes""" candidates = [] # keeps track of all valid moves for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): candidate = Point(row=r, col=c) if game_state.is_valid_move(Move.play(candidate)) and \ not is_point_an_eye(game_state.board, candidate, game_state.next_player): candidates.append(candidate) if not candidates: return Move.pass_turn() # if there are no valid moves left, we pass return Move.play(random.choice(candidates)) # choose a random move that is valid
def select_move(self, game_state): """Choose a random valid move that preserves our own eyes. 自分の眼を維持するランダムな有効な着手を選択する""" candidates = [] # 候補 for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): candidate = Point(row=r, col=c) if game_state.is_valid_move( Move.play( candidate ) ) and \ not is_point_an_eye( game_state.board, # <1> candidate, game_state.next_player ): candidates.append(candidate) # <2> if not candidates: # <3> return Move.pass_turn() return Move.play(random.choice(candidates)) # <4>
def select_move(self, game_state): """Choose a random valid move that preserves our own eyes.""" candidates = [] for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): candidate = Point(row=r, col=c) if game_state.is_valid_move(Move.play(candidate)) and \ not is_point_an_eye(game_state.board, candidate, game_state.next_player): candidates.append(candidate) if not candidates: return Move.pass_turn() return Move.play(random.choice(candidates)) # end::random_bot[]
def test_new_game(self): start = GameState.new_game(19) next_state = start.apply_move(Move.play(Point(16, 16))) self.assertEqual(start, next_state.previous_state) self.assertEqual(Player.white, next_state.next_player) self.assertEqual(Player.black, next_state.board.get(Point(16, 16)))
def select_move(self, game_state): """Choose a random valid move that preserves our own eyes.""" dim = (game_state.board.num_rows, game_state.board.num_cols) if dim != self.dim: self._update_cache(dim) idx = np.arange(len(self.point_cache)) np.random.shuffle(idx) for i in idx: p = self.point_cache[i] if game_state.is_valid_move(Move.play(p)) and \ not is_point_an_eye(game_state.board, p, game_state.next_player): return Move.play(p) return Move.pass_turn()
def select_move(self, game_state): # Chon random 1 nuoc di hop le dim = (game_state.board.num_rows, game_state.board.num_cols) if dim != self.dim: self._update_cache(dim) idx = np.arange(len(self.point_cache)) np.random.shuffle(idx) for i in idx: p = self.point_cache[i] if game_state.is_valid_move(Move.play(p)) and \ not is_point_an_eye(game_state.board, p, game_state.next_player): return Move.play(p) return Move.pass_turn()
def encode(self, game_state): # 空箱 board_tensor = np.zeros(self.shape()) # 白は0,1,2黒は3,4,5を使うためにズラすとき使う base_plane = { game_state.next_player: 0, game_state.next_player.other: 3 } # 全ての盤上の点を探索 for row in range(self.board_height): for col in range(self.board_width): p = Point(row=row + 1, col=col + 1) go_string = game_state.board.get_go_string(p) # 石が置かれていない点は劫かどうかだけ調べればよい if go_string is None: if game_state.does_move_violate_ko(game_state.next_player, Move.play(p)): board_tensor[6][row][col] = 1 else: # 呼吸点が3以上か,2か,1か liberty_plane = min(3, go_string.num_liberties) - 1 liberty_plane += base_plane[go_string.color] board_tensor[liberty_plane][row][col] = 1 return board_tensor
def process_zip(self, zip_file_name, data_file_name, game_list): tar_file = self.unzip_data(zip_file_name) zip_file = tarfile.open(self.data_dir + '/' + tar_file) name_list = zip_file.getnames() # Xac dinh tong so luong nuoc di trong tat ca games trong file zip total_examples = self.num_total_examples(zip_file, game_list, name_list) shape = self.encoder.shape() feature_shape = np.insert(shape, 0, np.asarray([total_examples])) features = np.zeros(feature_shape) labels = np.zeros((total_examples, )) counter = 0 for index in game_list: name = name_list[index + 1] if not name.endswith('.sgf'): raise ValueError(name + ' is not a valid sgf') sgf_content = zip_file.extractfile(name).read() # Doc noi dung SGF duoi dang chuoi ,sau khi giai nen tep zip sgf = Sgf_game.from_string(sgf_content) # Suy ra trang thai tro choi ban dau bang cach ap dung tat ca cac vien da handicap game_state, first_move_done = self.get_handicap(sgf) # Lap lai tat ca cac di chuyen trong tep SGF for item in sgf.main_sequence_iter(): color, move_tuple = item.get_move() point = None if color is not None: # Doc toa do cua hon da duoc choi if move_tuple is not None: row, col = move_tuple point = Point(row + 1, col + 1) move = Move.play(point) else: move = Move.pass_turn() if first_move_done and point is not None: # Ma hoa trang thai tro choi duoi dang feature features[counter] = self.encoder.encode(game_state) # Ma hoa nuoc di nhu nhan cua feature labels[counter] = self.encoder.encode_point(point) counter += 1 # Sau do, nuoc di duoc ap dung cho ban co de tien hanh nuoc di tiep theo game_state = game_state.apply_move(move) first_move_done = True feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d' label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d' chunk = 0 # Due to files with large content, split up after chunksize chunksize = 1024 while features.shape[0] >= chunksize: feature_file = feature_file_base % chunk label_file = label_file_base % chunk chunk += 1 current_features, features = features[:chunksize], features[ chunksize:] current_labels, labels = labels[:chunksize], labels[chunksize:] np.save(feature_file, current_features) np.save(label_file, current_labels)
def process_zip(self, zip_file_name, data_file_name, game_list): tar_file = self.unzip_data(zip_file_name) zip_file = tarfile.open(self.data_dir + '/' + tar_file) name_list = zip_file.getnames() #print(name_list) total_examples = self.num_total_examples(zip_file, game_list, name_list) #print(zip_file_name, game_list, total_examples) shape = self.encoder.shape() feature_shape = np.insert(shape, 0, np.asarray([total_examples])) features = np.zeros(feature_shape) labels = np.zeros((total_examples, )) counter = 0 for index in game_list: name = name_list[index + 1] if not name.endswith('.sgf'): raise ValueError(name + ' is not valid sgf') sgf_content = zip_file.extractfile(name).read() sgf = Sgf_game.from_string(sgf_content) #print(sgf) game_state, first_move_done = self.get_handicap(sgf) #print(game_state) for item in sgf.main_sequence_iter(): color, move_tuple = item.get_move() point = None if color is not None: if move_tuple is not None: row, col = move_tuple point = Point(row + 1, col + 1) move = Move.play(point) else: move = Move.pass_turn() if first_move_done and point is not None: features[counter] = self.encoder.encode(game_state) labels[counter] = self.encoder.encode_point(point) counter += 1 game_state = game_state.apply_move(move) first_move_done = True print('features and labels size is ', counter) feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d' label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d' chunk = 0 chunksize = 1024 zip_file.close() print(features.shape) while features.shape[0] >= chunksize: feature_file = feature_file_base % chunk label_file = label_file_base % chunk chunk += 1 current_features, features = features[:chunksize], features[ chunksize:] current_labels, labels = labels[:chunksize], labels[chunksize:] np.save(feature_file, current_features) np.save(label_file, current_labels)
def legal_moves(self): moves = [] for row in range(1, self.board.num_rows + 1): for col in range(1, self.board.num_cols + 1): move = Move.play(Point(row, col)) if self.is_valid_move(move): moves.append(move) # These two moves are always legal. moves.append(Move.pass_turn()) moves.append(Move.resign()) return moves
def process_zip(self, zip_file_name, data_file_name, game_list): tar_file = self.unzip_data(zip_file_name) zip_file = tarfile.open(self.data_dir + '/' + tar_file) name_list = zip_file.getnames() # Determine the total number of moves in all games in this zip file. total_examples = self.num_total_examples(zip_file, game_list, name_list) shape = self.encoder.shape() # Infer the shape of features and labels from the encoder we use feature_shape = np.insert(shape, 0, np.asarray([total_examples])) features = np.zeros(feature_shape) labels = np.zeros((total_examples,)) counter = 0 for index in game_list: name = name_list[index + 1] if not name.endswith('.sgf'): raise ValueError(name + ' is not a valid sgf') sgf_content = zip_file.extractfile(name).read() sgf = Sgf_game.from_string(sgf_content) # Read the SGF content as string, after extracting the zip file game_state, first_move_done = self.get_handicap(sgf) # Infer the initial game state by applying all handicap stones for item in sgf.main_sequence_iter(): # Iterate over all moves in the SGF file color, move_tuple = item.get_move() point = None if color is not None: if move_tuple is not None: # Read the coordinates of the stone to be played... row, col = move_tuple point = Point(row + 1, col + 1) move = Move.play(point) else: move = Move.pass_turn() # ... or pass, if there is none. if first_move_done and point is not None: features[counter] = self.encoder.encode(game_state) # We encode the current game state as features... labels[counter] = self.encoder.encode_point(point) # ... and the next move as label for the features counter += 1 game_state = game_state.apply_move(move) # Afterwards the move is applied to the board and we proceed with the next one first_move_done = True feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d' label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d' chunk = 0 # Due to files with large content, split up after chunksize chunksize = 1024 while features.shape[0] >= chunksize: # We process features and labels in chunks of size 1024. feature_file = feature_file_base % chunk label_file = label_file_base % chunk chunk += 1 current_features, features = features[:chunksize], features[chunksize:] current_labels, labels = labels[:chunksize], labels[chunksize:] # The current chunk is cut off from features and labels... np.save(feature_file, current_features) np.save(label_file, current_labels) # and then stored in a separate file.
def encode(self, game_state): board_tensor = np.zeros(self.shape()) base_plane = {game_state.next_player: 0, game_state.next_player.other: 3} for row in range(self.board_height): for col in range(self.board_width): p = Point(row=row + 1, col=col + 1) go_string = game_state.board.get_go_string(p) if go_string is None: if game_state.does_move_violate_ko(game_state.next_player, Move.play(p)): board_tensor[6][row][col] = 1 else: liberty_plane = min(3, go_string.num_liberties) - 1 liberty_plane += base_plane[go_string.color] board_tensor[liberty_plane][row][col] = 1 return board_tensor
def encode(self, game_state): board_tensor = np.zeros(self.shape()) if game_state.next_player == Player.black: board_tensor[8] = 1 else: board_tensor[9] = 1 for r in range(self.board_height): for c in range(self.board_width): p = Point(row=r + 1, col=c + 1) go_string = game_state.board.get_go_string(p) if go_string is None: if game_state.does_move_violate_ko(game_state.next_player, Move.play(p)): board_tensor[10][r][c] = 1 else: liberty_plane = min(4, go_string.num_liberties) - 1 if go_string.color == Player.white: liberty_plane += 4 board_tensor[liberty_plane][r][c] = 1 return board_tensor
def main(): board_size = 9 game: GameState = GameState.new_game(board_size) bot: Agent = RandomAgent() move: Move = None while not game.is_over(): print('\033c') # clear terminal [Linux and OS X] if move is not None: print_move(game.next_player, move) print_board(game.board) if game.next_player == gotypes.Player.black: print(compute_game_result(game)) human_move_str: str = input('-- ') try: point = point_from_coords(human_move_str.strip().upper()) move = Move.play(point) except (ValueError, Exception): move = Move.pass_turn() else: move = bot.select_move(game) game = game.apply_move(move)
from dlgo.goboard import GameState, Move from dlgo.gotypes import Point from dlgo.utils import print_board from dlgo.gosgf.sgf import Sgf_game sgf_content = "(;GM[1]FF[4]SZ[9];B[ee];W[ef];B[ff];W[df];B[fe];W[fc];B[ec];W[gd];B[fb])" sgf_game = Sgf_game.from_string(sgf_content) game_state = GameState.new_game(19) for item in sgf_game.main_sequence_iter(): color, move_tuple = item.get_move() if color is not None and move_tuple is not None: row, col = move_tuple point = Point(row + 1, col + 1) move = Move.play(point) game_state = game_state.apply_move(move) print_board(game_state.board)