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-2010 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/>.
25 // Simple macro to wrap a very common while loop, no facny, no flexibility,
26 // hardcoded list name 'mlist' and from square 'from'.
27 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
29 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
30 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
39 template<CastlingSide>
40 MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
42 template<Color, MoveType>
43 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
45 template<PieceType Pt>
46 inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
50 Bitboard b = pos.attacks_from<Pt>(from) & pos.empty_squares();
53 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
54 b &= ~QueenPseudoAttacks[ksq];
60 template<PieceType Pt>
61 inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
62 Bitboard dc, Square ksq) {
67 const Square* ptr = pos.piece_list_begin(us, Pt);
69 if ((from = *ptr++) == SQ_NONE)
72 checkSqs = pos.attacks_from<Pt>(ksq) & pos.empty_squares();
76 if ( (Pt == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
77 || (Pt == ROOK && !(RookPseudoAttacks[from] & checkSqs))
78 || (Pt == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
81 if (dc && bit_is_set(dc, from))
84 b = pos.attacks_from<Pt>(from) & checkSqs;
87 } while ((from = *ptr++) != SQ_NONE);
93 FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
95 return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
96 : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
99 template<PieceType Pt, MoveType Type>
100 FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
103 assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
105 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
106 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
109 template<PieceType Pt>
110 FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
114 const Square* ptr = pos.piece_list_begin(us, Pt);
120 b = pos.attacks_from<Pt>(from) & target;
122 } while (*++ptr != SQ_NONE);
128 FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
131 Square from = pos.king_square(us);
133 b = pos.attacks_from<KING>(from) & target;
141 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
142 /// promotions. Returns a pointer to the end of the move list.
144 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
145 /// underpromotions. Returns a pointer to the end of the move list.
147 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
148 /// non-captures. Returns a pointer to the end of the move list.
150 template<MoveType Type>
151 MoveStack* generate(const Position& pos, MoveStack* mlist) {
154 assert(!pos.is_check());
156 Color us = pos.side_to_move();
159 if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
160 target = pos.pieces_of_color(opposite_color(us));
161 else if (Type == MV_NON_CAPTURE)
162 target = pos.empty_squares();
166 if (Type == MV_NON_EVASION)
168 mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
169 mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
170 target |= pos.empty_squares();
173 mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
175 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
176 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
177 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
178 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
179 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
181 if (Type != MV_CAPTURE)
183 if (pos.can_castle_kingside(us))
184 mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
186 if (pos.can_castle_queenside(us))
187 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
193 // Explicit template instantiations
194 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
195 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
196 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
199 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
200 /// underpromotions that give check. Returns a pointer to the end of the move list.
202 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
205 assert(!pos.is_check());
209 Color us = pos.side_to_move();
210 Square ksq = pos.king_square(opposite_color(us));
212 assert(pos.piece_on(ksq) == make_piece(opposite_color(us), KING));
214 // Discovered non-capture checks
215 b = dc = pos.discovered_check_candidates(us);
219 from = pop_1st_bit(&b);
220 switch (pos.type_of_piece_on(from))
222 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
223 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
224 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
225 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
226 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
227 default: assert(false); break;
231 // Direct non-capture checks
232 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
233 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
234 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
235 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
236 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
240 /// generate_evasions() generates all pseudo-legal check evasions when
241 /// the side to move is in check. Returns a pointer to the end of the move list.
243 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
246 assert(pos.is_check());
249 Square from, checksq;
251 Color us = pos.side_to_move();
252 Square ksq = pos.king_square(us);
253 Bitboard checkers = pos.checkers();
254 Bitboard sliderAttacks = EmptyBoardBB;
256 assert(pos.piece_on(ksq) == make_piece(us, KING));
259 // Find squares attacked by slider checkers, we will remove
260 // them from the king evasions set so to early skip known
261 // illegal moves and avoid an useless legality check later.
266 checksq = pop_1st_bit(&b);
268 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
270 switch (pos.type_of_piece_on(checksq))
272 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
273 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
275 // In case of a queen remove also squares attacked in the other direction to
276 // avoid possible illegal moves when queen and king are on adjacent squares.
277 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
278 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
280 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
286 // Generate evasions for king, capture and non capture moves
287 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
291 // Generate evasions for other pieces only if not double check
295 // Find squares where a blocking evasion or a capture of the
296 // checker piece is possible.
297 target = squares_between(checksq, ksq) | checkers;
299 mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
300 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
301 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
302 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
303 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
307 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
308 /// or pseudo-legal moves in the current position.
310 MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
314 return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
315 : generate<MV_NON_EVASION>(pos, mlist);
319 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
323 MoveStack *last, *cur = mlist;
324 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
326 last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
328 // Remove illegal moves from the list
330 if (!pos.pl_move_is_legal(cur->move, pinned))
331 cur->move = (--last)->move;
341 template<Square Delta>
342 inline Bitboard move_pawns(Bitboard p) {
344 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
345 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
346 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
349 template<MoveType Type, Square Delta>
350 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
352 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
357 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
358 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
359 SERIALIZE_MOVES_D(b, -Delta);
363 template<Color Us, MoveType Type, Square Delta>
364 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
366 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
371 // Promotions and under-promotions, both captures and non-captures
372 b = move_pawns<Delta>(pawnsOn7) & target;
374 if (Delta != DELTA_N && Delta != DELTA_S)
379 to = pop_1st_bit(&b);
381 if (Type == MV_CAPTURE || Type == MV_EVASION)
382 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
384 if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
386 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
387 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
388 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
391 // This is the only possible under promotion that can give a check
392 // not already included in the queen-promotion.
393 if ( Type == MV_CHECK
394 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
395 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
396 else (void)pos; // Silence a warning under MSVC
401 template<Color Us, MoveType Type>
402 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
404 // Calculate our parametrized parameters at compile time, named
405 // according to the point of view of white side.
406 const Color Them = (Us == WHITE ? BLACK : WHITE);
407 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
408 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
409 const Square TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
410 const Square TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
411 const Square TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
414 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
415 Bitboard pawns = pos.pieces(PAWN, Us);
416 Bitboard pawnsOn7 = pawns & TRank7BB;
417 Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
419 // Pre-calculate pawn pushes before changing emptySquares definition
420 if (Type != MV_CAPTURE)
422 emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
423 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
426 if (Type == MV_EVASION)
428 emptySquares &= target; // Only blocking squares
429 enemyPieces &= target; // Capture only the checker piece
432 // Promotions and underpromotions
435 if (Type == MV_CAPTURE)
436 emptySquares = pos.empty_squares();
439 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
440 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
441 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
445 if (Type == MV_CAPTURE || Type == MV_EVASION)
447 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
448 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
451 // Single and double pawn pushes
452 if (Type != MV_CAPTURE)
454 b1 = pawnPushes & emptySquares;
455 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
457 if (Type == MV_CHECK)
459 // Consider only pawn moves which give direct checks
460 b1 &= pos.attacks_from<PAWN>(ksq, Them);
461 b2 &= pos.attacks_from<PAWN>(ksq, Them);
463 // Add pawn moves which gives discovered check. This is possible only
464 // if the pawn is not on the same file as the enemy king, because we
465 // don't generate captures.
466 if (pawns & target) // For CHECK type target is dc bitboard
468 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
469 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
475 SERIALIZE_MOVES_D(b1, -TDELTA_N);
476 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
479 // En passant captures
480 if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
482 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
483 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
485 // An en passant capture can be an evasion only if the checking piece
486 // is the double pushed pawn and so is in the target. Otherwise this
487 // is a discovery check and we are forced to do otherwise.
488 if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
491 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
493 assert(b1 != EmptyBoardBB);
497 to = pop_1st_bit(&b1);
498 (*mlist++).move = make_ep_move(to, pos.ep_square());
504 template<CastlingSide Side>
505 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
507 Color them = opposite_color(us);
508 Square ksq = pos.king_square(us);
510 assert(pos.piece_on(ksq) == make_piece(us, KING));
512 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
513 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
514 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
516 bool illegal = false;
518 assert(pos.piece_on(rsq) == make_piece(us, ROOK));
520 // It is a bit complicated to correctly handle Chess960
521 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
522 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
523 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
526 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
527 if (s != ksq && s != rsq && pos.square_is_occupied(s))
530 if ( Side == QUEEN_SIDE
531 && square_file(rsq) == FILE_B
532 && ( pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, ROOK)
533 || pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, QUEEN)))
537 (*mlist++).move = make_castle_move(ksq, rsq);