コード例 #1
0
        private int MinMaxWithoutTT(DamkaBoard board, int depth, bool isRed, int alpha = int.MinValue, int beta = int.MaxValue, bool isFirstMove = true, bool doNull = true)
        {
            if (depth <= 0 || board.WhoWins() != Winner.NoOne)
            {
                if (!board.IsSkipRequired(isRed))
                {
                    return(board.Evaluate(isRed));
                }
            }

            if (depth >= 4 && !isFirstMove && doNull)
            {
                int eval = MinMaxWithoutTT(board.MakeNullMove(), depth - 4, !isRed, alpha, beta - 1, isFirstMove, false);
                if (eval >= beta)
                {
                    return(beta);
                }
            }

            if (isRed)
            {
                int minEval = int.MaxValue;
                foreach (DamkaBoard tmpBoard in board.GetAllMoves(isRed))
                {
                    int eval = MinMaxWithoutTT(tmpBoard, depth - 1, !isRed, alpha, beta, false, doNull);
                    minEval = Math.Min(minEval, eval);
                    beta    = Math.Min(beta, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(minEval);
            }

            else
            {
                int maxEval = int.MinValue;
                foreach (DamkaBoard tmpBoard in board.GetAllMoves(isRed))
                {
                    int eval = MinMaxWithoutTT(tmpBoard, depth - 1, !isRed, alpha, beta, false, doNull);
                    maxEval = Math.Max(maxEval, eval);
                    alpha   = Math.Max(alpha, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(maxEval);
            }
        }
コード例 #2
0
        private int MinMax(DamkaBoard board, int depth, bool isRed, int alpha = int.MinValue, int beta = int.MaxValue, bool isFirstMove = true, bool doNull = true)
        {
            if (depth <= 0 || board.WhoWins() != Winner.NoOne)
            {
                if (!board.IsSkipRequired(isRed))
                {
                    return(board.Evaluate(isRed));
                }
            }

            if (isRed)
            {
                int minEval = int.MaxValue;
                foreach (DamkaBoard tmpBoard in board.GetAllMoves(isRed))
                {
                    int[] results = tTable.Check(tmpBoard, isRed, depth);
                    int   eval;
                    if (results[0] != -15 && results[0] != -20)
                    {
                        eval = results[1];
                    }
                    else
                    {
                        eval = MinMax(tmpBoard, depth - 1, !isRed, alpha, beta, false, doNull);
                        if (depth > 6)
                        {
                            if (results[0] == -20)
                            {
                                tTable.Update(tmpBoard, isRed, depth, eval);
                            }
                            else
                            {
                                tTable.Add(tmpBoard, isRed, depth, eval);
                            }
                        }
                    }
                    minEval = Math.Min(minEval, eval);
                    beta    = Math.Min(beta, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(minEval);
            }

            else
            {
                int maxEval = int.MinValue;
                foreach (DamkaBoard tmpBoard in board.GetAllMoves(isRed))
                {
                    int[] results = tTable.Check(tmpBoard, isRed, depth);
                    int   eval;
                    if (results[0] != -15 && results[0] != -20)
                    {
                        eval = results[1];
                    }
                    else
                    {
                        eval = MinMax(tmpBoard, depth - 1, !isRed, alpha, beta, false, doNull);
                        if (depth > 6)
                        {
                            if (results[0] == -20)
                            {
                                tTable.Update(tmpBoard, isRed, depth, eval);
                            }
                            else
                            {
                                tTable.Add(tmpBoard, isRed, depth, eval);
                            }
                        }
                    }
                    maxEval = Math.Max(maxEval, eval);
                    alpha   = Math.Max(alpha, eval);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(maxEval);
            }
        }