Пример #1
0
        private static int Quiescence(Board examineBoard, int alpha, int beta, ref int nodesSearched)
        {
            nodesSearched = nodesSearched + 1;
            Evaluation.EvaluateBoardScore(examineBoard);
            examineBoard.Score = Search.SideToMoveScore(examineBoard.Score, examineBoard.WhoseMove);
            if (examineBoard.Score >= beta)
            {
                return(beta);
            }
            if (examineBoard.Score > alpha)
            {
                alpha = examineBoard.Score;
            }
            List <Search.Position> positionList = examineBoard.WhiteCheck || examineBoard.BlackCheck ? Search.EvaluateMoves(examineBoard, (byte)0) : Search.EvaluateMovesQ(examineBoard);

            if (positionList.Count == 0)
            {
                return(examineBoard.Score);
            }
            positionList.Sort(new Comparison <Search.Position>(Search.Sort));
            foreach (Search.Position position in positionList)
            {
                if (Search.StaticExchangeEvaluation(examineBoard.Squares[(int)position.DstPosition]) < 0)
                {
                    Board board = examineBoard.FastCopy();
                    Board.MovePiece(board, position.SrcPosition, position.DstPosition, ChessPieceType.Queen);
                    PieceValidMoves.GenerateValidMoves(board);
                    if ((!board.BlackCheck || examineBoard.WhoseMove != ChessPieceColor.Black) && (!board.WhiteCheck || examineBoard.WhoseMove != ChessPieceColor.White))
                    {
                        int num = -Search.Quiescence(board, -beta, -alpha, ref nodesSearched);
                        if (num >= beta)
                        {
                            Search.KillerMove[2, 0].SrcPosition = position.SrcPosition;
                            Search.KillerMove[2, 0].DstPosition = position.DstPosition;
                            return(beta);
                        }
                        if (num > alpha)
                        {
                            alpha = num;
                        }
                    }
                }
            }
            return(alpha);
        }
Пример #2
0
        private static int AlphaBeta(Board examineBoard, byte depth, int alpha, int beta, ref int nodesSearched, ref int nodesQuiessence, ref List <Search.Position> pvLine, bool extended)
        {
            nodesSearched = nodesSearched + 1;
            if ((int)examineBoard.FiftyMove >= 50 || (int)examineBoard.RepeatedMove >= 3)
            {
                return(0);
            }
            int?nullable1 = Zobrist.Search(examineBoard.ZobristHash, depth, alpha, beta);

            if (nullable1.HasValue)
            {
                return(nullable1.Value);
            }
            if ((int)depth == 0)
            {
                if (!extended && examineBoard.BlackCheck || examineBoard.WhiteCheck)
                {
                    ++depth;
                    extended = true;
                }
                else
                {
                    int score = Search.Quiescence(examineBoard, alpha, beta, ref nodesQuiessence);
                    if (score >= beta)
                    {
                        Zobrist.AddEntry(examineBoard.ZobristHash, depth, score, Zobrist.NodeType.Beta);
                    }
                    else if (score <= alpha)
                    {
                        Zobrist.AddEntry(examineBoard.ZobristHash, depth, score, Zobrist.NodeType.Alpha);
                    }
                    else
                    {
                        Zobrist.AddEntry(examineBoard.ZobristHash, depth, score, Zobrist.NodeType.Exact);
                    }
                    return(score);
                }
            }
            Zobrist.NodeType       nodeType = Zobrist.NodeType.Alpha;
            List <Search.Position> moves    = Search.EvaluateMoves(examineBoard, depth);

            if ((examineBoard.WhiteCheck || examineBoard.BlackCheck || moves.Count == 0) && Search.SearchForMate(examineBoard.WhoseMove, examineBoard, ref examineBoard.BlackMate, ref examineBoard.WhiteMate, ref examineBoard.StaleMate))
            {
                if (examineBoard.BlackMate)
                {
                    if (examineBoard.WhoseMove == ChessPieceColor.Black)
                    {
                        return(-32767 - (int)depth);
                    }
                    return((int)short.MaxValue + (int)depth);
                }
                if (!examineBoard.WhiteMate)
                {
                    return(0);
                }
                if (examineBoard.WhoseMove == ChessPieceColor.Black)
                {
                    return((int)short.MaxValue + (int)depth);
                }
                return(-32767 - (int)depth);
            }
            moves.Sort(new Comparison <Search.Position>(Search.Sort));
            foreach (Search.Position position in moves)
            {
                List <Search.Position> pvLine1 = new List <Search.Position>();
                Board board = examineBoard.FastCopy();
                Board.MovePiece(board, position.SrcPosition, position.DstPosition, ChessPieceType.Queen);
                PieceValidMoves.GenerateValidMoves(board);
                if ((!board.BlackCheck || examineBoard.WhoseMove != ChessPieceColor.Black) && (!board.WhiteCheck || examineBoard.WhoseMove != ChessPieceColor.White))
                {
                    int?nullable2 = new int?(-Search.AlphaBeta(board, (byte)((uint)depth - 1U), -beta, -alpha, ref nodesSearched, ref nodesQuiessence, ref pvLine1, extended));
                    int?nullable3 = nullable2;
                    int num1      = beta;
                    if ((nullable3.GetValueOrDefault() >= num1 ? (nullable3.HasValue ? 1 : 0) : 0) != 0)
                    {
                        Search.KillerMove[Search.kIndex, (int)depth].SrcPosition = position.SrcPosition;
                        Search.KillerMove[Search.kIndex, (int)depth].DstPosition = position.DstPosition;
                        Search.kIndex = (Search.kIndex + 1) % 2;
                        Zobrist.AddEntry(examineBoard.ZobristHash, depth, nullable2.Value, Zobrist.NodeType.Beta);
                        return(beta);
                    }
                    nullable3 = nullable2;
                    int num2 = alpha;
                    if ((nullable3.GetValueOrDefault() > num2 ? (nullable3.HasValue ? 1 : 0) : 0) != 0)
                    {
                        pvLine1.Insert(0, new Search.Position()
                        {
                            SrcPosition = board.LastMove.MovingPiecePrimary.SrcPosition,
                            DstPosition = board.LastMove.MovingPiecePrimary.DstPosition,
                            Move        = board.LastMove.ToString()
                        });
                        pvLine   = pvLine1;
                        alpha    = nullable2.Value;
                        nodeType = Zobrist.NodeType.Exact;
                    }
                }
            }
            Zobrist.AddEntry(examineBoard.ZobristHash, depth, alpha, nodeType);
            return(alpha);
        }