2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 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)
33 *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
34 if (attacks_bb<KNIGHT>(to) & ksq)
35 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
38 if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
40 *moveList++ = make<PROMOTION>(to - D, to, ROOK);
41 *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
42 if (!(attacks_bb<KNIGHT>(to) & ksq))
43 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
50 template<Color Us, GenType Type>
51 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
53 constexpr Color Them = ~Us;
54 constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
55 constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
56 constexpr Direction Up = pawn_push(Us);
57 constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
58 constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
60 const Square ksq = pos.square<KING>(Them);
61 Bitboard emptySquares;
63 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
64 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
66 Bitboard enemies = (Type == EVASIONS ? pos.checkers():
67 Type == CAPTURES ? target : pos.pieces(Them));
69 // Single and double pawn pushes, no promotions
72 emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
74 Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
75 Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
77 if (Type == EVASIONS) // Consider only blocking squares
83 if (Type == QUIET_CHECKS)
85 b1 &= pawn_attacks_bb(Them, ksq);
86 b2 &= pawn_attacks_bb(Them, ksq);
88 // Add pawn pushes which give discovered check. This is possible only
89 // if the pawn is not on the same file as the enemy king, because we
90 // don't generate captures. Note that a possible discovered check
91 // promotion has been already generated amongst the captures.
92 Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
93 if (dcCandidateQuiets)
95 Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
96 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
105 Square to = pop_lsb(b1);
106 *moveList++ = make_move(to - Up, to);
111 Square to = pop_lsb(b2);
112 *moveList++ = make_move(to - Up - Up, to);
116 // Promotions and underpromotions
119 if (Type == CAPTURES)
120 emptySquares = ~pos.pieces();
122 if (Type == EVASIONS)
123 emptySquares &= target;
125 Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
126 Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
127 Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
130 moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(b1), ksq);
133 moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(b2), ksq);
136 moveList = make_promotions<Type, Up >(moveList, pop_lsb(b3), ksq);
139 // Standard and en passant captures
140 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
142 Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
143 Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
147 Square to = pop_lsb(b1);
148 *moveList++ = make_move(to - UpRight, to);
153 Square to = pop_lsb(b2);
154 *moveList++ = make_move(to - UpLeft, to);
157 if (pos.ep_square() != SQ_NONE)
159 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
161 // An en passant capture cannot resolve a discovered check
162 if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
165 b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
170 *moveList++ = make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
178 template<Color Us, PieceType Pt, bool Checks>
179 ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
181 static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
183 Bitboard bb = pos.pieces(Us, Pt);
187 Square from = pop_lsb(bb);
189 Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
190 if (Checks && (Pt == QUEEN || !(pos.blockers_for_king(~Us) & from)))
191 b &= pos.check_squares(Pt);
194 *moveList++ = make_move(from, pop_lsb(b));
201 template<Color Us, GenType Type>
202 ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
204 static_assert(Type != LEGAL, "Unsupported type in generate_all()");
206 constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
207 const Square ksq = pos.square<KING>(Us);
210 if (Type == EVASIONS && more_than_one(pos.checkers()))
211 goto kingMoves; // Double check, only a king move can save the day
216 target = pos.pieces(~Us);
220 target = ~pos.pieces();
223 target = between_bb(ksq, lsb(pos.checkers()));
226 target = ~pos.pieces(Us);
230 moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
231 moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
232 moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
233 moveList = generate_moves<Us, ROOK, Checks>(pos, moveList, target);
234 moveList = generate_moves<Us, QUEEN, Checks>(pos, moveList, target);
237 if (!Checks || pos.blockers_for_king(~Us) & ksq)
239 Bitboard b = attacks_bb<KING>(ksq) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
241 b &= ~attacks_bb<QUEEN>(pos.square<KING>(~Us));
244 *moveList++ = make_move(ksq, pop_lsb(b));
246 if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
247 for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
248 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
249 *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
258 /// <CAPTURES> Generates all pseudo-legal captures plus queen and checking knight promotions
259 /// <QUIETS> Generates all pseudo-legal non-captures and underpromotions (except checking knight)
260 /// <EVASIONS> Generates all pseudo-legal check evasions when the side to move is in check
261 /// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling
262 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
264 /// Returns a pointer to the end of the move list.
266 template<GenType Type>
267 ExtMove* generate(const Position& pos, ExtMove* moveList) {
269 static_assert(Type != LEGAL, "Unsupported type in generate()");
270 assert((Type == EVASIONS) == (bool)pos.checkers());
272 Color us = pos.side_to_move();
274 return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
275 : generate_all<BLACK, Type>(pos, moveList);
278 // Explicit template instantiations
279 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
280 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
281 template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
282 template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
283 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
286 /// generate<LEGAL> generates all the legal moves in the given position
289 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
291 Color us = pos.side_to_move();
292 Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
293 Square ksq = pos.square<KING>(us);
294 ExtMove* cur = moveList;
296 moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
297 : generate<NON_EVASIONS>(pos, moveList);
298 while (cur != moveList)
299 if ( ((pinned && pinned & from_sq(*cur)) || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
301 *cur = (--moveList)->move;
308 } // namespace Stockfish