Exemplo n.º 1
0
 static void ReturnPostorder(BTNode rootNode)
 {
     if (rootNode.getYesNode() != null)
     {
         ReturnPostorder(rootNode.getYesNode());
     }
     if (rootNode.getNoNode() != null)
     {
         ReturnPostorder(rootNode.getNoNode());
     }
     Console.WriteLine(rootNode.getMessage());
 }
Exemplo n.º 2
0
        public void PostOrder(BTNode node)
        {
            if (node.getYesNode() != null)
            {
                PostOrder(node.getYesNode());
            }

            if (node.getNoNode() != null)
            {
                PostOrder(node.getNoNode());
            }

            if (node.getMessage() != null)
            {
                Console.WriteLine(node.getMessage());
            }
        }
Exemplo n.º 3
0
        public int ABPruning(BTNode concerningNode, bool isMax, int alpha = int.MinValue, int beta = int.MaxValue)
        {
            Console.WriteLine(string.Format("Now checking node {0}", concerningNode.getMessage()));

            if (!concerningNode.isQuestion())
            {
                return(concerningNode.Evaluation());
            }

            if (isMax)
            {
                int best = int.MinValue;

                for (int i = 0; i < 2; i++)
                {
                    int value = ABPruning(i == 0 ? concerningNode.getYesNode() : concerningNode.getNoNode(), false, alpha, beta);

                    best  = Math.Max(best, value);
                    alpha = Math.Max(alpha, best);

                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(best);
            }
            else
            {
                int best = int.MaxValue;

                for (int i = 0; i < 2; i++)
                {
                    int value = ABPruning(i == 0 ? concerningNode.getYesNode() : concerningNode.getNoNode(), true, alpha, beta);

                    best = Math.Min(best, value);
                    beta = Math.Min(beta, best);

                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(best);
            }
        }
Exemplo n.º 4
0
        public void EvaluationFunction(BTNode node)
        {
            if (node.getYesNode() != null)
            {
                EvaluationFunction(node.getYesNode());
            }

            if (node.getNoNode() != null)
            {
                EvaluationFunction(node.getNoNode());
            }

            if (node.getYesNode() == null && node.getNoNode() == null)
            {
                node.value = (int)node.getMessage().LongCount();
                Console.WriteLine(node.getMessage() + ": " + node.value + ": " + node.IsLeafNode(node));
            }
        }
Exemplo n.º 5
0
        public int Minimax(BTNode concerningNode, bool isMax)
        {
            if (!concerningNode.isQuestion())
            {
                return(concerningNode.Evaluation());
            }

            if (isMax)
            {
                return(Math.Max(Minimax(concerningNode.getYesNode(), !isMax),
                                Minimax(concerningNode.getNoNode(), !isMax)));
            }
            else
            {
                return(Math.Min(Minimax(concerningNode.getYesNode(), !isMax),
                                Minimax(concerningNode.getNoNode(), !isMax)));
            }
        }
Exemplo n.º 6
0
        public void PrintOrder(BTNode concerningNode, Order order, string currentStreak = "")
        {
            switch (order)
            {
            case Order.PreOrder:
                Console.WriteLine(string.Format("CurrentStreak: {0}, Message: {1}", currentStreak, concerningNode.getMessage()));     //Printing the message
                if (concerningNode.getYesNode() != null)
                {
                    PrintOrder(concerningNode.getYesNode(), order, currentStreak + "Y");
                }
                if (concerningNode.getNoNode() != null)
                {
                    PrintOrder(concerningNode.getNoNode(), order, currentStreak + "N");
                }
                break;

            case Order.InOrder:
                if (concerningNode.getYesNode() != null)
                {
                    PrintOrder(concerningNode.getYesNode(), order, currentStreak + "Y");
                }
                Console.WriteLine(string.Format("CurrentStreak: {0}, Message: {1}", currentStreak, concerningNode.getMessage()));     //Printing the message
                if (concerningNode.getNoNode() != null)
                {
                    PrintOrder(concerningNode.getNoNode(), order, currentStreak + "N");
                }
                break;

            case Order.PostOrder:
                if (concerningNode.getYesNode() != null)
                {
                    PrintOrder(concerningNode.getYesNode(), order, currentStreak + "Y");
                }
                if (concerningNode.getNoNode() != null)
                {
                    PrintOrder(concerningNode.getNoNode(), order, currentStreak + "N");
                }
                Console.WriteLine(string.Format("CurrentStreak: {0}, Message: {1}", currentStreak, concerningNode.getMessage()));     //Printing the message
                break;
            }
        }
Exemplo n.º 7
0
        public int MiniMax(BTNode node, int depth)
        {
            if (node.IsLeafNode(node.getYesNode()))
            {
                if (depth % 2 == 0)
                {
                    if (node.value == 0)
                    {
                        node.value = node.getYesNode().value;
                    }
                    else if (node.value < node.getYesNode().value)
                    {
                        node.value = node.getYesNode().value;
                    }
                }
                else
                {
                    if (node.value == 0)
                    {
                        node.value = node.getYesNode().value;
                    }
                    else if (node.value > node.getYesNode().value)
                    {
                        node.value = node.getYesNode().value;
                    }
                }
            }

            if (node.IsLeafNode(node.getNoNode()))
            {
                if (depth % 2 == 0)
                {
                    if (node.value == 0)
                    {
                        node.value = node.getNoNode().value;
                    }
                    else if (node.value < node.getNoNode().value)
                    {
                        node.value = node.getNoNode().value;
                    }
                }
                else
                {
                    if (node.value == 0)
                    {
                        node.value = node.getNoNode().value;
                    }
                    else if (node.value > node.getNoNode().value)
                    {
                        node.value = node.getNoNode().value;
                    }
                }
            }

            if (!node.IsLeafNode(node.getYesNode()) && !node.IsLeafNode(node.getNoNode()))
            {
                if (node.getYesNode() != null)
                {
                    node.value = MiniMax(node.getYesNode(), depth += 1);
                }

                if (node.getNoNode() != null)
                {
                    node.value = MiniMax(node.getNoNode(), depth += 1);
                }
            }

            return(node.value);
        }