Exemplo n.º 1
0
def run_test(players, num_sim, save_to_file):
    report_df = pd.DataFrame(
        columns=['Winner', 'Cum_Wins', 'Cum_Wins_S', 'Runtime'],
        index=[*range(num_sim)])

    wins_p1 = 0
    wins_p2 = 0
    draws = 0

    for i in range(num_sim):
        start = time.time()

        board = Board.build_board()
        winner = play_game(players=players, board=board)

        runtime = time.time() - start

        if winner == players[0].name:
            wins_p1 += 1
            cum_wins = wins_p1
        if winner == players[1].name:
            wins_p2 += 1
            cum_wins = wins_p2
        if winner == 'DRAW':
            draws += 1
            cum_wins = draws

        cum_wins_p = cum_wins / (i + 1)

        report_df.iloc[i] = [winner, cum_wins, cum_wins_p, runtime]

    report_string = reporter(players, num_sim, report_df)

    timestamp = datetime.datetime.strftime(datetime.datetime.now(),
                                           format='%Y-%d-%m_%H:%M:%S')

    if save_to_file:
        text_file = open("report_{}.txt".format(timestamp), "w")
        text_file.write(report_string)
        text_file.close()

    print(report_string)
Exemplo n.º 2
0
        if should_print:
            Board.print_right_way(board)
            print("============\n")

        turn ^= 1

    if Board.so_won(board, players[turn ^ 1].no):
        print("VICTORY FOR " + players[turn ^ 1].name)
        return players[turn ^ 1].name
    else:
        print("DRAW")
        return 'DRAW'


if __name__ == '__main__':
    board = Board.build_board()

    H1 = Human(board, no=1, name='Leo')
    H2 = Human(board, no=2, name='Niko')

    AB1 = AlphaBeta(board, no=1, name='Alphabeta1', depth=1)
    AB2 = AlphaBeta(board, no=2, name='Alphabeta2', depth=3)

    M1 = MCTS(board, no=1, name='MCTS1')
    M2 = MCTS(board, no=2, name='MCTS2')

    players = [M1, M2]

    play_game(players, board)