Ejemplo n.º 1
0
def expand_leaf(node, state):
    """ Adds a new leaf to the tree by creating a new child node for the given node.

    Args:
        node:   The node for which a child will be added.
        state:  The state of the game.

    Returns:    The added child node.
    ######################
    Expansion Phase
        if unexplored child
            expand (random order reduces bias)
        if terminal state
            return
    
    """
    ### testing syntax since using dict
    if node.untried_actions:   # if the node has any untried actions 
    # expand node 
    # __init__(self, parent=None, parent_action=None, action_list=[]):
        move = choice(node.untried_actions)
        state.apply_move(move)
        new_node = MCTSNode(parent=node, parent_action=move, action_list=state.legal_moves)
        node.untried_actions.remove(move)
        new_node.parent = node
        new_node.parent_action = move
        new_node.action_list =state.legal_moves
        node.child_nodes[move] = new_node
        return new_node
    return None