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