2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5 Copyright (C) 2015-2020 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
7 Stockfish is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Stockfish is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 template<GenType Type, Direction D>
29 ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
31 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
32 *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
34 if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
36 *moveList++ = make<PROMOTION>(to - D, to, ROOK);
37 *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
38 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
41 // Knight promotion is the only promotion that can give a direct check
42 // that's not already included in the queen promotion.
43 if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq))
44 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
46 (void)ksq; // Silence a warning under MSVC
52 template<Color Us, GenType Type>
53 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
55 constexpr Color Them = ~Us;
56 constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
57 constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
58 constexpr Direction Up = pawn_push(Us);
59 constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
60 constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
62 const Square ksq = pos.square<KING>(Them);
63 Bitboard emptySquares;
65 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
66 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
68 Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
69 Type == CAPTURES ? target : pos.pieces(Them));
71 // Single and double pawn pushes, no promotions
74 emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
76 Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
77 Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
79 if (Type == EVASIONS) // Consider only blocking squares
85 if (Type == QUIET_CHECKS)
87 b1 &= pos.attacks_from<PAWN>(ksq, Them);
88 b2 &= pos.attacks_from<PAWN>(ksq, Them);
90 // Add pawn pushes which give discovered check. This is possible only
91 // if the pawn is not on the same file as the enemy king, because we
92 // don't generate captures. Note that a possible discovery check
93 // promotion has been already generated amongst the captures.
94 Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
95 if (dcCandidateQuiets)
97 Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
98 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
107 Square to = pop_lsb(&b1);
108 *moveList++ = make_move(to - Up, to);
113 Square to = pop_lsb(&b2);
114 *moveList++ = make_move(to - Up - Up, to);
118 // Promotions and underpromotions
121 if (Type == CAPTURES)
122 emptySquares = ~pos.pieces();
124 if (Type == EVASIONS)
125 emptySquares &= target;
127 Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
128 Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
129 Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
132 moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
135 moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
138 moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
141 // Standard and en-passant captures
142 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
144 Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
145 Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
149 Square to = pop_lsb(&b1);
150 *moveList++ = make_move(to - UpRight, to);
155 Square to = pop_lsb(&b2);
156 *moveList++ = make_move(to - UpLeft, to);
159 if (pos.ep_square() != SQ_NONE)
161 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
163 // An en passant capture can be an evasion only if the checking piece
164 // is the double pushed pawn and so is in the target. Otherwise this
165 // is a discovery check and we are forced to do otherwise.
166 if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
169 b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
174 *moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
182 template<PieceType Pt, bool Checks>
183 ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
186 static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
188 const Square* pl = pos.squares<Pt>(us);
190 for (Square from = *pl; from != SQ_NONE; from = *++pl)
194 if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
195 && !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
198 if (pos.blockers_for_king(~us) & from)
202 Bitboard b = pos.attacks_from<Pt>(from) & target;
205 b &= pos.check_squares(Pt);
208 *moveList++ = make_move(from, pop_lsb(&b));
215 template<Color Us, GenType Type>
216 ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
217 constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations
219 moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
220 moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
221 moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target);
222 moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target);
223 moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target);
225 if (Type != QUIET_CHECKS && Type != EVASIONS)
227 Square ksq = pos.square<KING>(Us);
228 Bitboard b = pos.attacks_from<KING>(ksq) & target;
230 *moveList++ = make_move(ksq, pop_lsb(&b));
232 if ((Type != CAPTURES) && pos.can_castle(Us & ANY_CASTLING))
233 for(CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
234 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
235 *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
244 /// <CAPTURES> Generates all pseudo-legal captures and queen promotions
245 /// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
246 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
248 /// Returns a pointer to the end of the move list.
250 template<GenType Type>
251 ExtMove* generate(const Position& pos, ExtMove* moveList) {
253 static_assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS, "Unsupported type in generate()");
254 assert(!pos.checkers());
256 Color us = pos.side_to_move();
258 Bitboard target = Type == CAPTURES ? pos.pieces(~us)
259 : Type == QUIETS ? ~pos.pieces()
260 : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
262 return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
263 : generate_all<BLACK, Type>(pos, moveList, target);
266 // Explicit template instantiations
267 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
268 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
269 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
272 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
273 /// underpromotions that give check. Returns a pointer to the end of the move list.
275 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
277 assert(!pos.checkers());
279 Color us = pos.side_to_move();
280 Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us);
284 Square from = pop_lsb(&dc);
285 PieceType pt = type_of(pos.piece_on(from));
288 continue; // Will be generated together with direct checks
290 Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces();
293 b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
296 *moveList++ = make_move(from, pop_lsb(&b));
299 return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
300 : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
304 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
305 /// to move is in check. Returns a pointer to the end of the move list.
307 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
309 assert(pos.checkers());
311 Color us = pos.side_to_move();
312 Square ksq = pos.square<KING>(us);
313 Bitboard sliderAttacks = 0;
314 Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
316 // Find all the squares attacked by slider checkers. We will remove them from
317 // the king evasions in order to skip known illegal moves, which avoids any
318 // useless legality checks later on.
321 Square checksq = pop_lsb(&sliders);
322 sliderAttacks |= LineBB[ksq][checksq] ^ checksq;
325 // Generate evasions for king, capture and non capture moves
326 Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
328 *moveList++ = make_move(ksq, pop_lsb(&b));
330 if (more_than_one(pos.checkers()))
331 return moveList; // Double check, only a king move can save the day
333 // Generate blocking evasions or captures of the checking piece
334 Square checksq = lsb(pos.checkers());
335 Bitboard target = between_bb(checksq, ksq) | checksq;
337 return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
338 : generate_all<BLACK, EVASIONS>(pos, moveList, target);
342 /// generate<LEGAL> generates all the legal moves in the given position
345 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
347 Color us = pos.side_to_move();
348 Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
349 Square ksq = pos.square<KING>(us);
350 ExtMove* cur = moveList;
352 moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
353 : generate<NON_EVASIONS>(pos, moveList);
354 while (cur != moveList)
355 if ( (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
357 *cur = (--moveList)->move;