static int MiniMax(BTNode rootNode, bool isMax) { calls++; if (!rootNode.IsQuestion()) { return(Evaluate(rootNode)); } if (isMax) { return(Math.Max(MiniMax(rootNode.GetNoNode(), false), MiniMax(rootNode.GetYesNode(), false))); } else { return(Math.Min(MiniMax(rootNode.GetNoNode(), true), MiniMax(rootNode.GetYesNode(), true))); } }
static void TraversePostOrder(BTNode rootNode) { if (rootNode != null) { TraversePostOrder(rootNode.GetNoNode()); TraversePostOrder(rootNode.GetYesNode()); Console.WriteLine(rootNode.GetMessage()); } }
static void GetLeafs(BTNode rootNode) { if (rootNode != null) { if (!rootNode.IsQuestion()) { leafs.Add(rootNode); } GetLeafs(rootNode.GetNoNode()); GetLeafs(rootNode.GetYesNode()); } }
static int MiniMaxABPruning(BTNode rootNode, bool isMax, int alpha, int beta) { calls++; if (!rootNode.IsQuestion()) { return(Evaluate(rootNode)); } BTNode[] children = new BTNode[2]; children[0] = rootNode.GetNoNode(); children[1] = rootNode.GetYesNode(); if (isMax) { int bestValue = int.MinValue; int value; foreach (BTNode child in children) { value = MiniMaxABPruning(child, false, alpha, beta); bestValue = Math.Max(bestValue, value); alpha = Math.Max(alpha, bestValue); if (beta <= alpha) { break; } } return(bestValue); } else { int bestValue = int.MaxValue; int value; foreach (BTNode child in children) { value = MiniMaxABPruning(child, true, alpha, beta); bestValue = Math.Min(bestValue, value); beta = Math.Min(beta, bestValue); if (beta <= alpha) { break; } } return(bestValue); } }