예제 #1
0
        private int AlphaBeta(Tree.Node node, int maxDepth, int currDepth, int alpha, int beta)
        {
            if (node.State.HeuristicFunction() == 0)
            {
                if (currDepth % 2 == 0)
                {
                    return(node.Value = 0);
                }
                else
                {
                    return(node.Value = 1);
                }
            }
            if (currDepth == maxDepth)
            {
                if (currDepth % 2 == 0)
                {
                    return(node.Value = 1);
                }
                else
                {
                    return(node.Value = 0);
                }
            }
            int bestValue, currValue;

            if (currDepth % 2 == 0)
            {
                bestValue = Int32.MinValue;
            }
            else
            {
                bestValue = Int32.MaxValue;
            }
            for (int j = 0; j < node.Children.Count; j++)
            {
                currValue = AlphaBeta(node.Children[j], maxDepth, currDepth + 1, alpha, beta);
                if (currDepth % 2 == 0 && currValue > bestValue)
                {
                    bestValue = currValue;
                    if (bestValue >= beta)
                    {
                        j = node.Children.Count;
                        return(node.Value = bestValue);
                    }
                    alpha = Math.Max(alpha, bestValue);
                }
                if (currDepth % 2 == 1 && currValue < bestValue)
                {
                    bestValue = currValue;
                    if (bestValue <= alpha)
                    {
                        return(node.Value = bestValue);
                    }
                    beta = Math.Min(beta, bestValue);
                }
            }
            return(node.Value = bestValue);
        }
예제 #2
0
        private int move(Tree.Node node, int maxDepth, int currDepth)
        {
            if (node.State.HeuristicFunction() == 0)
            {
                if (currDepth % 2 == 0)
                {
                    return(node.Value = 0);
                }
                else
                {
                    return(node.Value = 1);
                }
            }
            if (currDepth == maxDepth)
            {
                if (currDepth % 2 == 0)
                {
                    return(node.Value = 1);
                }
                else
                {
                    return(node.Value = 0);
                }
            }
            int bestValue, currValue;

            if (currDepth % 2 == 0)
            {
                bestValue = Int32.MinValue;
            }
            else
            {
                bestValue = Int32.MaxValue;
            }
            for (int j = 0; j < node.Children.Count; j++)
            {
                currValue = move(node.Children[j], maxDepth, currDepth + 1);
                if (currDepth % 2 == 0 && currValue > bestValue)
                {
                    bestValue = currValue;
                }
                if (currDepth % 2 == 1 && currValue < bestValue)
                {
                    bestValue = currValue;
                }
            }
            return(node.Value = bestValue);
        }