]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
Use correct chess terms + fix spelling.
[stockfish] / src / movegen.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 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 {
25
26   template<GenType Type, Direction D>
27   ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
28
29     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
30     {
31         *moveList++ = make<PROMOTION>(to - D, to, QUEEN);
32         if (attacks_bb<KNIGHT>(to) & ksq)
33             *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
34     }
35
36     if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
37     {
38         *moveList++ = make<PROMOTION>(to - D, to, ROOK);
39         *moveList++ = make<PROMOTION>(to - D, to, BISHOP);
40         if (!(attacks_bb<KNIGHT>(to) & ksq))
41             *moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
42     }
43
44     return moveList;
45   }
46
47
48   template<Color Us, GenType Type>
49   ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
50
51     constexpr Color     Them     = ~Us;
52     constexpr Bitboard  TRank7BB = (Us == WHITE ? Rank7BB    : Rank2BB);
53     constexpr Bitboard  TRank3BB = (Us == WHITE ? Rank3BB    : Rank6BB);
54     constexpr Direction Up       = pawn_push(Us);
55     constexpr Direction UpRight  = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
56     constexpr Direction UpLeft   = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
57
58     const Square ksq = pos.square<KING>(Them);
59     Bitboard emptySquares;
60
61     Bitboard pawnsOn7    = pos.pieces(Us, PAWN) &  TRank7BB;
62     Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
63
64     Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
65                         Type == CAPTURES ? target : pos.pieces(Them));
66
67     // Single and double pawn pushes, no promotions
68     if (Type != CAPTURES)
69     {
70         emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
71
72         Bitboard b1 = shift<Up>(pawnsNotOn7)   & emptySquares;
73         Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
74
75         if (Type == EVASIONS) // Consider only blocking squares
76         {
77             b1 &= target;
78             b2 &= target;
79         }
80
81         if (Type == QUIET_CHECKS)
82         {
83             b1 &= pawn_attacks_bb(Them, ksq);
84             b2 &= pawn_attacks_bb(Them, ksq);
85
86             // Add pawn pushes which give discovered check. This is possible only
87             // if the pawn is not on the same file as the enemy king, because we
88             // don't generate captures. Note that a possible discovered check
89             // promotion has been already generated amongst the captures.
90             Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
91             if (dcCandidateQuiets)
92             {
93                 Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
94                 Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
95
96                 b1 |= dc1;
97                 b2 |= dc2;
98             }
99         }
100
101         while (b1)
102         {
103             Square to = pop_lsb(&b1);
104             *moveList++ = make_move(to - Up, to);
105         }
106
107         while (b2)
108         {
109             Square to = pop_lsb(&b2);
110             *moveList++ = make_move(to - Up - Up, to);
111         }
112     }
113
114     // Promotions and underpromotions
115     if (pawnsOn7)
116     {
117         if (Type == CAPTURES)
118             emptySquares = ~pos.pieces();
119
120         if (Type == EVASIONS)
121             emptySquares &= target;
122
123         Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
124         Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
125         Bitboard b3 = shift<Up     >(pawnsOn7) & emptySquares;
126
127         while (b1)
128             moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
129
130         while (b2)
131             moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
132
133         while (b3)
134             moveList = make_promotions<Type, Up     >(moveList, pop_lsb(&b3), ksq);
135     }
136
137     // Standard and en passant captures
138     if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
139     {
140         Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
141         Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
142
143         while (b1)
144         {
145             Square to = pop_lsb(&b1);
146             *moveList++ = make_move(to - UpRight, to);
147         }
148
149         while (b2)
150         {
151             Square to = pop_lsb(&b2);
152             *moveList++ = make_move(to - UpLeft, to);
153         }
154
155         if (pos.ep_square() != SQ_NONE)
156         {
157             assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
158
159             // An en passant capture can be an evasion only if the checking piece
160             // is the double pushed pawn and so is in the target. Otherwise this
161             // is a discovery check and we are forced to do otherwise.
162             if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
163                 return moveList;
164
165             b1 = pawnsNotOn7 & pawn_attacks_bb(Them, pos.ep_square());
166
167             assert(b1);
168
169             while (b1)
170                 *moveList++ = make<EN_PASSANT>(pop_lsb(&b1), pos.ep_square());
171         }
172     }
173
174     return moveList;
175   }
176
177
178   template<PieceType Pt, bool Checks>
179   ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard piecesToMove, Bitboard target) {
180
181     static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
182
183     Bitboard bb = piecesToMove & pos.pieces(Pt);
184
185     while (bb) {
186         Square from = pop_lsb(&bb);
187
188         if (Checks && (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
189             && !(attacks_bb<Pt>(from) & target & pos.check_squares(Pt)))
190             continue;
191
192         Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
193
194         if (Checks)
195             b &= pos.check_squares(Pt);
196
197         while (b)
198             *moveList++ = make_move(from, pop_lsb(&b));
199     }
200
201     return moveList;
202   }
203
204
205   template<Color Us, GenType Type>
206   ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
207     constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantations
208     Bitboard target, piecesToMove = pos.pieces(Us);
209
210     if(Type == QUIET_CHECKS)
211         piecesToMove &= ~pos.blockers_for_king(~Us);
212
213     switch (Type)
214     {
215         case CAPTURES:
216             target =  pos.pieces(~Us);
217             break;
218         case QUIETS:
219         case QUIET_CHECKS:
220             target = ~pos.pieces();
221             break;
222         case EVASIONS:
223         {
224             Square checksq = lsb(pos.checkers());
225             target = between_bb(pos.square<KING>(Us), checksq) | checksq;
226             break;
227         }
228         case NON_EVASIONS:
229             target = ~pos.pieces(Us);
230             break;
231         default:
232             static_assert(true, "Unsupported type in generate_all()");
233     }
234
235     moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
236     moveList = generate_moves<KNIGHT, Checks>(pos, moveList, piecesToMove, target);
237     moveList = generate_moves<BISHOP, Checks>(pos, moveList, piecesToMove, target);
238     moveList = generate_moves<  ROOK, Checks>(pos, moveList, piecesToMove, target);
239     moveList = generate_moves< QUEEN, Checks>(pos, moveList, piecesToMove, target);
240
241     if (Type != QUIET_CHECKS && Type != EVASIONS)
242     {
243         Square ksq = pos.square<KING>(Us);
244         Bitboard b = attacks_bb<KING>(ksq) & target;
245         while (b)
246             *moveList++ = make_move(ksq, pop_lsb(&b));
247
248         if ((Type != CAPTURES) && pos.can_castle(Us & ANY_CASTLING))
249             for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
250                 if (!pos.castling_impeded(cr) && pos.can_castle(cr))
251                     *moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
252     }
253
254     return moveList;
255   }
256
257 } // namespace
258
259
260 /// <CAPTURES>     Generates all pseudo-legal captures plus queen and checking knight promotions
261 /// <QUIETS>       Generates all pseudo-legal non-captures and underpromotions(except checking knight)
262 /// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
263 ///
264 /// Returns a pointer to the end of the move list.
265
266 template<GenType Type>
267 ExtMove* generate(const Position& pos, ExtMove* moveList) {
268
269   static_assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS, "Unsupported type in generate()");
270   assert(!pos.checkers());
271
272   Color us = pos.side_to_move();
273
274   return us == WHITE ? generate_all<WHITE, Type>(pos, moveList)
275                      : generate_all<BLACK, Type>(pos, moveList);
276 }
277
278 // Explicit template instantiations
279 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
280 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
281 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
282
283
284 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures.
285 /// Returns a pointer to the end of the move list.
286 template<>
287 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
288
289   assert(!pos.checkers());
290
291   Color us = pos.side_to_move();
292   Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us) & ~pos.pieces(PAWN);
293
294   while (dc)
295   {
296      Square from = pop_lsb(&dc);
297      PieceType pt = type_of(pos.piece_on(from));
298
299      Bitboard b = attacks_bb(pt, from, pos.pieces()) & ~pos.pieces();
300
301      if (pt == KING)
302          b &= ~attacks_bb<QUEEN>(pos.square<KING>(~us));
303
304      while (b)
305          *moveList++ = make_move(from, pop_lsb(&b));
306   }
307
308   return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList)
309                      : generate_all<BLACK, QUIET_CHECKS>(pos, moveList);
310 }
311
312
313 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
314 /// to move is in check. Returns a pointer to the end of the move list.
315 template<>
316 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
317
318   assert(pos.checkers());
319
320   Color us = pos.side_to_move();
321   Square ksq = pos.square<KING>(us);
322   Bitboard sliderAttacks = 0;
323   Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
324
325   // Find all the squares attacked by slider checkers. We will remove them from
326   // the king evasions in order to skip known illegal moves, which avoids any
327   // useless legality checks later on.
328   while (sliders)
329       sliderAttacks |= line_bb(ksq, pop_lsb(&sliders)) & ~pos.checkers();
330
331   // Generate evasions for king, capture and non capture moves
332   Bitboard b = attacks_bb<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
333   while (b)
334       *moveList++ = make_move(ksq, pop_lsb(&b));
335
336   if (more_than_one(pos.checkers()))
337       return moveList; // Double check, only a king move can save the day
338
339   // Generate blocking evasions or captures of the checking piece
340   return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList)
341                      : generate_all<BLACK, EVASIONS>(pos, moveList);
342 }
343
344
345 /// generate<LEGAL> generates all the legal moves in the given position
346
347 template<>
348 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
349
350   Color us = pos.side_to_move();
351   Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
352   Square ksq = pos.square<KING>(us);
353   ExtMove* cur = moveList;
354
355   moveList = pos.checkers() ? generate<EVASIONS    >(pos, moveList)
356                             : generate<NON_EVASIONS>(pos, moveList);
357   while (cur != moveList)
358       if (   (pinned || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
359           && !pos.legal(*cur))
360           *cur = (--moveList)->move;
361       else
362           ++cur;
363
364   return moveList;
365 }