/// <summary> /// Builds the tree /// </summary> /// <param name="boardConfiguration">current board configuration</param> /// <returns>tree</returns> MinimaxTree <Configuration> BuildTree( Configuration boardConfiguration) { // build tree to appropriate depth MinimaxTree <Configuration> tree = new MinimaxTree <Configuration>(boardConfiguration); nodeList.Clear(); nodeList.AddLast(tree.Root); while (nodeList.Count > 0) { MinimaxTreeNode <Configuration> currentNode = nodeList.First.Value; nodeList.RemoveFirst(); List <Configuration> children = GetNextConfigurations(currentNode.Value); foreach (Configuration child in children) { // STUDENTS: only add to tree if within search depth MinimaxTreeNode <Configuration> childNode = new MinimaxTreeNode <Configuration>( child, currentNode); if (Level(childNode) <= searchDepth) { tree.AddNode(childNode); nodeList.AddLast(childNode); } } } return(tree); }
/// <summary> /// Handles the TakeTurn event /// </summary> /// <param name="player">whose turn it is</param> /// <param name="boardConfiguration">current board configuration</param> void HandleTakeTurnEvent(PlayerName player, Configuration boardConfiguration) { // only take turn if it's our turn if (player == myName) { tree = BuildTree(boardConfiguration); thinkingTimer.Run(); } }
/// <summary> /// Builds the tree /// </summary> /// <param name="boardConfiguration">current board configuration</param> /// <returns>tree</returns> MinimaxTree <Configuration> BuildTree( Configuration boardConfiguration) { // build tree to appropriate depth MinimaxTree <Configuration> tree = new MinimaxTree <Configuration>(boardConfiguration); // clear the list of nodes nodeList.Clear(); // add the root as the initial node nodeList.AddLast(tree.Root); // iterate over all nodes while there are nodes to process while (nodeList.Count > 0) { // get the currentNode MinimaxTreeNode <Configuration> currentNode = nodeList.First.Value; // remove it from the list of nodes (to avoid processing it again) nodeList.RemoveFirst(); // Get all possible configurations that might result from // from the one for the current node (as long as they // contain bears). // These configurations represent possible child nodes of this node. List <Configuration> possibleConfigurations = GetNextConfigurations(currentNode.Value); // get the depth of this node int childDepth = getChildDepth(currentNode); // if we are still within the search depth if (childDepth <= searchDepth) { // iterate over all possible configurations where // bins contain bears foreach (Configuration configuration in possibleConfigurations) { // create a new child node with this configuration MinimaxTreeNode <Configuration> childNode = new MinimaxTreeNode <Configuration>( configuration, currentNode); // add the child node to the tree tree.AddNode(childNode); // and add it to the node list nodeList.AddLast(childNode); } } } return(tree); }
void Start() { // build and mark the tree with minimax scores MinimaxTree <char> tree = BuildTree(); // print out tree Debug.Log("Root item value: " + tree.Root.Value); IList <MinimaxTreeNode <char> > children = tree.Root.Children; if (children.Count > 0) { foreach (MinimaxTreeNode <char> child in children) { Debug.Log("Node item value: " + child.Value + ", Node Parent: " + child.Parent.Value); printChildren(child); } } // sets whether we are maximizing bool maximizing = false; Minimax(tree.Root, maximizing); // find child node with maximum score MinimaxTreeNode <char> maxChildNode = children[0]; for (int i = 1; i < children.Count; i++) { if (maximizing) { if (children[i].MinimaxScore > maxChildNode.MinimaxScore) { maxChildNode = children[i]; } } else { if (children[i].MinimaxScore < maxChildNode.MinimaxScore) { maxChildNode = children[i]; } } } Debug.Log("Best move is to node: " + maxChildNode.Value); }
static MinimaxTree <char> BuildTree() { MinimaxTree <char> tree = new MinimaxTree <char>('A'); MinimaxTreeNode <char> bNode = new MinimaxTreeNode <char>('B', tree.Root); tree.AddNode(bNode); MinimaxTreeNode <char> cNode = new MinimaxTreeNode <char>('C', tree.Root); tree.AddNode(cNode); MinimaxTreeNode <char> dNode = new MinimaxTreeNode <char>('D', tree.Root); tree.AddNode(dNode); MinimaxTreeNode <char> eNode = new MinimaxTreeNode <char>('E', bNode); tree.AddNode(eNode); MinimaxTreeNode <char> fNode = new MinimaxTreeNode <char>('F', bNode); tree.AddNode(fNode); MinimaxTreeNode <char> gNode = new MinimaxTreeNode <char>('G', bNode); tree.AddNode(gNode); MinimaxTreeNode <char> hNode = new MinimaxTreeNode <char>('H', cNode); tree.AddNode(hNode); MinimaxTreeNode <char> iNode = new MinimaxTreeNode <char>('I', cNode); tree.AddNode(iNode); MinimaxTreeNode <char> jNode = new MinimaxTreeNode <char>('J', dNode); tree.AddNode(jNode); MinimaxTreeNode <char> kNode = new MinimaxTreeNode <char>('K', dNode); tree.AddNode(kNode); MinimaxTreeNode <char> lNode = new MinimaxTreeNode <char>('L', eNode); tree.AddNode(lNode); MinimaxTreeNode <char> mNode = new MinimaxTreeNode <char>('M', eNode); tree.AddNode(mNode); MinimaxTreeNode <char> nNode = new MinimaxTreeNode <char>('N', fNode); tree.AddNode(nNode); MinimaxTreeNode <char> oNode = new MinimaxTreeNode <char>('O', fNode); tree.AddNode(oNode); MinimaxTreeNode <char> pNode = new MinimaxTreeNode <char>('P', gNode); tree.AddNode(pNode); MinimaxTreeNode <char> qNode = new MinimaxTreeNode <char>('Q', gNode); tree.AddNode(qNode); MinimaxTreeNode <char> rNode = new MinimaxTreeNode <char>('R', hNode); tree.AddNode(rNode); MinimaxTreeNode <char> sNode = new MinimaxTreeNode <char>('S', hNode); tree.AddNode(sNode); MinimaxTreeNode <char> tNode = new MinimaxTreeNode <char>('T', iNode); tree.AddNode(tNode); MinimaxTreeNode <char> uNode = new MinimaxTreeNode <char>('U', iNode); tree.AddNode(uNode); MinimaxTreeNode <char> vNode = new MinimaxTreeNode <char>('V', jNode); tree.AddNode(vNode); MinimaxTreeNode <char> wNode = new MinimaxTreeNode <char>('W', jNode); tree.AddNode(wNode); MinimaxTreeNode <char> xNode = new MinimaxTreeNode <char>('X', kNode); tree.AddNode(xNode); MinimaxTreeNode <char> yNode = new MinimaxTreeNode <char>('Y', kNode); tree.AddNode(yNode); return(tree); }