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. """ try: action = node.untried_actions.pop() except IndexError: return node new_node = MCTSNode(parent=node, parent_action=action) new_node.state = fun_board.next_state(node.state, action) new_node.untried_actions = fun_board.legal_actions(new_node.state) node.child_nodes[action] = new_node return new_node