Ejemplo n.º 1
0
def gtp(strategy, read_file=None):
    n = PolicyNetwork(use_cpu=True)
    if strategy == 'random':
        instance = RandomPlayer()
    elif strategy == 'policy':
        instance = PolicyNetworkBestMovePlayer(n, read_file)
    elif strategy == 'randompolicy':
        instance = PolicyNetworkRandomMovePlayer(n, read_file)
    elif strategy == 'mcts':
        instance = MCTS(n, read_file)
    else:
        sys.stderr.write("Unknown strategy")
        sys.exit()
    gtp_engine = gtp_lib.Engine(instance)
    sys.stderr.write("GTP engine ready\n")
    sys.stderr.flush()
    while not gtp_engine.disconnect:
        inpt = input()
        # handle either single lines at a time
        # or multiple commands separated by '\n'
        try:
            cmd_list = inpt.split("\n")
        except:
            cmd_list = [inpt]
        for cmd in cmd_list:
            engine_reply = gtp_engine.send(cmd)
            sys.stdout.write(engine_reply)
            sys.stdout.flush()
Ejemplo n.º 2
0
                            # Notify players
                            current_player.see_no_card_from_nobody()

                            print("Nobody showed a card")

                            # Other players observe that no card was shown
                            for other_player in self.players:
                                if other_player != current_player:
                                    other_player.observe_no_card_from_nobody(
                                        current_player, questioned_cards)

                            # Leave showing loop
                            break

            # Next player
            current_player_index = (current_player_index + 1) % len(
                self.players)


if __name__ == "__main__":
    game = Game("config/classic_game_de.json")

    # Generate 4 random players
    players = [
        RandomPlayer(figure) for figure in random.sample(game.figures, 4)
    ]
    game.set_players(players)

    winner, turn_count = game.play()
    print(f"\nGame over. {winner} wins after {turn_count} turns.")
def AI(msgs, model=DEFAULT_MODEL_PATH, strategy=None):
    print("AI(msg) called,strategy:", strategy)

    # data_file = data_file_path + msg
    lastMsg = msgs[len(msgs) - 1]
    x, y, color = parse_input_msg(lastMsg)
    print('AI(lastMsg) parsed:', x, y, color)

    # Initialize the policy network
    n = PolicyNetwork(use_cpu=True)
    print("PolicyNetwork init.")
    # global read_file
    # read_file = read_file_prefix+str(RANK)+"/savedmodel"
    print("n,read_file:", n, model)

    if strategy == 'random':
        global instance
        instance = RandomPlayer()
    elif strategy == 'best_move':
        global instance
        instance = PolicyNetworkBestMovePlayer(n, model)
    elif strategy == 'random_move':
        global instance
        instance = PolicyNetworkRandomMovePlayer(n, model)
    elif strategy == 'mcts':
        global instance
        instance = MCTS(n, model)
    #instance = PolicyNetworkRandomMovePlayer(n, read_file)
    print("PolicyNetwork instanced.", instance)
    try:
        global gtp_engine
        gtp_engine = gtp_lib.Engine(instance)
    except Exception:
        print(traceback.format_exc())
    print("GTP Engine get ready.")
    #sys.stderr.write("GTP Enginene ready\n")
    AI_cmd = parse_AI_instruction(color)
    print("AI_cmd parsed.")
    # To see if it has started playing chess and logging
    # try:
    #     data_file_exist = os.path.exists(data_file)
    # except Exception:
    #     print(traceback.format_exc())
    # print("os.path.exists?",data_file_exist)
    #sys.setdefaultencoding('utf-8')
    # if os.path.exists(data_file):
    #     print("os.path.exists(data_file)!")
    #     rfile = open(data_file, 'r')
    #     cmd_list = rfile.readlines()
    #     for cmd in cmd_list:
    #         cmd = cmd.strip('\n ')
    #         if cmd == '':
    #             continue
    #         print("gtp_engine.send(cmd):", cmd)
    #         gtp_engine.send(cmd)
    #     # sys.stdout.write(cmd + '\n')
    #     # sys.stdout.flush()
    #     rfile.close()
    # # Parse the other side of the chess instructions, write into the record file
    # wfile = open(data_file, 'a')
    # print("wfiled!!!")
    # if msg['msg'][2].lower() == 't' and msg['msg'][3].lower() == 't':
    #     pass
    # else:
    #     player_cmd = parse_player_input(msg['msg'][0], x, y)
    #     wfile.write(player_cmd + '\n')
    #     gtp_engine.send(player_cmd)
    # sys.stdout.write(player_cmd + '\n')
    # sys.stdout.flush()
    for msg in msgs:
        x, y, color = parse_input_msg(msg)
        player_cmd = parse_player_input(color, x, y)
        print("gtp_engine.send(cmd):", player_cmd)
        gtp_engine.send(player_cmd)

    gtp_reply = gtp_engine.send(AI_cmd)
    gtp_cmd = parse_AI_input(color, gtp_reply)
    # wfile.write(gtp_cmd)
    # wfile.close()
    # sys.stdout.write(gtp_reply + '\n')
    # sys.stdout.flush()

    AI_x, AI_y = parse_AI_reply(gtp_reply)

    response = color + '[' + AI_x + AI_y + ']'
    # sys.stdout.write(response)
    # sys.stdout.flush()

    return {'game_id': msg['game_id'], 'msg': response}
Ejemplo n.º 4
0
                        # One complete round, nobody showed a card
                        else:
                            # Notify players
                            current_player.see_no_card_from_nobody()

                            print("Nobody showed a card")

                            # Other players observe that no card was shown
                            for other_player in self.players:
                                if other_player != current_player:
                                    other_player.observe_no_card_from_nobody(current_player, questioned_cards)

                            # Leave showing loop
                            break

            # Next player
            current_player_index = (current_player_index + 1) % len(self.players)


if __name__ == "__main__":
    game = Game("config/classic_game_de.json")

    # Generate 4 random players
    current_figures = random.sample(game.figures, 3)
    players = [RandomPlayer(current_figures[0]), CheatingRandomPlayer(current_figures[1]), ManualRulePlayer(current_figures[2])]
    game.set_players(players)

    winner, turn_count = game.play()
    print(f"\nGame over. {winner} wins after {turn_count} turns.")