/// KQ vs KP. In general, a win for the stronger side, however, there are a few /// important exceptions. Pawn on 7th rank, A,C,F or H file, with king next can /// be a draw, so we scale down to distance between kings only. internal static int Endgame_KQKP(int strongerSide, Position pos) { var weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.QueenValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0); Debug.Assert(pos.non_pawn_material(weakerSide) == 0); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1); Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); Square pawnSq = pos.pieceList[weakerSide][PieceTypeC.PAWN][0]; var result = Constants.QueenValueEndgame - Constants.PawnValueEndgame + DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)]; if ( Utils.square_distance(loserKSq, pawnSq) == 1 && Utils.relative_rank_CS(weakerSide, pawnSq) == RankC.RANK_7) { File f = Utils.file_of(pawnSq); if (f == FileC.FILE_A || f == FileC.FILE_C || f == FileC.FILE_F || f == FileC.FILE_H) { result = DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)]; } } return strongerSide == pos.sideToMove ? result : -result; }
/// K and queen vs K, rook and one or more pawns. It tests for fortress draws with /// a rook on the third rank defended by a pawn. internal static ScaleFactor Endgame_KQKRPs(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.QueenValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.QUEEN) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.ROOK) == 1); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) >= 1); Square kingSq = pos.king_square(weakerSide); if (Utils.relative_rank_CS(weakerSide, kingSq) <= RankC.RANK_2 && Utils.relative_rank_CS(weakerSide, pos.king_square(strongerSide)) >= RankC.RANK_4 && ((pos.pieces_PTC(PieceTypeC.ROOK, weakerSide) & Utils.rank_bb_R(Utils.relative_rank_CR(weakerSide, RankC.RANK_3))) != 0) && ((pos.pieces_PTC(PieceTypeC.PAWN, weakerSide) & Utils.rank_bb_R(Utils.relative_rank_CR(weakerSide, RankC.RANK_2))) != 0) && ((Position.attacks_from_KING(kingSq) & pos.pieces_PTC(PieceTypeC.PAWN, weakerSide)) != 0) ) { Square rsq = pos.pieceList[weakerSide][PieceTypeC.ROOK][0]; if ((Position.attacks_from_PAWN(rsq, strongerSide) & pos.pieces_PTC(PieceTypeC.PAWN, weakerSide)) != 0) return ScaleFactorC.SCALE_FACTOR_DRAW; } return ScaleFactorC.SCALE_FACTOR_NONE; }
internal static bool is_KXK(int Us, Position pos) { var Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE); return pos.non_pawn_material(Them) == ValueC.VALUE_ZERO && pos.piece_count(Them, PieceTypeC.PAWN) == 0 && pos.non_pawn_material(Us) >= Constants.RookValueMidgame; }
internal static bool is_KQKRPs(int Us, Position pos) { var Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE); return pos.piece_count(Us, PieceTypeC.PAWN) == 0 && pos.non_pawn_material(Us) == Constants.QueenValueMidgame && pos.piece_count(Us, PieceTypeC.QUEEN) == 1 && pos.piece_count(Them, PieceTypeC.ROOK) == 1 && pos.piece_count(Them, PieceTypeC.PAWN) >= 1; }
/// 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; }
// evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides, this is quite // conservative and returns a winning score only when we are very sure that the pawn is winning. private static int evaluate_unstoppable_pawns(Position pos, EvalInfo ei) { ulong b, b2, blockers, supporters, queeningPath, candidates; int s, blockSq, queeningSquare; int c, winnerSide, loserSide; bool pathDefended, opposed; int pliesToGo = 0, movesToGo, oppMovesToGo = 0, sacptg, blockersCount, minKingDist, kingptg, d; int pliesToQueenWHITE = 256, pliesToQueenBLACK = 256, pliesToQueenWinner = 256; // Step 1. Hunt for unstoppable passed pawns. If we find at least one, // record how many plies are required for promotion. for (c = ColorC.WHITE; c <= ColorC.BLACK; c++) { // Skip if other side has non-pawn pieces if (pos.non_pawn_material(Utils.flip_C(c)) != 0) { continue; } b = ei.pi.passed_pawns(c); while (b != 0) { s = Utils.pop_lsb(ref b); queeningSquare = Utils.relative_square(c, Utils.make_square(Utils.file_of(s), RankC.RANK_8)); queeningPath = Utils.forward_bb(c, s); // Compute plies to queening and check direct advancement movesToGo = Utils.rank_distance(s, queeningSquare) - (Utils.relative_rank_CS(c, s) == RankC.RANK_2 ? 1 : 0); oppMovesToGo = Utils.square_distance(pos.king_square(Utils.flip_C(c)), queeningSquare) - ((c != pos.sideToMove) ? 1 : 0); pathDefended = ((ei.attackedBy[c][0] & queeningPath) == queeningPath); if (movesToGo >= oppMovesToGo && !pathDefended) { continue; } // Opponent king cannot block because path is defended and position // is not in check. So only friendly pieces can be blockers. Debug.Assert(!pos.in_check()); Debug.Assert((queeningPath & pos.occupied_squares) == (queeningPath & pos.pieces_C(c))); // Add moves needed to free the path from friendly pieces and retest condition movesToGo += Bitcount.popcount_1s_Max15(queeningPath & pos.pieces_C(c)); if (movesToGo >= oppMovesToGo && !pathDefended) { continue; } pliesToGo = 2 * movesToGo - ((c == pos.sideToMove) ? 1 : 0); if (c == ColorC.WHITE) { pliesToQueenWHITE = Math.Min(pliesToQueenWHITE, pliesToGo); } else { pliesToQueenBLACK = Math.Min(pliesToQueenBLACK, pliesToGo); } } } // Step 2. If either side cannot promote at least three plies before the other side then situation // becomes too complex and we give up. Otherwise we determine the possibly "winning side" if (Math.Abs(pliesToQueenWHITE - pliesToQueenBLACK) < 3) { return ScoreC.SCORE_ZERO; } winnerSide = (pliesToQueenWHITE < pliesToQueenBLACK ? ColorC.WHITE : ColorC.BLACK); pliesToQueenWinner = (winnerSide == ColorC.WHITE) ? pliesToQueenWHITE : pliesToQueenBLACK; loserSide = Utils.flip_C(winnerSide); // Step 3. Can the losing side possibly create a new passed pawn and thus prevent the loss? b = candidates = pos.pieces_PTC(PieceTypeC.PAWN, loserSide); while (b != 0) { s = Utils.pop_lsb(ref b); // Compute plies from queening queeningSquare = Utils.relative_square(loserSide, Utils.make_square(Utils.file_of(s), RankC.RANK_8)); movesToGo = Utils.rank_distance(s, queeningSquare) - ((Utils.relative_rank_CS(loserSide, s) == RankC.RANK_2) ? 1 : 0); pliesToGo = 2 * movesToGo - ((loserSide == pos.sideToMove) ? 1 : 0); // Check if (without even considering any obstacles) we're too far away or doubled if ((pliesToQueenWinner + 3 <= pliesToGo) || ((Utils.forward_bb(loserSide, s) & pos.pieces_PTC(PieceTypeC.PAWN, loserSide)) != 0)) { Utils.xor_bit(ref candidates, s); } } // If any candidate is already a passed pawn it _may_ promote in time. We give up. if ((candidates & ei.pi.passed_pawns(loserSide)) != 0) { return ScoreC.SCORE_ZERO; } // Step 4. Check new passed pawn creation through king capturing and pawn sacrifices b = candidates; while (b != 0) { s = Utils.pop_lsb(ref b); sacptg = blockersCount = 0; minKingDist = kingptg = 256; // Compute plies from queening queeningSquare = Utils.relative_square(loserSide, Utils.make_square(Utils.file_of(s), RankC.RANK_8)); movesToGo = Utils.rank_distance(s, queeningSquare) - ((Utils.relative_rank_CS(loserSide, s) == RankC.RANK_2) ? 1 : 0); pliesToGo = 2 * movesToGo - ((loserSide == pos.sideToMove) ? 1 : 0); // Generate list of blocking pawns and supporters supporters = Utils.adjacent_files_bb(Utils.file_of(s)) & candidates; opposed = (Utils.forward_bb(loserSide, s) & pos.pieces_PTC(PieceTypeC.PAWN, winnerSide)) != 0; blockers = Utils.passed_pawn_mask(loserSide, s) & pos.pieces_PTC(PieceTypeC.PAWN, winnerSide); Debug.Assert(blockers != 0); // How many plies does it take to remove all the blocking pawns? while (blockers != 0) { blockSq = Utils.pop_lsb(ref blockers); movesToGo = 256; // Check pawns that can give support to overcome obstacle, for instance // black pawns: a4, b4 white: b2 then pawn in b4 is giving support. if (!opposed) { b2 = supporters & Utils.in_front_bb_CS(winnerSide, blockSq + Utils.pawn_push(winnerSide)); while (b2 != 0) // This while-loop could be replaced with LSB/MSB (depending on color) { d = Utils.square_distance(blockSq, Utils.pop_lsb(ref b2)) - 2; movesToGo = Math.Min(movesToGo, d); } } // Check pawns that can be sacrificed against the blocking pawn b2 = Utils.attack_span_mask(winnerSide, blockSq) & candidates & ~(1UL << s); while (b2 != 0) // This while-loop could be replaced with LSB/MSB (depending on color) { d = Utils.square_distance(blockSq, Utils.pop_lsb(ref b2)) - 2; movesToGo = Math.Min(movesToGo, d); } // If obstacle can be destroyed with an immediate pawn exchange / sacrifice, // it's not a real obstacle and we have nothing to add to pliesToGo. if (movesToGo <= 0) { continue; } // Plies needed to sacrifice against all the blocking pawns sacptg += movesToGo * 2; blockersCount++; // Plies needed for the king to capture all the blocking pawns d = Utils.square_distance(pos.king_square(loserSide), blockSq); minKingDist = Math.Min(minKingDist, d); kingptg = (minKingDist + blockersCount) * 2; } // Check if pawn sacrifice plan _may_ save the day if (pliesToQueenWinner + 3 > pliesToGo + sacptg) { return ScoreC.SCORE_ZERO; } // Check if king capture plan _may_ save the day (contains some false positives) if (pliesToQueenWinner + 3 > pliesToGo + kingptg) { return ScoreC.SCORE_ZERO; } } // Winning pawn is unstoppable and will promote as first, return big score var score = Utils.make_score(0, 0x500 - 0x20 * pliesToQueenWinner); return winnerSide == ColorC.WHITE ? score : -score; }
/// K, bishop and two pawns vs K and bishop. It detects a few basic draws with /// opposite-colored bishops. internal static ScaleFactor Endgame_KBPPKB(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 2); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square wbsq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; Square bbsq = pos.pieceList[weakerSide][PieceTypeC.BISHOP][0]; if (!Utils.opposite_colors(wbsq, bbsq)) return ScaleFactorC.SCALE_FACTOR_NONE; Square ksq = pos.king_square(weakerSide); Square psq1 = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Square psq2 = pos.pieceList[strongerSide][PieceTypeC.PAWN][1]; Rank r1 = Utils.rank_of(psq1); Rank r2 = Utils.rank_of(psq2); Square blockSq1, blockSq2; if (Utils.relative_rank_CS(strongerSide, psq1) > Utils.relative_rank_CS(strongerSide, psq2)) { blockSq1 = psq1 + Utils.pawn_push(strongerSide); blockSq2 = Utils.make_square(Utils.file_of(psq2), Utils.rank_of(psq1)); } else { blockSq1 = psq2 + Utils.pawn_push(strongerSide); blockSq2 = Utils.make_square(Utils.file_of(psq1), Utils.rank_of(psq2)); } switch (Utils.file_distance(psq1, psq2)) { case 0: // Both pawns are on the same file. Easy draw if defender firmly controls // some square in the frontmost pawn's path. if (Utils.file_of(ksq) == Utils.file_of(blockSq1) && Utils.relative_rank_CS(strongerSide, ksq) >= Utils.relative_rank_CS(strongerSide, blockSq1) && Utils.opposite_colors(ksq, wbsq)) return ScaleFactorC.SCALE_FACTOR_DRAW; else return ScaleFactorC.SCALE_FACTOR_NONE; case 1: // Pawns on adjacent files. Draw if defender firmly controls the square // in front of the frontmost pawn's path, and the square diagonally behind // this square on the file of the other pawn. if (ksq == blockSq1 && Utils.opposite_colors(ksq, wbsq) && (bbsq == blockSq2 || (((pos.attacks_from_BISHOP(blockSq2) & pos.pieces_PTC(PieceTypeC.BISHOP, weakerSide))) != 0) || Math.Abs(r1 - r2) >= 2)) return ScaleFactorC.SCALE_FACTOR_DRAW; else if (ksq == blockSq2 && Utils.opposite_colors(ksq, wbsq) && (bbsq == blockSq1 || (((pos.attacks_from_BISHOP(blockSq1) & pos.pieces_PTC(PieceTypeC.BISHOP, weakerSide)))) != 0)) return ScaleFactorC.SCALE_FACTOR_DRAW; else return ScaleFactorC.SCALE_FACTOR_NONE; default: // The pawns are not on the same file or adjacent files. No scaling. return ScaleFactorC.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. internal static Value Endgame_KRKP(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) == 0); Debug.Assert(pos.non_pawn_material(weakerSide) == 0); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1); Square wksq, wrsq, bksq, bpsq; int tempo = (pos.sideToMove == strongerSide ? 1 : 0); wksq = pos.king_square(strongerSide); wrsq = pos.pieceList[strongerSide][PieceTypeC.ROOK][0]; bksq = pos.king_square(weakerSide); bpsq = pos.pieceList[weakerSide][PieceTypeC.PAWN][0]; if (strongerSide == ColorC.BLACK) { wksq = Utils.flip_S(wksq); wrsq = Utils.flip_S(wrsq); bksq = Utils.flip_S(bksq); bpsq = Utils.flip_S(bpsq); } Square queeningSq = Utils.make_square(Utils.file_of(bpsq), RankC.RANK_1); Value result; // If the stronger side's king is in front of the pawn, it's a win if (wksq < bpsq && Utils.file_of(wksq) == Utils.file_of(bpsq)) result = Constants.RookValueEndgame - (Utils.square_distance(wksq, bpsq)); // If the weaker side's king is too far from the pawn and the rook, // it's a win else if (Utils.square_distance(bksq, bpsq) - (tempo ^ 1) >= 3 && Utils.square_distance(bksq, wrsq) >= 3) result = Constants.RookValueEndgame - (Utils.square_distance(wksq, bpsq)); // If the pawn is far advanced and supported by the defending king, // the position is drawish else if (Utils.rank_of(bksq) <= RankC.RANK_3 && Utils.square_distance(bksq, bpsq) == 1 && Utils.rank_of(wksq) >= RankC.RANK_4 && Utils.square_distance(wksq, bpsq) - tempo > 2) result = (80 - Utils.square_distance(wksq, bpsq) * 8); else result = (200) - (Utils.square_distance(wksq, bpsq + SquareC.DELTA_S) * 8) + (Utils.square_distance(bksq, bpsq + SquareC.DELTA_S) * 8) + (Utils.square_distance(bpsq, queeningSq) * 8); return strongerSide == pos.sideToMove ? result : -result; }
/// K, bishop and a pawn vs K and a bishop. 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. internal static ScaleFactor Endgame_KBPKB(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 1); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square pawnSq = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Square strongerBishopSq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; Square weakerBishopSq = pos.pieceList[weakerSide][PieceTypeC.BISHOP][0]; Square weakerKingSq = pos.king_square(weakerSide); // Case 1: Defending king blocks the pawn, and cannot be driven away if (Utils.file_of(weakerKingSq) == Utils.file_of(pawnSq) && Utils.relative_rank_CS(strongerSide, pawnSq) < Utils.relative_rank_CS(strongerSide, weakerKingSq) && (Utils.opposite_colors(weakerKingSq, strongerBishopSq) || Utils.relative_rank_CS(strongerSide, weakerKingSq) <= RankC.RANK_6)) return ScaleFactorC.SCALE_FACTOR_DRAW; // Case 2: Opposite colored bishops if (Utils.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 (Utils.relative_rank_CS(strongerSide, pawnSq) <= RankC.RANK_5) return ScaleFactorC.SCALE_FACTOR_DRAW; else { Bitboard path = Utils.forward_bb(strongerSide, pawnSq); if ((path & pos.pieces_PTC(PieceTypeC.KING, weakerSide)) != 0) return ScaleFactorC.SCALE_FACTOR_DRAW; if (((pos.attacks_from_BISHOP(weakerBishopSq) & path) != 0) && Utils.square_distance(weakerBishopSq, pawnSq) >= 3) return ScaleFactorC.SCALE_FACTOR_DRAW; } } return ScaleFactorC.SCALE_FACTOR_NONE; }
/// K, bishop and a pawn vs K and knight. There is a single rule: 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. internal static ScaleFactor Endgame_KBPKN(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 1); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.KnightValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.KNIGHT) == 1); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square pawnSq = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Square strongerBishopSq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; Square weakerKingSq = pos.king_square(weakerSide); if (Utils.file_of(weakerKingSq) == Utils.file_of(pawnSq) && Utils.relative_rank_CS(strongerSide, pawnSq) < Utils.relative_rank_CS(strongerSide, weakerKingSq) && (Utils.opposite_colors(weakerKingSq, strongerBishopSq) || Utils.relative_rank_CS(strongerSide, weakerKingSq) <= RankC.RANK_6)) return ScaleFactorC.SCALE_FACTOR_DRAW; return ScaleFactorC.SCALE_FACTOR_NONE; }
/// 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. internal static Value Endgame_KBNK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == ValueC.VALUE_ZERO); Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.KnightValueMidgame + Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.KNIGHT) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0); Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); Square bishopSquare = pos.pieceList[strongerSide][PieceTypeC.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 // mirror the kings so to drive enemy toward corners A8 or H1. if (Utils.opposite_colors(bishopSquare, SquareC.SQ_A1)) { winnerKSq = Utils.mirror(winnerKSq); loserKSq = Utils.mirror(loserKSq); } Value result = ValueC.VALUE_KNOWN_WIN + DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)] + KBNKMateTable[loserKSq]; return strongerSide == pos.sideToMove ? result : -result; }
internal static Value Endgame_KBBKN(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 2); Debug.Assert(pos.non_pawn_material(strongerSide) == 2 * Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.KNIGHT) == 1); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.KnightValueMidgame); Debug.Assert(pos.pieces_PT(PieceTypeC.PAWN) == 0); Value result = Constants.BishopValueEndgame; Square wksq = pos.king_square(strongerSide); Square bksq = pos.king_square(weakerSide); Square nsq = pos.pieceList[weakerSide][PieceTypeC.KNIGHT][0]; // Bonus for attacking king close to defending king result += (DistanceBonus[Utils.square_distance(wksq, bksq)]); // Bonus for driving the defending king and knight apart result += (Utils.square_distance(bksq, nsq) * 32); // Bonus for restricting the knight's mobility result += ((8 - Bitcount.popcount_1s_Max15(Position.attacks_from_KNIGHT(nsq))) * 8); return strongerSide == pos.sideToMove ? result : -result; }
// search<>() is the main search function for both PV and non-PV nodes and for // normal and SplitPoint nodes. When called just after a split point the search // is simpler because we have already probed the hash table, done a null move // search, and searched the first move before splitting, we don't have to repeat // all this work again. We also don't need to store anything to the hash table // here: This is taken care of after we return from the split point. internal static int search(int NT, Position pos, Stack[] ss, int ssPos, int alpha, int beta, int depth) { var PvNode = (NT == NodeTypeC.PV || NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointRoot); var SpNode = (NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointNonPV || NT == NodeTypeC.SplitPointRoot); var RootNode = (NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointRoot); Debug.Assert(alpha >= -ValueC.VALUE_INFINITE && alpha < beta && beta <= ValueC.VALUE_INFINITE); Debug.Assert((PvNode || alpha == beta - 1)); Debug.Assert(depth > DepthC.DEPTH_ZERO); var ms = MovesSearchedBroker.GetObject(); var movesSearched = ms.movesSearched; StateInfo st = null; var tte = TT.StaticEntry; var tteHasValue = false; uint ttePos = 0; ulong posKey = 0; int ttMove, move, excludedMove, bestMove, threatMove; int ext, newDepth; int bestValue, value, ttValue; int eval = 0, nullValue, futilityValue; bool inCheck, givesCheck, pvMove, singularExtensionNode; bool captureOrPromotion, dangerous, doFullDepthSearch; int moveCount = 0, playedMoveCount = 0; SplitPoint sp = null; // Step 1. Initialize node var thisThread = pos.this_thread(); //var threatExtension = false; inCheck = pos.in_check(); if (SpNode) { sp = ss[ssPos].sp; bestMove = sp.bestMove; threatMove = sp.threatMove; bestValue = sp.bestValue; ttMove = excludedMove = MoveC.MOVE_NONE; ttValue = ValueC.VALUE_NONE; Debug.Assert(sp.bestValue > -ValueC.VALUE_INFINITE && sp.moveCount > 0); goto split_point_start; } bestValue = -ValueC.VALUE_INFINITE; ss[ssPos].currentMove = threatMove = ss[ssPos + 1].excludedMove = bestMove = MoveC.MOVE_NONE; ss[ssPos].ply = ss[ssPos - 1].ply + 1; ss[ssPos + 1].skipNullMove = 0; ss[ssPos + 1].reduction = DepthC.DEPTH_ZERO; ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = MoveC.MOVE_NONE; // Used to send selDepth info to GUI if (PvNode && thisThread.maxPly < ss[ssPos].ply) { thisThread.maxPly = ss[ssPos].ply; } if (!RootNode) { // Step 2. Check for aborted search and immediate draw if ((SignalsStop || pos.is_draw(false) || ss[ssPos].ply > Constants.MAX_PLY)) { MovesSearchedBroker.Free(); return DrawValue[pos.sideToMove]; } // Step 3. Mate distance pruning. Even if we mate at the next move our score // would be at best mate_in(ss->ply+1), but if alpha is already bigger because // a shorter mate was found upward in the tree then there is no need to search // further, we will never beat current alpha. Same logic but with reversed signs // applies also in the opposite condition of being mated instead of giving mate, // in this case return a fail-high score. alpha = Math.Max(Utils.mated_in(ss[ssPos].ply), alpha); beta = Math.Min(Utils.mate_in(ss[ssPos].ply + 1), beta); if (alpha >= beta) { MovesSearchedBroker.Free(); return alpha; } } // Step 4. Transposition table lookup // We don't want the score of a partial search to overwrite a previous full search // TT value, so we use a different position key in case of an excluded move. excludedMove = ss[ssPos].excludedMove; posKey = (excludedMove != 0) ? pos.exclusion_key() : pos.key(); tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tteHasValue ? tte.move() : MoveC.MOVE_NONE; ttValue = tteHasValue ? value_from_tt(tte.value(), ss[ssPos].ply) : ValueC.VALUE_NONE; // At PV nodes we check for exact scores, while at non-PV nodes we check for // a fail high/low. Biggest advantage at probing at PV nodes is to have a // smooth experience in analysis mode. We don't probe at Root nodes otherwise // we should also update RootMoveList to avoid bogus output. if (!RootNode && tteHasValue && tte.depth() >= depth && ttValue != ValueC.VALUE_NONE // Only in case of TT access race && (PvNode ? tte.type() == Bound.BOUND_EXACT : ttValue >= beta ? ((tte.type() & Bound.BOUND_LOWER) != 0 ) : ((tte.type() & Bound.BOUND_UPPER) != 0))) { Debug.Assert(ttValue != ValueC.VALUE_NONE); // Due to depth > DEPTH_NONE TT.table[ttePos].set_generation(TT.generation); ss[ssPos].currentMove = ttMove; // Can be MOVE_NONE if (ttValue >= beta && (ttMove != 0) && !pos.is_capture_or_promotion(ttMove) && ttMove != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = ttMove; } MovesSearchedBroker.Free(); return ttValue; } // Step 5. Evaluate the position statically and update parentSplitPoint's gain statistics if (inCheck) { ss[ssPos].staticEval = ss[ssPos].evalMargin = eval = ValueC.VALUE_NONE; } else if (tteHasValue) { // Never assume anything on values stored in TT if ((ss[ssPos].staticEval = eval = tte.eval_value()) == ValueC.VALUE_NONE || (ss[ssPos].evalMargin = tte.eval_margin()) == ValueC.VALUE_NONE) { eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin); } // Can ttValue be used as a better position evaluation? if (ttValue != ValueC.VALUE_NONE) { if ((((tte.type() & Bound.BOUND_LOWER) != 0) && ttValue > eval) || (((tte.type() & Bound.BOUND_UPPER) != 0) && ttValue < eval)) { eval = ttValue; } } } else { eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin); TT.store( posKey, ValueC.VALUE_NONE, Bound.BOUND_NONE, DepthC.DEPTH_NONE, MoveC.MOVE_NONE, ss[ssPos].staticEval, ss[ssPos].evalMargin); } // Update gain for the parentSplitPoint non-capture move given the static position // evaluation before and after the move. if ((move = ss[ssPos - 1].currentMove) != MoveC.MOVE_NULL && ss[ssPos - 1].staticEval != ValueC.VALUE_NONE && ss[ssPos].staticEval != ValueC.VALUE_NONE && (pos.captured_piece_type() == 0) && Utils.type_of_move(move) == MoveTypeC.NORMAL) { var to = Utils.to_sq(move); H.update_gain(pos.piece_on(to), to, -ss[ssPos - 1].staticEval - ss[ssPos].staticEval); } // Step 6. Razoring (is omitted in PV nodes) if (!PvNode && !inCheck && depth < 4 * DepthC.ONE_PLY && eval + razor_margin(depth) < beta && ttMove == MoveC.MOVE_NONE && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && !pos.pawn_on_7th(pos.sideToMove)) { var rbeta = beta - razor_margin(depth); var v = qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos, rbeta - 1, rbeta, DepthC.DEPTH_ZERO); if (v < rbeta) { // Logically we should return (v + razor_margin(depth)), but // surprisingly this did slightly weaker in tests. MovesSearchedBroker.Free(); return v; } } // Step 7. Static null move pruning (is omitted in PV nodes) // We're betting that the opponent doesn't have a move that will reduce // the score by more than futility_margin(depth) if we do a null move. if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth < 4 * DepthC.ONE_PLY && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && eval - FutilityMargins[depth][0] >= beta && (pos.non_pawn_material(pos.sideToMove) != 0)) { MovesSearchedBroker.Free(); return eval - FutilityMargins[depth][0]; } // Step 8. Null move search with verification search (is omitted in PV nodes) if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth > DepthC.ONE_PLY && eval >= beta && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && (pos.non_pawn_material(pos.sideToMove) != 0)) { ss[ssPos].currentMove = MoveC.MOVE_NULL; // Null move dynamic reduction based on depth Depth R = 3 * DepthC.ONE_PLY + depth / 4; // Null move dynamic reduction based on value if (eval - Constants.PawnValueMidgame > beta) { R += DepthC.ONE_PLY; } if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_null_move(st); ss[ssPos + 1].skipNullMove = 1; nullValue = depth - R < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, depth - R); ss[ssPos + 1].skipNullMove = 0; pos.undo_null_move(st); if (nullValue >= beta) { // Do not return unproven mate scores if (nullValue >= ValueC.VALUE_MATE_IN_MAX_PLY) { nullValue = beta; } if (depth < 6 * DepthC.ONE_PLY) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } // Do verification search at high depths ss[ssPos].skipNullMove = 1; var v = search(NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, depth - R); ss[ssPos].skipNullMove = 0; if (v >= beta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } } else { // The null move failed low, which means that we may be faced with // some kind of threat. If the previous move was reduced, check if // the move that refuted the null move was somehow connected to the // the move that refuted the null move was somehow connected to the // move which was reduced. If a connection is found extend moves that // defend against threat. threatMove = ss[ssPos + 1].currentMove; if (depth < 5 * DepthC.ONE_PLY && (ss[ssPos - 1].reduction != 0) && threatMove != MoveC.MOVE_NONE && allows(pos, ss[ssPos - 1].currentMove, threatMove)) { //threatExtension = true; if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return beta - 1; } } } // Step 9. ProbCut (is omitted in PV nodes) // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type]) // and a reduced search returns a value much above beta, we can (almost) safely // prune the previous move. if (!PvNode && !inCheck && excludedMove == MoveC.MOVE_NONE && depth >= 4 * DepthC.ONE_PLY + DepthC.ONE_PLY && (ss[ssPos].skipNullMove == 0) && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY) { var rbeta = beta + 200; var rdepth = depth - DepthC.ONE_PLY - 3 * DepthC.ONE_PLY; Debug.Assert(rdepth >= DepthC.ONE_PLY); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NONE); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NULL); var mp2 = MovePickerBroker.GetObject(); mp2.MovePickerC(pos, ttMove, H, pos.captured_piece_type()); var ci2 = CheckInfoBroker.GetObject(); ci2.CreateCheckInfo(pos); while ((move = mp2.next_move()) != MoveC.MOVE_NONE) { if (pos.pl_move_is_legal(move, ci2.pinned)) { ss[ssPos].currentMove = move; if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci2, pos.move_gives_check(move, ci2)); value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth); pos.undo_move(move); if (value >= rbeta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); MovesSearchedBroker.Free(); return value; } } } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); } // Step 10. Internal iterative deepening if (ttMove == MoveC.MOVE_NONE && depth >= (PvNode ? 5 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY) && (PvNode || (!inCheck && ss[ssPos].staticEval + 256 >= beta))) { var d = (PvNode ? depth - 2 * DepthC.ONE_PLY : depth / 2); ss[ssPos].skipNullMove = 1; search(PvNode ? NodeTypeC.PV : NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, d); ss[ssPos].skipNullMove = 0; tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = (tteHasValue) ? tte.move() : MoveC.MOVE_NONE; } else { // Re-read (needed as TTEntry is a struct in the port) if ((tteHasValue) && (TT.table[ttePos].key == tte.key)) { tte = TT.table[ttePos]; } } split_point_start: // At split points actual search starts from here var mp = MovePickerBroker.GetObject(); mp.MovePickerC( pos, ttMove, depth, H, ss[ssPos], PvNode ? -ValueC.VALUE_INFINITE : beta, SpNode ? ss[ssPos].sp.movePicker : null); var ci = CheckInfoBroker.GetObject(); ci.CreateCheckInfo(pos); value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc singularExtensionNode = !RootNode && !SpNode && depth >= (PvNode ? 6 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY) && ttMove != MoveC.MOVE_NONE && (excludedMove == 0) // Recursive singular search is not allowed && ((tte.type() & Bound.BOUND_LOWER) != 0) // FIXME: uninitialized! && tte.depth() >= depth - 3 * DepthC.ONE_PLY; // Step 11. Loop through moves // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs while ((move = mp.next_move()) != MoveC.MOVE_NONE && !thisThread.cutoff_occurred() && !SignalsStop) { Debug.Assert(Utils.is_ok_M(move)); if (move == excludedMove) { continue; } // At root obey the "searchmoves" option and skip moves not listed in Root // Move List, as a consequence any illegal move is also skipped. In MultiPV // mode we also skip PV moves which have been already searched. // If we find none, it means !count if (RootNode && (find(RootMoves, PVIdx, RootMoves.Count, move) == -1)) { continue; } if (SpNode) { // Shared counter cannot be decremented later if move turns out to be illegal if (!pos.pl_move_is_legal(move, ci.pinned)) { continue; } moveCount = ++sp.moveCount; ThreadHelper.lock_release(sp.Lock); } else { moveCount++; } if (RootNode) { SignalsFirstRootMove = (moveCount == 1); if (thisThread == Threads.main_thread() && SearchTime.ElapsedMilliseconds > 3000) { Plug.Write("info depth "); Plug.Write((depth / DepthC.ONE_PLY).ToString()); Plug.Write(" currmove "); Plug.Write(Utils.move_to_uci(move, pos.chess960)); Plug.Write(" nodes "); Plug.Write(pos.nodes.ToString()); Plug.Write(" currmovenumber "); Plug.Write((moveCount + PVIdx).ToString()); Plug.Write(Constants.endl); } } ext = DepthC.DEPTH_ZERO; captureOrPromotion = pos.is_capture_or_promotion(move); givesCheck = pos.move_gives_check(move, ci); dangerous = givesCheck || pos.is_passed_pawn_push(move) || Utils.type_of_move(move) == MoveTypeC.CASTLING || (captureOrPromotion // Entering a pawn endgame? && Utils.type_of(pos.piece_on(Utils.to_sq(move))) != PieceTypeC.PAWN && Utils.type_of_move(move) == MoveTypeC.NORMAL && (pos.non_pawn_material(ColorC.WHITE) + pos.non_pawn_material(ColorC.BLACK) - Position.PieceValue[PhaseC.MG][pos.piece_on(Utils.to_sq(move))] == ValueC.VALUE_ZERO)); // Step 12. Extend checks and, in PV nodes, also dangerous moves if (PvNode && dangerous) { ext = DepthC.ONE_PLY; } // else if (threatExtension && refutes(pos, move, threatMove)) // { // ext = DepthC.ONE_PLY; // } else if (givesCheck && pos.see(move, true) >= 0) { ext = DepthC.ONE_PLY / 2; } // Singular extension search. If all moves but one fail low on a search of // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move // is singular and should be extended. To verify this we do a reduced search // on all the other moves but the ttMove, if result is lower than ttValue minus // a margin then we extend ttreMove. if (singularExtensionNode && move == ttMove && (ext == 0) && pos.pl_move_is_legal(move, ci.pinned)) { Debug.Assert(ttValue != ValueC.VALUE_NONE); var rBeta = ttValue - depth; ss[ssPos].excludedMove = move; ss[ssPos].skipNullMove = 1; value = search(NodeTypeC.NonPV, pos, ss, ssPos, rBeta - 1, rBeta, depth / 2); ss[ssPos].skipNullMove = 0; ss[ssPos].excludedMove = MoveC.MOVE_NONE; if (value < rBeta) { ext = DepthC.ONE_PLY; } } // Update current move (this must be done after singular extension search) newDepth = depth - DepthC.ONE_PLY + ext; // Step 13. Futility pruning (is omitted in PV nodes) if (!PvNode && !captureOrPromotion && !inCheck && !dangerous && move != ttMove && (bestValue > ValueC.VALUE_MATED_IN_MAX_PLY || (bestValue == -ValueC.VALUE_INFINITE && alpha > ValueC.VALUE_MATED_IN_MAX_PLY))) { // Move count based pruning if (depth < 16 * DepthC.ONE_PLY && moveCount >= FutilityMoveCounts[depth] && ((threatMove == 0) || !refutes(pos, move, threatMove))) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } // Value based pruning // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth, // but fixing this made program slightly weaker. var predictedDepth = newDepth - reduction(PvNode, depth, moveCount); futilityValue = ss[ssPos].staticEval + ss[ssPos].evalMargin + futility_margin(predictedDepth, moveCount) + H.gain(pos.piece_moved(move), Utils.to_sq(move)); if (futilityValue < beta) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } // Prune moves with negative SEE at low depths if (predictedDepth < 2 * DepthC.ONE_PLY && pos.see(move, true) < 0) { if (SpNode) { ThreadHelper.lock_grab(sp.Lock); } continue; } } // Check for legality only before to do the move if (!RootNode && !SpNode && !pos.pl_move_is_legal(move, ci.pinned)) { moveCount--; continue; } pvMove = (PvNode && moveCount == 1); ss[ssPos].currentMove = move; if (!SpNode && !captureOrPromotion && playedMoveCount < 64) { movesSearched[playedMoveCount++] = move; } // Step 14. Make the move if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci, givesCheck); // Step 15. Reduced depth search (LMR). If the move fails high will be // re-searched at full depth. if (depth > 3 * DepthC.ONE_PLY && !pvMove && !captureOrPromotion && !dangerous && move != ttMove && move != ss[ssPos].killers0 && move != ss[ssPos].killers1) { ss[ssPos].reduction = reduction(PvNode, depth, moveCount); var d = Math.Max(newDepth - ss[ssPos].reduction, DepthC.ONE_PLY); alpha = SpNode ? sp.alpha : alpha; value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, d); doFullDepthSearch = (value > alpha && ss[ssPos].reduction != DepthC.DEPTH_ZERO); ss[ssPos].reduction = DepthC.DEPTH_ZERO; } else { doFullDepthSearch = !pvMove; } // Step 16. Full depth search, when LMR is skipped or fails high if (doFullDepthSearch) { alpha = SpNode ? sp.alpha : alpha; value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, givesCheck, pos, ss, ssPos + 1, -(alpha + 1), -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth); } // Only for PV nodes do a full PV search on the first move or after a fail // high, in the latter case search only if value < beta, otherwise let the // parentSplitPoint node to fail low with value <= alpha and to try another move. if (PvNode && (pvMove || (value > alpha && (RootNode || value < beta)))) { value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.PV, givesCheck, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, newDepth); } // Step 17. Undo move pos.undo_move(move); Debug.Assert(value > -ValueC.VALUE_INFINITE && value < ValueC.VALUE_INFINITE); // Step 18. Check for new best move if (SpNode) { ThreadHelper.lock_grab(sp.Lock); bestValue = sp.bestValue; alpha = sp.alpha; } // Finished searching the move. If Signals.stop is true, the search // was aborted because the user interrupted the search or because we // ran out of time. In this case, the return value of the search cannot // be trusted, and we don't update the best move and/or PV. if (SignalsStop || thisThread.cutoff_occurred()) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return value; // To avoid returning VALUE_INFINITE } // Finished searching the move. If Signals.stop is true, the search // was aborted because the user interrupted the search or because we // ran out of time. In this case, the return value of the search cannot // be trusted, and we don't update the best move and/or PV. if (RootNode) { var rmPos = find(RootMoves, 0, RootMoves.Count, move); // PV move or new best move ? if (pvMove || value > alpha) { RootMoves[rmPos].score = value; RootMoves[rmPos].extract_pv_from_tt(pos); // We record how often the best move has been changed in each // iteration. This information is used for time management: When // the best move changes frequently, we allocate some more time. if (!pvMove) { BestMoveChanges++; } } else { // All other moves but the PV are set to the lowest value, this // is not a problem when sorting becuase sort is stable and move // position in the list is preserved, just the PV is pushed up. RootMoves[rmPos].score = -ValueC.VALUE_INFINITE; } } if (value > bestValue) { bestValue = value; if (SpNode) sp.bestValue = value; if (value > alpha) { bestMove = move; if (SpNode) sp.bestMove = move; if (PvNode && value < beta) { alpha = value; // Update alpha here! Always alpha < beta if (SpNode) sp.alpha = value; } else { Debug.Assert(value >= beta); // Fail high if (SpNode) sp.cutoff = true; break; } } } // Step 19. Check for split if (!SpNode && depth >= Threads.minimumSplitDepth && Threads.available_slave(thisThread) != null && thisThread.splitPointsSize < Constants.MAX_SPLITPOINTS_PER_THREAD) { Debug.Assert(bestValue < beta); Threads.split( Constants.FakeSplit, pos, ss, ssPos, alpha, beta, ref bestValue, ref bestMove, depth, threatMove, moveCount, mp, NT); if (bestValue >= beta) { break; } } } // Step 20. Check for mate and stalemate // All legal moves have been searched and if there are no legal moves, it // must be mate or stalemate. Note that we can have a false positive in // case of Signals.stop or thread.cutoff_occurred() are set, but this is // harmless because return value is discarded anyhow in the parentSplitPoint nodes. // If we are in a singular extension search then return a fail low score. // A split node has at least one move, the one tried before to be splitted. if (!SpNode && moveCount == 0) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return (excludedMove != 0) ? alpha : inCheck ? Utils.mated_in(ss[ssPos].ply) : DrawValue[pos.sideToMove]; } // If we have pruned all the moves without searching return a fail-low score if (bestValue == -ValueC.VALUE_INFINITE) { Debug.Assert(playedMoveCount == 0); bestValue = alpha; } if (bestValue >= beta) // Failed high { TT.store( posKey, value_to_tt(bestValue, ss[ssPos].ply), Bound.BOUND_LOWER, depth, bestMove, ss[ssPos].staticEval, ss[ssPos].evalMargin); if (!pos.is_capture_or_promotion(bestMove) && !inCheck) { if (bestMove != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = bestMove; } // Increase history value of the cut-off move var bonus = (depth * depth); H.add(pos.piece_moved(bestMove), Utils.to_sq(bestMove), bonus); // Decrease history of all the other played non-capture moves for (var i = 0; i < playedMoveCount - 1; i++) { var m = movesSearched[i]; H.add(pos.piece_moved(m), Utils.to_sq(m), -bonus); } } } else // Failed low or PV search { TT.store(posKey, value_to_tt(bestValue, ss[ssPos].ply), PvNode && bestMove != MoveC.MOVE_NONE ? Bound.BOUND_EXACT : Bound.BOUND_UPPER, depth, bestMove, ss[ssPos].staticEval, ss[ssPos].evalMargin); } Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && bestValue < ValueC.VALUE_INFINITE); if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return bestValue; }
/// KR vs KB. This is very simple, and always returns drawish scores. The /// score is slightly bigger when the defending king is close to the edge. internal static Value Endgame_KRKB(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) == 0); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.BISHOP) == 1); Value result = (MateTable[pos.king_square(weakerSide)]); return strongerSide == pos.sideToMove ? result : -result; }
/// K, bishop 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. internal static ScaleFactor Endgame_KBPsK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) >= 1); // No Debug.Assertions about the material of weakerSide, because we want draws to // be detected even when the weaker side has some pawns. Bitboard pawns = pos.pieces_PTC(PieceTypeC.PAWN, strongerSide); File pawnFile = Utils.file_of(pos.pieceList[strongerSide][PieceTypeC.PAWN][0]); // All pawns are on a single rook file ? if ((pawnFile == FileC.FILE_A || pawnFile == FileC.FILE_H) && (((pawns & ~Utils.file_bb_F(pawnFile))) == 0)) { Square bishopSq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; Square queeningSq = Utils.relative_square(strongerSide, Utils.make_square(pawnFile, RankC.RANK_8)); Square kingSq = pos.king_square(weakerSide); if (Utils.opposite_colors(queeningSq, bishopSq) && Math.Abs(Utils.file_of(kingSq) - pawnFile) <= 1) { // The bishop has the wrong color, and the defending king is on the // file of the pawn(s) or the adjacent file. Find the rank of the // frontmost pawn. Rank rank; if (strongerSide == ColorC.WHITE) { for (rank = RankC.RANK_7; (((Utils.rank_bb_R(rank) & pawns)) == 0); rank--) { } Debug.Assert(rank >= RankC.RANK_2 && rank <= RankC.RANK_7); } else { for (rank = RankC.RANK_2; (((Utils.rank_bb_R(rank) & pawns)) == 0); rank++) { } rank = (rank ^ 7); // HACK to get the relative rank Debug.Assert(rank >= RankC.RANK_2 && rank <= RankC.RANK_7); } // If the defending king has distance 1 to the promotion square or // is placed somewhere in front of the pawn, it's a draw. if (Utils.square_distance(kingSq, queeningSq) <= 1 || Utils.relative_rank_CS(strongerSide, kingSq) >= rank) return ScaleFactorC.SCALE_FACTOR_DRAW; } } return ScaleFactorC.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. internal static Value Endgame_KRKN(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) == 0); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.KnightValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.KNIGHT) == 1); Square bksq = pos.king_square(weakerSide); Square bnsq = pos.pieceList[weakerSide][PieceTypeC.KNIGHT][0]; Value result = (MateTable[bksq] + penalty[Utils.square_distance(bksq, bnsq)]); return strongerSide == pos.sideToMove ? result : -result; }
/// K, knight and a pawn 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. internal static ScaleFactor Endgame_KNPK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.KnightValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.KNIGHT) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 1); Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square pawnSq = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Square weakerKingSq = pos.king_square(weakerSide); if (pawnSq == Utils.relative_square(strongerSide, SquareC.SQ_A7) && Utils.square_distance(weakerKingSq, Utils.relative_square(strongerSide, SquareC.SQ_A8)) <= 1) return ScaleFactorC.SCALE_FACTOR_DRAW; if (pawnSq == Utils.relative_square(strongerSide, SquareC.SQ_H7) && Utils.square_distance(weakerKingSq, Utils.relative_square(strongerSide, SquareC.SQ_H8)) <= 1) return ScaleFactorC.SCALE_FACTOR_DRAW; return ScaleFactorC.SCALE_FACTOR_NONE; }
/// K, rook and one pawn vs K and a rook. 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 not very pretty. internal static ScaleFactor Endgame_KRPKR(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) == 1); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.RookValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square wksq = pos.king_square(strongerSide); Square wrsq = pos.pieceList[strongerSide][PieceTypeC.ROOK][0]; Square wpsq = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Square bksq = pos.king_square(weakerSide); Square brsq = pos.pieceList[weakerSide][PieceTypeC.ROOK][0]; // Orient the board in such a way that the stronger side is white, and the // pawn is on the left half of the board. if (strongerSide == ColorC.BLACK) { wksq = Utils.flip_S(wksq); wrsq = Utils.flip_S(wrsq); wpsq = Utils.flip_S(wpsq); bksq = Utils.flip_S(bksq); brsq = Utils.flip_S(brsq); } if (Utils.file_of(wpsq) > FileC.FILE_D) { wksq = Utils.mirror(wksq); wrsq = Utils.mirror(wrsq); wpsq = Utils.mirror(wpsq); bksq = Utils.mirror(bksq); brsq = Utils.mirror(brsq); } File f = Utils.file_of(wpsq); Rank r = Utils.rank_of(wpsq); Square queeningSq = Utils.make_square(f, RankC.RANK_8); int tempo = (pos.sideToMove == strongerSide ? 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 <= RankC.RANK_5 && Utils.square_distance(bksq, queeningSq) <= 1 && wksq <= SquareC.SQ_H5 && (Utils.rank_of(brsq) == RankC.RANK_6 || (r <= RankC.RANK_3 && Utils.rank_of(wrsq) != RankC.RANK_6))) return ScaleFactorC.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 == RankC.RANK_6 && Utils.square_distance(bksq, queeningSq) <= 1 && Utils.rank_of(wksq) + tempo <= RankC.RANK_6 && (Utils.rank_of(brsq) == RankC.RANK_1 || ((tempo == 0) && Math.Abs(Utils.file_of(brsq) - f) >= 3))) return ScaleFactorC.SCALE_FACTOR_DRAW; if (r >= RankC.RANK_6 && bksq == queeningSq && Utils.rank_of(brsq) == RankC.RANK_1 && ((tempo == 0) || Utils.square_distance(wksq, wpsq) >= 2)) return ScaleFactorC.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 == SquareC.SQ_A7 && wrsq == SquareC.SQ_A8 && (bksq == SquareC.SQ_H7 || bksq == SquareC.SQ_G7) && Utils.file_of(brsq) == FileC.FILE_A && (Utils.rank_of(brsq) <= RankC.RANK_3 || Utils.file_of(wksq) >= FileC.FILE_D || Utils.rank_of(wksq) <= RankC.RANK_5)) return ScaleFactorC.SCALE_FACTOR_DRAW; // If the defending king blocks the pawn and the attacking king is too far // away, it's a draw. if (r <= RankC.RANK_5 && bksq == wpsq + SquareC.DELTA_N && Utils.square_distance(wksq, wpsq) - tempo >= 2 && Utils.square_distance(wksq, brsq) - tempo >= 2) return ScaleFactorC.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 == RankC.RANK_7 && f != FileC.FILE_A && Utils.file_of(wrsq) == f && wrsq != queeningSq && (Utils.square_distance(wksq, queeningSq) < Utils.square_distance(bksq, queeningSq) - 2 + tempo) && (Utils.square_distance(wksq, queeningSq) < Utils.square_distance(bksq, wrsq) + tempo)) return (ScaleFactorC.SCALE_FACTOR_MAX - 2 * Utils.square_distance(wksq, queeningSq)); // Similar to the above, but with the pawn further back if (f != FileC.FILE_A && Utils.file_of(wrsq) == f && wrsq < wpsq && (Utils.square_distance(wksq, queeningSq) < Utils.square_distance(bksq, queeningSq) - 2 + tempo) && (Utils.square_distance(wksq, wpsq + SquareC.DELTA_N) < Utils.square_distance(bksq, wpsq + SquareC.DELTA_N) - 2 + tempo) && (Utils.square_distance(bksq, wrsq) + tempo >= 3 || (Utils.square_distance(wksq, queeningSq) < Utils.square_distance(bksq, wrsq) + tempo && (Utils.square_distance(wksq, wpsq + SquareC.DELTA_N) < Utils.square_distance(bksq, wrsq) + tempo)))) return (ScaleFactorC.SCALE_FACTOR_MAX - 8 * Utils.square_distance(wpsq, queeningSq) - 2 * Utils.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 <= RankC.RANK_4 && bksq > wpsq) { if (Utils.file_of(bksq) == Utils.file_of(wpsq)) return (10); if (Math.Abs(Utils.file_of(bksq) - Utils.file_of(wpsq)) == 1 && Utils.square_distance(wksq, bksq) > 2) return (24 - 2 * Utils.square_distance(wksq, bksq)); } return ScaleFactorC.SCALE_FACTOR_NONE; }
/// KP vs K. This endgame is evaluated with the help of a bitbase. internal static Value Endgame_KPK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 1); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square wksq, bksq, wpsq; Color stm; if (strongerSide == ColorC.WHITE) { wksq = pos.king_square(ColorC.WHITE); bksq = pos.king_square(ColorC.BLACK); wpsq = pos.pieceList[ColorC.WHITE][PieceTypeC.PAWN][0]; stm = pos.sideToMove; } else { wksq = Utils.flip_S(pos.king_square(ColorC.BLACK)); bksq = Utils.flip_S(pos.king_square(ColorC.WHITE)); wpsq = Utils.flip_S(pos.pieceList[ColorC.BLACK][PieceTypeC.PAWN][0]); stm = Utils.flip_C(pos.sideToMove); } if (Utils.file_of(wpsq) >= FileC.FILE_E) { wksq = Utils.mirror(wksq); bksq = Utils.mirror(bksq); wpsq = Utils.mirror(wpsq); } if (KPKPosition.probe_kpk_bitbase(wksq, wpsq, bksq, stm) == 0) return ValueC.VALUE_DRAW; Value result = ValueC.VALUE_KNOWN_WIN + Constants.PawnValueEndgame + Utils.rank_of(wpsq); return strongerSide == pos.sideToMove ? result : -result; }
/// 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. /// KXK internal static Value Endgame_KXK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == ValueC.VALUE_ZERO); // Stalemate detection with lone king MList mlist = MListBroker.GetObject(); mlist.pos = 0; Movegen.generate_legal(pos, mlist.moves, ref mlist.pos); bool any = mlist.pos > 0; MListBroker.Free(); if (pos.sideToMove == weakerSide && !pos.in_check() && !any) { return ValueC.VALUE_DRAW; } Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); Value result = pos.non_pawn_material(strongerSide) + pos.piece_count(strongerSide, PieceTypeC.PAWN) * Constants.PawnValueEndgame + MateTable[loserKSq] + DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)]; if (pos.piece_count(strongerSide, PieceTypeC.QUEEN)!=0 || pos.piece_count(strongerSide, PieceTypeC.ROOK)!=0 || pos.bishop_pair(strongerSide)) { result += ValueC.VALUE_KNOWN_WIN; } return strongerSide == pos.sideToMove ? result : -result; }
/// K and a pawn vs K and a pawn. This is done by removing the weakest side's /// pawn and probing the KP vs K bitbase: If the weakest side has a draw without /// the pawn, she probably has at least a draw with the pawn as well. The exception /// is when the stronger side's pawn is far advanced and not on a rook file; in /// this case it is often possible to win (e.g. 8/4k3/3p4/3P4/6K1/8/8/8 w - - 0 1). internal static ScaleFactor Endgame_KPKP(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN) == 1); Debug.Assert(pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN) == 1); Square wksq = pos.king_square(strongerSide); Square bksq = pos.king_square(weakerSide); Square wpsq = pos.pieceList[strongerSide][PieceTypeC.PAWN][0]; Color stm = pos.sideToMove; if (strongerSide == ColorC.BLACK) { wksq = Utils.flip_S(wksq); bksq = Utils.flip_S(bksq); wpsq = Utils.flip_S(wpsq); stm = Utils.flip_C(stm); } if (Utils.file_of(wpsq) >= FileC.FILE_E) { wksq = Utils.mirror(wksq); bksq = Utils.mirror(bksq); wpsq = Utils.mirror(wpsq); } // If the pawn has advanced to the fifth rank or further, and is not a // rook pawn, it's too dangerous to assume that it's at least a draw. if (Utils.rank_of(wpsq) >= RankC.RANK_5 && Utils.file_of(wpsq) != FileC.FILE_A) return ScaleFactorC.SCALE_FACTOR_NONE; // Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw, // it's probably at least a draw even with the pawn. return (KPKPosition.probe_kpk_bitbase(wksq, wpsq, bksq, stm) != 0) ? ScaleFactorC.SCALE_FACTOR_NONE : ScaleFactorC.SCALE_FACTOR_DRAW; }
// search<>() is the main search function for both PV and non-PV nodes and for // normal and SplitPoint nodes. When called just after a split point the search // is simpler because we have already probed the hash table, done a null move // search, and searched the first move before splitting, we don't have to repeat // all this work again. We also don't need to store anything to the hash table // here: This is taken care of after we return from the split point. internal static Value search(NodeType NT, Position pos, Stack[] ss, int ssPos, Value alpha, Value beta, Depth depth) { bool PvNode = (NT == NodeTypeC.PV || NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointRoot); bool SpNode = (NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointNonPV || NT == NodeTypeC.SplitPointRoot); bool RootNode = (NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointRoot); Debug.Assert(alpha >= -ValueC.VALUE_INFINITE && alpha < beta && beta <= ValueC.VALUE_INFINITE); Debug.Assert((alpha == beta - 1) || PvNode); Debug.Assert(depth > DepthC.DEPTH_ZERO); MovesSearched ms = MovesSearchedBroker.GetObject(); Move[] movesSearched = ms.movesSearched; StateInfo st = null; TTEntry tte = TT.StaticEntry; bool tteHasValue = false; UInt32 ttePos = 0; Key posKey = 0; Move ttMove, move, excludedMove, bestMove, threatMove; Depth ext, newDepth; Bound bt; Value bestValue, value, oldAlpha, ttValue; Value refinedValue, nullValue, futilityBase, futilityValue; bool isPvMove, inCheck, singularExtensionNode, givesCheck; bool captureOrPromotion, dangerous, doFullDepthSearch; int moveCount = 0, playedMoveCount = 0; Thread thisThread = pos.this_thread(); SplitPoint sp = null; refinedValue = bestValue = value = -ValueC.VALUE_INFINITE; oldAlpha = alpha; inCheck = pos.in_check(); ss[ssPos].ply = ss[ssPos - 1].ply + 1; // Used to send selDepth info to GUI if (PvNode && thisThread.maxPly < ss[ssPos].ply) thisThread.maxPly = ss[ssPos].ply; // Step 1. Initialize node if (SpNode) { ttMove = excludedMove = MoveC.MOVE_NONE; ttValue = ValueC.VALUE_ZERO; sp = ss[ssPos].sp; bestMove = sp.bestMove; threatMove = sp.threatMove; bestValue = sp.bestValue; moveCount = sp.moveCount; // Lock must be held here Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && moveCount > 0); goto split_point_start; } else { ss[ssPos].currentMove = threatMove = ss[ssPos + 1].excludedMove = bestMove = MoveC.MOVE_NONE; ss[ssPos + 1].skipNullMove = 0; ss[ssPos + 1].reduction = DepthC.DEPTH_ZERO; ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = MoveC.MOVE_NONE; } // Step 2. Check for aborted search and immediate draw // Enforce node limit here. FIXME: This only works with 1 search thread. if ((Limits.nodes != 0) && pos.nodes >= Limits.nodes) SignalsStop = true; if ((SignalsStop || pos.is_draw(false) || ss[ssPos].ply > Constants.MAX_PLY) && !RootNode) { MovesSearchedBroker.Free(); return ValueC.VALUE_DRAW; } // Step 3. Mate distance pruning. Even if we mate at the next move our score // would be at best mate_in(ss[ssPos].ply+1), but if alpha is already bigger because // a shorter mate was found upward in the tree then there is no need to search // further, we will never beat current alpha. Same logic but with reversed signs // applies also in the opposite condition of being mated instead of giving mate, // in this case return a fail-high score. if (!RootNode) { alpha = Math.Max(Utils.mated_in(ss[ssPos].ply), alpha); beta = Math.Min(Utils.mate_in(ss[ssPos].ply + 1), beta); if (alpha >= beta) { MovesSearchedBroker.Free(); return alpha; } } // Step 4. Transposition table lookup // We don't want the score of a partial search to overwrite a previous full search // TT value, so we use a different position key in case of an excluded move. excludedMove = ss[ssPos].excludedMove; posKey = (excludedMove != 0) ? pos.exclusion_key() : pos.key(); tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tteHasValue ? tte.move() : MoveC.MOVE_NONE; ttValue = tteHasValue ? value_from_tt(tte.value(), ss[ssPos].ply) : ValueC.VALUE_ZERO; // At PV nodes we check for exact scores, while at non-PV nodes we check for // a fail high/low. Biggest advantage at probing at PV nodes is to have a // smooth experience in analysis mode. We don't probe at Root nodes otherwise // we should also update RootMoveList to avoid bogus output. if (!RootNode && tteHasValue && (PvNode ? tte.depth() >= depth && tte.type() == Bound.BOUND_EXACT : can_return_tt(tte, depth, ttValue, beta))) { TT.entries[ttePos].set_generation(TT.generation); ss[ssPos].currentMove = ttMove; // Can be MOVE_NONE if (ttValue >= beta && (ttMove != 0) && !pos.is_capture_or_promotion(ttMove) && ttMove != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = ttMove; } MovesSearchedBroker.Free(); return ttValue; } // Step 5. Evaluate the position statically and update parent's gain statistics if (inCheck) ss[ssPos].eval = ss[ssPos].evalMargin = ValueC.VALUE_NONE; else if (tteHasValue) { Debug.Assert(tte.static_value() != ValueC.VALUE_NONE); ss[ssPos].eval = tte.static_value(); ss[ssPos].evalMargin = tte.static_value_margin(); refinedValue = refine_eval(tte, ttValue, ss[ssPos].eval); } else { refinedValue = ss[ssPos].eval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin); TT.store(posKey, ValueC.VALUE_NONE, Bound.BOUND_NONE, DepthC.DEPTH_NONE, MoveC.MOVE_NONE, ss[ssPos].eval, ss[ssPos].evalMargin); } // Update gain for the parent non-capture move given the static position // evaluation before and after the move. if ((move = ss[ssPos - 1].currentMove) != MoveC.MOVE_NULL && ss[ssPos - 1].eval != ValueC.VALUE_NONE && ss[ssPos].eval != ValueC.VALUE_NONE && (pos.captured_piece_type() == 0) && !Utils.is_special(move)) { Square to = Utils.to_sq(move); H.update_gain(pos.piece_on(to), to, -ss[ssPos - 1].eval - ss[ssPos].eval); } // Step 6. Razoring (is omitted in PV nodes) if (!PvNode && !inCheck && depth < RazorDepth && refinedValue + razor_margin(depth) < beta && ttMove == MoveC.MOVE_NONE && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && !pos.pawn_on_7th(pos.sideToMove)) { Value rbeta = beta - razor_margin(depth); Value v = qsearch(NodeTypeC.NonPV, pos, ss, ssPos, rbeta - 1, rbeta, DepthC.DEPTH_ZERO); if (v < rbeta) { // Logically we should return (v + razor_margin(depth)), but // surprisingly this did slightly weaker in tests. MovesSearchedBroker.Free(); return v; } } // Step 7. Static null move pruning (is omitted in PV nodes) // We're betting that the opponent doesn't have a move that will reduce // the score by more than futility_margin(depth) if we do a null move. if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth < RazorDepth && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && refinedValue - futility_margin(depth, 0) >= beta && (pos.non_pawn_material(pos.sideToMove) != 0)) { MovesSearchedBroker.Free(); return refinedValue - futility_margin(depth, 0); } // Step 8. Null move search with verification search (is omitted in PV nodes) if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth > DepthC.ONE_PLY && refinedValue >= beta && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && (pos.non_pawn_material(pos.sideToMove) != 0)) { ss[ssPos].currentMove = MoveC.MOVE_NULL; // Null move dynamic reduction based on depth int R = 3 + (depth >= 5 * DepthC.ONE_PLY ? depth / 8 : 0); // Null move dynamic reduction based on value if (refinedValue - Constants.PawnValueMidgame > beta) R++; if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_null_move(true, st); ss[ssPos + 1].skipNullMove = 1; nullValue = depth - R * DepthC.ONE_PLY < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, depth - R * DepthC.ONE_PLY); ss[ssPos + 1].skipNullMove = 0; pos.do_null_move(false, st); if (nullValue >= beta) { // Do not return unproven mate scores if (nullValue >= ValueC.VALUE_MATE_IN_MAX_PLY) nullValue = beta; if (depth < 6 * DepthC.ONE_PLY) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } // Do verification search at high depths ss[ssPos].skipNullMove = 1; Value v = search(NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, depth - R * DepthC.ONE_PLY); ss[ssPos].skipNullMove = 0; if (v >= beta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return nullValue; } } else { // The null move failed low, which means that we may be faced with // some kind of threat. If the previous move was reduced, check if // the move that refuted the null move was somehow connected to the // move which was reduced. If a connection is found, return a fail // low score (which will cause the reduced move to fail high in the // parent node, which will trigger a re-search with full depth). threatMove = ss[ssPos + 1].currentMove; if (depth < ThreatDepth && (ss[ssPos - 1].reduction != 0) && threatMove != MoveC.MOVE_NONE && connected_moves(pos, ss[ssPos - 1].currentMove, threatMove)) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } MovesSearchedBroker.Free(); return beta - 1; } } } // Step 9. ProbCut (is omitted in PV nodes) // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type]) // and a reduced search returns a value much above beta, we can (almost) safely // prune the previous move. if (!PvNode && !inCheck && excludedMove == MoveC.MOVE_NONE && depth >= RazorDepth + DepthC.ONE_PLY && (ss[ssPos].skipNullMove == 0) && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY) { Value rbeta = beta + 200; Depth rdepth = depth - DepthC.ONE_PLY - 3 * DepthC.ONE_PLY; Debug.Assert(rdepth >= DepthC.ONE_PLY); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NONE); Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NULL); MovePicker mp2 = MovePickerBroker.GetObject(); mp2.MovePickerC(pos, ttMove, H, pos.captured_piece_type()); CheckInfo ci2 = CheckInfoBroker.GetObject(); ci2.CreateCheckInfo(pos); while ((move = mp2.next_move()) != MoveC.MOVE_NONE) { if (pos.pl_move_is_legal(move, ci2.pinned)) { ss[ssPos].currentMove = move; if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci2, pos.move_gives_check(move, ci2)); value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth); pos.undo_move(move); if (value >= rbeta) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); MovesSearchedBroker.Free(); return value; } } } CheckInfoBroker.Free(); MovePickerBroker.Free(mp2); } // Step 10. Internal iterative deepening if (ttMove == MoveC.MOVE_NONE && depth >= IIDDepth[PvNode ? 1 : 0] && (PvNode || (!inCheck && ss[ssPos].eval + IIDMargin >= beta))) { Depth d = (PvNode ? depth - 2 * DepthC.ONE_PLY : depth / 2); ss[ssPos].skipNullMove = 1; search(PvNode ? NodeTypeC.PV : NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, d); ss[ssPos].skipNullMove = 0; tteHasValue = TT.probe(posKey, ref ttePos, out tte); ttMove = (tteHasValue) ? tte.move() : MoveC.MOVE_NONE; } else { // Re-read (needed as TTEntry is a struct in the port) if ((tteHasValue) && (TT.entries[ttePos].key == tte.key)) { tte = TT.entries[ttePos]; } } split_point_start: // At split points actual search starts from here MovePicker mp = MovePickerBroker.GetObject(); mp.MovePickerC(pos, ttMove, depth, H, ss[ssPos], PvNode ? -ValueC.VALUE_INFINITE : beta, SpNode ? ss[ssPos].sp.mp : null); CheckInfo ci = CheckInfoBroker.GetObject(); ci.CreateCheckInfo(pos); futilityBase = ss[ssPos].eval + ss[ssPos].evalMargin; singularExtensionNode = !RootNode && !SpNode && depth >= SingularExtensionDepth[PvNode ? 1 : 0] && ttMove != MoveC.MOVE_NONE && (excludedMove == 0) // Recursive singular search is not allowed && ((tte.type() & Bound.BOUND_LOWER) != 0) // FIXME: uninitialized! && tte.depth() >= depth - 3 * DepthC.ONE_PLY; // Step 11. Loop through moves // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs while (bestValue < beta && (move = mp.next_move()) != MoveC.MOVE_NONE && !thisThread.cutoff_occurred() && !SignalsStop) { Debug.Assert(Utils.is_ok_M(move)); if (move == excludedMove) continue; // At root obey the "searchmoves" option and skip moves not listed in Root // Move List, as a consequence any illegal move is also skipped. In MultiPV // mode we also skip PV moves which have been already searched. // If we find none, it means !count if (RootNode && (find(RootMoves, PVIdx, RootMoves.Count, move) == -1)) continue; // At PV and SpNode nodes we want all moves to be legal since the beginning if ((PvNode || SpNode) && !pos.pl_move_is_legal(move, ci.pinned)) continue; if (SpNode) { moveCount = ++sp.moveCount; ThreadHelper.lock_release(sp.Lock); } else moveCount++; if (RootNode) { SignalsFirstRootMove = (moveCount == 1); if (thisThread == Threads.main_thread() && SearchTime.ElapsedMilliseconds > 2000) { Plug.Write("info depth "); Plug.Write((depth / DepthC.ONE_PLY).ToString()); Plug.Write(" currmove "); Plug.Write(Utils.move_to_uci(move, Chess960)); Plug.Write(" nodes "); Plug.Write(pos.nodes.ToString()); Plug.Write(" currmovenumber "); Plug.Write((moveCount + PVIdx).ToString()); Plug.Write(Constants.endl); } } isPvMove = (PvNode && moveCount <= 1); captureOrPromotion = pos.is_capture_or_promotion(move); givesCheck = pos.move_gives_check(move, ci); dangerous = givesCheck || is_dangerous(pos, move, captureOrPromotion); ext = DepthC.DEPTH_ZERO; // Step 12. Extend checks and, in PV nodes, also dangerous moves if (PvNode && dangerous) ext = DepthC.ONE_PLY; else if (givesCheck && pos.see(move, true) >= 0) ext = DepthC.ONE_PLY / 2; // Singular extension search. If all moves but one fail low on a search of // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move // is singular and should be extended. To verify this we do a reduced search // on all the other moves but the ttMove, if result is lower than ttValue minus // a margin then we extend ttMove. if (singularExtensionNode && (ext == 0) && move == ttMove && pos.pl_move_is_legal(move, ci.pinned)) { if (Math.Abs(ttValue) < ValueC.VALUE_KNOWN_WIN) { Value rBeta = ttValue - (int)(depth); ss[ssPos].excludedMove = move; ss[ssPos].skipNullMove = 1; value = search(NodeTypeC.NonPV, pos, ss, ssPos, rBeta - 1, rBeta, depth / 2); ss[ssPos].skipNullMove = 0; ss[ssPos].excludedMove = MoveC.MOVE_NONE; if (value < rBeta) ext = DepthC.ONE_PLY; } } // Update current move (this must be done after singular extension search) newDepth = depth - DepthC.ONE_PLY + ext; // Step 13. Futility pruning (is omitted in PV nodes) if (!PvNode && !inCheck && !captureOrPromotion && !dangerous && move != ttMove && (bestValue > ValueC.VALUE_MATED_IN_MAX_PLY || bestValue == -ValueC.VALUE_INFINITE) && !Utils.is_castle(move)) { // Move count based pruning if (moveCount >= futility_move_count(depth) && ((threatMove == 0) || !connected_threat(pos, move, threatMove))) { if (SpNode) ThreadHelper.lock_grab(sp.Lock); continue; } // Value based pruning // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth, // but fixing this made program slightly weaker. Depth predictedDepth = newDepth - reduction(PvNode, depth, moveCount); futilityValue = futilityBase + futility_margin(predictedDepth, moveCount) + H.gain(pos.piece_moved(move), Utils.to_sq(move)); if (futilityValue < beta) { if (SpNode) ThreadHelper.lock_grab(sp.Lock); continue; } // Prune moves with negative SEE at low depths if (predictedDepth < 2 * DepthC.ONE_PLY && pos.see(move, true) < 0) { if (SpNode) ThreadHelper.lock_grab(sp.Lock); continue; } } // Check for legality only before to do the move if (!pos.pl_move_is_legal(move, ci.pinned)) { moveCount--; continue; } ss[ssPos].currentMove = move; if (!SpNode && !captureOrPromotion && playedMoveCount < 64) movesSearched[playedMoveCount++] = move; // Step 14. Make the move if (st == null) { st = StateInfoBroker.GetObject(); } pos.do_move(move, st, ci, givesCheck); // Step 15. Reduced depth search (LMR). If the move fails high will be // re-searched at full depth. if ( !isPvMove && !captureOrPromotion && !dangerous && ss[ssPos].killers0 != move && ss[ssPos].killers1 != move && depth > 3 * DepthC.ONE_PLY && !Utils.is_castle(move)) { ss[ssPos].reduction = reduction(PvNode, depth, moveCount); Depth d = Math.Max(newDepth - ss[ssPos].reduction, DepthC.ONE_PLY); alpha = SpNode ? sp.alpha : alpha; value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, d); doFullDepthSearch = (value > alpha && ss[ssPos].reduction != DepthC.DEPTH_ZERO); ss[ssPos].reduction = DepthC.DEPTH_ZERO; } else doFullDepthSearch = !isPvMove; // Step 16. Full depth search, when LMR is skipped or fails high if (doFullDepthSearch) { alpha = SpNode ? sp.alpha : alpha; value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth); } // Only for PV nodes do a full PV search on the first move or after a fail // high, in the latter case search only if value < beta, otherwise let the // parent node to fail low with value <= alpha and to try another move. if (PvNode && (isPvMove || (value > alpha && (RootNode || value < beta)))) { value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO) : -search(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, newDepth); } // Step 17. Undo move pos.undo_move(move); Debug.Assert(value > -ValueC.VALUE_INFINITE && value < ValueC.VALUE_INFINITE); // Step 18. Check for new best move if (SpNode) { ThreadHelper.lock_grab(sp.Lock); bestValue = sp.bestValue; alpha = sp.alpha; } // Finished searching the move. If Signals.stop is true, the search // was aborted because the user interrupted the search or because we // ran out of time. In this case, the return value of the search cannot // be trusted, and we don't update the best move and/or PV. if (RootNode && !SignalsStop) { int rmPos = find(RootMoves, 0, RootMoves.Count, move); // PV move or new best move ? if (isPvMove || value > alpha) { RootMoves[rmPos].score = value; RootMoves[rmPos].extract_pv_from_tt(pos); // We record how often the best move has been changed in each // iteration. This information is used for time management: When // the best move changes frequently, we allocate some more time. if (!isPvMove && MultiPV == 1) BestMoveChanges++; } else // All other moves but the PV are set to the lowest value, this // is not a problem when sorting becuase sort is stable and move // position in the list is preserved, just the PV is pushed up. RootMoves[rmPos].score = -ValueC.VALUE_INFINITE; } if (value > bestValue) { bestValue = value; bestMove = move; if (PvNode && value > alpha && value < beta) // We want always alpha < beta alpha = value; if (SpNode && !thisThread.cutoff_occurred()) { sp.bestValue = value; sp.bestMove = move; sp.alpha = alpha; if (value >= beta) sp.cutoff = true; } } // Step 19. Check for split if (!SpNode && depth >= Threads.min_split_depth() && bestValue < beta && Threads.available_slave_exists(thisThread) && !SignalsStop && !thisThread.cutoff_occurred()) { bestValue = Threads.split(Constants.FakeSplit, pos, ss, ssPos, alpha, beta, bestValue, ref bestMove, depth, threatMove, moveCount, mp, NT); } } // Step 20. Check for mate and stalemate // All legal moves have been searched and if there are no legal moves, it // must be mate or stalemate. Note that we can have a false positive in // case of Signals.stop or thread.cutoff_occurred() are set, but this is // harmless because return value is discarded anyhow in the parent nodes. // If we are in a singular extension search then return a fail low score. if (moveCount == 0) { if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return (excludedMove != 0) ? oldAlpha : inCheck ? Utils.mated_in(ss[ssPos].ply) : ValueC.VALUE_DRAW; } // If we have pruned all the moves without searching return a fail-low score if (bestValue == -ValueC.VALUE_INFINITE) { Debug.Assert(playedMoveCount == 0); bestValue = oldAlpha; } // Step 21. Update tables // Update transposition table entry, killers and history if (!SpNode && !SignalsStop && !thisThread.cutoff_occurred()) { move = bestValue <= oldAlpha ? MoveC.MOVE_NONE : bestMove; bt = bestValue <= oldAlpha ? Bound.BOUND_UPPER : bestValue >= beta ? Bound.BOUND_LOWER : Bound.BOUND_EXACT; TT.store(posKey, value_to_tt(bestValue, ss[ssPos].ply), bt, depth, move, ss[ssPos].eval, ss[ssPos].evalMargin); // Update killers and history for non capture cut-off moves if (!inCheck && bestValue >= beta && !pos.is_capture_or_promotion(move) ) { if (move != ss[ssPos].killers0) { ss[ssPos].killers1 = ss[ssPos].killers0; ss[ssPos].killers0 = move; } // Increase history value of the cut-off move Value bonus = (depth * depth); H.add(pos.piece_moved(move), Utils.to_sq(move), bonus); // Decrease history of all the other played non-capture moves for (int i = 0; i < playedMoveCount - 1; i++) { Move m = movesSearched[i]; H.add(pos.piece_moved(m), Utils.to_sq(m), -bonus); } } } Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && bestValue < ValueC.VALUE_INFINITE); if (st != null) { st.previous = null; StateInfoBroker.Free(); } CheckInfoBroker.Free(); MovePickerBroker.Free(mp); MovesSearchedBroker.Free(); return bestValue; }
/// K and two or more pawns vs K. There is just a single rule here: If all pawns /// are on the same rook file and are blocked by the defending king, it's a draw. internal static ScaleFactor Endgame_KPsK(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) >= 2); Debug.Assert(pos.non_pawn_material(weakerSide) == ValueC.VALUE_ZERO); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square ksq = pos.king_square(weakerSide); Bitboard pawns = pos.pieces_PTC(PieceTypeC.PAWN, strongerSide); // Are all pawns on the 'a' file? if ((pawns & ~Constants.FileABB) == 0) { // Does the defending king block the pawns? if (Utils.square_distance(ksq, Utils.relative_square(strongerSide, SquareC.SQ_A8)) <= 1 || (Utils.file_of(ksq) == FileC.FILE_A && ((Utils.in_front_bb_CS(strongerSide, ksq)) & pawns) == 0)) return ScaleFactorC.SCALE_FACTOR_DRAW; } // Are all pawns on the 'h' file? else if ((pawns & ~Constants.FileHBB) == 0) { // Does the defending king block the pawns? if (Utils.square_distance(ksq, Utils.relative_square(strongerSide, SquareC.SQ_H8)) <= 1 || (Utils.file_of(ksq) == FileC.FILE_H && ((Utils.in_front_bb_CS(strongerSide, ksq)) & pawns) == 0)) return ScaleFactorC.SCALE_FACTOR_DRAW; } return ScaleFactorC.SCALE_FACTOR_NONE; }
internal static bool is_KBPsKs(int Us, Position pos) { return pos.non_pawn_material(Us) == Constants.BishopValueMidgame && pos.piece_count(Us, PieceTypeC.BISHOP) == 1 && pos.piece_count(Us, PieceTypeC.PAWN) >= 1; }
/// 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 be /// able to win KQ vs KR. internal static Value Endgame_KQKR(Color strongerSide, Position pos) { Color weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.QueenValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0); Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.RookValueMidgame); Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0); Square winnerKSq = pos.king_square(strongerSide); Square loserKSq = pos.king_square(weakerSide); Value result = Constants.QueenValueEndgame - Constants.RookValueEndgame + MateTable[loserKSq] + DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)]; return strongerSide == pos.sideToMove ? result : -result; }
/// MaterialTable::material_info() takes a position object as input, /// computes or looks up a MaterialInfo object, and returns a pointer to it. /// If the material configuration is not already present in the table, it /// is stored there, so we don't have to recompute everything when the /// same material configuration occurs again. internal void probe(Position pos, out MaterialEntry e) { var key = pos.material_key(); e = this.entries[((uint)key) & Constants.MaterialTableMask]; // If mi->key matches the position's material hash key, it means that we // have analysed this material configuration before, and we can simply // return the information we found the last time instead of recomputing it. if (e.key == key) { return; } // Initialize MaterialInfo entry var npm = pos.non_pawn_material(ColorC.WHITE) + pos.non_pawn_material(ColorC.BLACK); e.value = 0; e.scalingFunctionWHITE = null; e.scalingFunctionBLACK = null; e.spaceWeight = 0; e.key = key; e.factorWHITE = e.factorBLACK = ScaleFactorC.SCALE_FACTOR_NORMAL; e.gamePhase = npm >= MidgameLimit ? PhaseC.PHASE_MIDGAME : npm <= EndgameLimit ? PhaseC.PHASE_ENDGAME : (((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit)); // Let's look if we have a specialized evaluation function for this // particular material configuration. First we look for a fixed // configuration one, then a generic one if previous search failed. if ((e.evaluationFunction = Endgame.probeValue(key, out e.evaluationFunctionColor)) != null) { return; } if (is_KXK(ColorC.WHITE, pos)) { e.evaluationFunction = Endgame.Endgame_KXK; e.evaluationFunctionColor = ColorC.WHITE; return; } if (is_KXK(ColorC.BLACK, pos)) { e.evaluationFunction = Endgame.Endgame_KXK; e.evaluationFunctionColor = ColorC.BLACK; return; } if ((pos.pieces_PT(PieceTypeC.PAWN) == 0) && (pos.pieces_PT(PieceTypeC.ROOK) == 0) && (pos.pieces_PT(PieceTypeC.QUEEN) == 0)) { // Minor piece endgame with at least one minor piece per side and // no pawns. Note that the case KmmK is already handled by KXK. Debug.Assert( (pos.pieces_PTC(PieceTypeC.KNIGHT, ColorC.WHITE) | pos.pieces_PTC(PieceTypeC.BISHOP, ColorC.WHITE)) != 0); Debug.Assert( (pos.pieces_PTC(PieceTypeC.KNIGHT, ColorC.BLACK) | pos.pieces_PTC(PieceTypeC.BISHOP, ColorC.BLACK)) != 0); if (pos.piece_count(ColorC.WHITE, PieceTypeC.BISHOP) + pos.piece_count(ColorC.WHITE, PieceTypeC.KNIGHT) <= 2 && pos.piece_count(ColorC.BLACK, PieceTypeC.BISHOP) + pos.piece_count(ColorC.BLACK, PieceTypeC.KNIGHT) <= 2) { e.evaluationFunction = Endgame.Endgame_KmmKm; e.evaluationFunctionColor = pos.sideToMove; return; } } // OK, we didn't find any special evaluation function for the current // material configuration. Is there a suitable scaling function? // // We face problems when there are several conflicting applicable // scaling functions and we need to decide which one to use. EndgameScaleFactor sf; int c; if ((sf = Endgame.probeScaleFactor(key, out c)) != null) { if (c == ColorC.WHITE) { e.scalingFunctionWHITE = sf; } else { e.scalingFunctionBLACK = sf; } return; } // Generic scaling functions that refer to more then one material // distribution. Should be probed after the specialized ones. // Note that these ones don't return after setting the function. if (is_KBPsKs(ColorC.WHITE, pos)) { e.scalingFunctionWHITE = Endgame.Endgame_KBPsK; } if (is_KBPsKs(ColorC.BLACK, pos)) { e.scalingFunctionBLACK = Endgame.Endgame_KBPsK; } if (is_KQKRPs(ColorC.WHITE, pos)) { e.scalingFunctionWHITE = Endgame.Endgame_KQKRPs; } else if (is_KQKRPs(ColorC.BLACK, pos)) { e.scalingFunctionBLACK = Endgame.Endgame_KQKRPs; } var npm_w = pos.non_pawn_material(ColorC.WHITE); var npm_b = pos.non_pawn_material(ColorC.BLACK); if (npm_w + npm_b == ValueC.VALUE_ZERO) { if (pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN) == 0) { Debug.Assert(pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN) >= 2); e.scalingFunctionWHITE = Endgame.Endgame_KPsK; } else if (pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN) == 0) { Debug.Assert(pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN) >= 2); e.scalingFunctionBLACK = Endgame.Endgame_KPsK; } else if (pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN) == 1 && pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN) == 1) { // This is a special case because we set scaling functions // for both colors instead of only one. e.scalingFunctionWHITE = Endgame.Endgame_KPKP; e.scalingFunctionBLACK = Endgame.Endgame_KPKP; } } // No pawns makes it difficult to win, even with a material advantage if (pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN) == 0 && npm_w - npm_b <= Constants.BishopValueMidgame) { e.factorWHITE = (byte) (npm_w == npm_b || npm_w < Constants.RookValueMidgame ? 0 : NoPawnsSF[Math.Min(pos.piece_count(ColorC.WHITE, PieceTypeC.BISHOP), 2)]); } if (pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN) == 0 && npm_b - npm_w <= Constants.BishopValueMidgame) { e.factorBLACK = (byte) (npm_w == npm_b || npm_b < Constants.RookValueMidgame ? 0 : NoPawnsSF[Math.Min(pos.piece_count(ColorC.BLACK, PieceTypeC.BISHOP), 2)]); } // Compute the space weight if (npm_w + npm_b >= 2 * Constants.QueenValueMidgame + 4 * Constants.RookValueMidgame + 2 * Constants.KnightValueMidgame) { var minorPieceCount = pos.piece_count(ColorC.WHITE, PieceTypeC.KNIGHT) + pos.piece_count(ColorC.WHITE, PieceTypeC.BISHOP) + pos.piece_count(ColorC.BLACK, PieceTypeC.KNIGHT) + pos.piece_count(ColorC.BLACK, PieceTypeC.BISHOP); e.spaceWeight = minorPieceCount * minorPieceCount; } // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder // for the bishop pair "extended piece", this allow us to be more flexible // in defining bishop pair bonuses. this.pieceCount[0][0] = pos.piece_count(ColorC.WHITE, PieceTypeC.BISHOP) > 1 ? 1 : 0; this.pieceCount[0][1] = pos.piece_count(ColorC.WHITE, PieceTypeC.PAWN); this.pieceCount[0][2] = pos.piece_count(ColorC.WHITE, PieceTypeC.KNIGHT); this.pieceCount[0][3] = pos.piece_count(ColorC.WHITE, PieceTypeC.BISHOP); this.pieceCount[0][4] = pos.piece_count(ColorC.WHITE, PieceTypeC.ROOK); this.pieceCount[0][5] = pos.piece_count(ColorC.WHITE, PieceTypeC.QUEEN); this.pieceCount[1][0] = pos.piece_count(ColorC.BLACK, PieceTypeC.BISHOP) > 1 ? 1 : 0; this.pieceCount[1][1] = pos.piece_count(ColorC.BLACK, PieceTypeC.PAWN); this.pieceCount[1][2] = pos.piece_count(ColorC.BLACK, PieceTypeC.KNIGHT); this.pieceCount[1][3] = pos.piece_count(ColorC.BLACK, PieceTypeC.BISHOP); this.pieceCount[1][4] = pos.piece_count(ColorC.BLACK, PieceTypeC.ROOK); this.pieceCount[1][5] = pos.piece_count(ColorC.BLACK, PieceTypeC.QUEEN); e.value = (short)((this.imbalance(ColorC.WHITE) - this.imbalance(ColorC.BLACK)) / 16); }
/// K, bishop 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. internal static int Endgame_KBPsK(int strongerSide, Position pos) { var weakerSide = strongerSide ^ 1; Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.BishopValueMidgame); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 1); Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) >= 1); // No Debug.Assertions about the material of weakerSide, because we want draws to // be detected even when the weaker side has some pawns. var pawns = pos.pieces_PTC(PieceTypeC.PAWN, strongerSide); var pawnFile = Utils.file_of(pos.pieceList[strongerSide][PieceTypeC.PAWN][0]); // All pawns are on a single rook file ? if ((pawnFile == FileC.FILE_A || pawnFile == FileC.FILE_H) && (((pawns & ~Utils.file_bb_F(pawnFile))) == 0)) { var bishopSq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; var queeningSq = Utils.relative_square(strongerSide, Utils.make_square(pawnFile, RankC.RANK_8)); var kingSq = pos.king_square(weakerSide); if (Utils.opposite_colors(queeningSq, bishopSq) && Math.Abs(Utils.file_of(kingSq) - pawnFile) <= 1) { // The bishop has the wrong color, and the defending king is on the // file of the pawn(s) or the adjacent file. Find the rank of the // frontmost pawn. int rank; if (strongerSide == ColorC.WHITE) { for (rank = RankC.RANK_7; (((Utils.rank_bb_R(rank) & pawns)) == 0); rank--) { } Debug.Assert(rank >= RankC.RANK_2 && rank <= RankC.RANK_7); } else { for (rank = RankC.RANK_2; (((Utils.rank_bb_R(rank) & pawns)) == 0); rank++) { } rank = (rank ^ 7); // HACK to get the relative rank Debug.Assert(rank >= RankC.RANK_2 && rank <= RankC.RANK_7); } // If the defending king has distance 1 to the promotion square or // is placed somewhere in front of the pawn, it's a draw. if (Utils.square_distance(kingSq, queeningSq) <= 1 || Utils.relative_rank_CS(strongerSide, kingSq) >= rank) { return ScaleFactorC.SCALE_FACTOR_DRAW; } } } // All pawns on same B or G file? Then potential draw if ((pawnFile == FileC.FILE_B || pawnFile == FileC.FILE_G) && (pos.pieces_PT(PieceTypeC.PAWN) & ~Utils.file_bb_F(pawnFile)) == 0 && pos.non_pawn_material(weakerSide) == 0 && pos.piece_count(weakerSide, PieceTypeC.PAWN) >= 1) { // Get weaker pawn closest to opponent's queening square Bitboard wkPawns = pos.pieces_PTC(PieceTypeC.PAWN, weakerSide); Square weakerPawnSq = strongerSide == ColorC.WHITE ? Utils.msb(wkPawns) : Utils.lsb(wkPawns); Square strongerKingSq = pos.king_square(strongerSide); Square weakerKingSq = pos.king_square(weakerSide); Square bishopSq = pos.pieceList[strongerSide][PieceTypeC.BISHOP][0]; // Draw if weaker pawn is on rank 7, bishop can't attack the pawn, and // weaker king can stop opposing opponent's king from penetrating. if (Utils.relative_rank_CS(strongerSide, weakerPawnSq) == RankC.RANK_7 && Utils.opposite_colors(bishopSq, weakerPawnSq) && Utils.square_distance(weakerPawnSq, weakerKingSq) <= Utils.square_distance(weakerPawnSq, strongerKingSq)) return ScaleFactorC.SCALE_FACTOR_DRAW; } return ScaleFactorC.SCALE_FACTOR_NONE; }