Example #1
0
def MCTS_Move(board, combinations, values, playerCombinations, playerValues,
              curPlayer):
    #Define the starting node.  Set it's parent to itself so that we can verify
    #later on whether or not the node we are currently on is the head
    #(if node == node.parent) this is the head
    head = c.Node(c.CoordinatePair(-1, -1), 5, 0, 0)
    head.parent = head

    #Variables for the simulation
    simBoard = [[0 for i in range(g.width)] for j in range(g.width)]
    simValues = [[0 for i in range(g.width)] for j in range(g.width)]
    simPlayerValues = [[0 for i in range(g.width)] for j in range(g.width)]
    simCombinations = []
    simPlayerCombinations = []
    simCurPlayer = c.Boolean(curPlayer)

    #Create a list of all untaken spaces
    untaken = []
    for i in range(g.width):
        for j in range(g.width):
            if (board[i][j] == 0):
                untaken.append(c.CoordinatePair(i, j))

    #Run simulations according to the number of simulations required
    for i in range(g.numberOfSimulations):
        print(f"Simulation #{i}!")
        #Copy simulated values
        func.copy2DList(board, simBoard)
        func.copy2DValue(values, simValues)
        func.copy2DValue(playerValues, simPlayerValues)
        simCombinations = []
        simPlayerCombinations = []
        func.copyCombination(combinations, simCombinations)
        func.copyCombination(playerCombinations, simPlayerCombinations)
        simCurPlayer.value = curPlayer
        simUntaken = trial.getBestMovesInAnArrayFast(simBoard, simValues,
                                                     simPlayerValues,
                                                     simCombinations,
                                                     simPlayerCombinations,
                                                     simCurPlayer.value)
        #Selection and Expansion of Node Tree
        start = SelectAndExpand(head, simUntaken, simBoard, simValues,
                                simPlayerValues, simCombinations,
                                simPlayerCombinations, simCurPlayer)
        #Simulate Game is Simulation
        #downPropegate after calling upPropegate
        upPropegate(
            start,
            simulateGame(simBoard, simCombinations, simPlayerCombinations,
                         simValues, simPlayerValues, start.coordinates,
                         simUntaken, simCurPlayer))
        downPropegate(head)

    #Prints out final values.  Commend out if you don't want to see them
    #for i in range(len(head.children)):
    #print(f"Child ({head.children[i].coordinates.y}, {head.children[i].coordinates.x}) was visited {head.children[i].visited} times, had a score of {head.children[i].score} and had a uct of {head.children[i].uct}")

    #Get the highest scoring node and return it back to main
    highest = 0
    for i in range(len(head.children)):
        if (head.children[i].visited > head.children[highest].visited):
            highest = i

    return head.children[highest].coordinates