2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 template<CastlingRight Cr, bool Checks, bool Chess960>
28 ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us, const CheckInfo* ci) {
30 static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
32 if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
35 // After castling, the rook and king final positions are the same in Chess960
36 // as they would be in standard chess.
37 Square kfrom = pos.king_square(us);
38 Square rfrom = pos.castling_rook_square(Cr);
39 Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
40 Bitboard enemies = pos.pieces(~us);
42 assert(!pos.checkers());
44 const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
45 : KingSide ? DELTA_W : DELTA_E;
47 for (Square s = kto; s != kfrom; s += K)
48 if (pos.attackers_to(s) & enemies)
51 // Because we generate only legal castling moves we need to verify that
52 // when moving the castling rook we do not discover some hidden checker.
53 // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
54 if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
57 Move m = make<CASTLING>(kfrom, rfrom);
59 if (Checks && !pos.gives_check(m, *ci))
62 (moveList++)->move = m;
68 template<GenType Type, Square Delta>
69 inline ExtMove* make_promotions(ExtMove* moveList, Square to, const CheckInfo* ci) {
71 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
72 (moveList++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
74 if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
76 (moveList++)->move = make<PROMOTION>(to - Delta, to, ROOK);
77 (moveList++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
78 (moveList++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
81 // Knight promotion is the only promotion that can give a direct check
82 // that's not already included in the queen promotion.
83 if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
84 (moveList++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
86 (void)ci; // Silence a warning under MSVC
92 template<Color Us, GenType Type>
93 ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList,
94 Bitboard target, const CheckInfo* ci) {
96 // Compute our parametrized parameters at compile time, named according to
97 // the point of view of white side.
98 const Color Them = (Us == WHITE ? BLACK : WHITE);
99 const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
100 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
101 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
102 const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
103 const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
104 const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
106 Bitboard emptySquares;
108 Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
109 Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
111 Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
112 Type == CAPTURES ? target : pos.pieces(Them));
114 // Single and double pawn pushes, no promotions
115 if (Type != CAPTURES)
117 emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
119 Bitboard b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
120 Bitboard b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
122 if (Type == EVASIONS) // Consider only blocking squares
128 if (Type == QUIET_CHECKS)
130 b1 &= pos.attacks_from<PAWN>(ci->ksq, Them);
131 b2 &= pos.attacks_from<PAWN>(ci->ksq, Them);
133 // Add pawn pushes which give discovered check. This is possible only
134 // if the pawn is not on the same file as the enemy king, because we
135 // don't generate captures. Note that a possible discovery check
136 // promotion has been already generated amongst the captures.
137 if (pawnsNotOn7 & ci->dcCandidates)
139 Bitboard dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
140 Bitboard dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
149 Square to = pop_lsb(&b1);
150 (moveList++)->move = make_move(to - Up, to);
155 Square to = pop_lsb(&b2);
156 (moveList++)->move = make_move(to - Up - Up, to);
160 // Promotions and underpromotions
161 if (pawnsOn7 && (Type != EVASIONS || (target & TRank8BB)))
163 if (Type == CAPTURES)
164 emptySquares = ~pos.pieces();
166 if (Type == EVASIONS)
167 emptySquares &= target;
169 Bitboard b1 = shift_bb<Right>(pawnsOn7) & enemies;
170 Bitboard b2 = shift_bb<Left >(pawnsOn7) & enemies;
171 Bitboard b3 = shift_bb<Up >(pawnsOn7) & emptySquares;
174 moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ci);
177 moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ci);
180 moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ci);
183 // Standard and en-passant captures
184 if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
186 Bitboard b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
187 Bitboard b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
191 Square to = pop_lsb(&b1);
192 (moveList++)->move = make_move(to - Right, to);
197 Square to = pop_lsb(&b2);
198 (moveList++)->move = make_move(to - Left, to);
201 if (pos.ep_square() != SQ_NONE)
203 assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
205 // An en passant capture can be an evasion only if the checking piece
206 // is the double pushed pawn and so is in the target. Otherwise this
207 // is a discovery check and we are forced to do otherwise.
208 if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
211 b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
216 (moveList++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
224 template<PieceType Pt, bool Checks> FORCE_INLINE
225 ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
226 Bitboard target, const CheckInfo* ci) {
228 assert(Pt != KING && Pt != PAWN);
230 const Square* pl = pos.list<Pt>(us);
232 for (Square from = *pl; from != SQ_NONE; from = *++pl)
236 if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
237 && !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
240 if (ci->dcCandidates && (ci->dcCandidates & from))
244 Bitboard b = pos.attacks_from<Pt>(from) & target;
247 b &= ci->checkSq[Pt];
250 (moveList++)->move = make_move(from, pop_lsb(&b));
257 template<Color Us, GenType Type> FORCE_INLINE
258 ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target,
259 const CheckInfo* ci = NULL) {
261 const bool Checks = Type == QUIET_CHECKS;
263 moveList = generate_pawn_moves<Us, Type>(pos, moveList, target, ci);
264 moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target, ci);
265 moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target, ci);
266 moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target, ci);
267 moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target, ci);
269 if (Type != QUIET_CHECKS && Type != EVASIONS)
271 Square ksq = pos.king_square(Us);
272 Bitboard b = pos.attacks_from<KING>(ksq) & target;
274 (moveList++)->move = make_move(ksq, pop_lsb(&b));
277 if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
279 if (pos.is_chess960())
281 moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, moveList, Us, ci);
282 moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us, ci);
286 moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, moveList, Us, ci);
287 moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us, ci);
297 /// generate<CAPTURES> generates all pseudo-legal captures and queen
298 /// promotions. Returns a pointer to the end of the move list.
300 /// generate<QUIETS> generates all pseudo-legal non-captures and
301 /// underpromotions. Returns a pointer to the end of the move list.
303 /// generate<NON_EVASIONS> generates all pseudo-legal captures and
304 /// non-captures. Returns a pointer to the end of the move list.
306 template<GenType Type>
307 ExtMove* generate(const Position& pos, ExtMove* moveList) {
309 assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
310 assert(!pos.checkers());
312 Color us = pos.side_to_move();
314 Bitboard target = Type == CAPTURES ? pos.pieces(~us)
315 : Type == QUIETS ? ~pos.pieces()
316 : Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
318 return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
319 : generate_all<BLACK, Type>(pos, moveList, target);
322 // Explicit template instantiations
323 template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
324 template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
325 template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
328 /// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
329 /// underpromotions that give check. Returns a pointer to the end of the move list.
331 ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
333 assert(!pos.checkers());
335 Color us = pos.side_to_move();
337 Bitboard dc = ci.dcCandidates;
341 Square from = pop_lsb(&dc);
342 PieceType pt = type_of(pos.piece_on(from));
345 continue; // Will be generated together with direct checks
347 Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
350 b &= ~PseudoAttacks[QUEEN][ci.ksq];
353 (moveList++)->move = make_move(from, pop_lsb(&b));
356 return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci)
357 : generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci);
361 /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
362 /// to move is in check. Returns a pointer to the end of the move list.
364 ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
366 assert(pos.checkers());
368 Color us = pos.side_to_move();
369 Square ksq = pos.king_square(us);
370 Bitboard sliderAttacks = 0;
371 Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
373 // Find all the squares attacked by slider checkers. We will remove them from
374 // the king evasions in order to skip known illegal moves, which avoids any
375 // useless legality checks later on.
378 Square checksq = pop_lsb(&sliders);
379 sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
382 // Generate evasions for king, capture and non capture moves
383 Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
385 (moveList++)->move = make_move(ksq, pop_lsb(&b));
387 if (more_than_one(pos.checkers()))
388 return moveList; // Double check, only a king move can save the day
390 // Generate blocking evasions or captures of the checking piece
391 Square checksq = lsb(pos.checkers());
392 Bitboard target = between_bb(checksq, ksq) | checksq;
394 return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
395 : generate_all<BLACK, EVASIONS>(pos, moveList, target);
399 /// generate<LEGAL> generates all the legal moves in the given position
402 ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
404 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
405 Square ksq = pos.king_square(pos.side_to_move());
406 ExtMove* cur = moveList;
408 moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
409 : generate<NON_EVASIONS>(pos, moveList);
410 while (cur != moveList)
411 if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
412 && !pos.legal(cur->move, pinned))
413 cur->move = (--moveList)->move;