/// <summary> /// Expands a selected node, if possible. /// </summary> /// <param name="id">The selected node.</param> /// <param name="tree">The game tree object.</param> /// <returns></returns> private static int Expand(int id, GameTree tree) { // Get the node object that corresponds with the input ID. GameTreeNode node = tree.GetNode(id); // If the node has at least one unplayed child, is not a dead end, and is not a goal state. if (node.Unplayed.Count > 0 && !node.DeadEnd && !node.IsGoal) { // Choose an unplayed child at random. GameTreeEdge outgoing = node.Unplayed.PickRandom <GameTreeEdge>(); // Remove the child from the list of unplayed edges. node.Unplayed.Remove(outgoing); // Create the node object and store the child's ID. int child = tree.CreateNode(node.Domain, node.Problem, outgoing).ID; // Save the parent node to disk. tree.SetNode(node); // Return the child ID. return(child); } // Return the leaf node. return(node.ID); }
/// <summary> /// Propagates a roll out's result back through the tree. /// </summary> /// <param name="result">Whether the roll out resulted in a win or loss.</param> /// <param name="node">The current node.</param> /// <param name="tree">The game tree object.</param> private static void Propagate(bool result, int node, GameTree tree) { // Get the current node's parent ID. int parent = tree.GetParent(node); // Loop until we hit the root node's null parent link. while (parent != -1) { // Get the parent's node object from the game tree. GameTreeNode parentNode = tree.GetNode(parent); // Add the win/loss to the parent's node object. parentNode.AddResult(result); // Save the parent's node object to disk. tree.SetNode(parentNode); // Set the parent ID to the grandparent ID. parent = tree.GetParent(parent); } }