]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Fix capturing underpromotions issue
[stockfish] / src / movegen.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
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.
9
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.
14
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/>.
17 */
18
19 #include <cassert>
20
21 #include "movegen.h"
22 #include "position.h"
23
24 namespace Stockfish {
25
26 namespace {
27
28   template<GenType Type, Direction D, bool Enemy>
29   ExtMove* make_promotions(ExtMove* moveList, [[maybe_unused]] Square to) {
30
31     if constexpr (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
32     {
33         *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
34         if constexpr (Enemy && Type == CAPTURES)
35         {
36             *moveList++ = make<PROMOTION>(to - D, to, ROOK);
37             *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
38             *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
39         }
40     }
41
42     if constexpr ((Type == QUIETS && !Enemy) || Type == EVASIONS || Type == NON_EVASIONS)
43     {
44         *moveList++ = make<PROMOTION>(to - D, to, ROOK);
45         *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
46         *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
47     }
48
49     return moveList;
50   }
51
52
53   template<Color Us, GenType Type>
54   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
55
56     constexpr Color     Them     = ~Us;
57     constexpr Bitboard  TRank7BB = (Us == WHITE ? Rank7BB    : Rank2BB);
58     constexpr Bitboard  TRank3BB = (Us == WHITE ? Rank3BB    : Rank6BB);
59     constexpr Direction Up       = pawn_push(Us);
60     constexpr Direction UpRight  = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
61     constexpr Direction UpLeft   = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
62
63     const Bitboard emptySquares = ~pos.pieces();
64     const Bitboard enemies      =  Type == EVASIONS ? pos.checkers()
65                                                     : pos.pieces(Them);
66
67     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
68     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
69
70     // Single and double pawn pushes, no promotions
71     if constexpr (Type != CAPTURES)
72     {
73         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
74         Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
75
76         if constexpr (Type == EVASIONS) // Consider only blocking squares
77         {
78             b1 &= target;
79             b2 &= target;
80         }
81
82         if constexpr (Type == QUIET_CHECKS)
83         {
84             // To make a quiet check, you either make a direct check by pushing a pawn
85             // or push a blocker pawn that is not on the same file as the enemy king.
86             // Discovered check promotion has been already generated amongst the captures.
87             Square ksq = pos.square<KING>(Them);
88             Bitboard dcCandidatePawns = pos.blockers_for_king(Them) & ~file_bb(ksq);
89             b1 &= pawn_attacks_bb(Them, ksq) | shift<   Up>(dcCandidatePawns);
90             b2 &= pawn_attacks_bb(Them, ksq) | shift<Up+Up>(dcCandidatePawns);
91         }
92
93         while (b1)
94         {
95             Square to = pop_lsb(b1);
96             *moveList++ = make_move(to - Up, to);
97         }
98
99         while (b2)
100         {
101             Square to = pop_lsb(b2);
102             *moveList++ = make_move(to - Up - Up, to);
103         }
104     }
105
106     // Promotions and underpromotions
107     if (pawnsOn7)
108     {
109         Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
110         Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
111         Bitboard b3 = shift<Up     >(pawnsOn7) & emptySquares;
112
113         if constexpr (Type == EVASIONS)
114             b3 &= target;
115
116         while (b1)
117             moveList = make_promotions<Type, UpRight, true>(moveList, pop_lsb(b1));
118
119         while (b2)
120             moveList = make_promotions<Type, UpLeft, true>(moveList, pop_lsb(b2));
121
122         while (b3)
123             moveList = make_promotions<Type, Up,    false>(moveList, pop_lsb(b3));
124     }
125
126     // Standard and en passant captures
127     if constexpr (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
128     {
129         Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
130         Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
131
132         while (b1)
133         {
134             Square to = pop_lsb(b1);
135             *moveList++ = make_move(to - UpRight, to);
136         }
137
138         while (b2)
139         {
140             Square to = pop_lsb(b2);
141             *moveList++ = make_move(to - UpLeft, to);
142         }
143
144         if (pos.ep_square() != SQ_NONE)
145         {
146             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
147
148             // An en passant capture cannot resolve a discovered check
149             if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
150                 return moveList;
151
152             b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
153
154             assert(b1);
155
156             while (b1)
157                 *moveList++ = make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
158         }
159     }
160
161     return moveList;
162   }
163
164
165   template<Color Us, PieceType Pt, bool Checks>
166   ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
167
168     static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
169
170     Bitboard bb = pos.pieces(Us, Pt);
171
172     while (bb)
173     {
174         Square from = pop_lsb(bb);
175         Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
176
177         // To check, you either move freely a blocker or make a direct check.
178         if (Checks && (Pt == QUEEN || !(pos.blockers_for_king(~Us) & from)))
179             b &= pos.check_squares(Pt);
180
181         while (b)
182             *moveList++ = make_move(from, pop_lsb(b));
183     }
184
185     return moveList;
186   }
187
188
189   template<Color Us, GenType Type>
190   ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
191
192     static_assert(Type != LEGAL, "Unsupported type in generate_all()");
193
194     constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
195     const Square ksq = pos.square<KING>(Us);
196     Bitboard target;
197
198     // Skip generating non-king moves when in double check
199     if (Type != EVASIONS || !more_than_one(pos.checkers()))
200     {
201         target = Type == EVASIONS     ?  between_bb(ksq, lsb(pos.checkers()))
202                : Type == NON_EVASIONS ? ~pos.pieces( Us)
203                : Type == CAPTURES     ?  pos.pieces(~Us)
204                                       : ~pos.pieces(   ); // QUIETS || QUIET_CHECKS
205
206         moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
207         moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
208         moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
209         moveList = generate_moves<Us,   ROOK, Checks>(pos, moveList, target);
210         moveList = generate_moves<Us,  QUEEN, Checks>(pos, moveList, target);
211     }
212
213     if (!Checks || pos.blockers_for_king(~Us) & ksq)
214     {
215         Bitboard b = attacks_bb<KING>(ksq) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
216         if (Checks)
217             b &= ~attacks_bb<QUEEN>(pos.square<KING>(~Us));
218
219         while (b)
220             *moveList++ = make_move(ksq, pop_lsb(b));
221
222         if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
223             for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
224                 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
225                     *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
226     }
227
228     return moveList;
229   }
230
231 } // namespace
232
233
234 /// <CAPTURES>     Generates all pseudo-legal captures plus queen promotions
235 /// <QUIETS>       Generates all pseudo-legal non-captures and underpromotions
236 /// <EVASIONS>     Generates all pseudo-legal check evasions when the side to move is in check
237 /// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling and promotions
238 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
239 ///
240 /// Returns a pointer to the end of the move list.
241
242 template<GenType Type>
243 ExtMove* generate(const Position& pos, ExtMove* moveList) {
244
245   static_assert(Type != LEGAL, "Unsupported type in generate()");
246   assert((Type == EVASIONS) == (bool)pos.checkers());
247
248   Color us = pos.side_to_move();
249
250   return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
251                      : generate_all<BLACK, Type>(pos, moveList);
252 }
253
254 // Explicit template instantiations
255 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
256 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
257 template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
258 template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
259 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
260
261
262 /// generate<LEGAL> generates all the legal moves in the given position
263
264 template<>
265 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
266
267   Color us = pos.side_to_move();
268   Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
269   Square ksq = pos.square<KING>(us);
270   ExtMove* cur = moveList;
271
272   moveList = pos.checkers() ? generate<EVASIONS    >(pos, moveList)
273                             : generate<NON_EVASIONS>(pos, moveList);
274   while (cur != moveList)
275       if (  ((pinned & from_sq(*cur)) || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
276           && !pos.legal(*cur))
277           *cur = (--moveList)->move;
278       else
279           ++cur;
280
281   return moveList;
282 }
283
284 } // namespace Stockfish