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