import math


def UCB1(v, N, n):
    """
        Parameters:
                    v: int -> value of the terminal_state
                    N: int -> number of times the parent node was visited
                    n: int -> number of times the node was visited
        returns:
                int for MCTS selection
    """
    try:
        # UCB1 formula
        return v/n+2*math.sqrt(2*math.log(N)/n)
    except ZeroDivisionError:
        # if n is zero return infinity -> this node needs to be expanded
        return float("inf")
    except ValueError:
        # if n is not set return infinity -> this node needs to be expanded
        return float("inf")