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