2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2023 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/>.
22 #include <initializer_list>
31 template<GenType Type, Direction D, bool Enemy>
32 ExtMove* make_promotions(ExtMove* moveList, [[maybe_unused]] Square to) {
34 if constexpr (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
36 *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
37 if constexpr (Enemy && Type == CAPTURES)
39 *moveList++ = make<PROMOTION>(to - D, to, ROOK);
40 *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
41 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
45 if constexpr ((Type == QUIETS && !Enemy) || Type == EVASIONS || Type == NON_EVASIONS)
47 *moveList++ = make<PROMOTION>(to - D, to, ROOK);
48 *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
49 *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
56 template<Color Us, GenType Type>
57 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
59 constexpr Color Them = ~Us;
60 constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
61 constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
62 constexpr Direction Up = pawn_push(Us);
63 constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
64 constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
66 const Bitboard emptySquares = ~pos.pieces();
67 const Bitboard enemies = Type == EVASIONS ? pos.checkers() : pos.pieces(Them);
69 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
70 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
72 // Single and double pawn pushes, no promotions
73 if constexpr (Type != CAPTURES)
75 Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
76 Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
78 if constexpr (Type == EVASIONS) // Consider only blocking squares
84 if constexpr (Type == QUIET_CHECKS)
86 // To make a quiet check, you either make a direct check by pushing a pawn
87 // or push a blocker pawn that is not on the same file as the enemy king.
88 // Discovered check promotion has been already generated amongst the captures.
89 Square ksq = pos.square<KING>(Them);
90 Bitboard dcCandidatePawns = pos.blockers_for_king(Them) & ~file_bb(ksq);
91 b1 &= pawn_attacks_bb(Them, ksq) | shift<Up>(dcCandidatePawns);
92 b2 &= pawn_attacks_bb(Them, ksq) | shift<Up + Up>(dcCandidatePawns);
97 Square to = pop_lsb(b1);
98 *moveList++ = make_move(to - Up, to);
103 Square to = pop_lsb(b2);
104 *moveList++ = make_move(to - Up - Up, to);
108 // Promotions and underpromotions
111 Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
112 Bitboard b2 = shift<UpLeft>(pawnsOn7) & enemies;
113 Bitboard b3 = shift<Up>(pawnsOn7) & emptySquares;
115 if constexpr (Type == EVASIONS)
119 moveList = make_promotions<Type, UpRight, true>(moveList, pop_lsb(b1));
122 moveList = make_promotions<Type, UpLeft, true>(moveList, pop_lsb(b2));
125 moveList = make_promotions<Type, Up, false>(moveList, pop_lsb(b3));
128 // Standard and en passant captures
129 if constexpr (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
131 Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
132 Bitboard b2 = shift<UpLeft>(pawnsNotOn7) & enemies;
136 Square to = pop_lsb(b1);
137 *moveList++ = make_move(to - UpRight, to);
142 Square to = pop_lsb(b2);
143 *moveList++ = make_move(to - UpLeft, to);
146 if (pos.ep_square() != SQ_NONE)
148 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
150 // An en passant capture cannot resolve a discovered check
151 if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
154 b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
159 *moveList++ = make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
167 template<Color Us, PieceType Pt, bool Checks>
168 ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
170 static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
172 Bitboard bb = pos.pieces(Us, Pt);
176 Square from = pop_lsb(bb);
177 Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
179 // To check, you either move freely a blocker or make a direct check.
180 if (Checks && (Pt == QUEEN || !(pos.blockers_for_king(~Us) & from)))
181 b &= pos.check_squares(Pt);
184 *moveList++ = make_move(from, pop_lsb(b));
191 template<Color Us, GenType Type>
192 ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
194 static_assert(Type != LEGAL, "Unsupported type in generate_all()");
196 constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
197 const Square ksq = pos.square<KING>(Us);
200 // Skip generating non-king moves when in double check
201 if (Type != EVASIONS || !more_than_one(pos.checkers()))
203 target = Type == EVASIONS ? between_bb(ksq, lsb(pos.checkers()))
204 : Type == NON_EVASIONS ? ~pos.pieces(Us)
205 : Type == CAPTURES ? pos.pieces(~Us)
206 : ~pos.pieces(); // QUIETS || QUIET_CHECKS
208 moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
209 moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
210 moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
211 moveList = generate_moves<Us, ROOK, Checks>(pos, moveList, target);
212 moveList = generate_moves<Us, QUEEN, Checks>(pos, moveList, target);
215 if (!Checks || pos.blockers_for_king(~Us) & ksq)
217 Bitboard b = attacks_bb<KING>(ksq) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
219 b &= ~attacks_bb<QUEEN>(pos.square<KING>(~Us));
222 *moveList++ = make_move(ksq, pop_lsb(b));
224 if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
225 for (CastlingRights cr : {Us & KING_SIDE, Us & QUEEN_SIDE})
226 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
227 *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
236 // <CAPTURES> Generates all pseudo-legal captures plus queen promotions
237 // <QUIETS> Generates all pseudo-legal non-captures and underpromotions
238 // <EVASIONS> Generates all pseudo-legal check evasions
239 // <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
240 // <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check,
241 // except castling and promotions
243 // Returns a pointer to the end of the move list.
244 template<GenType Type>
245 ExtMove* generate(const Position& pos, ExtMove* moveList) {
247 static_assert(Type != LEGAL, "Unsupported type in generate()");
248 assert((Type == EVASIONS) == bool(pos.checkers()));
250 Color us = pos.side_to_move();
252 return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
253 : generate_all<BLACK, Type>(pos, moveList);
256 // Explicit template instantiations
257 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
258 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
259 template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
260 template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
261 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
264 // generate<LEGAL> generates all the legal moves in the given position
267 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
269 Color us = pos.side_to_move();
270 Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
271 Square ksq = pos.square<KING>(us);
272 ExtMove* cur = moveList;
275 pos.checkers() ? generate<EVASIONS>(pos, moveList) : generate<NON_EVASIONS>(pos, moveList);
276 while (cur != moveList)
277 if (((pinned & from_sq(*cur)) || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
279 *cur = (--moveList)->move;
286 } // namespace Stockfish