Пример #1
0
        // evaluate_passed_pawns<>() evaluates the passed pawns of the given color
        private static int evaluate_passed_pawns(int Us, Position pos, EvalInfo ei)
        {
            var Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE);

            ulong b, squaresToQueen, defendedSquares, unsafeSquares, supportingPawns;
            var score = ScoreC.SCORE_ZERO;

            b = (Us == ColorC.WHITE) ? ei.pi.passedPawnsWHITE : ei.pi.passedPawnsBLACK;

            if (b == 0)
            {
                return ScoreC.SCORE_ZERO;
            }

            do
            {
#if X64
                Bitboard bb = b;
                b &= (b - 1);
                Square s = (Utils.BSFTable[((bb & (0xffffffffffffffff - bb + 1)) * DeBruijn_64) >> 58]);
#else
                var s = Utils.pop_lsb(ref b);
#endif
                Debug.Assert(pos.pawn_is_passed(Us, s));

                var r = ((s >> 3) ^ (Us * 7)) - RankC.RANK_2;
                var rr = r * (r - 1);

                // Base bonus based on rank
                var mbonus = (20 * rr);
                var ebonus = (10 * (rr + r + 1));

                if (rr != 0)
                {
                    var blockSq = s + (Us == ColorC.WHITE ? SquareC.DELTA_N : SquareC.DELTA_S);

                    // Adjust bonus based on kings proximity
                    ebonus += (Utils.SquareDistance[pos.pieceList[Them][PieceTypeC.KING][0]][blockSq] * 5 * rr);
                    ebonus -= (Utils.SquareDistance[pos.pieceList[Us][PieceTypeC.KING][0]][blockSq] * 2 * rr);

                    // If blockSq is not the queening square then consider also a second push
                    if ((blockSq >> 3) != (Us == ColorC.WHITE ? RankC.RANK_8 : RankC.RANK_1))
                    {
                        ebonus -=
                            (Utils.SquareDistance[pos.pieceList[Us][PieceTypeC.KING][0]][
                                blockSq + (Us == ColorC.WHITE ? SquareC.DELTA_N : SquareC.DELTA_S)] * rr);
                    }

                    // If the pawn is free to advance, increase bonus
                    if (pos.board[blockSq] == PieceC.NO_PIECE)
                    {
                        squaresToQueen = Utils.ForwardBB[Us][s];
                        defendedSquares = squaresToQueen & ei.attackedBy[Us][0];

                        // If there is an enemy rook or queen attacking the pawn from behind,
                        // add all X-ray attacks by the rook or queen. Otherwise consider only
                        // the squares in the pawn's path attacked or occupied by the enemy.
                        if (((Utils.ForwardBB[Them][s]
                              & ((pos.byTypeBB[PieceTypeC.ROOK] | pos.byTypeBB[PieceTypeC.QUEEN]) & pos.byColorBB[Them]))
                             != 0)
                            && ((Utils.ForwardBB[Them][s]
                                 & ((pos.byTypeBB[PieceTypeC.ROOK] | pos.byTypeBB[PieceTypeC.QUEEN])
                                    & pos.byColorBB[Them]) & pos.attacks_from_ROOK(s)) != 0))
                        {
                            unsafeSquares = squaresToQueen;
                        }
                        else
                        {
                            unsafeSquares = squaresToQueen & (ei.attackedBy[Them][0] | pos.byColorBB[Them]);
                        }

                        // If there aren't enemy attacks or pieces along the path to queen give
                        // huge bonus. Even bigger if we protect the pawn's path.
                        if (unsafeSquares == 0)
                        {
                            ebonus += (rr * (squaresToQueen == defendedSquares ? 17 : 15));
                        }
                        else
                        {
                            // OK, there are enemy attacks or pieces (but not pawns). Are those
                            // squares which are attacked by the enemy also attacked by us ?
                            // If yes, big bonus (but smaller than when there are no enemy attacks),
                            // if no, somewhat smaller bonus.
                            ebonus += (rr * ((unsafeSquares & defendedSquares) == unsafeSquares ? 13 : 8));
                        }
                    }
                } // rr != 0

                // Increase the bonus if the passed pawn is supported by a friendly pawn
                // on the same rank and a bit smaller if it's on the previous rank.
                supportingPawns = (pos.byTypeBB[PieceTypeC.PAWN] & pos.byColorBB[Us]) & Utils.AdjacentFilesBB[s & 7];
                if ((supportingPawns & Utils.RankBB[s >> 3]) != 0)
                {
                    ebonus += (r * 20);
                }
                else if ((supportingPawns
                          & Utils.RankBB[(s - (Us == ColorC.WHITE ? SquareC.DELTA_N : SquareC.DELTA_S)) >> 3]) != 0)
                {
                    ebonus += (r * 12);
                }

                // Rook pawns are a special case: They are sometimes worse, and
                // sometimes better than other passed pawns. It is difficult to find
                // good rules for determining whether they are good or bad. For now,
                // we try the following: Increase the value for rook pawns if the
                // other side has no pieces apart from a knight, and decrease the
                // value if the other side has a rook or queen.
                if ((s & 7) == FileC.FILE_A || (s & 7) == FileC.FILE_H)
                {
                    if ((Them == 0 ? pos.st.npMaterialWHITE : pos.st.npMaterialBLACK) <= Constants.KnightValueMidgame)
                    {
                        ebonus += ebonus / 4;
                    }
                    else if (((pos.byTypeBB[PieceTypeC.ROOK] | pos.byTypeBB[PieceTypeC.QUEEN]) & pos.byColorBB[Them])
                             != 0)
                    {
                        ebonus -= ebonus / 4;
                    }
                }
                score += ((mbonus << 16) + ebonus);
            }
            while (b != 0);

            // Add the scores to the middle game and endgame eval
            return Utils.apply_weight(score, Weights[EvalWeightC.PassedPawns]);
        }
Пример #2
0
        /// K, rook and two pawns vs K, rook and one pawn. There is only a single
        /// pattern: If the stronger side has no passed pawns and the defending king
        /// is actively placed, the position is drawish.
        internal static ScaleFactor Endgame_KRPPKRP(Color strongerSide, Position pos)
        {
            Color weakerSide = strongerSide ^ 1;

            Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.RookValueMidgame);
            Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 2);
            Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.RookValueMidgame);
            Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1);

            Square wpsq1 = pos.pieceList[strongerSide][PieceTypeC.PAWN][0];
            Square wpsq2 = pos.pieceList[strongerSide][PieceTypeC.PAWN][1];
            Square bksq = pos.king_square(weakerSide);

            // Does the stronger side have a passed pawn?
            if (pos.pawn_is_passed(strongerSide, wpsq1)
                || pos.pawn_is_passed(strongerSide, wpsq2))
                return ScaleFactorC.SCALE_FACTOR_NONE;

            Rank r = Math.Max(Utils.relative_rank_CS(strongerSide, wpsq1), Utils.relative_rank_CS(strongerSide, wpsq2));

            if (Utils.file_distance(bksq, wpsq1) <= 1
                && Utils.file_distance(bksq, wpsq2) <= 1
                && Utils.relative_rank_CS(strongerSide, bksq) > r)
            {
                switch (r)
                {
                    case RankC.RANK_2: return (10);
                    case RankC.RANK_3: return (10);
                    case RankC.RANK_4: return (15);
                    case RankC.RANK_5: return (20);
                    case RankC.RANK_6: return (40);
                    default: Debug.Assert(false); break;
                }
            }
            return ScaleFactorC.SCALE_FACTOR_NONE;
        }