def READY_configuration(game): # Game Configuration response = client.handle_ready() if not response[0]: # server END message return False, response[1] parameters = response[1] ''' Start Game configuration ''' # Initial State for y,row in enumerate(parameters['initialState']['board']): for x,color in enumerate(row): if color == '.': continue game.next_player = gotypes.Player.black if color == 'B' else gotypes.Player.white move = goboard.Move(gotypes.Point((y+1,x+1))) game,_ = game.apply_move(move) captures = { gotypes.Player.black : parameters['initialState']['players']['B']['prisoners'], gotypes.Player.white : parameters['initialState']['players']['W']['prisoners'], } remainingTime = { gotypes.Player.black : parameters['initialState']['players']['B']['remainingTime'], gotypes.Player.white : parameters['initialState']['players']['W']['remainingTime'], } game.next_player = gotypes.Player.black if parameters['initialState']['turn'] == 'B' else gotypes.Player.white # fill the board with move logs for logEntry in parameters['moveLog']: move = logEntry["move"] deltaTime = logEntry['deltaTime'] if move['type'] == 'pass': move = goboard.Move(is_pass=True) elif move['type'] == 'resign': move = goboard.Move(is_resign=True) else: col = move['point']['column'] + 1 row = move['point']['row'] + 1 move = goboard.Move(gotypes.Point(row,col)) remainingTime[game.next_player] -= deltaTime game,numberOfCaptures = game.apply_move(move) if game.next_player == gotypes.Player.white: captures[gotypes.Player.black] += numberOfCaptures else: captures[gotypes.Player.white] += numberOfCaptures ourColor = gotypes.Player.black if parameters['ourColor'] == 'B' else gotypes.Player.white ''' End Game configuration ''' return game, captures, remainingTime, ourColor
def print_board_ext(board_ext): for row in range(board_ext.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board_ext.num_cols + 1): stone, cost = board_ext.get(gotypes.Point(row=row, col=col)) color_stone = ' ' if stone is not None: cost = round(cost, 2) color_stone = STONE_TO_CHAR[stone] if color_stone == ' B ': out_line = str(cost) if cost == 1: outline = '+1.0 ' elif color_stone == ' W ': out_line = str(-cost) if cost == 1: outline = '-1.0' else: out_line = '0.000' #out_line = STONE_TO_CHAR[stone]+'/'+cost line.append(out_line) #line.append(STONE_TO_CHAR[stone] ) print('%s%d %s' % (bump, row, ' '.join(line))) print(' ' + ' '.join(COLS[:board_ext.num_cols]))
def print_board(board): for row in range(1, board.num_rows + 1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone])
def print_board(board): for row in range(board.num_rows, 0, -1): line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) print('%d %s' % (row, ''.join(line))) print(' ' + COLS[:board.num_cols])
def print_board(board): for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) print(f"{bump}{row} {''.join(line)}") print(' ' + ' '.join(COLS[:board.num_cols]))
def print_board(board): for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) print("%s%d %s" % (bump, row, "".join(line))) print(" " + " ".join(COLS[:board.num_cols]))
def board_to_json(board): rows = [] for row in range(1, board.num_rows + 1): cols = [] for col in range(1, board.num_rows + 1): stone = board.get(gotypes.Point(row=row, col=col)) cols.append(STONE_TO_CHAR[stone].strip()) rows.append(cols) return rows
def print_board(board): """ 盤面全体の状況を表示 """ for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" # 二桁の行番号になったときのための空白 line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) # 全ての点の打石状況を確認 line.append(STONE_TO_CHAR[stone]) print('%s%d %s' % (bump, row, ''.join(line))) # 空白,行番号,行の石 print(' ' + ''.join(COLS[:board.num_cols])) # 列記号,ABCD...
def print_board(board): lines = [] for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) lines.append('%s%d %s' % (bump, row, ''.join(line))) lines.append(' ' + ' '.join(COLS[:board.num_cols])) return '```\n' + '\n'.join(lines) + '\n```'
def print_board_file(board, file): for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) file.write('%s%d %s' % (bump, row, ''.join(line))) file.write('\n') file.write(' ' + ' '.join(COLS[:board.num_cols])) file.write('\n')
def print_board(board): for row in range(board.num_rows, 0, -1): # For aligning cols with numbers with one and two digits bump = ' ' if row <= 9 else "" line = [] for col in range(1, board.num_cols + 1): # Get the stone for the specific point stone = board.get(gotypes.Point(row=row, col=col)) # Add up stones in the line line.append(STONE_TO_CHAR[stone]) print('%s%d %s' % (bump, row, ''.join(line))) print(' ' + ' '.join(COLS[:board.num_cols]))
def print_board(board, last_move=None): for row in range(board.num_rows, 0, -1): bump = ' ' if row <= 9 else '' # to make sure all numbers are right justified (other ways to do this) line = [] for col in range(1, board.num_cols + 1): current_point = gotypes.Point(row=row, col=col) stone = board.get(current_point) stone_color = STONE_TO_COLOR[stone] if last_move is not None and last_move.is_play and current_point == last_move.point: stone_color = STONE_TO_COLOR['last_move'] line.append(colored(STONE_TO_CHAR[stone], stone_color)) print('%s%d %s' % (bump, row, ''.join(line))) print(' ' + ' '.join(COLS[:board.num_cols]))
def print_board(board): for row in range(board.num_rows, 0, -1): bump = " " if row <= 9 else "" line = [] for col in range(1, board.num_cols +1): stone = board.get(gotypes.Point(row=row, col=col)) line.append(STONE_TO_CHAR[stone]) print('%s%d %s' % (bump, row, '---'.join(line))) if row > 1: print(' ' + ('| ' * (board.num_cols-1) + '|')) print(' ' +' '.join(COLS[:board.num_cols]))
def send_board_to_gui(decision,board): # print("send_board_to_gui") stone_list = [] if decision == '0': for i in range(1,20): for j in range(1,20): stone = gotypes.Point(row= i,col= j) color = board.get(stone) if( color is not None): c='0' if color == gotypes.Player.white : c ='1' stone_list.append([j,i,c]) interface.update_board(stone_list) return
def capture_diff(game_state): black_stones = 0 white_stones = 0 for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): p = gotypes.Point(r, c) color = game_state.board.get(p) if color == gotypes.Player.black: black_stones += 1 elif color == gotypes.Player.white: white_stones += 1 diff = black_stones - white_stones # <1> if game_state.next_player == gotypes.Player.black: # <2> return diff # <2> return -1 * diff # <3>
def get_game_mode_from_gui(): global game_mode game_mode = int(interface.get_game_mode()) # # print("game mode received --> ", game_mode) game = goboard.GameState.new_game(board_size) player = '0' opponent = '1' captures = { '0': 0, '1': 0, } if (game_mode == 1): # PC mode #get player color from server # # print('ai vs ai') pass elif (game_mode == 0): # # print('ai vs human') player, opponent = get_player_color_from_gui() initial_state = interface.get_initial_board() # print("initial Board len",len(initial_state)) if (len(initial_state) == 0): pass else: # for move_ in initial_state: # if move_[i][2] == '.': # continue # game.next_player = gotypes.Player.black if move_[i][2] == '0' else gotypes.Player.white # move = goboard.Move(gotypes.Point((int(move_[i][1]),int(move_[i][0])))) # game,captures = game.apply_move(move) for i in range(0, len(initial_state)): point = gotypes.Point(row=int(initial_state[i][1]), col=int(initial_state[i][0])) move = goboard.Move(point) # print(">>>> move", move.point) next_player = gotypes.Player.black prisoners = [0] turn = '0' if (initial_state[i][2] == '1'): next_player = gotypes.Player.white turn = '1' game.next_player = next_player game, _ = game.apply_move(move) if (prisoners[0] > 0): captures[turn] += prisoners[0] send_board_to_gui(0, game.board) # print(opponent) return game, captures, player, opponent
def IDLE(game, captures): response = client.handle_idle() if not response[0]: # server END msg return False, response[1] if response[1]['type'] == 'place': point = gotypes.Point(response[1]['row'] + 1, response[1]['column'] + 1) move = goboard.Move(point) elif response[1]['type'] == 'pass': move = goboard.Move(is_pass = True) elif response[1]['type'] == 'resign': move = goboard.Move(is_resign = True) game, prisoners = game.apply_move(move) captures[game.next_player.other] += prisoners return game, captures, response[1]['remaning_time'], move
def capture_diff(game_state): black_stones = 0 white_stones = 0 for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): p = gotypes.Point(r, c) color = game_state.board.get(p) if color == gotypes.Player.black: black_stones += 1 elif color == gotypes.Player.white: white_stones += 1 # Calculate the difference between the number of black stones and white # stones on the board. This will be the same as the difference in the # number of captures, unless one player passes early diff = black_stones - white_stones # If it’s black’s move, return (black stones) – (white stones) if game_state.next_player == gotypes.Player.black: return diff # If it’s white’s move, return (white stones) – (black stones) return -1 * diff
def capture_diff(game_state): black_stones = 0 white_stones = 0 for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): p = gotypes.Point(r, c) color = game_state.board.get(p) if color == gotypes.Player.black: black_stones += 1 elif color == gotypes.Player.white: white_stones += 1 # 盤上の黒石と白石の数の差を計算する # プレイヤーがあ早々にパスしない限り、これはとった石の数の差と等しい diff = black_stones - white_stones # <1> # 黒の手番の場合、(黒石 - 白石)を返す if game_state.next_player == gotypes.Player.black: # <2> return diff # <2> # 白の手番の場合、(白石 - 黒石)を返す return -1 * diff # <3>
def get_opponent_game_from_gui(current_state,captures,opponent): # # print("get_opponent_game_from_gui") global consequitive_passes global opponont_resigns new_game_state= current_state point = 0 decision = interface.get_opponent_move().split('#') if decision[0] == '0' : # play consequitive_passes = 0 # print("decision ", decision) pos = decision[1].split('-') point = gotypes.Point(row= int(pos[1]),col= int(pos[0])) move = goboard.Move(point) # # print(">>>> move", move.point) new_game_state , prisoners = current_state.apply_move(move) captures[opponent]+=prisoners elif decision[0] == '1' : # opponont_opponont_resignss opponont_resigns = True elif decision[0] == '2' : # pass consequitive_passes+=1 return decision[0] , new_game_state , captures , point
def recommend_move(game_state): state = elevenplanes.ElevenPlaneEncoder((19,19)) state = state.encode(game_state) state = np.expand_dims(state,axis=0) probability_matrix=predict.model.predict(state)[0] probability_matrix = np.reshape(probability_matrix, (-1, 19)) new_point = -1 for j in range(361): max = probability_matrix.max() coordinates = np.where(probability_matrix == max) row = coordinates[0][0] col = coordinates[1][0] probability_matrix[row][col]= 0 new_point = gotypes.Point( row=row+1,col=col+1) move = goboard.Move(new_point) if game_state.is_valid_move(move): break if(j == 361): return False , game_state , new_point new_game_state , prisoners = game_state.apply_move(move) # print('recommend move function',new_point) return True , new_game_state , new_point
def capture_diff(game_state): """Calculate the difference between the number of black stones and white stones on the board. This will be the same as the difference in the number of captures, unless one player passes early. Returns the difference from the perspective of the next player to play. If it's black's move, we return (black stones) - (white stones). If it's white's move, we return (white stones) - (black stones). """ black_stones = 0 white_stones = 0 for r in range(1, game_state.board.num_rows + 1): for c in range(1, game_state.board.num_cols + 1): p = gotypes.Point(r, c) color = game_state.board.get(p) if color == gotypes.Player.black: black_stones += 1 elif color == gotypes.Player.white: white_stones += 1 diff = black_stones - white_stones if game_state.next_player == gotypes.Player.black: return diff return -1 * diff
def get_best_three(root, available_moves): state = elevenplanes.ElevenPlaneEncoder((19, 19)) state = state.encode(root.game_state) state = np.expand_dims(state, axis=0) probability_matrix = predict.model.predict(state)[0] probability_matrix = np.reshape(probability_matrix, (-1, 19)) num_moves = 3 if (available_moves == 0): return False if (available_moves < num_moves): num_moves = available_moves for i in range(num_moves): for k in range(361): max = probability_matrix.max() coordinates = np.where(probability_matrix == max) row = coordinates[0][0] col = coordinates[1][0] probability_matrix[row][col] = 0 new_point = gotypes.Point(row=row + 1, col=col + 1) move = goboard.Move(new_point) ## print(new_point) if root.game_state.is_valid_move(move): break if (k == 361): return False #print('move ',move) legal_state, prisoners = root.game_state.apply_move(move) capture = 0 ## print('prisoners',prisoners) child_captures = copy.copy(root.captures) child_captures[player] += prisoners child = MCTS_node(legal_state, root, child_captures, new_point) root.children.append(child) # print(probability_matrix) return True
def rollout(node, depth): game_state = node.game_state parent = node j = 0 global encoding_time global move_time global cnn_time global prisoners_time prisoners = 0 while not game_state.is_over() and j < depth: j += 1 t1 = time.time() state = elevenplanes.ElevenPlaneEncoder((19, 19)) state = state.encode(game_state) t2 = time.time() state = np.expand_dims(state, axis=0) probability_matrix = predict.model.predict(state)[0] probability_matrix = np.reshape(probability_matrix, (-1, 19)) t3 = time.time() new_point = 0 for j in range(361): max = probability_matrix.max() coordinates = np.where(probability_matrix == max) row = coordinates[0][0] col = coordinates[1][0] probability_matrix[row][col] = 0 new_point = gotypes.Point(row=row + 1, col=col + 1) move = goboard.Move(new_point) if game_state.is_valid_move(move): break if (j == 361): break new_game_state, prisoners = game_state.apply_move(move) t4 = time.time() capture = 0 child_captures = copy.copy(parent.captures) child_captures[player] += prisoners t5 = time.time() new_node = MCTS_node(new_game_state, parent, child_captures, new_point) # check if game is over game_state = new_game_state parent = new_node encoding_time += (t2 - t1) cnn_time += (t3 - t2) move_time += (t4 - t3) prisoners_time += (t5 - t4) # evaluate game state last_captures = copy.copy(parent.captures) # # print(last_captures) game_captures = { gotypes.Player.black: last_captures['0'], gotypes.Player.white: last_captures['1'] } winner, _ = game_state.semi_winner(game_captures) result = '0' if (winner == gotypes.Player.white): result = '1' return result, parent
def point_from_coords(coords): col = COLS.index(coords[0]) + 1 row = int(coords[1:]) return gotypes.Point(row=row, col=col)
def point_from_coords(coords): """ 人間の入力をBoardの座標に変換 ex. C3 -> (3, 3) """ col = COLS.index(coords[0]) + 1 row = int(coords[1:]) return gotypes.Point(row=row, col=col)
def point_from_coords(coords): """This function allows for human_v_bot""" col = COLS.index(coords[0]) + 1 row = int(coords[1:]) return gotypes.Point(row=row, col=col)