import time


def game(engine1, engine2, print_move_times=False):
    """engine1 and engine2 are instances of EngineInterface.
    engine1 makes the first move.
    Return (result, engine1 time, engine2 time)
    result is 0 if the game is a draw, 1 if engine1 win and 2 if engine2 win.
    """
    total_time_engine_1 = 0
    total_time_engine_2 = 0

    engine1.new_game()
    engine2.new_game()
    while True:
        t0 = time.perf_counter()
        column_number = engine1.engine_move()
        t1 = time.perf_counter()
        total_time_engine_1 += t1 - t0
        if print_move_times:
            print(engine1.name + ": Move time:", t1 - t0, "s")
        engine1.make_move(column_number)
        engine2.make_move(column_number)
        if engine1.four_in_a_row():
            return (1, total_time_engine_1, total_time_engine_2)
        if engine1.draw():
            return (0, total_time_engine_1, total_time_engine_2)
        t0 = time.perf_counter()
        column_number = engine2.engine_move()
        t1 = time.perf_counter()
        total_time_engine_2 += t1 - t0
        if print_move_times:
            print(engine2.name + ": Move time:", t1 - t0, "s")
        engine1.make_move(column_number)
        engine2.make_move(column_number)
        if engine2.four_in_a_row():
            return (2, total_time_engine_1, total_time_engine_2)
        if engine2.draw():
            return (0, total_time_engine_1, total_time_engine_2)