def main(): parser = argparse.ArgumentParser() parser.add_argument( "--player_type", type=int, default=1, help="palyer type you want to fight,1 is human,2 is computer") parser.add_argument("--computer_type", type=int, default=0, help="computer type,1 is Alpha MCTS,2 is pure MCTS") args = parser.parse_args() game = Quoridor() human1 = ManualPygameAgent('Kurumi') human2 = ManualPygameAgent('Cryer') MCTS_Alpha = A_Player(PolicyValueNet().policy_value_fn, c_puct=5, n_playout=30, is_selfplay=0) MCTS_Pure = B_Player(c_puct=5, n_playout=50) # 50层400秒 if args.player_type == 1: player_types = {1: 'human', 2: 'human'} players = {1: human1, 2: human2} if args.computer_type == 0: pass elif args.player_type == 2: player_types = {1: 'human', 2: 'computer'} if args.computer_type == 1: players = {1: human1, 2: MCTS_Alpha} elif args.computer_type == 2: players = {1: human1, 2: MCTS_Pure} elif args.computer_type == 0: print("Set computer type to 1 or 2 for choosing computer!") # pygame.quit() # game.load(player1, player2) pygame.init() WINDOW_SIZE = [SCREEN_WIDTH, SCREEN_HEIGHT] screen = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("QUORIDOR") clock = pygame.time.Clock() # valid_actions = game.valid_actions 11 valid_actions = game.actions() done = False winner = None t1 = time.time() while not done: player_moved = False # 定义落子历史 # move_history = [] pawn_moves, walls = draw_game(game, screen, valid_actions) # text(screen, "player1 move:", position1=2, position2=0.8, color=BLUE) valid_walls = [wall for wall in walls if wall[2] in valid_actions] if player_types[game.current_player] == 'human': touch = pygame.mouse.get_pos() for wall, collides, _ in valid_walls: for collide in collides: if collide.collidepoint(touch): pygame.draw.rect(screen, LIGHTBROWN, wall) break for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: touch = pygame.mouse.get_pos() # This is messy - fix later for rect, action in pawn_moves: if rect.collidepoint(touch): players[game.current_player].receive_action(action) player_moved = True break if player_moved: break # if player_moved: break # 添加 if player_moved: real_action = players[ game.current_player].choose_action() # move_history.append(real_action) done, winner = game.step(real_action) render(game, screen) # 渲染游戏 break for rect, collide_points, action in valid_walls: for collides in collide_points: if collides.collidepoint(touch): players[game.current_player].receive_action( action) player_moved = True break # 修改 if player_moved == True: real_action = players[ game.current_player].choose_action() # move_history.append(real_action) done, winner = game.step(real_action) render(game, screen) # 渲染游戏 break clock.tick(30) pygame.display.flip() valid_actions = game.actions() # 待改 if player_types[game.current_player] == 'computer': print("computer %s thinking..." % str(game.current_player)) tic = time.time() # real_action = np.random.choice(valid_actions) real_action = players[game.current_player].choose_action(game) # move_history.append(real_action) toc = time.time() print("MCTS choose action:", real_action, " ,spend %s seconds" % str(toc - tic)) done, winner = game.step(real_action) # render(game, screen) # valid_actions = game.valid_actions # if game.current_player == 1: # text(screen, text, position1=2, position2=0.8, color=BLUE) if done: print("game over! winner is %s player:%s" % (player_types[winner], winner)) break t2 = time.time() print("total time :", t2 - t1) pygame.quit()
def main(): logger.info("Loading Game Environment") game = Quoridor() player1 = ManualPygameAgent('Matt') player2 = ManualPygameAgent('Kelsy') player_types = {1: 'human', 2: 'human'} players = {1: player1, 2: player2} game.load(player1, player2) logger.info("Initializing Visuals") pygame.init() WINDOW_SIZE = [SCREEN_WIDTH, SCREEN_HEIGHT] screen = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("QUORIDOR") clock = pygame.time.Clock() valid_actions = game.valid_actions done = False while not done: player_moved = False pawn_moves, walls = draw_game(game, screen, valid_actions) valid_walls = [wall for wall in walls if wall[2] in valid_actions] if player_types[game.current_player] == 'human': touch = pygame.mouse.get_pos() for wall, collides, _ in valid_walls: for collide in collides: if collide.collidepoint(touch): pygame.draw.rect(screen, LIGHTBROWN, wall) break for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN: touch = pygame.mouse.get_pos() # This is messy - fix later for rect, action in pawn_moves: if rect.collidepoint(touch): players[game.current_player].receive_action(action) player_moved = True break if player_moved: break if player_moved: break for rect, collide_points, action in valid_walls: for collides in collide_points: if collides.collidepoint(touch): players[game.current_player].receive_action( action) player_moved = True break if player_moved == True: break clock.tick(30) pygame.display.flip() if player_moved or player_types[game.current_player] == 'computer': winner = game.step() valid_actions = game.valid_actions if winner: logger.info(f"Winner is {winner.name}") pygame.quit()