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-2019 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<Color Us, CastlingSide Cs, bool Checks, bool Chess960>
29 ExtMove* generate_castling(const Position& pos, ExtMove* moveList) {
31 constexpr CastlingRight Cr = Us | Cs;
32 constexpr bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
34 if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
37 // After castling, the rook and king final positions are the same in Chess960
38 // as they would be in standard chess.
39 Square kfrom = pos.square<KING>(Us);
40 Square rfrom = pos.castling_rook_square(Cr);
41 Square kto = relative_square(Us, KingSide ? SQ_G1 : SQ_C1);
42 Bitboard enemies = pos.pieces(~Us);
44 assert(!pos.checkers());
46 const Direction step = Chess960 ? kto > kfrom ? WEST : EAST
47 : KingSide ? WEST : EAST;
49 for (Square s = kto; s != kfrom; s += step)
50 if (pos.attackers_to(s) & enemies)
53 // Because we generate only legal castling moves we need to verify that
54 // when moving the castling rook we do not discover some hidden checker.
55 // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
56 if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~Us, ROOK, QUEEN)))
59 Move m = make<CASTLING>(kfrom, rfrom);
61 if (Checks && !pos.gives_check(m))
69 template<GenType Type, Direction D>
70 ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
72 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
73 *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
75 if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
77 *moveList++ = make<PROMOTION>(to - D, to, ROOK);
78 *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
79 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
82 // Knight promotion is the only promotion that can give a direct check
83 // that's not already included in the queen promotion.
84 if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq))
85 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
87 (void)ksq; // Silence a warning under MSVC
93 template<Color Us, GenType Type>
94 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
96 // Compute our parametrized parameters at compile time, named according to
97 // the point of view of white side.
98 constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
99 constexpr Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
100 constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
101 constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
102 constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
103 constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
104 constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
106 Bitboard emptySquares;
108 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
109 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
111 Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
112 Type == CAPTURES ? target : pos.pieces(Them));
114 // Single and double pawn pushes, no promotions
115 if (Type != CAPTURES)
117 emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
119 Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
120 Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
122 if (Type == EVASIONS) // Consider only blocking squares
128 if (Type == QUIET_CHECKS)
130 Square ksq = pos.square<KING>(Them);
132 b1 &= pos.attacks_from<PAWN>(ksq, Them);
133 b2 &= pos.attacks_from<PAWN>(ksq, Them);
135 // Add pawn pushes which give discovered check. This is possible only
136 // if the pawn is not on the same file as the enemy king, because we
137 // don't generate captures. Note that a possible discovery check
138 // promotion has been already generated amongst the captures.
139 Bitboard dcCandidates = pos.blockers_for_king(Them);
140 if (pawnsNotOn7 & dcCandidates)
142 Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
143 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
152 Square to = pop_lsb(&b1);
153 *moveList++ = make_move(to - Up, to);
158 Square to = pop_lsb(&b2);
159 *moveList++ = make_move(to - Up - Up, to);
163 // Promotions and underpromotions
164 if (pawnsOn7 && (Type != EVASIONS || (target & TRank8BB)))
166 if (Type == CAPTURES)
167 emptySquares = ~pos.pieces();
169 if (Type == EVASIONS)
170 emptySquares &= target;
172 Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
173 Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
174 Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
176 Square ksq = pos.square<KING>(Them);
179 moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
182 moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
185 moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
188 // Standard and en-passant captures
189 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
191 Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
192 Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
196 Square to = pop_lsb(&b1);
197 *moveList++ = make_move(to - UpRight, to);
202 Square to = pop_lsb(&b2);
203 *moveList++ = make_move(to - UpLeft, to);
206 if (pos.ep_square() != SQ_NONE)
208 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
210 // An en passant capture can be an evasion only if the checking piece
211 // is the double pushed pawn and so is in the target. Otherwise this
212 // is a discovery check and we are forced to do otherwise.
213 if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
216 b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
221 *moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
229 template<PieceType Pt, bool Checks>
230 ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
233 assert(Pt != KING && Pt != PAWN);
235 const Square* pl = pos.squares<Pt>(us);
237 for (Square from = *pl; from != SQ_NONE; from = *++pl)
241 if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
242 && !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
245 if (pos.blockers_for_king(~us) & from)
249 Bitboard b = pos.attacks_from<Pt>(from) & target;
252 b &= pos.check_squares(Pt);
255 *moveList++ = make_move(from, pop_lsb(&b));
262 template<Color Us, GenType Type>
263 ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
265 constexpr bool Checks = Type == QUIET_CHECKS;
267 moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
268 moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
269 moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target);
270 moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target);
271 moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target);
273 if (Type != QUIET_CHECKS && Type != EVASIONS)
275 Square ksq = pos.square<KING>(Us);
276 Bitboard b = pos.attacks_from<KING>(ksq) & target;
278 *moveList++ = make_move(ksq, pop_lsb(&b));
281 if (Type != CAPTURES && Type != EVASIONS && pos.castling_rights(Us))
283 if (pos.is_chess960())
285 moveList = generate_castling<Us, KING_SIDE, Checks, true>(pos, moveList);
286 moveList = generate_castling<Us, QUEEN_SIDE, Checks, true>(pos, moveList);
290 moveList = generate_castling<Us, KING_SIDE, Checks, false>(pos, moveList);
291 moveList = generate_castling<Us, QUEEN_SIDE, Checks, false>(pos, moveList);
301 /// generate<CAPTURES> generates all pseudo-legal captures and queen
302 /// promotions. Returns a pointer to the end of the move list.
304 /// generate<QUIETS> generates all pseudo-legal non-captures and
305 /// underpromotions. Returns a pointer to the end of the move list.
307 /// generate<NON_EVASIONS> generates all pseudo-legal captures and
308 /// non-captures. Returns a pointer to the end of the move list.
310 template<GenType Type>
311 ExtMove* generate(const Position& pos, ExtMove* moveList) {
313 assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
314 assert(!pos.checkers());
316 Color us = pos.side_to_move();
318 Bitboard target = Type == CAPTURES ? pos.pieces(~us)
319 : Type == QUIETS ? ~pos.pieces()
320 : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
322 return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
323 : generate_all<BLACK, Type>(pos, moveList, target);
326 // Explicit template instantiations
327 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
328 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
329 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
332 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
333 /// underpromotions that give check. Returns a pointer to the end of the move list.
335 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
337 assert(!pos.checkers());
339 Color us = pos.side_to_move();
340 Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us);
344 Square from = pop_lsb(&dc);
345 PieceType pt = type_of(pos.piece_on(from));
348 continue; // Will be generated together with direct checks
350 Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces();
353 b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
356 *moveList++ = make_move(from, pop_lsb(&b));
359 return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
360 : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
364 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
365 /// to move is in check. Returns a pointer to the end of the move list.
367 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
369 assert(pos.checkers());
371 Color us = pos.side_to_move();
372 Square ksq = pos.square<KING>(us);
373 Bitboard sliderAttacks = 0;
374 Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
376 // Find all the squares attacked by slider checkers. We will remove them from
377 // the king evasions in order to skip known illegal moves, which avoids any
378 // useless legality checks later on.
381 Square checksq = pop_lsb(&sliders);
382 sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
385 // Generate evasions for king, capture and non capture moves
386 Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
388 *moveList++ = make_move(ksq, pop_lsb(&b));
390 if (more_than_one(pos.checkers()))
391 return moveList; // Double check, only a king move can save the day
393 // Generate blocking evasions or captures of the checking piece
394 Square checksq = lsb(pos.checkers());
395 Bitboard target = between_bb(checksq, ksq) | checksq;
397 return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
398 : generate_all<BLACK, EVASIONS>(pos, moveList, target);
402 /// generate<LEGAL> generates all the legal moves in the given position
405 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
407 Color us = pos.side_to_move();
408 Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
409 Square ksq = pos.square<KING>(us);
410 ExtMove* cur = moveList;
412 moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
413 : generate<NON_EVASIONS>(pos, moveList);
414 while (cur != moveList)
415 if ( (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
417 *cur = (--moveList)->move;