コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="turn"></param>
        /// <param name="depth"></param>
        /// <param name="alpha"></param>
        /// <param name="beta"></param>
        /// <param name="maximizing"></param>
        /// <returns></returns>
        public int MinMax(Turn turn, int depth, int alpha, int beta, bool maximizing,
                          List <BoardMovement> appendMovements = null)
        {
            if (depth == 0 || !turn.HasMovementsLeft)
            {
                return(EvaluateTurn(turn));
            }

            turn.CacheCurrentPossibleMovements();
            turn.FilterCachedMovements();

            /*
             * MinMaxCalculation calculation = maximizing ? (MinMaxCalculation)
             * ((int evaluation, ref int maxEvaluation) =>
             * {
             *  maxEvaluation = Math.Max(maxEvaluation, evaluation);
             *  alpha = Math.Max(alpha, evaluation);
             * }) :
             * ((int evaluation, ref int minEvaluation) =>
             * {
             *  minEvaluation = Math.Min(minEvaluation, evaluation);
             *  beta = Math.Min(beta, evaluation);
             * });
             */


            if (maximizing)
            {
                int maxEvaluation = int.MinValue;
                foreach (var movementTurn in turn.AllPossibleMovements)
                {
                    int evaluation = MinMax(movementTurn.Value, depth - 1, alpha, beta, false);

                    if (evaluation >= maxEvaluation)
                    {
                        maxEvaluation = evaluation;

                        if (appendMovements != null)
                        {
                            if (evaluation > maxEvaluation)
                            {
                                chosenMovements.Clear();
                            }

                            chosenMovements.Add(movementTurn.Key);
                        }
                    }
                    alpha = Math.Max(alpha, evaluation);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(maxEvaluation);
            }
            else
            {
                int minEvaluation = int.MaxValue;
                foreach (var movementTurn in turn.AllPossibleMovements)
                {
                    int evaluation = MinMax(movementTurn.Value, depth - 1, alpha, beta, true);

                    if (evaluation <= minEvaluation)
                    {
                        minEvaluation = evaluation;

                        if (appendMovements != null)
                        {
                            if (evaluation < minEvaluation)
                            {
                                chosenMovements.Clear();
                            }

                            chosenMovements.Add(movementTurn.Key);
                        }
                    }
                    beta = Math.Min(beta, evaluation);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(minEvaluation);
            }
        }