import random


def TestModels(model1, model2, temp, numTests):
    """ Base function for playing a BlackBird instance against another model.

        Args:
            `model1`: The Blackbird model to test.
            `model2`: The model to play against.
            `temp`: A float between 0 and 1 determining the exploitation
                temp for MCTS. Usually this should be close to 0.1 to ensure
                optimal move selection.
            `numTests`: An int determining the number of games to play.

        Returns:
            An integer representing a win (1), draw (0), or loss (-1)
    """
    for _ in range(numTests):
        model1ToMove = random.choice([True, False])
        model1Player = 1 if model1ToMove else 2
        winner = None
        model1.DropRoot()
        model2.DropRoot()
        state = model1.Game()

        while winner is None:
            if model1ToMove:
                (nextState, *_) = model1.FindMove(state, temp)
            else:
                (nextState, *_) = model2.FindMove(state, temp)
            state = nextState
            model1.MoveRoot(state)
            model2.MoveRoot(state)

            model1ToMove = not model1ToMove
            winner = state.Winner()

        if winner == model1Player:
            return 1
        elif winner == 0:
            return 0
        else:
            return -1