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/>.
31 // Simple macro to wrap a very common while loop, no facny, no flexibility,
32 // hardcoded list name 'mlist' and from square 'from'.
33 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
35 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
36 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
39 //// Local definitions
49 template<CastlingSide Side>
50 MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
52 template<Color Us, MoveType Type>
53 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
55 template<PieceType Piece>
56 inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
58 assert(Piece != QUEEN);
60 Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
63 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
64 b &= ~QueenPseudoAttacks[ksq];
70 template<PieceType Piece>
71 inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
72 Bitboard dc, Square ksq) {
73 assert(Piece != KING);
77 const Square* ptr = pos.piece_list_begin(us, Piece);
79 if ((from = *ptr++) == SQ_NONE)
82 checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
86 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
87 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
88 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
91 if (dc && bit_is_set(dc, from))
94 b = pos.attacks_from<Piece>(from) & checkSqs;
97 } while ((from = *ptr++) != SQ_NONE);
103 FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
105 return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
106 : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
109 template<PieceType Piece, MoveType Type>
110 FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
112 assert(Piece == PAWN);
113 assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
115 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
116 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
119 template<PieceType Piece>
120 FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
124 const Square* ptr = pos.piece_list_begin(us, Piece);
130 b = pos.attacks_from<Piece>(from) & target;
132 } while (*++ptr != SQ_NONE);
138 FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
141 Square from = pos.king_square(us);
143 b = pos.attacks_from<KING>(from) & target;
155 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
156 /// promotions. Returns a pointer to the end of the move list.
158 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
159 /// underpromotions. Returns a pointer to the end of the move list.
161 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
162 /// non-captures. Returns a pointer to the end of the move list.
164 template<MoveType Type>
165 MoveStack* generate(const Position& pos, MoveStack* mlist) {
168 assert(!pos.is_check());
170 Color us = pos.side_to_move();
173 if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
174 target = pos.pieces_of_color(opposite_color(us));
175 else if (Type == MV_NON_CAPTURE)
176 target = pos.empty_squares();
180 if (Type == MV_NON_EVASION)
182 mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
183 mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
184 target |= pos.empty_squares();
187 mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
189 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
190 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
191 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
192 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
193 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
195 if (Type != MV_CAPTURE)
197 if (pos.can_castle_kingside(us))
198 mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
200 if (pos.can_castle_queenside(us))
201 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
207 // Explicit template instantiation
208 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
209 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
210 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
213 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
214 /// underpromotions that give check. Returns a pointer to the end of the move list.
216 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
219 assert(!pos.is_check());
223 Color us = pos.side_to_move();
224 Square ksq = pos.king_square(opposite_color(us));
226 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
228 // Discovered non-capture checks
229 b = dc = pos.discovered_check_candidates(us);
233 from = pop_1st_bit(&b);
234 switch (pos.type_of_piece_on(from))
236 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
237 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
238 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
239 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
240 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
241 default: assert(false); break;
245 // Direct non-capture checks
246 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
247 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
248 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
249 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
250 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
254 /// generate_evasions() generates all pseudo-legal check evasions when
255 /// the side to move is in check. Returns a pointer to the end of the move list.
257 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
260 assert(pos.is_check());
263 Square from, checksq;
265 Color us = pos.side_to_move();
266 Square ksq = pos.king_square(us);
267 Bitboard checkers = pos.checkers();
268 Bitboard sliderAttacks = EmptyBoardBB;
270 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
273 // Find squares attacked by slider checkers, we will remove
274 // them from the king evasions set so to early skip known
275 // illegal moves and avoid an useless legality check later.
280 checksq = pop_1st_bit(&b);
282 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
284 switch (pos.type_of_piece_on(checksq))
286 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
287 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
289 // In case of a queen remove also squares attacked in the other direction to
290 // avoid possible illegal moves when queen and king are on adjacent squares.
291 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
292 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
294 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
300 // Generate evasions for king, capture and non capture moves
301 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
305 // Generate evasions for other pieces only if not double check
309 // Find squares where a blocking evasion or a capture of the
310 // checker piece is possible.
311 target = squares_between(checksq, ksq) | checkers;
313 mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
314 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
315 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
316 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
317 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
321 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
322 /// or pseudo-legal moves in the current position.
324 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
328 return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
329 : generate<MV_NON_EVASION>(pos, mlist);
333 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
337 MoveStack *last, *cur = mlist;
338 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
340 last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
342 // Remove illegal moves from the list
344 if (pos.pl_move_is_legal(cur->move, pinned))
347 cur->move = (--last)->move;
353 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
354 /// move and tests whether the move is legal. This version is not very fast
355 /// and should be used only in non time-critical paths.
357 bool move_is_legal(const Position& pos, const Move m) {
359 MoveStack mlist[MOVES_MAX];
360 MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
362 for (cur = mlist; cur != last; cur++)
364 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
370 /// Fast version of move_is_legal() that takes a position a move and a
371 /// bitboard of pinned pieces as input, and tests whether the move is legal.
373 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
376 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
378 Color us = pos.side_to_move();
379 Color them = opposite_color(us);
380 Square from = move_from(m);
381 Square to = move_to(m);
382 Piece pc = pos.piece_on(from);
384 // Use a slower but simpler function for uncommon cases
385 if (move_is_special(m))
386 return move_is_legal(pos, m);
388 // If the from square is not occupied by a piece belonging to the side to
389 // move, the move is obviously not legal.
390 if (color_of_piece(pc) != us)
393 // The destination square cannot be occupied by a friendly piece
394 if (pos.color_of_piece_on(to) == us)
397 // Handle the special case of a pawn move
398 if (type_of_piece(pc) == PAWN)
400 // Move direction must be compatible with pawn color
401 int direction = to - from;
402 if ((us == WHITE) != (direction > 0))
405 // We have already handled promotion moves, so destination
406 // cannot be on the 8/1th rank.
407 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
410 // Proceed according to the square delta between the origin and
411 // destination squares.
418 // Capture. The destination square must be occupied by an enemy
419 // piece (en passant captures was handled earlier).
420 if (pos.color_of_piece_on(to) != them)
426 // Pawn push. The destination square must be empty.
427 if (!pos.square_is_empty(to))
432 // Double white pawn push. The destination square must be on the fourth
433 // rank, and both the destination square and the square between the
434 // source and destination squares must be empty.
435 if ( square_rank(to) != RANK_4
436 || !pos.square_is_empty(to)
437 || !pos.square_is_empty(from + DELTA_N))
442 // Double black pawn push. The destination square must be on the fifth
443 // rank, and both the destination square and the square between the
444 // source and destination squares must be empty.
445 if ( square_rank(to) != RANK_5
446 || !pos.square_is_empty(to)
447 || !pos.square_is_empty(from + DELTA_S))
455 else if (!bit_is_set(pos.attacks_from(pc, from), to))
458 // The move is pseudo-legal, check if it is also legal
459 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
465 template<Square Delta>
466 inline Bitboard move_pawns(Bitboard p) {
468 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
469 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
470 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
473 template<MoveType Type, Square Delta>
474 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
476 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
481 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
482 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
483 SERIALIZE_MOVES_D(b, -Delta);
487 template<Color Us, MoveType Type, Square Delta>
488 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
490 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
495 // Promotions and under-promotions, both captures and non-captures
496 b = move_pawns<Delta>(pawnsOn7) & target;
498 if (Delta != DELTA_N && Delta != DELTA_S)
503 to = pop_1st_bit(&b);
505 if (Type == MV_CAPTURE || Type == MV_EVASION)
506 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
508 if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
510 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
511 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
512 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
515 // This is the only possible under promotion that can give a check
516 // not already included in the queen-promotion.
517 if ( Type == MV_CHECK
518 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
519 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
520 else (void)pos; // Silence a warning under MSVC
525 template<Color Us, MoveType Type>
526 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
528 // Calculate our parametrized parameters at compile time, named
529 // according to the point of view of white side.
530 const Color Them = (Us == WHITE ? BLACK : WHITE);
531 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
532 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
533 const Square TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
534 const Square TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
535 const Square TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
538 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
539 Bitboard pawns = pos.pieces(PAWN, Us);
540 Bitboard pawnsOn7 = pawns & TRank7BB;
541 Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
543 // Pre-calculate pawn pushes before changing emptySquares definition
544 if (Type != MV_CAPTURE)
546 emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
547 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
550 if (Type == MV_EVASION)
552 emptySquares &= target; // Only blocking squares
553 enemyPieces &= target; // Capture only the checker piece
556 // Promotions and underpromotions
559 if (Type == MV_CAPTURE)
560 emptySquares = pos.empty_squares();
563 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
564 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
565 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
569 if (Type == MV_CAPTURE || Type == MV_EVASION)
571 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
572 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
575 // Single and double pawn pushes
576 if (Type != MV_CAPTURE)
578 b1 = pawnPushes & emptySquares;
579 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
581 if (Type == MV_CHECK)
583 // Consider only pawn moves which give direct checks
584 b1 &= pos.attacks_from<PAWN>(ksq, Them);
585 b2 &= pos.attacks_from<PAWN>(ksq, Them);
587 // Add pawn moves which gives discovered check. This is possible only
588 // if the pawn is not on the same file as the enemy king, because we
589 // don't generate captures.
590 if (pawns & target) // For CHECK type target is dc bitboard
592 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
593 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
599 SERIALIZE_MOVES_D(b1, -TDELTA_N);
600 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
603 // En passant captures
604 if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
606 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
607 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
609 // An en passant capture can be an evasion only if the checking piece
610 // is the double pushed pawn and so is in the target. Otherwise this
611 // is a discovery check and we are forced to do otherwise.
612 if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
615 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
617 assert(b1 != EmptyBoardBB);
621 to = pop_1st_bit(&b1);
622 (*mlist++).move = make_ep_move(to, pos.ep_square());
628 template<CastlingSide Side>
629 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
631 Color them = opposite_color(us);
632 Square ksq = pos.king_square(us);
634 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
636 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
637 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
638 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
640 bool illegal = false;
642 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
644 // It is a bit complicated to correctly handle Chess960
645 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
646 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
647 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
650 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
651 if (s != ksq && s != rsq && pos.square_is_occupied(s))
654 if ( Side == QUEEN_SIDE
655 && square_file(rsq) == FILE_B
656 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
657 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
661 (*mlist++).move = make_castle_move(ksq, rsq);