public KPKPosition(uint idx) { wksq = (Square)((idx >> 0) & 0x3F); bksq = (Square)((idx >> 6) & 0x3F); us = (Color)((idx >> 12) & 0x01); psq = Types.make_square((File)((idx >> 13) & 0x03), (Rank)(RankS.RANK_7 - (idx >> 15))); result = Result.UNKNOWN; // Check if two pieces are on the same square or if a king can be captured if (BitBoard.square_distance(wksq, bksq) <= 1 || wksq == psq || bksq == psq || (us == ColorS.WHITE && (BitBoard.StepAttacksBB[PieceTypeS.PAWN][psq] & BitBoard.SquareBB[bksq]) != 0)) { result = Result.INVALID; } else if (us == ColorS.WHITE) { // Immediate win if pawn can be promoted without getting captured if (Types.rank_of(psq) == RankS.RANK_7 && wksq != psq + SquareS.DELTA_N && (BitBoard.square_distance(bksq, psq + SquareS.DELTA_N) > 1 || (BitBoard.StepAttacksBB[PieceTypeS.KING][wksq] & BitBoard.SquareBB[psq + SquareS.DELTA_N]) != 0)) { result = Result.WIN; } } // Immediate draw if it is a stalemate or a king captures undefended pawn else if (0 == (BitBoard.StepAttacksBB[PieceTypeS.KING][bksq] & ~(BitBoard.StepAttacksBB[PieceTypeS.KING][wksq] | BitBoard.StepAttacksBB[PieceTypeS.PAWN][psq])) || (BitBoard.StepAttacksBB[PieceTypeS.KING][bksq] & BitBoard.SquareBB[psq] & ~BitBoard.StepAttacksBB[PieceTypeS.KING][wksq]) != 0) { result = Result.DRAW; } }
/// move_to_uci() converts a move to a string in coordinate notation /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960 /// mode. Internally castling moves are always encoded as "king captures rook". public static string move_to_uci(Move m, bool chess960) { Square from = Types.from_sq(m); Square to = Types.to_sq(m); if (m == MoveS.MOVE_NONE) { return("(none)"); } if (m == MoveS.MOVE_NULL) { return("0000"); } if (Types.type_of_move(m) == MoveTypeS.CASTLING && !chess960) { to = Types.make_square((to > from ? FileS.FILE_G : FileS.FILE_C), Types.rank_of(from)); } string move = Types.square_to_string(from) + Types.square_to_string(to); if (Types.type_of_move(m) == MoveTypeS.PROMOTION) { move += PieceToChar[ColorS.BLACK][Types.promotion_type(m)]; // Lower case } return(move); }
/// KR vs KP. This is a somewhat tricky endgame to evaluate precisely without /// a bitbase. The function below returns drawish scores when the pawn is /// far advanced with support of the king, while the attacking king is far /// away. public Value KRKP(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 0)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 1)); Square wksq = Types.relative_square(strongSide, pos.king_square(strongSide)); Square bksq = Types.relative_square(strongSide, pos.king_square(weakSide)); Square rsq = Types.relative_square(strongSide, pos.list(strongSide, PieceTypeS.ROOK)[0]); Square psq = Types.relative_square(strongSide, pos.list(weakSide, PieceTypeS.PAWN)[0]); Square queeningSq = Types.make_square(Types.file_of(psq), RankS.RANK_1); Value result; // If the stronger side's king is in front of the pawn, it's a win if (wksq < psq && Types.file_of(wksq) == Types.file_of(psq)) { result = ValueS.RookValueEg - (BitBoard.square_distance(wksq, psq)); } // If the weaker side's king is too far from the pawn and the rook, // it's a win else if (BitBoard.square_distance(bksq, psq) >= 3 + ((pos.side_to_move() == weakSide)?1:0) && BitBoard.square_distance(bksq, rsq) >= 3) { result = ValueS.RookValueEg - (BitBoard.square_distance(wksq, psq)); } // If the pawn is far advanced and supported by the defending king, // the position is drawish else if (Types.rank_of(bksq) <= RankS.RANK_3 && BitBoard.square_distance(bksq, psq) == 1 && Types.rank_of(wksq) >= RankS.RANK_4 && BitBoard.square_distance(wksq, psq) > 2 + ((pos.side_to_move() == strongSide)?1:0)) { result = 80 - 8 * BitBoard.square_distance(wksq, psq); } else { result = (Value)(200) - 8 * (BitBoard.square_distance(wksq, psq + SquareS.DELTA_S) - BitBoard.square_distance(bksq, psq + SquareS.DELTA_S) - BitBoard.square_distance(psq, queeningSq)); } return(strongSide == pos.side_to_move() ? result : -result); }
public Result classify(KPKPosition[] db, Color Us) { // White to Move: If one move leads to a position classified as WIN, the result // of the current position is WIN. If all moves lead to positions classified // as DRAW, the current position is classified as DRAW, otherwise the current // position is classified as UNKNOWN. // // Black to Move: If one move leads to a position classified as DRAW, the result // of the current position is DRAW. If all moves lead to positions classified // as WIN, the position is classified as WIN, otherwise the current position is // classified as UNKNOWN. Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE); Result r = Result.INVALID; Bitboard b = BitBoard.StepAttacksBB[PieceTypeS.KING][Us == ColorS.WHITE ? wksq : bksq]; while (b != 0) { r |= (Us == ColorS.WHITE) ? db[Bitbases.index(Them, bksq, BitBoard.pop_lsb(ref b), psq)].result : db[Bitbases.index(Them, BitBoard.pop_lsb(ref b), wksq, psq)].result; } if (Us == ColorS.WHITE && Types.rank_of(psq) < RankS.RANK_7) { Square s = (psq + SquareS.DELTA_N); r |= db[Bitbases.index(ColorS.BLACK, bksq, wksq, s)].result; // Single push if (Types.rank_of(psq) == RankS.RANK_2 && s != wksq && s != bksq) { r |= db[Bitbases.index(ColorS.BLACK, bksq, wksq, s + SquareS.DELTA_N)].result; // Double push } } if (Us == ColorS.WHITE) { return(result = (r & Result.WIN) != 0 ? Result.WIN : (r & Result.UNKNOWN) != 0 ? Result.UNKNOWN : Result.DRAW); } else { return(result = (r & Result.DRAW) != 0 ? Result.DRAW : (r & Result.UNKNOWN) != 0 ? Result.UNKNOWN : Result.WIN); } }
/// 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. public ScaleFactor KPsK(Position pos) { Debug.Assert(pos.non_pawn_material(strongSide) == ValueS.VALUE_ZERO); Debug.Assert(pos.count(strongSide, PieceTypeS.PAWN) >= 2); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 0)); Square ksq = pos.king_square(weakSide); Bitboard pawns = pos.pieces_color_piecetype(strongSide, PieceTypeS.PAWN); Square psq = pos.list(strongSide, PieceTypeS.PAWN)[0]; // If all pawns are ahead of the king, on a single rook file and // the king is within one file of the pawns, it's a draw. if (0 == (pawns & ~BitBoard.in_front_bb(weakSide, Types.rank_of(ksq))) && !((pawns & ~BitBoard.FileABB) != 0 && (pawns & ~BitBoard.FileHBB) != 0) && BitBoard.file_distance(ksq, psq) <= 1) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } return(ScaleFactorS.SCALE_FACTOR_NONE); }
/// KP vs K. This endgame is evaluated with the help of a bitbase. public Value KPK(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.VALUE_ZERO, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 0)); // Assume strongSide is white and the pawn is on files A-D Square wksq = normalize(pos, strongSide, pos.king_square(strongSide)); Square bksq = normalize(pos, strongSide, pos.king_square(weakSide)); Square psq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.PAWN)[0]); Color us = strongSide == pos.side_to_move() ? ColorS.WHITE : ColorS.BLACK; if (!Bitbases.probe_kpk(wksq, psq, bksq, us)) { return(ValueS.VALUE_DRAW); } Value result = ValueS.VALUE_KNOWN_WIN + ValueS.PawnValueEg + Types.rank_of(psq); return(strongSide == pos.side_to_move() ? result : -result); }
/// KP vs KP. 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, it 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). public ScaleFactor KPKP(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.VALUE_ZERO, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.VALUE_ZERO, 1)); // Assume strongSide is white and the pawn is on files A-D Square wksq = normalize(pos, strongSide, pos.king_square(strongSide)); Square bksq = normalize(pos, strongSide, pos.king_square(weakSide)); Square psq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.PAWN)[0]); Color us = strongSide == pos.side_to_move() ? ColorS.WHITE : ColorS.BLACK; // 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 (Types.rank_of(psq) >= RankS.RANK_5 && Types.file_of(psq) != FileS.FILE_A) { return(ScaleFactorS.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(Bitbases.probe_kpk(wksq, psq, bksq, us) ? ScaleFactorS.SCALE_FACTOR_NONE : ScaleFactorS.SCALE_FACTOR_DRAW); }
/// Entry::shelter_storm() calculates shelter and storm penalties for the file /// the king is on, as well as the two adjacent files. public Value shelter_storm(Position pos, Square ksq, Color Us) { Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE); Value safety = Pawns.MaxSafetyBonus; Bitboard b = pos.pieces_piecetype(PieceTypeS.PAWN) & (BitBoard.in_front_bb(Us, Types.rank_of(ksq)) | BitBoard.rank_bb_square(ksq)); Bitboard ourPawns = b & pos.pieces_color(Us); Bitboard theirPawns = b & pos.pieces_color(Them); Rank rkUs, rkThem; File kf = Math.Max(FileS.FILE_B, Math.Min(FileS.FILE_G, Types.file_of(ksq))); for (File f = kf - 1; f <= kf + 1; ++f) { b = ourPawns & BitBoard.file_bb_file(f); rkUs = b != 0 ? Types.relative_rank_square(Us, BitBoard.backmost_sq(Us, b)) : RankS.RANK_1; b = theirPawns & BitBoard.file_bb_file(f); rkThem = b != 0 ? Types.relative_rank_square(Us, BitBoard.frontmost_sq(Them, b)) : RankS.RANK_1; if ((MiddleEdges & BitBoard.SquareBB[Types.make_square(f, rkThem)]) != 0 && Types.file_of(ksq) == f && Types.relative_rank_square(Us, ksq) == rkThem - 1) { safety += 200; } else { safety -= ShelterWeakness[rkUs] + StormDanger[rkUs == RankS.RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem]; } } return(safety); }
// init_magics() computes all rook and bishop attacks at startup. Magic // bitboards are used to look up attacks of sliding pieces. As a reference see // chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we // use the so called "fancy" approach. public static void init_magics(PieceType pt, Bitboard[][] attacks, Bitboard[] magics, Bitboard[] masks, uint[] shifts, Square[] deltas, Fn index) { int[][] MagicBoosters = new int[2][] { new int[] { 969, 1976, 2850, 542, 2069, 2852, 1708, 164 }, new int[] { 3101, 552, 3555, 926, 834, 26, 2131, 1117 } }; RKISS rk = new RKISS(); Bitboard[] occupancy = new UInt64[4096], reference = new UInt64[4096]; Bitboard edges, b; int i, size, booster; for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; s++) { // Board edges are not considered in the relevant occupancies edges = ((BitBoard.Rank1BB | BitBoard.Rank8BB) & ~BitBoard.rank_bb_square(s)) | ((BitBoard.FileABB | BitBoard.FileHBB) & ~BitBoard.file_bb_square(s)); // Given a square 's', the mask is the bitboard of sliding attacks from // 's' computed on an empty board. The index must be big enough to contain // all the attacks for each possible subset of the mask and so is 2 power // the number of 1s of the mask. Hence we deduce the size of the shift to // apply to the 64 or 32 bits word to get the index. masks[s] = sliding_attack(deltas, s, 0) & ~edges; shifts[s] = 32 - (uint)Bitcount.popcount_Max15(masks[s]); // Use Carry-Rippler trick to enumerate all subsets of masks[s] and // store the corresponding sliding attack bitboard in reference[]. b = 0; size = 0; do { occupancy[size] = b; reference[size] = sliding_attack(deltas, s, b); size++; b = (b - masks[s]) & masks[s]; } while (b != 0); // Set the offset for the table of the next square. We have individual // table sizes for each square with "Fancy Magic Bitboards". attacks[s] = new Bitboard[size]; booster = MagicBoosters[0][Types.rank_of(s)]; // Find a magic for square 's' picking up an (almost) random number // until we find the one that passes the verification test. do { do { magics[s] = rk.magic_rand(booster); }while (Bitcount.popcount_Max15((magics[s] * masks[s]) >> 56) < 6); Array.Clear(attacks[s], 0, size); // A good magic must map every possible occupancy to an index that // looks up the correct sliding attack in the attacks[s] database. // Note that we build up the database for square 's' as a side // effect of verifying the magic. for (i = 0; i < size; i++) { Bitboard attack = attacks[s][index(s, occupancy[i], pt)]; if (attack != 0 && attack != reference[i]) { break; } Debug.Assert(reference[i] != 0); //attack = reference[i]; attacks[s][index(s, occupancy[i], pt)] = reference[i]; } } while (i != size); } }
/// Bitboards::init() initializes various bitboard tables. It is called at /// startup and relies on global objects to be already zero-initialized. public static void init() { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { BSFTable[bsf_index(SquareBB[s] = 1UL << s)] = s; } for (Bitboard b = 1; b < 256; ++b) { MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b); } for (File f = FileS.FILE_A; f <= FileS.FILE_H; ++f) { FileBB[f] = f > FileS.FILE_A ? FileBB[f - 1] << 1 : FileABB; } for (Rank r = RankS.RANK_1; r <= RankS.RANK_8; ++r) { RankBB[r] = r > RankS.RANK_1 ? RankBB[r - 1] << 8 : Rank1BB; } for (File f = FileS.FILE_A; f <= FileS.FILE_H; ++f) { AdjacentFilesBB[f] = (f > FileS.FILE_A ? FileBB[f - 1] : 0) | (f < FileS.FILE_H ? FileBB[f + 1] : 0); } for (int c = ColorS.WHITE; c <= ColorS.BLACK; c++) { InFrontBB[c] = new Bitboard[RankS.RANK_NB]; } for (Rank r = RankS.RANK_1; r < RankS.RANK_8; ++r) { InFrontBB[ColorS.WHITE][r] = ~(InFrontBB[ColorS.BLACK][r + 1] = InFrontBB[ColorS.BLACK][r] | RankBB[r]); } for (int c = ColorS.WHITE; c <= ColorS.BLACK; c++) { ForwardBB[c] = new Bitboard[SquareS.SQUARE_NB]; PawnAttackSpan[c] = new Bitboard[SquareS.SQUARE_NB]; PassedPawnMask[c] = new Bitboard[SquareS.SQUARE_NB]; } for (Color c = ColorS.WHITE; c <= ColorS.BLACK; ++c) { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { ForwardBB[c][s] = InFrontBB[c][Types.rank_of(s)] & FileBB[Types.file_of(s)]; PawnAttackSpan[c][s] = InFrontBB[c][Types.rank_of(s)] & AdjacentFilesBB[Types.file_of(s)]; PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s]; } } for (Square c = 0; c < SquareS.SQUARE_NB; c++) { SquareDistance[c] = new int[SquareS.SQUARE_NB]; DistanceRingsBB[c] = new Bitboard[8]; } for (Square s1 = SquareS.SQ_A1; s1 <= SquareS.SQ_H8; ++s1) { for (Square s2 = SquareS.SQ_A1; s2 <= SquareS.SQ_H8; ++s2) { if (s1 != s2) { SquareDistance[s1][s2] = Math.Max(file_distance(s1, s2), rank_distance(s1, s2)); DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= SquareBB[s2]; } } } int[][] steps = new int[7][]; steps[0] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[1] = new int[] { 7, 9, 0, 0, 0, 0, 0, 0, 0 }; steps[2] = new int[] { 17, 15, 10, 6, -6, -10, -15, -17, 0 }; steps[3] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[4] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[5] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; steps[6] = new int[] { 9, 7, -7, -9, 8, 1, -1, -8, 0 }; for (Piece p = PieceS.NO_PIECE; p < PieceS.PIECE_NB; p++) { StepAttacksBB[p] = new Bitboard[SquareS.SQUARE_NB]; } for (Color c = ColorS.WHITE; c <= ColorS.BLACK; ++c) { for (PieceType pt = PieceTypeS.PAWN; pt <= PieceTypeS.KING; ++pt) { for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; ++s) { for (int i = 0; steps[pt][i] != 0; ++i) { Square to = s + (Square)(c == ColorS.WHITE ? steps[pt][i] : -steps[pt][i]); if (Types.is_ok_square(to) && BitBoard.square_distance(s, to) < 3) { StepAttacksBB[Types.make_piece(c, pt)][s] |= SquareBB[to]; } } } } } Square[] RDeltas = new Square[] { SquareS.DELTA_N, SquareS.DELTA_E, SquareS.DELTA_S, SquareS.DELTA_W }; Square[] BDeltas = new Square[] { SquareS.DELTA_NE, SquareS.DELTA_SE, SquareS.DELTA_SW, SquareS.DELTA_NW }; init_magics(PieceTypeS.ROOK, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index); init_magics(PieceTypeS.BISHOP, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index); for (PieceType pt = PieceTypeS.NO_PIECE_TYPE; pt < PieceTypeS.PIECE_TYPE_NB; pt++) { PseudoAttacks[pt] = new Bitboard[SquareS.SQUARE_NB]; } for (Square s = SquareS.SQ_A1; s <= SquareS.SQ_H8; s++) { BetweenBB[s] = new Bitboard[SquareS.SQUARE_NB]; LineBB[s] = new Bitboard[SquareS.SQUARE_NB]; } for (Square s1 = SquareS.SQ_A1; s1 <= SquareS.SQ_H8; ++s1) { PseudoAttacks[PieceTypeS.QUEEN][s1] = PseudoAttacks[PieceTypeS.BISHOP][s1] = attacks_bb_SBBPT(s1, 0, PieceTypeS.BISHOP); PseudoAttacks[PieceTypeS.QUEEN][s1] |= PseudoAttacks[PieceTypeS.ROOK][s1] = attacks_bb_SBBPT(s1, 0, PieceTypeS.ROOK); for (Square s2 = SquareS.SQ_A1; s2 <= SquareS.SQ_H8; ++s2) { Piece pc = (PseudoAttacks[PieceTypeS.BISHOP][s1] & SquareBB[s2]) != 0 ? PieceS.W_BISHOP : (PseudoAttacks[PieceTypeS.ROOK][s1] & SquareBB[s2]) != 0 ? PieceS.W_ROOK : PieceS.NO_PIECE; if (pc == PieceS.NO_PIECE) { continue; } LineBB[s1][s2] = (attacks_bb_PSBB(pc, s1, 0) & attacks_bb_PSBB(pc, s2, 0)) | SquareBB[s1] | SquareBB[s2]; BetweenBB[s1][s2] = attacks_bb_PSBB(pc, s1, SquareBB[s2]) & attacks_bb_PSBB(pc, s2, SquareBB[s1]); } } }
public static Bitboard rank_bb_square(Square s) { return(RankBB[Types.rank_of(s)]); }
public static int rank_distance(Square s1, Square s2) { return(Math.Abs(Types.rank_of(s1) - Types.rank_of(s2))); }
/// KRP vs KR. This function knows a handful of the most important classes of /// drawn positions, but is far from perfect. It would probably be a good idea /// to add more knowledge in the future. /// /// It would also be nice to rewrite the actual code for this function, /// which is mostly copied from Glaurung 1.x, and isn't very pretty. public ScaleFactor KRPKR(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.RookValueMg, 1)); Debug.Assert(verify_material(pos, weakSide, ValueS.RookValueMg, 0)); // Assume strongSide is white and the pawn is on files A-D Square wksq = normalize(pos, strongSide, pos.king_square(strongSide)); Square bksq = normalize(pos, strongSide, pos.king_square(weakSide)); Square wrsq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.ROOK)[0]); Square wpsq = normalize(pos, strongSide, pos.list(strongSide, PieceTypeS.PAWN)[0]); Square brsq = normalize(pos, strongSide, pos.list(weakSide, PieceTypeS.ROOK)[0]); File f = Types.file_of(wpsq); Rank r = Types.rank_of(wpsq); Square queeningSq = Types.make_square(f, RankS.RANK_8); int tempo = (pos.side_to_move() == strongSide ? 1 : 0); // If the pawn is not too far advanced and the defending king defends the // queening square, use the third-rank defence. if (r <= RankS.RANK_5 && BitBoard.square_distance(bksq, queeningSq) <= 1 && wksq <= SquareS.SQ_H5 && (Types.rank_of(brsq) == RankS.RANK_6 || (r <= RankS.RANK_3 && Types.rank_of(wrsq) != RankS.RANK_6))) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // The defending side saves a draw by checking from behind in case the pawn // has advanced to the 6th rank with the king behind. if (r == RankS.RANK_6 && BitBoard.square_distance(bksq, queeningSq) <= 1 && Types.rank_of(wksq) + tempo <= RankS.RANK_6 && (Types.rank_of(brsq) == RankS.RANK_1 || (0 == tempo && Math.Abs(Types.file_of(brsq) - f) >= 3))) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } if (r >= RankS.RANK_6 && bksq == queeningSq && Types.rank_of(brsq) == RankS.RANK_1 && (0 == tempo || BitBoard.square_distance(wksq, wpsq) >= 2)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7 // and the black rook is behind the pawn. if (wpsq == SquareS.SQ_A7 && wrsq == SquareS.SQ_A8 && (bksq == SquareS.SQ_H7 || bksq == SquareS.SQ_G7) && Types.file_of(brsq) == FileS.FILE_A && (Types.rank_of(brsq) <= RankS.RANK_3 || Types.file_of(wksq) >= FileS.FILE_D || Types.rank_of(wksq) <= RankS.RANK_5)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // If the defending king blocks the pawn and the attacking king is too far // away, it's a draw. if (r <= RankS.RANK_5 && bksq == wpsq + SquareS.DELTA_N && BitBoard.square_distance(wksq, wpsq) - tempo >= 2 && BitBoard.square_distance(wksq, brsq) - tempo >= 2) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } // Pawn on the 7th rank supported by the rook from behind usually wins if the // attacking king is closer to the queening square than the defending king, // and the defending king cannot gain tempi by threatening the attacking rook. if (r == RankS.RANK_7 && f != FileS.FILE_A && Types.file_of(wrsq) == f && wrsq != queeningSq && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, queeningSq) - 2 + tempo) && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, wrsq) + tempo)) { return((ScaleFactor)(ScaleFactorS.SCALE_FACTOR_MAX - 2 * BitBoard.square_distance(wksq, queeningSq))); } // Similar to the above, but with the pawn further back if (f != FileS.FILE_A && Types.file_of(wrsq) == f && wrsq < wpsq && (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, queeningSq) - 2 + tempo) && (BitBoard.square_distance(wksq, wpsq + SquareS.DELTA_N) < BitBoard.square_distance(bksq, wpsq + SquareS.DELTA_N) - 2 + tempo) && (BitBoard.square_distance(bksq, wrsq) + tempo >= 3 || (BitBoard.square_distance(wksq, queeningSq) < BitBoard.square_distance(bksq, wrsq) + tempo && (BitBoard.square_distance(wksq, wpsq + SquareS.DELTA_N) < BitBoard.square_distance(bksq, wrsq) + tempo)))) { return((ScaleFactor)(ScaleFactorS.SCALE_FACTOR_MAX - 8 * BitBoard.square_distance(wpsq, queeningSq) - 2 * BitBoard.square_distance(wksq, queeningSq))); } // If the pawn is not far advanced, and the defending king is somewhere in // the pawn's path, it's probably a draw. if (r <= RankS.RANK_4 && bksq > wpsq) { if (Types.file_of(bksq) == Types.file_of(wpsq)) { return(10); } if (Math.Abs(Types.file_of(bksq) - Types.file_of(wpsq)) == 1 && BitBoard.square_distance(wksq, bksq) > 2) { return(24 - 2 * BitBoard.square_distance(wksq, bksq)); } } return(ScaleFactorS.SCALE_FACTOR_NONE); }
// evaluate_pieces() assigns bonuses and penalties to the pieces of a given color public static Score evaluate_pieces(Position pos, EvalInfo ei, Score[] mobility, Bitboard[] mobilityArea, PieceType Pt, Color Us, bool Trace) { if (Us == ColorS.WHITE && Pt == PieceTypeS.KING) { return(ScoreS.SCORE_ZERO); } Bitboard b; Square s; Score score = ScoreS.SCORE_ZERO; PieceType NextPt = (Us == ColorS.WHITE ? Pt : (Pt + 1)); Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE); Square[] pl = pos.list(Us, Pt); int plPos = 0; ei.attackedBy[Us][Pt] = 0; while ((s = pl[plPos++]) != SquareS.SQ_NONE) { // Find attacked squares, including x-ray attacks for bishops and rooks b = Pt == PieceTypeS.BISHOP ? BitBoard.attacks_bb_SBBPT(s, pos.pieces() ^ pos.pieces_color_piecetype(Us, PieceTypeS.QUEEN), PieceTypeS.BISHOP) : Pt == PieceTypeS.ROOK ? BitBoard.attacks_bb_SBBPT(s, pos.pieces() ^ pos.pieces_color_piecetype(Us, PieceTypeS.ROOK, PieceTypeS.QUEEN), PieceTypeS.ROOK) : pos.attacks_from_square_piecetype(s, Pt); if ((ei.pinnedPieces[Us] & BitBoard.SquareBB[s]) != 0) { b &= BitBoard.LineBB[pos.king_square(Us)][s]; } ei.attackedBy[Us][PieceTypeS.ALL_PIECES] |= ei.attackedBy[Us][Pt] |= b; if ((b & ei.kingRing[Them]) != 0) { ei.kingAttackersCount[Us]++; ei.kingAttackersWeight[Us] += KingAttackWeights[Pt]; Bitboard bb = (b & ei.attackedBy[Them][PieceTypeS.KING]); if (bb != 0) { ei.kingAdjacentZoneAttacksCount[Us] += Bitcount.popcount_Max15(bb); } } if (Pt == PieceTypeS.QUEEN) { b &= ~(ei.attackedBy[Them][PieceTypeS.KNIGHT] | ei.attackedBy[Them][PieceTypeS.BISHOP] | ei.attackedBy[Them][PieceTypeS.ROOK]); } int mob = (Pt != PieceTypeS.QUEEN ? Bitcount.popcount_Max15(b & mobilityArea[Us]) : Bitcount.popcount(b & mobilityArea[Us])); mobility[Us] += MobilityBonus[Pt][mob]; // Decrease score if we are attacked by an enemy pawn. The remaining part // of threat evaluation must be done later when we have full attack info. if ((ei.attackedBy[Them][PieceTypeS.PAWN] & BitBoard.SquareBB[s]) != 0) { score -= ThreatenedByPawn[Pt]; } if (Pt == PieceTypeS.BISHOP || Pt == PieceTypeS.KNIGHT) { // Penalty for bishop with same coloured pawns if (Pt == PieceTypeS.BISHOP) { score -= BishopPawns * ei.pi.pawns_on_same_color_squares(Us, s); } // Bishop and knight outposts squares if (0 == (pos.pieces_color_piecetype(Them, PieceTypeS.PAWN) & BitBoard.pawn_attack_span(Us, s))) { score += evaluate_outposts(pos, ei, s, Pt, Us); } // Bishop or knight behind a pawn if (Types.relative_rank_square(Us, s) < RankS.RANK_5 && (pos.pieces_piecetype(PieceTypeS.PAWN) & BitBoard.SquareBB[(s + Types.pawn_push(Us))]) != 0) { score += MinorBehindPawn; } } if (Pt == PieceTypeS.ROOK) { // Rook piece attacking enemy pawns on the same rank/file if (Types.relative_rank_square(Us, s) >= RankS.RANK_5) { Bitboard pawns = pos.pieces_color_piecetype(Them, PieceTypeS.PAWN) & BitBoard.PseudoAttacks[PieceTypeS.ROOK][s]; if (pawns != 0) { score += Bitcount.popcount_Max15(pawns) * RookOnPawn; } } // Give a bonus for a rook on a open or semi-open file if (ei.pi.semiopen_file(Us, Types.file_of(s)) != 0) { score += ei.pi.semiopen_file(Them, Types.file_of(s)) != 0 ? RookOpenFile : RookSemiopenFile; } if (mob > 3 || ei.pi.semiopen_file(Us, Types.file_of(s)) != 0) { continue; } Square ksq = pos.king_square(Us); // Penalize rooks which are trapped by a king. Penalize more if the // king has lost its castling capability. if (((Types.file_of(ksq) < FileS.FILE_E) == (Types.file_of(s) < Types.file_of(ksq))) && (Types.rank_of(ksq) == Types.rank_of(s) || Types.relative_rank_square(Us, ksq) == RankS.RANK_1) && 0 == ei.pi.semiopen_side(Us, Types.file_of(ksq), Types.file_of(s) < Types.file_of(ksq))) { score -= (TrappedRook - Types.make_score(mob * 8, 0)) * (1 + (pos.can_castle_color(Us) == 0 ? 1 : 0)); } } // 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 (Pt == PieceTypeS.BISHOP && pos.is_chess960() != 0 && (s == Types.relative_square(Us, SquareS.SQ_A1) || s == Types.relative_square(Us, SquareS.SQ_H1))) { Square d = Types.pawn_push(Us) + (Types.file_of(s) == FileS.FILE_A ? SquareS.DELTA_E : SquareS.DELTA_W); if (pos.piece_on(s + d) == Types.make_piece(Us, PieceTypeS.PAWN)) { score -= !pos.empty(s + d + Types.pawn_push(Us)) ? TrappedBishopA1H1 * 4 : pos.piece_on(s + d + d) == Types.make_piece(Us, PieceTypeS.PAWN) ? TrappedBishopA1H1 * 2 : TrappedBishopA1H1; } } } if (Trace) { Tracing.terms[Us][Pt] = score; } return(score - evaluate_pieces(pos, ei, mobility, mobilityArea, NextPt, Them, Trace)); }
public static int generate_pawn_moves(Position pos, ExtMove[] mlist, int mPos, Bitboard target, CheckInfo ci, Color Us, GenType Type) { // Compute our parametrized parameters at compile time, named according to // the point of view of white side. Color Them = (Us == ColorS.WHITE ? ColorS.BLACK : ColorS.WHITE); Bitboard TRank8BB = (Us == ColorS.WHITE ? BitBoard.Rank8BB : BitBoard.Rank1BB); Bitboard TRank7BB = (Us == ColorS.WHITE ? BitBoard.Rank7BB : BitBoard.Rank2BB); Bitboard TRank3BB = (Us == ColorS.WHITE ? BitBoard.Rank3BB : BitBoard.Rank6BB); Square Up = (Us == ColorS.WHITE ? SquareS.DELTA_N : SquareS.DELTA_S); Square Right = (Us == ColorS.WHITE ? SquareS.DELTA_NE : SquareS.DELTA_SW); Square Left = (Us == ColorS.WHITE ? SquareS.DELTA_NW : SquareS.DELTA_SE); Bitboard b1, b2, dc1, dc2, emptySquares = 0; Bitboard pawnsOn7 = pos.pieces_color_piecetype(Us, PieceTypeS.PAWN) & TRank7BB; Bitboard pawnsNotOn7 = pos.pieces_color_piecetype(Us, PieceTypeS.PAWN) & ~TRank7BB; Bitboard enemies = (Type == GenTypeS.EVASIONS ? pos.pieces_color(Them) & target : Type == GenTypeS.CAPTURES ? target : pos.pieces_color(Them)); // Single and double pawn pushes, no promotions if (Type != GenTypeS.CAPTURES) { emptySquares = (Type == GenTypeS.QUIETS || Type == GenTypeS.QUIET_CHECKS ? target : ~pos.pieces()); b1 = BitBoard.shift_bb(pawnsNotOn7, Up) & emptySquares; b2 = BitBoard.shift_bb(b1 & TRank3BB, Up) & emptySquares; if (Type == GenTypeS.EVASIONS) // Consider only blocking squares { b1 &= target; b2 &= target; } if (Type == GenTypeS.QUIET_CHECKS) { b1 &= pos.attacks_from_pawn(ci.ksq, Them); b2 &= pos.attacks_from_pawn(ci.ksq, Them); // Add pawn pushes which give discovered check. This is possible only // if the pawn is not on the same file as the enemy king, because we // don't generate captures. Note that a possible discovery check // promotion has been already generated among captures. if ((pawnsNotOn7 & ci.dcCandidates) != 0) { dc1 = BitBoard.shift_bb(pawnsNotOn7 & ci.dcCandidates, Up) & emptySquares & ~BitBoard.file_bb_square(ci.ksq); dc2 = BitBoard.shift_bb(dc1 & TRank3BB, Up) & emptySquares; b1 |= dc1; b2 |= dc2; } } while (b1 != 0) { Square to = BitBoard.pop_lsb(ref b1); mlist[mPos++].move = Types.make_move(to - Up, to); } while (b2 != 0) { Square to = BitBoard.pop_lsb(ref b2); mlist[mPos++].move = Types.make_move(to - Up - Up, to); } } // Promotions and underpromotions if (pawnsOn7 != 0 && (Type != GenTypeS.EVASIONS || (target & TRank8BB) != 0)) { if (Type == GenTypeS.CAPTURES) { emptySquares = ~pos.pieces(); } if (Type == GenTypeS.EVASIONS) { emptySquares &= target; } mPos = generate_promotions(mlist, mPos, pawnsOn7, enemies, ci, Type, Right); mPos = generate_promotions(mlist, mPos, pawnsOn7, enemies, ci, Type, Left); mPos = generate_promotions(mlist, mPos, pawnsOn7, emptySquares, ci, Type, Up); } // Standard and en-passant captures if (Type == GenTypeS.CAPTURES || Type == GenTypeS.EVASIONS || Type == GenTypeS.NON_EVASIONS) { b1 = BitBoard.shift_bb(pawnsNotOn7, Right) & enemies; b2 = BitBoard.shift_bb(pawnsNotOn7, Left) & enemies; while (b1 != 0) { Square to = BitBoard.pop_lsb(ref b1); mlist[mPos++].move = Types.make_move(to - Right, to); } while (b2 != 0) { Square to = BitBoard.pop_lsb(ref b2); mlist[mPos++].move = Types.make_move(to - Left, to); } if (pos.ep_square() != SquareS.SQ_NONE) { Debug.Assert(Types.rank_of(pos.ep_square()) == Types.relative_rank_rank(Us, RankS.RANK_6)); // An en passant capture can be an evasion only if the checking piece // is the double pushed pawn and so is in the target. Otherwise this // is a discovery check and we are forced to do otherwise. if (Type == GenTypeS.EVASIONS && (target & BitBoard.SquareBB[(pos.ep_square() - Up)]) == 0) { return(mPos); } b1 = pawnsNotOn7 & pos.attacks_from_pawn(pos.ep_square(), Them); Debug.Assert(b1 != 0); while (b1 != 0) { mlist[mPos++].move = Types.make(BitBoard.pop_lsb(ref b1), pos.ep_square(), MoveTypeS.ENPASSANT); } } } return(mPos); }
/// move_to_san() takes a position and a legal Move as input and returns its /// short algebraic notation representation. public static string move_to_san(Position pos, Move m) { if (m == MoveS.MOVE_NONE) { return("(none)"); } if (m == MoveS.MOVE_NULL) { return("(null)"); } Debug.Assert((new MoveList(pos, GenTypeS.LEGAL).contains(m))); Bitboard others, b; string san = ""; Color us = pos.side_to_move(); Square from = Types.from_sq(m); Square to = Types.to_sq(m); Piece pc = pos.piece_on(from); PieceType pt = Types.type_of_piece(pc); if (Types.type_of_move(m) == MoveTypeS.CASTLING) { san = to > from ? "O-O" : "O-O-O"; } else { if (pt != PieceTypeS.PAWN) { san = "" + PieceToChar[ColorS.WHITE][pt]; // Upper case // A disambiguation occurs if we have more then one piece of type 'pt' // that can reach 'to' with a legal move. others = b = (pos.attacks_from_piece_square(pc, to) & pos.pieces_color_piecetype(us, pt)) ^ BitBoard.SquareBB[from]; while (b != 0) { Square s = BitBoard.pop_lsb(ref b); if (!pos.legal(Types.make_move(s, to), pos.pinned_pieces(us))) { others ^= BitBoard.SquareBB[s]; } } if (0 == others) { /* Disambiguation is not needed */ } else if (0 == (others & BitBoard.file_bb_square(from))) { san += Types.file_to_char(Types.file_of(from)); } else if (0 == (others & BitBoard.rank_bb_square(from))) { san += Types.rank_to_char(Types.rank_of(from)); } else { san += Types.square_to_string(from); } } else if (pos.capture(m)) { san = "" + Types.file_to_char(Types.file_of(from)); } if (pos.capture(m)) { san += 'x'; } san += Types.square_to_string(to); if (Types.type_of_move(m) == MoveTypeS.PROMOTION) { san += "=" + PieceToChar[ColorS.WHITE][Types.promotion_type(m)]; } } if (pos.gives_check(m, new CheckInfo(pos))) { StateInfo st = new StateInfo(); pos.do_move(m, st); san += (new MoveList(pos, GenTypeS.LEGAL)).size() > 0 ? "+" : "#"; pos.undo_move(m); } return(san); }
/// KBPP vs KB. It detects a few basic draws with opposite-colored bishops public ScaleFactor KBPPKB(Position pos) { Debug.Assert(verify_material(pos, strongSide, ValueS.BishopValueMg, 2)); Debug.Assert(verify_material(pos, weakSide, ValueS.BishopValueMg, 0)); Square wbsq = pos.list(strongSide, PieceTypeS.BISHOP)[0]; Square bbsq = pos.list(weakSide, PieceTypeS.BISHOP)[0]; if (!Types.opposite_colors(wbsq, bbsq)) { return(ScaleFactorS.SCALE_FACTOR_NONE); } Square ksq = pos.king_square(weakSide); Square psq1 = pos.list(strongSide, PieceTypeS.PAWN)[0]; Square psq2 = pos.list(strongSide, PieceTypeS.PAWN)[1]; Rank r1 = Types.rank_of(psq1); Rank r2 = Types.rank_of(psq2); Square blockSq1, blockSq2; if (Types.relative_rank_square(strongSide, psq1) > Types.relative_rank_square(strongSide, psq2)) { blockSq1 = psq1 + Types.pawn_push(strongSide); blockSq2 = Types.make_square(Types.file_of(psq2), Types.rank_of(psq1)); } else { blockSq1 = psq2 + Types.pawn_push(strongSide); blockSq2 = Types.make_square(Types.file_of(psq1), Types.rank_of(psq2)); } switch (BitBoard.file_distance(psq1, psq2)) { case 0: // Both pawns are on the same file. It's an easy draw if the defender firmly // controls some square in the frontmost pawn's path. if (Types.file_of(ksq) == Types.file_of(blockSq1) && Types.relative_rank_square(strongSide, ksq) >= Types.relative_rank_square(strongSide, blockSq1) && Types.opposite_colors(ksq, wbsq)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } else { return(ScaleFactorS.SCALE_FACTOR_NONE); } case 1: // Pawns on adjacent files. It's a draw if the 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 && Types.opposite_colors(ksq, wbsq) && (bbsq == blockSq2 || (pos.attacks_from_square_piecetype(blockSq2, PieceTypeS.BISHOP) & pos.pieces_color_piecetype(weakSide, PieceTypeS.BISHOP)) != 0 || Math.Abs(r1 - r2) >= 2)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } else if (ksq == blockSq2 && Types.opposite_colors(ksq, wbsq) && (bbsq == blockSq1 || (pos.attacks_from_square_piecetype(blockSq1, PieceTypeS.BISHOP) & pos.pieces_color_piecetype(weakSide, PieceTypeS.BISHOP)) != 0)) { return(ScaleFactorS.SCALE_FACTOR_DRAW); } else { return(ScaleFactorS.SCALE_FACTOR_NONE); } default: // The pawns are not on the same file or adjacent files. No scaling. return(ScaleFactorS.SCALE_FACTOR_NONE); } }
// A KPK bitbase index is an integer in [0, IndexMax] range // // Information is mapped in a way that minimizes the number of iterations: // // bit 0- 5: white king square (from SQ_A1 to SQ_H8) // bit 6-11: black king square (from SQ_A1 to SQ_H8) // bit 12: side to move (WHITE or BLACK) // bit 13-14: white pawn file (from FILE_A to FILE_D) // bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2) public static uint index(Color us, Square bksq, Square wksq, Square psq) { return((uint)(wksq + (bksq << 6) + (us << 12) + (Types.file_of(psq) << 13) + ((RankS.RANK_7 - Types.rank_of(psq)) << 15))); }