private static void generate_castle( int Side, bool Checks, Position pos, MoveStack[] ms, ref int mpos, int us) { if (pos.castle_impeded(us, Side) || (pos.can_castle_CR(Utils.make_castle_right(us, Side)) == 0)) { return; } // After castling, the rook and king final positions are the same in Chess960 // as they would be in standard chess. var kfrom = pos.king_square(us); var rfrom = pos.castle_rook_square(us, Side); var kto = Utils.relative_square(us, Side == CastlingSideC.KING_SIDE ? SquareC.SQ_G1 : SquareC.SQ_C1); var enemies = pos.pieces_C(us ^ 1); Debug.Assert(!pos.in_check()); int K = pos.chess960 ? kto > kfrom ? -1 : 1 : Side == CastlingSideC.KING_SIDE ? -1 : 1; for (Square s = kto; s != kfrom; s += (Square)K) { if ((pos.attackers_to(s) & enemies) != 0) { return; } } // Because we generate only legal castling moves we need to verify that // when moving the castling rook we do not discover some hidden checker. // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1. if (pos.chess960 && ((pos.attackers_to(kto, Utils.xor_bit(pos.occupied_squares, rfrom)) & enemies) != 0)) { return; } var m = Utils.make(kfrom, rfrom, MoveTypeC.CASTLING); if (Checks) { var ci = CheckInfoBroker.GetObject(); ci.CreateCheckInfo(pos); var givesCheck = pos.move_gives_check(m, ci); CheckInfoBroker.Free(); if (!givesCheck) { return; } } ms[mpos++].move = m; }
private static void generate_castle(CastlingSide Side, bool OnlyChecks, Position pos, MoveStack[] ms, ref int mpos, Color us) { if (pos.castle_impeded(us, Side) || (pos.can_castle_CR(Utils.make_castle_right(us, Side))==0) ) return; // After castling, the rook and king final positions are the same in Chess960 // as they would be in standard chess. Square kfrom = pos.king_square(us); Square rfrom = pos.castle_rook_square(us, Side); Square kto = Utils.relative_square(us, Side == CastlingSideC.KING_SIDE ? SquareC.SQ_G1 : SquareC.SQ_C1); Bitboard enemies = pos.pieces_C(us ^ 1); Debug.Assert(!pos.in_check()); for (Square s = Math.Min(kfrom, kto), e = Math.Max(kfrom, kto); s <= e; s++) if (s != kfrom // We are not in check && ((pos.attackers_to(s) & enemies) != 0)) return; // Because we generate only legal castling moves we need to verify that // when moving the castling rook we do not discover some hidden checker. // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1. if (pos.chess960 && ((pos.attackers_to(kto, Utils.xor_bit(pos.occupied_squares, rfrom)) & enemies) != 0)) return; Move m = Utils.make_castle(kfrom, rfrom); if (OnlyChecks) { CheckInfo ci = CheckInfoBroker.GetObject(); ci.CreateCheckInfo(pos); bool givesCheck = pos.move_gives_check(m, ci); CheckInfoBroker.Free(); if (!givesCheck) return; } ms[mpos++].move = m; }
/// 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; }
// check_is_dangerous() tests if a checking move can be pruned in qsearch(). // bestValue is updated only when returning false because in that case move // will be pruned. static bool check_is_dangerous(Position pos, Move move, Value futilityBase, Value beta) { Bitboard b, occ, oldAtt, newAtt, kingAtt; Square from, to, ksq; Piece pc; Color them; from = Utils.from_sq(move); to = Utils.to_sq(move); them = Utils.flip_C(pos.sideToMove); ksq = pos.king_square(them); kingAtt = Position.attacks_from_KING(ksq); pc = pos.piece_moved(move); occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq]; oldAtt = Position.attacks_from(pc, from, occ); newAtt = Position.attacks_from(pc, to, occ); // Rule 1. Checks which give opponent's king at most one escape square are dangerous b = kingAtt & ~pos.pieces_C(them) & ~newAtt & ~(1UL << to); if ((b & (b - 1)) == 0) // Catches also !b return true; // Rule 2. Queen contact check is very dangerous if (Utils.type_of(pc) == PieceTypeC.QUEEN && (Utils.bit_is_set(kingAtt, to) != 0)) return true; // Rule 3. Creating new double threats with checks b = pos.pieces_C(them) & newAtt & ~oldAtt & ~(1UL << ksq); while (b != 0) { // Note that here we generate illegal "double move"! if (futilityBase + Position.PieceValueEndgame[pos.piece_on(Utils.pop_1st_bit(ref b))] >= beta) return true; } return false; }
// 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, 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; }
/// 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; }
/// 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; }
/// 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; }
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; }
internal static void generate_evasion(Position pos, MoveStack[] ms, ref int mpos) { /// generate<EVASIONS> generates all pseudo-legal check evasions when the side /// to move is in check. Returns a pointer to the end of the move list. Debug.Assert(pos.in_check()); ulong b; int from, checksq; var checkersCnt = 0; var us = pos.sideToMove; var ksq = pos.king_square(us); ulong sliderAttacks = 0; var checkers = pos.st.checkersBB; Debug.Assert(checkers != 0); // Find squares attacked by slider checkers, we will remove them from the king // evasions so to skip known illegal moves avoiding useless legality check later. b = checkers; do { checkersCnt++; checksq = Utils.pop_lsb(ref b); Debug.Assert(Utils.color_of(pos.piece_on(checksq)) == Utils.flip_C(us)); switch (Utils.type_of(pos.piece_on(checksq))) { case PieceTypeC.BISHOP: sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq]; break; case PieceTypeC.ROOK: sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.ROOK][checksq]; break; case PieceTypeC.QUEEN: // If queen and king are far or not on a diagonal line we can safely // remove all the squares attacked in the other direction becuase are // not reachable by the king anyway. if ((Utils.between_bb(ksq, checksq) != 0) || ((Utils.bit_is_set(Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq], ksq)) == 0)) { sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.QUEEN][checksq]; } // Otherwise we need to use real rook attacks to check if king is safe // to move in the other direction. For example: king in B2, queen in A1 // a knight in B1, and we can safely move to C1. else { sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq] | pos.attacks_from_ROOK(checksq); } break; default: break; } } while (b != 0); // Generate evasions for king, capture and non capture moves b = Position.attacks_from_KING(ksq) & ~pos.pieces_C(us) & ~sliderAttacks; from = ksq; while (b != 0) { ms[mpos++].move = Utils.make_move(from, Utils.pop_lsb(ref b)); } // Generate evasions for other pieces only if not under a double check if (checkersCnt > 1) { return; } // Blocking evasions or captures of the checking piece var target = Utils.between_bb(checksq, ksq) | checkers; generate_all(GenType.EVASIONS, pos, ms, ref mpos, us, target, null); }
internal static void generate_legal(Position pos, MoveStack[] ms, ref int mpos) { /// generate<LEGAL> generates all the legal moves in the given position var pinned = pos.pinned_pieces(); Square ksq = pos.king_square(pos.sideToMove); if (pos.in_check()) { generate_evasion(pos, ms, ref mpos); } else { generate_non_evasion(pos, ms, ref mpos); } var last = mpos; var cur = 0; while (cur != last) { var curMove = ms[cur].move; //if (!pos.pl_move_is_legal(ms[cur].move, pinned)) if ((pinned != 0 || Utils.from_sq(curMove) == ksq || Utils.type_of_move(curMove) == MoveTypeC.ENPASSANT) && !pos.pl_move_is_legal(curMove, pinned)) { ms[cur].move = ms[--last].move; } else { cur++; } } mpos = last; }
private static void generate_all( GenType type, Position pos, MoveStack[] mlist, ref int mpos, int us, ulong target, CheckInfo ci) { var Checks = type == GenType.QUIET_CHECKS; generate_pawn_moves(us, type, pos, mlist, ref mpos, target, ci); generate_moves(PieceTypeC.KNIGHT, Checks, pos, mlist, ref mpos, us, target, ci); generate_moves(PieceTypeC.BISHOP, Checks, pos, mlist, ref mpos, us, target, ci); generate_moves(PieceTypeC.ROOK, Checks, pos, mlist, ref mpos, us, target, ci); generate_moves(PieceTypeC.QUEEN, Checks, pos, mlist, ref mpos, us, target, ci); if (!Checks && type != GenType.EVASIONS) { Square from = pos.king_square(us); Bitboard b = Position.attacks_from_KING(from) & target; // SERIALIZE(b); while (b != 0) { #if X64 Bitboard bb = b; b &= (b - 1); mlist[mpos++].move = ((Utils.BSFTable[((bb & (0xffffffffffffffff - bb + 1)) * DeBruijn_64) >> 58]) | (from << 6)); #else mlist[mpos++].move = Utils.make_move(from, Utils.pop_lsb(ref b)); #endif } } if (type != GenType.CAPTURES && type != GenType.EVASIONS && pos.can_castle_C(us) != 0) { generate_castle(CastlingSideC.KING_SIDE, Checks, pos, mlist, ref mpos, us); generate_castle(CastlingSideC.QUEEN_SIDE, Checks, pos, mlist, ref mpos, us); } }
// allows() tests whether the 'first' move at previous ply somehow makes the // 'second' move possible, for instance if the moving piece is the same in // both moves. Normally the second move is the threat (the best move returned // from a null search that fails low). internal static bool allows(Position pos, int first, int second) { Debug.Assert(Utils.is_ok_M(first)); Debug.Assert(Utils.is_ok_M(second)); Debug.Assert(Utils.color_of(pos.piece_on(Utils.from_sq(second))) == 1 - pos.sideToMove); Square m1to = Utils.to_sq(first); Square m1from = Utils.from_sq(first); Square m2to = Utils.to_sq(second); Square m2from = Utils.from_sq(second); // The piece is the same or second's destination was vacated by the first move if (m1to == m2from || m2to == m1from) { return true; } // Second one moves through the square vacated by first one if (Utils.bit_is_set(Utils.between_bb(m2from, m2to), m1from) != 0) { return true; } // Second's destination is defended by the first move's piece Bitboard m1att = Position.attacks_from(pos.piece_on(m1to), m1to, pos.occupied_squares ^ (ulong)m2from); if (Utils.bit_is_set(m1att, m2to) != 0) { return true; } // Second move gives a discovered check through the first's checking piece if (Utils.bit_is_set(m1att, pos.king_square(pos.sideToMove)) != 0 && Utils.bit_is_set(Utils.between_bb(m1to, pos.king_square(pos.sideToMove)), m2from) != 0) // TODO: removing condition asserts below { Debug.Assert(Utils.bit_is_set(Utils.between_bb(m1to, pos.king_square(pos.sideToMove)), m2from) != 0); return true; } return false; }
// check_is_dangerous() tests if a checking move can be pruned in qsearch(). // bestValue is updated only when returning false because in that case move // will be pruned. private static bool check_is_dangerous(Position pos, int move, int futilityBase, int beta) { //ulong b, occ, oldAtt, newAtt, kingAtt; //int from, to, ksq; //int pc; //int them; //from = Utils.from_sq(move); //to = Utils.to_sq(move); //them = Utils.flip_C(pos.sideToMove); //ksq = pos.king_square(them); //kingAtt = Position.attacks_from_KING(ksq); //pc = pos.piece_moved(move); //occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq]; //oldAtt = Position.attacks_from(pc, from, occ); //newAtt = Position.attacks_from(pc, to, occ); //// Rule 1. Checks which give opponent's king at most one escape square are dangerous //b = kingAtt & ~pos.pieces_C(them) & ~newAtt & ~(1UL << to); //if ((b & (b - 1)) == 0) // Catches also !b Piece pc = pos.piece_moved(move); Square from = Utils.from_sq(move); Square to = Utils.to_sq(move); Color them = pos.sideToMove ^ 1; Square ksq = pos.king_square(them); Bitboard enemies = pos.pieces_C(them); Bitboard kingAtt = Position.attacks_from_KING(ksq); Bitboard occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq]; Bitboard oldAtt = Position.attacks_from(pc, from, occ); Bitboard newAtt = Position.attacks_from(pc, to, occ); // Checks which give opponent's king at most one escape square are dangerous if (!Utils.more_than_one(kingAtt & ~(enemies | newAtt | (ulong)to))) { return true; } // Queen contact check is very dangerous if (Utils.type_of(pc) == PieceTypeC.QUEEN && (Utils.bit_is_set(kingAtt, to) != 0)) { return true; } // Creating new double threats with checks is dangerous Bitboard b = (enemies ^ (ulong)ksq) & newAtt & ~oldAtt; while (b != 0) { // Note that here we generate illegal "double move"! if (futilityBase + Position.PieceValue[PhaseC.EG][pos.piece_on(Utils.pop_lsb(ref b))] >= beta) { return true; } } return false; }
/// 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; }
/// 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; }
/// 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; }
/// 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; }
/// 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; }
/// 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, 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; }
/// 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; }
/// 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; } }
/// 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, 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; }
// evaluate_pieces_of_color<>() assigns bonuses and penalties to all the // pieces of a given color. private static int evaluate_pieces_of_color(int Us, bool Trace, Position pos, EvalInfo ei, ref int mobility) { var Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE); mobility = ScoreC.SCORE_ZERO; // Do not include in mobility squares protected by enemy pawns or occupied by our pieces var mobilityArea = ~(ei.attackedBy[Them][PieceTypeC.PAWN] | pos.byColorBB[Us]); #region Evaluate pieces ulong between = 0; var plPos = 0; int s, ksq; int mob; int f; int score, scores = ScoreC.SCORE_ZERO; var attackedByThemKing = ei.attackedBy[Them][PieceTypeC.KING]; var attackedByThemPawn = ei.attackedBy[Them][PieceTypeC.PAWN]; var kingRingThem = ei.kingRing[Them]; for (var Piece = PieceTypeC.KNIGHT; Piece < PieceTypeC.KING; Piece++) { score = ScoreC.SCORE_ZERO; ei.attackedBy[Us][Piece] = 0; var pl = pos.pieceList[Us][Piece]; plPos = 0; while ((s = pl[plPos++]) != SquareC.SQ_NONE) { // Find attacked squares, including x-ray attacks for bishops and rooks if (Piece == PieceTypeC.KNIGHT) { between = Utils.StepAttacksBB_KNIGHT[s]; } else if (Piece == PieceTypeC.QUEEN) { #if X64 b = Utils.BAttacks[s][(((pos.occupied_squares & Utils.BMasks[s]) * Utils.BMagics[s]) >> Utils.BShifts[s])] | Utils.RAttacks[s][(((pos.occupied_squares & Utils.RMasks[s]) * Utils.RMagics[s]) >> Utils.RShifts[s])]; #else between = Utils.bishop_attacks_bb(s, pos.occupied_squares) | Utils.rook_attacks_bb(s, pos.occupied_squares); #endif } else if (Piece == PieceTypeC.BISHOP) { #if X64 b = Utils.BAttacks[s][((( (pos.occupied_squares ^ (pos.byTypeBB[PieceTypeC.QUEEN] & pos.byColorBB[Us])) & Utils.BMasks[s]) * Utils.BMagics[s]) >> Utils.BShifts[s])]; #else between = Utils.bishop_attacks_bb(s, pos.occupied_squares ^ pos.pieces_PTC(PieceTypeC.QUEEN, Us)); #endif } else if (Piece == PieceTypeC.ROOK) { #if X64 b = Utils.RAttacks[s][((( (pos.occupied_squares ^ ((pos.byTypeBB[PieceTypeC.ROOK] | pos.byTypeBB[PieceTypeC.QUEEN]) & pos.byColorBB[Us])) & Utils.RMasks[s]) * Utils.RMagics[s]) >> Utils.RShifts[s])]; #else between = Utils.rook_attacks_bb( s, pos.occupied_squares ^ pos.pieces(PieceTypeC.ROOK, PieceTypeC.QUEEN, Us)); #endif } // Update attack info ei.attackedBy[Us][Piece] |= between; // King attacks if ((between & kingRingThem) != 0) { ei.kingAttackersCount[Us]++; ei.kingAttackersWeight[Us] += KingAttackWeights[Piece]; var bb = (between & attackedByThemKing); //ei.attackedBy[Them][PieceTypeC.KING]); if (bb != 0) { #if X64 bb -= (bb >> 1) & 0x5555555555555555UL; bb = ((bb >> 2) & 0x3333333333333333UL) + (bb & 0x3333333333333333UL); ei.kingAdjacentZoneAttacksCount[Us] += (int)((bb * 0x1111111111111111UL) >> 60); #else ei.kingAdjacentZoneAttacksCount[Us] += Bitcount.popcount_1s_Max15(bb); #endif } } // Mobility #if X64 Bitboard bmob = b & mobilityArea; if (Piece != PieceTypeC.QUEEN) { bmob -= (bmob >> 1) & 0x5555555555555555UL; bmob = ((bmob >> 2) & 0x3333333333333333UL) + (bmob & 0x3333333333333333UL); mob = (int)((bmob * 0x1111111111111111UL) >> 60); } else { bmob -= ((bmob >> 1) & 0x5555555555555555UL); bmob = ((bmob >> 2) & 0x3333333333333333UL) + (bmob & 0x3333333333333333UL); bmob = ((bmob >> 4) + bmob) & 0x0F0F0F0F0F0F0F0FUL; mob = (int)((bmob * 0x0101010101010101UL) >> 56); } #else mob = (Piece != PieceTypeC.QUEEN ? Bitcount.popcount_1s_Max15(between & mobilityArea) : Bitcount.popcount_1s_Full(between & mobilityArea)); #endif mobility += MobilityBonus[Piece][mob]; // Decrease score if we are attacked by an enemy pawn. Remaining part // of threat evaluation must be done later when we have full attack info. if ((attackedByThemPawn & Utils.SquareBB[s]) != 0) { score -= ThreatenedByPawnPenalty[Piece]; } else if ((Piece == PieceTypeC.BISHOP) && ((Utils.PseudoAttacks[Piece][pos.pieceList[Them][PieceTypeC.KING][0]] & Utils.SquareBB[s]) != 0)) { between = Utils.BetweenBB[s][pos.pieceList[Them][PieceTypeC.KING][0]] & pos.occupied_squares; if (!Utils.more_than_one(between)) { score += Utils.make_score(15, 25); } } // Bishop and knight outposts squares if ((Piece == PieceTypeC.BISHOP || Piece == PieceTypeC.KNIGHT) && (((pos.byTypeBB[PieceTypeC.PAWN] & pos.byColorBB[Them]) & Utils.AttackSpanMask[Us][s]) == 0)) { #region Evaluate outposts inlined // evaluate_outposts() evaluates bishop and knight outposts squares // Initial bonus based on square var bonus = OutpostBonus[Piece == PieceTypeC.BISHOP ? 1 : 0][s ^ (Us * 56)]; // Increase bonus if supported by pawn, especially if the opponent has // no minor piece which can exchange the outpost piece. if ((bonus != 0) && ((ei.attackedBy[Us][PieceTypeC.PAWN] & Utils.SquareBB[s]) != 0)) { if (((pos.byTypeBB[PieceTypeC.KNIGHT] & pos.byColorBB[Them]) == 0) && (((((0xAA55AA55AA55AA55UL & Utils.SquareBB[s]) != 0) ? 0xAA55AA55AA55AA55UL : ~0xAA55AA55AA55AA55UL) & (pos.byTypeBB[PieceTypeC.BISHOP] & pos.byColorBB[Them])) == 0)) { bonus += bonus + bonus / 2; } else { bonus += bonus / 2; } } score += ((bonus << 16) + bonus); // Utils.make_score(bonus, bonus); #endregion } if ((Piece == PieceTypeC.ROOK || Piece == PieceTypeC.QUEEN) && Utils.relative_rank_CS(Us, s) >= RankC.RANK_5) { // Major piece on 7th rank if (Utils.relative_rank_CS(Us, s) == RankC.RANK_7 && Utils.relative_rank_CS(Us, pos.king_square(Them)) == RankC.RANK_8) score += (Piece == PieceTypeC.ROOK ? RookOn7thBonus : QueenOn7thBonus); // Major piece attacking pawns on the same rank Bitboard pawns = pos.pieces_PTC(PieceTypeC.PAWN, Them) & Utils.rank_bb_S(s); if (pawns != 0) { score += (Piece == PieceTypeC.ROOK ? RookOnPawnBonus : QueenOnPawnBonus) * Bitcount.popcount_1s_Max15(pawns); } } // Special extra evaluation for bishops if (pos.chess960 && (Piece == PieceTypeC.BISHOP)) { // An important Chess960 pattern: A cornered bishop blocked by // a friendly pawn diagonally in front of it is a very serious // problem, especially when that pawn is also blocked. if (s == Utils.relative_square(Us, SquareC.SQ_A1) || s == Utils.relative_square(Us, SquareC.SQ_H1)) { var d = Utils.pawn_push(Us) + (Utils.file_of(s) == FileC.FILE_A ? SquareC.DELTA_E : SquareC.DELTA_W); if (pos.piece_on(s + d) == Utils.make_piece(Us, PieceTypeC.PAWN)) { if (!pos.is_empty(s + d + Utils.pawn_push(Us))) { score -= 2 * TrappedBishopA1H1Penalty; } else if (pos.piece_on(s + 2 * d) == Utils.make_piece(Us, PieceTypeC.PAWN)) { score -= TrappedBishopA1H1Penalty; } else { score -= TrappedBishopA1H1Penalty / 2; } } } } // Special extra evaluation for rooks if (Piece == PieceTypeC.ROOK) { // Open and half-open files f = (s & 7); var halfOpenUs = ((Us == ColorC.WHITE) ? (ei.pi.halfOpenFilesWHITE & (1 << f)) : (ei.pi.halfOpenFilesBLACK & (1 << f))) != 0; if (halfOpenUs) { if (((Them == ColorC.WHITE) ? (ei.pi.halfOpenFilesWHITE & (1 << f)) : (ei.pi.halfOpenFilesBLACK & (1 << f))) != 0) { score += RookOpenFileBonus; } else { score += RookHalfOpenFileBonus; } } // Penalize rooks which are trapped inside a king. Penalize more if // king has lost right to castle. if (mob > 6 || halfOpenUs) { continue; } ksq = pos.pieceList[Us][PieceTypeC.KING][0]; if (((ksq >> 3) ^ (Us * 7)) == RankC.RANK_1 || (ksq >> 3) == (s >> 3)) { if ((ksq & 7) >= FileC.FILE_E) { if (f > (ksq & 7)) { // Is there a half-open file between the king and the edge of the board? if (((Us == ColorC.WHITE) ? (ei.pi.halfOpenFilesWHITE & ~((1 << ((ksq & 7) + 1)) - 1)) : (ei.pi.halfOpenFilesBLACK & ~((1 << ((ksq & 7) + 1)) - 1))) == 0) { score -= ((((pos.st.castleRights & (CastleRightC.WHITE_ANY << (Us << 1))) != 0) ? (TrappedRookPenalty - mob * 16) / 2 : (TrappedRookPenalty - mob * 16)) << 16); } } } else { if (f < (ksq & 7)) { // Is there a half-open file between the king and the edge of the board? if (((Us == ColorC.WHITE) ? (ei.pi.halfOpenFilesWHITE & ((1 << (ksq & 7)) - 1)) : (ei.pi.halfOpenFilesBLACK & ((1 << (ksq & 7)) - 1))) == 0) { score -= ((((pos.st.castleRights & (CastleRightC.WHITE_ANY << (Us << 1))) != 0) ? (TrappedRookPenalty - mob * 16) / 2 : (TrappedRookPenalty - mob * 16)) << 16); } } } } } } scores += score; if (Trace) { TracedScores[Us][Piece] = score; } } #endregion // Sum up all attacked squares ei.attackedBy[Us][0] = ei.attackedBy[Us][PieceTypeC.PAWN] | ei.attackedBy[Us][PieceTypeC.KNIGHT] | ei.attackedBy[Us][PieceTypeC.BISHOP] | ei.attackedBy[Us][PieceTypeC.ROOK] | ei.attackedBy[Us][PieceTypeC.QUEEN] | ei.attackedBy[Us][PieceTypeC.KING]; return scores; }
/// 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; }
// connected_moves() tests whether two moves are 'connected' in the sense // that the first move somehow made the second move possible (for instance // if the moving piece is the same in both moves). The first move is assumed // to be the move that was made to reach the current position, while the // second move is assumed to be a move from the current position. internal static bool connected_moves(Position pos, Move m1, Move m2) { Square f1, t1, f2, t2; Piece p1, p2; Square ksq; Debug.Assert(Utils.is_ok_M(m1)); Debug.Assert(Utils.is_ok_M(m2)); // Case 1: The moving piece is the same in both moves f2 = Utils.from_sq(m2); t1 = Utils.to_sq(m1); if (f2 == t1) return true; // Case 2: The destination square for m2 was vacated by m1 t2 = Utils.to_sq(m2); f1 = Utils.from_sq(m1); if (t2 == f1) return true; // Case 3: Moving through the vacated square p2 = pos.piece_on(f2); if (piece_is_slider(p2) && (Utils.bit_is_set(Utils.between_bb(f2, t2), f1) != 0)) return true; // Case 4: The destination square for m2 is defended by the moving piece in m1 p1 = pos.piece_on(t1); if ((Utils.bit_is_set(pos.attacks_from_PS(p1, t1), t2)) != 0) return true; // Case 5: Discovered check, checking piece is the piece moved in m1 ksq = pos.king_square(pos.sideToMove); if ( piece_is_slider(p1) && (Utils.bit_is_set(Utils.between_bb(t1, ksq), f2) != 0) && (Utils.bit_is_set(Position.attacks_from(p1, t1, Utils.xor_bit(pos.occupied_squares, f2)), ksq) != 0) ) return true; return false; }
/// 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; }