/// Mate with KX vs K. This function is used to evaluate positions with /// king and plenty of material vs a lone king. It simply gives the /// attacking side a bonus for driving the defending king towards the edge /// of the board, and for keeping the distance between the two kings small. public Value KXK(Position pos) { Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 0)); Debug.Assert(0 == pos.checkers()); // Eval is never called when in check // Stalemate detection with lone king if (pos.side_to_move() == weakSide && 0 == (new MoveList(pos, GenTypeS.LEGAL)).size()) { return(ValueS.VALUE_DRAW); } Square winnerKSq = pos.king_square(strongSide); Square loserKSq = pos.king_square(weakSide); Value result = pos.non_pawn_material(strongSide) + pos.count(strongSide, PieceTypeS.PAWN) * ValueS.PawnValueEg + PushToEdges[loserKSq] + PushClose[BitBoard.square_distance(winnerKSq, loserKSq)]; if (pos.count(strongSide, PieceTypeS.QUEEN) != 0 || pos.count(strongSide, PieceTypeS.ROOK) != 0 || (pos.count(strongSide, PieceTypeS.BISHOP) != 0 && pos.count(strongSide, PieceTypeS.KNIGHT) != 0) || pos.bishop_pair(strongSide)) { result += ValueS.VALUE_KNOWN_WIN; } return(strongSide == pos.side_to_move() ? result : -result); }
public KPKPosition(uint idx) { wksq = (Square)((idx >> 0) & 0x3F); bksq = (Square)((idx >> 6) & 0x3F); us = (Color)((idx >> 12) & 0x01); psq = Types.make_square((File)((idx >> 13) & 0x03), (Rank)(RankS.RANK_7 - (idx >> 15))); result = Result.UNKNOWN; // Check if two pieces are on the same square or if a king can be captured if (BitBoard.square_distance(wksq, bksq) <= 1 || wksq == psq || bksq == psq || (us == ColorS.WHITE && (BitBoard.StepAttacksBB[PieceTypeS.PAWN][psq] & BitBoard.SquareBB[bksq]) != 0)) { result = Result.INVALID; } else if (us == ColorS.WHITE) { // Immediate win if pawn can be promoted without getting captured if (Types.rank_of(psq) == RankS.RANK_7 && wksq != psq + SquareS.DELTA_N && (BitBoard.square_distance(bksq, psq + SquareS.DELTA_N) > 1 || (BitBoard.StepAttacksBB[PieceTypeS.KING][wksq] & BitBoard.SquareBB[psq + SquareS.DELTA_N]) != 0)) { result = Result.WIN; } } // Immediate draw if it is a stalemate or a king captures undefended pawn else if (0 == (BitBoard.StepAttacksBB[PieceTypeS.KING][bksq] & ~(BitBoard.StepAttacksBB[PieceTypeS.KING][wksq] | BitBoard.StepAttacksBB[PieceTypeS.PAWN][psq])) || (BitBoard.StepAttacksBB[PieceTypeS.KING][bksq] & BitBoard.SquareBB[psq] & ~BitBoard.StepAttacksBB[PieceTypeS.KING][wksq]) != 0) { result = Result.DRAW; } }
/// KBP vs KB. There are two rules: if the defending king is somewhere along the /// path of the pawn, and the square of the king is not of the same color as the /// stronger side's bishop, it's a draw. If the two bishops have opposite color, /// it's almost always a draw. public ScaleFactor KBPKB(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.BishopValueMg, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.BishopValueMg, 0)); Square pawnSq = pos.list(strongSide, PieceTypeS.PAWN)[0]; Square strongerBishopSq = pos.list(strongSide, PieceTypeS.BISHOP)[0]; Square weakerBishopSq = pos.list(weakSide, PieceTypeS.BISHOP)[0]; Square weakerKingSq = pos.king_square(weakSide); // Case 1: Defending king blocks the pawn, and cannot be driven away if (Types.file_of(weakerKingSq) == Types.file_of(pawnSq) && Types.relative_rank_square(strongSide, pawnSq) < Types.relative_rank_square(strongSide, weakerKingSq) && (Types.opposite_colors(weakerKingSq, strongerBishopSq) || Types.relative_rank_square(strongSide, weakerKingSq) <= RankS.RANK_6)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // Case 2: Opposite colored bishops if (Types.opposite_colors(strongerBishopSq, weakerBishopSq)) { // We assume that the position is drawn in the following three situations: // // a. The pawn is on rank 5 or further back. // b. The defending king is somewhere in the pawn's path. // c. The defending bishop attacks some square along the pawn's path, // and is at least three squares away from the pawn. // // These rules are probably not perfect, but in practice they work // reasonably well. if (Types.relative_rank_square(strongSide, pawnSq) <= RankS.RANK_5) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } else { Bitboard path = BitBoard.forward_bb(strongSide, pawnSq); if ((path & pos.pieces_color_piecetype(weakSide, PieceTypeS.KING)) != 0) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } if (((pos.attacks_from_square_piecetype(weakerBishopSq, PieceTypeS.BISHOP) & path) != 0) && BitBoard.square_distance(weakerBishopSq, pawnSq) >= 3) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } } } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KR vs KN. The attacking side has slightly better winning chances than /// in KR vs KB, particularly if the king and the knight are far apart. public Value KRKN(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.KnightValueMg, 0)); Square bksq = pos.king_square(weakSide); Square bnsq = pos.list(weakSide, PieceTypeS.KNIGHT)[0]; Value result = (PushToEdges[bksq] + PushAway[BitBoard.square_distance(bksq, bnsq)]); return(strongSide == pos.side_to_move() ? result : -result); }
/// KNP vs KB. If knight can block bishop from taking pawn, it's a win. /// Otherwise the position is drawn. public ScaleFactor KNPKB(Position pos) { Square pawnSq = pos.list(strongSide, PieceTypeS.PAWN)[0]; Square bishopSq = pos.list(weakSide, PieceTypeS.BISHOP)[0]; Square weakerKingSq = pos.king_square(weakSide); // King needs to get close to promoting pawn to prevent knight from blocking. // Rules for this are very tricky, so just approximate. if ((BitBoard.forward_bb(strongSide, pawnSq) & pos.attacks_from_square_piecetype(bishopSq, PieceTypeS.BISHOP)) != 0) { return(BitBoard.square_distance(weakerKingSq, pawnSq)); } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KQ vs KR. This is almost identical to KX vs K: We give the attacking /// king a bonus for having the kings close together, and for forcing the /// defending king towards the edge. If we also take care to avoid null move for /// the defending side in the search, this is usually sufficient to win KQ vs KR. public Value KQKR(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.QueenValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.RookValueMg, 0)); Square winnerKSq = pos.king_square(strongSide); Square loserKSq = pos.king_square(weakSide); Value result = ValueS.QueenValueEg - ValueS.RookValueEg + PushToEdges[loserKSq] + PushClose[BitBoard.square_distance(winnerKSq, loserKSq)]; return(strongSide == pos.side_to_move() ? result : -result); }
public ScaleFactor KRPKB(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.BishopValueMg, 0)); // Test for a rook pawn if ((pos.pieces_piecetype(PieceTypeS.PAWN) & (BitBoard.FileABB | BitBoard.FileHBB)) != 0) { Square ksq = pos.king_square(weakSide); Square bsq = pos.list(weakSide, PieceTypeS.BISHOP)[0]; Square psq = pos.list(strongSide, PieceTypeS.PAWN)[0]; Rank rk = Types.relative_rank_square(strongSide, psq); Square push = Types.pawn_push(strongSide); // If the pawn is on the 5th rank and the pawn (currently) is on // the same color square as the bishop then there is a chance of // a fortress. Depending on the king position give a moderate // reduction or a stronger one if the defending king is near the // corner but not trapped there. if (rk == RankS.RANK_5 && !Types.opposite_colors(bsq, psq)) { int d = BitBoard.square_distance(psq + 3 * push, ksq); if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push)) { return(24); } else { return(48); } } // When the pawn has moved to the 6th rank we can be fairly sure // it's drawn if the bishop attacks the square in front of the // pawn from a reasonable distance and the defending king is near // the corner if (rk == RankS.RANK_6 && BitBoard.square_distance(psq + 2 * push, ksq) <= 1 && (BitBoard.PseudoAttacks[PieceTypeS.BISHOP][bsq] & BitBoard.SquareBB[(psq + push)]) != 0 && BitBoard.file_distance(bsq, psq) >= 2) { return(8); } } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KNP vs K. There is a single rule: if the pawn is a rook pawn on the 7th rank /// and the defending king prevents the pawn from advancing, the position is drawn. public ScaleFactor KNPK(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.KnightValueMg, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 0)); // Assume strongSide is white and the pawn is on files A-D Square pawnSq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.PAWN)[0]); Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide)); if (pawnSq == SquareS.SQ_A7 && BitBoard.square_distance(SquareS.SQ_A8, weakKingSq) <= 1) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KR vs KP. This is a somewhat tricky endgame to evaluate precisely without /// a bitbase. The function below returns drawish scores when the pawn is /// far advanced with support of the king, while the attacking king is far /// away. public Value KRKP(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 1)); Square wksq = Types.relative_square(strongSide, pos.king_square(strongSide)); Square bksq = Types.relative_square(strongSide, pos.king_square(weakSide)); Square rsq = Types.relative_square(strongSide, pos.list(strongSide, PieceTypeS.ROOK)[0]); Square psq = Types.relative_square(strongSide, pos.list(weakSide, PieceTypeS.PAWN)[0]); Square queeningSq = Types.make_square(Types.file_of(psq), RankS.RANK_1); Value result; // If the stronger side's king is in front of the pawn, it's a win if (wksq < psq && Types.file_of(wksq) == Types.file_of(psq)) { result = ValueS.RookValueEg - (BitBoard.square_distance(wksq, psq)); } // If the weaker side's king is too far from the pawn and the rook, // it's a win else if (BitBoard.square_distance(bksq, psq) >= 3 + ((pos.side_to_move() == weakSide)?1:0) && BitBoard.square_distance(bksq, rsq) >= 3) { result = ValueS.RookValueEg - (BitBoard.square_distance(wksq, psq)); } // If the pawn is far advanced and supported by the defending king, // the position is drawish else if (Types.rank_of(bksq) <= RankS.RANK_3 && BitBoard.square_distance(bksq, psq) == 1 && Types.rank_of(wksq) >= RankS.RANK_4 && BitBoard.square_distance(wksq, psq) > 2 + ((pos.side_to_move() == strongSide)?1:0)) { result = 80 - 8 * BitBoard.square_distance(wksq, psq); } else { result = (Value)(200) - 8 * (BitBoard.square_distance(wksq, psq + SquareS.DELTA_S) - BitBoard.square_distance(bksq, psq + SquareS.DELTA_S) - BitBoard.square_distance(psq, queeningSq)); } return(strongSide == pos.side_to_move() ? result : -result); }
/// KQ vs KP. In general, this is a win for the stronger side, but there are a /// few important exceptions. A pawn on 7th rank and on the A,C,F or H files /// with a king positioned next to it can be a draw, so in that case, we only /// use the distance between the kings. public Value KQKP(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.QueenValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 1)); Square winnerKSq = pos.king_square(strongSide); Square loserKSq = pos.king_square(weakSide); Square pawnSq = pos.list(weakSide, PieceTypeS.PAWN)[0]; Value result = (PushClose[BitBoard.square_distance(winnerKSq, loserKSq)]); if (Types.relative_rank_square(weakSide, pawnSq) != RankS.RANK_7 || BitBoard.square_distance(loserKSq, pawnSq) != 1 || 0 == ((BitBoard.FileABB | BitBoard.FileCBB | BitBoard.FileFBB | BitBoard.FileHBB) & BitBoard.SquareBB[pawnSq])) { result += ValueS.QueenValueEg - ValueS.PawnValueEg; } return(strongSide == pos.side_to_move() ? result : -result); }
public static Bitboard sliding_attack(Square[] deltas, Square sq, Bitboard occupied) { Bitboard attack = 0; for (int i = 0; i < 4; ++i) { for (Square s = sq + deltas[i]; Types.is_ok_square(s) && BitBoard.square_distance(s, s - deltas[i]) == 1; s += deltas[i]) { attack |= SquareBB[s]; if ((occupied & SquareBB[s]) != 0) { break; } } } return(attack); }
/// Mate with KBN vs K. This is similar to KX vs K, but we have to drive the /// defending king towards a corner square of the right color. public Value KBNK(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.KnightValueMg + ValueS.BishopValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 0)); Square winnerKSq = pos.king_square(strongSide); Square loserKSq = pos.king_square(weakSide); Square bishopSq = pos.list(strongSide, PieceTypeS.BISHOP)[0]; // kbnk_mate_table() tries to drive toward corners A1 or H8. If we have a // bishop that cannot reach the above squares, we flip the kings in order // to drive the enemy toward corners A8 or H1. if (Types.opposite_colors(bishopSq, SquareS.SQ_A1)) { winnerKSq = Types.notSquare(winnerKSq); loserKSq = Types.notSquare(loserKSq); } Value result = ValueS.VALUE_KNOWN_WIN + PushClose[BitBoard.square_distance(winnerKSq, loserKSq)] + PushToCorners[loserKSq]; return(strongSide == pos.side_to_move() ? result : -result); }
/// Bitboards::init() initializes various bitboard tables. It is called at /// startup and relies on global objects to be already zero-initialized. public static void init() { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { BSFTable[bsf_index(SquareBB[s] = 1UL << s)] = s; } for (Bitboard b = 1; b < 256; ++b) { MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b); } for (File f = FileS.FILE_A; f <= FileS.FILE_H; ++f) { FileBB[f] = f > FileS.FILE_A ? FileBB[f - 1] << 1 : FileABB; } for (Rank r = RankS.RANK_1; r <= RankS.RANK_8; ++r) { RankBB[r] = r > RankS.RANK_1 ? RankBB[r - 1] << 8 : Rank1BB; } for (File f = FileS.FILE_A; f <= FileS.FILE_H; ++f) { AdjacentFilesBB[f] = (f > FileS.FILE_A ? FileBB[f - 1] : 0) | (f < FileS.FILE_H ? FileBB[f + 1] : 0); } for (int c = ColorS.WHITE; c <= ColorS.BLACK; c++) { InFrontBB[c] = new Bitboard[RankS.RANK_NB]; } for (Rank r = RankS.RANK_1; r < RankS.RANK_8; ++r) { InFrontBB[ColorS.WHITE][r] = ~(InFrontBB[ColorS.BLACK][r + 1] = InFrontBB[ColorS.BLACK][r] | RankBB[r]); } for (int c = ColorS.WHITE; c <= ColorS.BLACK; c++) { ForwardBB[c] = new Bitboard[SquareS.SQUARE_NB]; PawnAttackSpan[c] = new Bitboard[SquareS.SQUARE_NB]; PassedPawnMask[c] = new Bitboard[SquareS.SQUARE_NB]; } for (Color c = ColorS.WHITE; c <= ColorS.BLACK; ++c) { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { ForwardBB[c][s] = InFrontBB[c][Types.rank_of(s)] & FileBB[Types.file_of(s)]; PawnAttackSpan[c][s] = InFrontBB[c][Types.rank_of(s)] & AdjacentFilesBB[Types.file_of(s)]; PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s]; } } for (Square c = 0; c < SquareS.SQUARE_NB; c++) { SquareDistance[c] = new int[SquareS.SQUARE_NB]; DistanceRingsBB[c] = new Bitboard[8]; } for (Square s1 = SquareS.SQ_A1; s1 <= SquareS.SQ_H8; ++s1) { for (Square s2 = SquareS.SQ_A1; s2 <= SquareS.SQ_H8; ++s2) { if (s1 != s2) { SquareDistance[s1][s2] = Math.Max(file_distance(s1, s2), rank_distance(s1, s2)); DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= SquareBB[s2]; } } } int[][] steps = new int[7][]; steps[0] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[1] = new int[] { 7, 9, 0, 0, 0, 0, 0, 0, 0 }; steps[2] = new int[] { 17, 15, 10, 6, -6, -10, -15, -17, 0 }; steps[3] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[4] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[5] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[6] = new int[] { 9, 7, -7, -9, 8, 1, -1, -8, 0 }; for (Piece p = PieceS.NO_PIECE; p < PieceS.PIECE_NB; p++) { StepAttacksBB[p] = new Bitboard[SquareS.SQUARE_NB]; } for (Color c = ColorS.WHITE; c <= ColorS.BLACK; ++c) { for (PieceType pt = PieceTypeS.PAWN; pt <= PieceTypeS.KING; ++pt) { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { for (int i = 0; steps[pt][i] != 0; ++i) { Square to = s + (Square)(c == ColorS.WHITE ? steps[pt][i] : -steps[pt][i]); if (Types.is_ok_square(to) && BitBoard.square_distance(s, to) < 3) { StepAttacksBB[Types.make_piece(c, pt)][s] |= SquareBB[to]; } } } } } Square[] RDeltas = new Square[] { SquareS.DELTA_N, SquareS.DELTA_E, SquareS.DELTA_S, SquareS.DELTA_W }; Square[] BDeltas = new Square[] { SquareS.DELTA_NE, SquareS.DELTA_SE, SquareS.DELTA_SW, SquareS.DELTA_NW }; init_magics(PieceTypeS.ROOK, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index); init_magics(PieceTypeS.BISHOP, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index); for (PieceType pt = PieceTypeS.NO_PIECE_TYPE; pt < PieceTypeS.PIECE_TYPE_NB; pt++) { PseudoAttacks[pt] = new Bitboard[SquareS.SQUARE_NB]; } for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; s++) { BetweenBB[s] = new Bitboard[SquareS.SQUARE_NB]; LineBB[s] = new Bitboard[SquareS.SQUARE_NB]; } for (Square s1 = SquareS.SQ_A1; s1 <= SquareS.SQ_H8; ++s1) { PseudoAttacks[PieceTypeS.QUEEN][s1] = PseudoAttacks[PieceTypeS.BISHOP][s1] = attacks_bb_SBBPT(s1, 0, PieceTypeS.BISHOP); PseudoAttacks[PieceTypeS.QUEEN][s1] |= PseudoAttacks[PieceTypeS.ROOK][s1] = attacks_bb_SBBPT(s1, 0, PieceTypeS.ROOK); for (Square s2 = SquareS.SQ_A1; s2 <= SquareS.SQ_H8; ++s2) { Piece pc = (PseudoAttacks[PieceTypeS.BISHOP][s1] & SquareBB[s2]) != 0 ? PieceS.W_BISHOP : (PseudoAttacks[PieceTypeS.ROOK][s1] & SquareBB[s2]) != 0 ? PieceS.W_ROOK : PieceS.NO_PIECE; if (pc == PieceS.NO_PIECE) { continue; } LineBB[s1][s2] = (attacks_bb_PSBB(pc, s1, 0) & attacks_bb_PSBB(pc, s2, 0)) | SquareBB[s1] | SquareBB[s2]; BetweenBB[s1][s2] = attacks_bb_PSBB(pc, s1, SquareBB[s2]) & attacks_bb_PSBB(pc, s2, SquareBB[s1]); } } }
/// KRP vs KR. This function knows a handful of the most important classes of /// drawn positions, but is far from perfect. It would probably be a good idea /// to add more knowledge in the future. /// /// It would also be nice to rewrite the actual code for this function, /// which is mostly copied from Glaurung 1.x, and isn't very pretty. public ScaleFactor KRPKR(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.RookValueMg, 0)); // Assume strongSide is white and the pawn is on files A-D Square wksq = normalize(pos, strongSide, pos.king_square(strongSide)); Square bksq = normalize(pos, strongSide, pos.king_square(weakSide)); Square wrsq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.ROOK)[0]); Square wpsq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.PAWN)[0]); Square brsq = normalize(pos, strongSide, pos.list(weakSide, PieceTypeS.ROOK)[0]); File f = Types.file_of(wpsq); Rank r = Types.rank_of(wpsq); Square queeningSq = Types.make_square(f, RankS.RANK_8); int tempo = (pos.side_to_move() == strongSide ? 1 : 0); // If the pawn is not too far advanced and the defending king defends the // queening square, use the third-rank defence. if (r <= RankS.RANK_5 && BitBoard.square_distance(bksq, queeningSq) <= 1 && wksq <= SquareS.SQ_H5 && (Types.rank_of(brsq) == RankS.RANK_6 || (r <= RankS.RANK_3 && Types.rank_of(wrsq) != RankS.RANK_6))) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // The defending side saves a draw by checking from behind in case the pawn // has advanced to the 6th rank with the king behind. if (r == RankS.RANK_6 && BitBoard.square_distance(bksq, queeningSq) <= 1 && Types.rank_of(wksq) + tempo <= RankS.RANK_6 && (Types.rank_of(brsq) == RankS.RANK_1 || (0 == tempo && Math.Abs(Types.file_of(brsq) - f) >= 3))) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } if (r >= RankS.RANK_6 && bksq == queeningSq && Types.rank_of(brsq) == RankS.RANK_1 && (0 == tempo || BitBoard.square_distance(wksq, wpsq) >= 2)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7 // and the black rook is behind the pawn. if (wpsq == SquareS.SQ_A7 && wrsq == SquareS.SQ_A8 && (bksq == SquareS.SQ_H7 || bksq == SquareS.SQ_G7) && Types.file_of(brsq) == FileS.FILE_A && (Types.rank_of(brsq) <= RankS.RANK_3 || Types.file_of(wksq) >= FileS.FILE_D || Types.rank_of(wksq) <= RankS.RANK_5)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // If the defending king blocks the pawn and the attacking king is too far // away, it's a draw. if (r <= RankS.RANK_5 && bksq == wpsq + SquareS.DELTA_N && BitBoard.square_distance(wksq, wpsq) - tempo >= 2 && BitBoard.square_distance(wksq, brsq) - tempo >= 2) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // Pawn on the 7th rank supported by the rook from behind usually wins if the // attacking king is closer to the queening square than the defending king, // and the defending king cannot gain tempi by threatening the attacking rook. if (r == RankS.RANK_7 && f != FileS.FILE_A && Types.file_of(wrsq) == f && wrsq != queeningSq && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, queeningSq) - 2 + tempo) && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, wrsq) + tempo)) { return((ScaleFactor)(ScaleFactorS.SCALE_FACTOR_MAX - 2 * BitBoard.square_distance(wksq, queeningSq))); } // Similar to the above, but with the pawn further back if (f != FileS.FILE_A && Types.file_of(wrsq) == f && wrsq < wpsq && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, queeningSq) - 2 + tempo) && (BitBoard.square_distance(wksq, wpsq + SquareS.DELTA_N) < BitBoard.square_distance(bksq, wpsq + SquareS.DELTA_N) - 2 + tempo) && (BitBoard.square_distance(bksq, wrsq) + tempo >= 3 || (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, wrsq) + tempo && (BitBoard.square_distance(wksq, wpsq + SquareS.DELTA_N) < BitBoard.square_distance(bksq, wrsq) + tempo)))) { return((ScaleFactor)(ScaleFactorS.SCALE_FACTOR_MAX - 8 * BitBoard.square_distance(wpsq, queeningSq) - 2 * BitBoard.square_distance(wksq, queeningSq))); } // If the pawn is not far advanced, and the defending king is somewhere in // the pawn's path, it's probably a draw. if (r <= RankS.RANK_4 && bksq > wpsq) { if (Types.file_of(bksq) == Types.file_of(wpsq)) { return(10); } if (Math.Abs(Types.file_of(bksq) - Types.file_of(wpsq)) == 1 && BitBoard.square_distance(wksq, bksq) > 2) { return(24 - 2 * BitBoard.square_distance(wksq, bksq)); } } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KB and one or more pawns vs K. It checks for draws with rook pawns and /// a bishop of the wrong color. If such a draw is detected, SCALE_FACTOR_DRAW /// is returned. If not, the return value is SCALE_FACTOR_NONE, i.e. no scaling /// will be used. public ScaleFactor KBPsK(Position pos) { Debug.Assert(pos.non_pawn_material(strongSide) == ValueS.BishopValueMg); Debug.Assert(pos.count(strongSide, PieceTypeS.PAWN) >= 1); // No assertions about the material of weakSide, because we want draws to // be detected even when the weaker side has some pawns. Bitboard pawns = pos.pieces_color_piecetype(strongSide, PieceTypeS.PAWN); File pawnFile = Types.file_of(pos.list(strongSide, PieceTypeS.PAWN)[0]); // All pawns are on a single rook file ? if ((pawnFile == FileS.FILE_A || pawnFile == FileS.FILE_H) && 0 == (pawns & ~BitBoard.file_bb_file(pawnFile))) { Square bishopSq = pos.list(strongSide, PieceTypeS.BISHOP)[0]; Square queeningSq = Types.relative_square(strongSide, Types.make_square(pawnFile, RankS.RANK_8)); Square kingSq = pos.king_square(weakSide); if (Types.opposite_colors(queeningSq, bishopSq) && BitBoard.square_distance(queeningSq, kingSq) <= 1) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } } // If all the pawns are on the same B or G file, then it's potentially a draw if ((pawnFile == FileS.FILE_B || pawnFile == FileS.FILE_G) && 0 == (pos.pieces_piecetype(PieceTypeS.PAWN) & ~BitBoard.file_bb_file(pawnFile)) && pos.non_pawn_material(weakSide) == 0 && pos.count(weakSide, PieceTypeS.PAWN) >= 1) { // Get weakSide pawn that is closest to the home rank Square weakPawnSq = BitBoard.backmost_sq(weakSide, pos.pieces_color_piecetype(weakSide, PieceTypeS.PAWN)); Square strongKingSq = pos.king_square(strongSide); Square weakKingSq = pos.king_square(weakSide); Square bishopSq = pos.list(strongSide, PieceTypeS.BISHOP)[0]; // There's potential for a draw if our pawn is blocked on the 7th rank, // the bishop cannot attack it or they only have one pawn left if (Types.relative_rank_square(strongSide, weakPawnSq) == RankS.RANK_7 && (pos.pieces_color_piecetype(strongSide, PieceTypeS.PAWN) & BitBoard.SquareBB[(weakPawnSq + Types.pawn_push(weakSide))]) != 0 && (Types.opposite_colors(bishopSq, weakPawnSq) || pos.count(strongSide, PieceTypeS.PAWN) == 1)) { int strongKingDist = BitBoard.square_distance(weakPawnSq, strongKingSq); int weakKingDist = BitBoard.square_distance(weakPawnSq, weakKingSq); // It's a draw if the weak king is on its back two ranks, within 2 // squares of the blocking pawn and the strong king is not // closer. (I think this rule only fails in practically // unreachable positions such as 5k1K/6p1/6P1/8/8/3B4/8/8 w // and positions where qsearch will immediately correct the // problem such as 8/4k1p1/6P1/1K6/3B4/8/8/8 w) if (Types.relative_rank_square(strongSide, weakKingSq) >= RankS.RANK_7 && weakKingDist <= 2 && weakKingDist <= strongKingDist) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } } } return(ScaleFactorS.SCALE_FACTOR_NONE); }
// evaluate_passed_pawns() evaluates the passed pawns of the given color public static Score evaluate_passed_pawns(Position pos, EvalInfo ei, Color Us, bool Trace) { Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE); Bitboard b, squaresToQueen, defendedSquares, unsafeSquares; Score score = ScoreS.SCORE_ZERO; b = ei.pi.passed_pawns(Us); while (b != 0) { Square s = BitBoard.pop_lsb(ref b); Debug.Assert(pos.pawn_passed(Us, s)); int r = (int)(Types.relative_rank_square(Us, s) - RankS.RANK_2); int rr = r * (r - 1); // Base bonus based on rank Value mbonus = (17 * rr), ebonus = (7 * (rr + r + 1)); if (rr != 0) { Square blockSq = s + Types.pawn_push(Us); // Adjust bonus based on kings proximity ebonus += (BitBoard.square_distance(pos.king_square(Them), blockSq) * 5 * rr) - (BitBoard.square_distance(pos.king_square(Us), blockSq) * 2 * rr); // If blockSq is not the queening square then consider also a second push if (Types.relative_rank_square(Us, blockSq) != RankS.RANK_8) { ebonus -= (BitBoard.square_distance(pos.king_square(Us), blockSq + Types.pawn_push(Us)) * rr); } // If the pawn is free to advance, increase bonus if (pos.empty(blockSq)) { squaresToQueen = BitBoard.forward_bb(Us, s); // 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 ((BitBoard.forward_bb(Them, s) & pos.pieces_color_piecetype(Them, PieceTypeS.ROOK, PieceTypeS.QUEEN)) != 0 && (BitBoard.forward_bb(Them, s) & pos.pieces_color_piecetype(Them, PieceTypeS.ROOK, PieceTypeS.QUEEN) & pos.attacks_from_square_piecetype(s, PieceTypeS.ROOK)) != 0) { unsafeSquares = squaresToQueen; } else { unsafeSquares = squaresToQueen & (ei.attackedBy[Them][PieceTypeS.ALL_PIECES] | pos.pieces_color(Them)); } if ((BitBoard.forward_bb(Them, s) & pos.pieces_color_piecetype(Us, PieceTypeS.ROOK, PieceTypeS.QUEEN)) != 0 && (BitBoard.forward_bb(Them, s) & pos.pieces_color_piecetype(Us, PieceTypeS.ROOK, PieceTypeS.QUEEN) & pos.attacks_from_square_piecetype(s, PieceTypeS.ROOK)) != 0) { defendedSquares = squaresToQueen; } else { defendedSquares = squaresToQueen & ei.attackedBy[Us][PieceTypeS.ALL_PIECES]; } // If there aren't any enemy attacks, assign a big bonus. Otherwise // assign a smaller bonus if the block square isn't attacked. int k = 0 == unsafeSquares? 15 : 0 == (unsafeSquares & BitBoard.SquareBB[blockSq]) ? 9 : 0; // If the path to queen is fully defended, assign a big bonus. // Otherwise assign a smaller bonus if the block square is defended. if (defendedSquares == squaresToQueen) { k += 6; } else if ((defendedSquares & BitBoard.SquareBB[blockSq]) != 0) { k += 4; } mbonus += (k * rr); ebonus += (k * rr); } } // rr != 0 if (pos.count(Us, PieceTypeS.PAWN) < pos.count(Them, PieceTypeS.PAWN)) { ebonus += ebonus / 4; } score += Types.make_score(mbonus, ebonus); } if (Trace) { Tracing.terms[Us][TermsS.PASSED] = apply_weight(score, Weights[EvalWeightS.PassedPawns]); } // Add the scores to the middle game and endgame eval return(Eval.apply_weight(score, Weights[EvalWeightS.PassedPawns])); }