Example #1
0
        static float NegaScout(INode node, SearchConfig config, float alpha, float beta)
        {
            if (config.NodesSearched != null)
            {
                config.NodesSearched.Add(node);
            }

            if (node.IsGameOver || config.Depth == config.MaxDepth)
            {
                return(Sign[config.Colour] * node.Value);
            }

            ProcessNode(node, config.Depth, config.MaxDepth);

            // Order children so NegaScout can search effectively
            var orderedChildren = node.Children.OrderByDescending(x => x.Value);

            var b = beta;

            config.Colour = 1 - config.Colour;
            config.Depth++;

            var firstChildSearched = false;

            foreach (var child in orderedChildren)
            {
                var t = -NegaScout(child, config, -b, -alpha);
                if ((t > alpha) && (t < beta) && firstChildSearched)
                {
                    t = -NegaScout(child, config, -beta, -alpha);
                }

                alpha = Math.Max(alpha, t);

                if (alpha >= beta)
                {
                    return(alpha);
                }

                b = alpha + 1;

                firstChildSearched = true;
            }
            return(alpha);
        }
Example #2
0
        static float AlphaBetaNegaMax(INode node, SearchConfig config, float alpha, float beta)
        {
            if (config.NodesSearched != null)
            {
                config.NodesSearched.Add(node);
            }

            if (node.IsGameOver || config.Depth == config.MaxDepth)
            {
                return(Sign[config.Colour] * node.Value);
            }

            ProcessNode(node, config.Depth, config.MaxDepth);

            config.Colour = 1 - config.Colour;
            config.Depth++;

            var bestScore = Minimum;

            foreach (var child in node.Children)
            {
                var gameState = child.GameState;

                var score = GetScore(config.UseTranspositionTable, gameState) ?? -AlphaBetaNegaMax(child, config, -beta, -alpha);

                AddHashToTranspositionTable(config.UseTranspositionTable, gameState, score);

                if (score >= beta)
                {
                    return(score);
                }

                if (score > bestScore)
                {
                    bestScore = score;
                }

                if (score > alpha)
                {
                    alpha = score;
                }
            }
            return(bestScore);
        }
Example #3
0
 public static float AlphaBetaNegaMax(INode node, SearchConfig config)
 {
     return(AlphaBetaNegaMax(node, config, -InitialAlphaBeta, InitialAlphaBeta));
 }
Example #4
0
 public static float NegaScout(INode node, SearchConfig config)
 {
     return(NegaScout(node, config, -InitialAlphaBeta, InitialAlphaBeta));
 }
Example #5
0
 public static float AlphaBetaNegaMax(INode node, SearchConfig config, IGameController gameController, Stopwatch searchTime = null)
 {
     return(AlphaBetaNegaMax(node, config, -InitialAlphaBeta, InitialAlphaBeta, gameController, searchTime));
 }