def playgame(n,maxtime,maxnodes,defaultmaxtime=1.0, defaultmaxnodes=1250): # plays an entire game and returns results g = Game(n) nextpiece = g.GetRandomPiece() # does not matter which piece starts player = random.randint(0,1) # even = variable AI, odd = control AI # stats sp = None if(player % 2 == 0): sp = "v" else: sp = "c" variableelapsed = 0 controlelapsed = 0 variablenodes = 0 controlnodes = 0 while (not g.CheckGameEnd()): if(player % 2 == 0): # variable AI # pass piece & game to root root = Node(game = g, piece = nextpiece) # pass root & constraints to MCTS choice = monte_carlo_tree_search(maxtime,maxnodes,root) piece = choice[3].piece space = choice[1] x = g.AddPieceToBoard(nextpiece,space[0],space[1]) nextpiece = choice[0] #stats variableelapsed += choice[4] variablenodes += choice[5] else: # control AI # pass piece & game to root root = Node(game = g, piece = nextpiece) # pass root & constraints to MCTS choice = monte_carlo_tree_search(defaultmaxtime,defaultmaxnodes,root) piece = choice[3].piece space = choice[1] x = g.AddPieceToBoard(nextpiece,space[0],space[1]) nextpiece = choice[0] #stats controlelapsed += choice[4] controlnodes += choice[5] player += 1 winner = None if(player % 2 == 0): # last player to act = winner winner = "v" else: winner = "c" dict = {"board size":n,"variable max nodes":maxnodes,"variable max time":'%.3f'%(maxtime), "start player":sp,"winner":winner, "control time":'%.3f'%(controlelapsed),"variable time":'%.3f'%(variableelapsed), "control nodes":controlnodes,"variable nodes":variablenodes} return dict
def THINKING(game, captures): global agent global encoder played = False remaining_time = None captures = { '0': captures[gotypes.Player.black], '1': captures[gotypes.Player.white] } moves_count = 0 while not played: point = -1 player = '0' if game.next_player == gotypes.Player.black else '1' if moves_count == 0 or True: move_result, new_game, new_captures, play_point = monte_carlo_tree_search( agent, encoder, game, point, player, num_rounds, captures, depth, len(game.legal_moves()) - 2) # print(new_captures , play_point) else: # another option pass if (move_result): decision = 0 else: decision = 1 b_time = 0 w_time = 0 moves_count += 1 play_move = goboard.Move(play_point) client.handle_thinking(play_move) response = client.handle_await_response() if not response[0]: # server END msg return False, response[1] if response[1]['valid']: played = True game = new_game captures = { gotypes.Player.black: new_captures['0'], gotypes.Player.white: new_captures['1'] } remaining_time = response[1]['remaning_time'] if not response[1]['valid']: reason = response[1]['message'] # print('not valid: ', reason) remaining_time = response[1]['remaning_time'] # send_move_to_gui(decision,play_point,b_time,w_time,player) # send_board_to_gui(decision,game.board) return game, captures, remaining_time, play_move
def action(board, pre_pos): move_pos = monte_carlo_tree_search(board, pre_pos) board = board.move(move_pos) # 新的棋盘 return board, move_pos
def main(): global consequitive_passes, opponont_resigns, game_mode if init_gui: game, captures, player, opponent = get_game_mode_from_gui() first_game = False if( opponent == "1"): first_game = True # print('first',first_game) else: game_mode = 1 if(game_mode == 0 ): while ( not game.is_over()): # print_board(game.board) start = time.time() point = -1 if(not first_game): if(len(game.legal_moves()) == 2): break send_valid_moves_to_gui(game) old_game = copy.deepcopy(game) recommend_result , recommended , recommended_move = recommend_move(old_game) old_captures = copy.copy(captures[opponent]) decision , game , captures , point = get_opponent_game_from_gui(game,captures,opponent) if(recommend_result): result = compare_state(recommended,game,captures,player) # print("Recommended Move is : ",recommended_move) if(result == "gt"): # print("Send Recommended move condition in main") send_recommended_move('0',recommended_move) pass else: send_congrats() # print("captures[opponent] old_captures : ", captures[opponent], old_captures) if( captures[opponent] > old_captures): # print('opponent captures') send_board_to_gui(decision,game.board) b_time = 0 w_time = 0 if(consequitive_passes == 2 or opponont_resigns == True ): break if(len(game.legal_moves()) == 2): break old_captures = copy.copy(captures[player]) move_result , game , captures , play_point = monte_carlo_tree_search( game,point,player,num_rounds,captures,depth,len(game.legal_moves())-2) print('after monto carlo') if(move_result): decision = '0' else: decision = '1' b_time = '0' w_time = '0' send_move_to_gui(decision,play_point,b_time,w_time,player) if(captures[player] > old_captures): # print('player captures') send_board_to_gui(decision,game.board) first_game = False if(consequitive_passes == 2): break end = time.time() # # print(end - start) game_captures ={ gotypes.Player.black : captures['0'], gotypes.Player.white : captures['1'] } #game_result,winner,score = game.winner(game_captures) game_result,score = game.winner(game_captures) reason = 'IDK!' send_score_to_gui(game_result,player,reason) # if winner == '0': # # print("Black is the WINNER!!!!") # else: # # print("White is the WINNER!!!!") # # print('Black:', score[gotypes.Player.black] ,'\tWhite:', score[gotypes.Player.white]) elif(game_mode == 1 ): # AI vs AI AI_vs_AI()