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/>.
30 // Simple macro to wrap a very common while loop, no facny, no flexibility,
31 // hardcoded list name 'mlist' and from square 'from'.
32 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
34 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
35 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
38 //// Local definitions
56 template<CastlingSide Side>
57 MoveStack* generate_castle_moves(const Position&, MoveStack*);
59 template<Color Us, MoveType Type>
60 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
62 // Template generate_piece_moves (captures and non-captures) with specializations and overloads
64 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
67 MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
69 template<PieceType Piece, MoveType Type>
70 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
72 assert(Piece == PAWN);
73 assert(Type == CAPTURE || Type == NON_CAPTURE || Type == EVASION);
75 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
76 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
79 // Templates for non-capture checks generation
81 template<PieceType Piece>
82 MoveStack* generate_discovered_checks(const Position&, MoveStack*, Square);
85 MoveStack* generate_direct_checks(const Position&, MoveStack*, Color, Bitboard, Square);
88 inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
90 return (us == WHITE ? generate_pawn_moves<WHITE, CHECK>(p, m, dc, ksq)
91 : generate_pawn_moves<BLACK, CHECK>(p, m, dc, ksq));
101 /// generate_captures() generates all pseudo-legal captures and queen
102 /// promotions. Returns a pointer to the end of the move list.
104 MoveStack* generate_captures(const Position& pos, MoveStack* mlist) {
107 assert(!pos.is_check());
109 Color us = pos.side_to_move();
110 Bitboard target = pos.pieces_of_color(opposite_color(us));
112 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
113 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
114 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
115 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
116 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us, target);
117 return generate_piece_moves<KING>(pos, mlist, us, target);
121 /// generate_noncaptures() generates all pseudo-legal non-captures and
122 /// underpromotions. Returns a pointer to the end of the move list.
124 MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist) {
127 assert(!pos.is_check());
129 Color us = pos.side_to_move();
130 Bitboard target = pos.empty_squares();
132 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us, target);
133 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
134 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
135 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
136 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
137 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
138 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
139 return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
143 /// generate_non_evasions() generates all pseudo-legal captures and
144 /// non-captures. Returns a pointer to the end of the move list.
146 MoveStack* generate_non_evasions(const Position& pos, MoveStack* mlist) {
149 assert(!pos.is_check());
151 Color us = pos.side_to_move();
152 Bitboard target = pos.pieces_of_color(opposite_color(us));
154 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us, target);
155 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
157 target |= pos.empty_squares();
159 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
160 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
161 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
162 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
163 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
164 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
165 return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
169 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
170 /// underpromotions that give check. Returns a pointer to the end of the move list.
172 MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist) {
175 assert(!pos.is_check());
179 Color us = pos.side_to_move();
180 Square ksq = pos.king_square(opposite_color(us));
182 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
184 // Discovered non-capture checks
185 b = dc = pos.discovered_check_candidates(us);
189 from = pop_1st_bit(&b);
190 switch (pos.type_of_piece_on(from))
192 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
193 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
194 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
195 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
196 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
197 default: assert(false); break;
201 // Direct non-capture checks
202 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
203 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
204 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
205 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
206 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
210 /// generate_evasions() generates all pseudo-legal check evasions when
211 /// the side to move is in check. Returns a pointer to the end of the move list.
213 MoveStack* generate_evasions(const Position& pos, MoveStack* mlist) {
216 assert(pos.is_check());
219 Square from, checksq;
221 Color us = pos.side_to_move();
222 Square ksq = pos.king_square(us);
223 Bitboard checkers = pos.checkers();
224 Bitboard sliderAttacks = EmptyBoardBB;
226 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
229 // Find squares attacked by slider checkers, we will remove
230 // them from the king evasions set so to early skip known
231 // illegal moves and avoid an useless legality check later.
236 checksq = pop_1st_bit(&b);
238 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
240 switch (pos.type_of_piece_on(checksq))
242 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
243 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
245 // In case of a queen remove also squares attacked in the other direction to
246 // avoid possible illegal moves when queen and king are on adjacent squares.
247 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
248 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
250 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
256 // Generate evasions for king, capture and non capture moves
257 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
261 // Generate evasions for other pieces only if not double check
265 // Find squares where a blocking evasion or a capture of the
266 // checker piece is possible.
267 target = squares_between(checksq, ksq) | checkers;
269 mlist = generate_piece_moves<PAWN, EVASION>(pos, mlist, us, target);
270 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
271 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
272 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
273 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
277 /// generate_moves() computes a complete list of legal or pseudo-legal moves in
278 /// the current position. This function is not very fast, and should be used
279 /// only in non time-critical paths.
281 MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLegal) {
285 MoveStack *last, *cur = mlist;
286 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
288 // Generate pseudo-legal moves
289 last = pos.is_check() ? generate_evasions(pos, mlist)
290 : generate_non_evasions(pos, mlist);
294 // Remove illegal moves from the list
296 if (pos.pl_move_is_legal(cur->move, pinned))
299 cur->move = (--last)->move;
305 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
306 /// move and tests whether the move is legal. This version is not very fast
307 /// and should be used only in non time-critical paths.
309 bool move_is_legal(const Position& pos, const Move m) {
311 MoveStack mlist[MOVES_MAX];
312 MoveStack *cur, *last = generate_moves(pos, mlist, true);
314 for (cur = mlist; cur != last; cur++)
316 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
322 /// Fast version of move_is_legal() that takes a position a move and a
323 /// bitboard of pinned pieces as input, and tests whether the move is legal.
325 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
328 assert(move_is_ok(m));
329 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
331 Color us = pos.side_to_move();
332 Color them = opposite_color(us);
333 Square from = move_from(m);
334 Square to = move_to(m);
335 Piece pc = pos.piece_on(from);
337 // Use a slower but simpler function for uncommon cases
338 if (move_is_special(m))
339 return move_is_legal(pos, m);
341 // If the from square is not occupied by a piece belonging to the side to
342 // move, the move is obviously not legal.
343 if (color_of_piece(pc) != us)
346 // The destination square cannot be occupied by a friendly piece
347 if (pos.color_of_piece_on(to) == us)
350 // Handle the special case of a pawn move
351 if (type_of_piece(pc) == PAWN)
353 // Move direction must be compatible with pawn color
354 int direction = to - from;
355 if ((us == WHITE) != (direction > 0))
358 // We have already handled promotion moves, so destination
359 // cannot be on the 8/1th rank.
360 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
363 // Proceed according to the square delta between the origin and
364 // destination squares.
371 // Capture. The destination square must be occupied by an enemy
372 // piece (en passant captures was handled earlier).
373 if (pos.color_of_piece_on(to) != them)
379 // Pawn push. The destination square must be empty.
380 if (!pos.square_is_empty(to))
385 // Double white pawn push. The destination square must be on the fourth
386 // rank, and both the destination square and the square between the
387 // source and destination squares must be empty.
388 if ( square_rank(to) != RANK_4
389 || !pos.square_is_empty(to)
390 || !pos.square_is_empty(from + DELTA_N))
395 // Double black pawn push. The destination square must be on the fifth
396 // rank, and both the destination square and the square between the
397 // source and destination squares must be empty.
398 if ( square_rank(to) != RANK_5
399 || !pos.square_is_empty(to)
400 || !pos.square_is_empty(from + DELTA_S))
408 else if (!bit_is_set(pos.attacks_from(pc, from), to))
411 // The move is pseudo-legal, check if it is also legal
412 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
418 template<PieceType Piece>
419 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
423 const Square* ptr = pos.piece_list_begin(us, Piece);
429 b = pos.attacks_from<Piece>(from) & target;
431 } while (*++ptr != SQ_NONE);
437 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
440 Square from = pos.king_square(us);
442 b = pos.attacks_from<KING>(from) & target;
447 template<SquareDelta Delta>
448 inline Bitboard move_pawns(Bitboard p) {
450 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
451 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
452 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
455 template<MoveType Type, SquareDelta Delta>
456 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
458 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
463 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
464 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
465 SERIALIZE_MOVES_D(b, -Delta);
469 template<Color Us, MoveType Type, SquareDelta Delta>
470 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
472 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
477 // Promotions and under-promotions, both captures and non-captures
478 b = move_pawns<Delta>(pawnsOn7) & target;
480 if (Delta != DELTA_N && Delta != DELTA_S)
485 to = pop_1st_bit(&b);
487 if (Type == CAPTURE || Type == EVASION)
488 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
490 if (Type == NON_CAPTURE || Type == EVASION)
492 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
493 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
494 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
497 // This is the only possible under promotion that can give a check
498 // not already included in the queen-promotion.
500 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
501 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
502 else (void)pos; // Silence a warning under MSVC
507 template<Color Us, MoveType Type>
508 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
510 // Calculate our parametrized parameters at compile time, named
511 // according to the point of view of white side.
512 const Color Them = (Us == WHITE ? BLACK : WHITE);
513 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
514 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
515 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
516 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
517 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
520 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
521 Bitboard pawns = pos.pieces(PAWN, Us);
522 Bitboard pawnsOn7 = pawns & TRank7BB;
523 Bitboard enemyPieces = (Type == CAPTURE ? target : pos.pieces_of_color(Them));
525 // Pre-calculate pawn pushes before changing emptySquares definition
528 emptySquares = (Type == NON_CAPTURE ? target : pos.empty_squares());
529 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
534 emptySquares &= target; // Only blocking squares
535 enemyPieces &= target; // Capture only the checker piece
538 // Promotions and underpromotions
542 emptySquares = pos.empty_squares();
545 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
546 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
547 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
551 if (Type == CAPTURE || Type == EVASION)
553 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
554 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
557 // Single and double pawn pushes
560 b1 = pawnPushes & emptySquares;
561 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
565 // Condider only pawn moves which give direct checks
566 b1 &= pos.attacks_from<PAWN>(ksq, Them);
567 b2 &= pos.attacks_from<PAWN>(ksq, Them);
569 // Add pawn moves which gives discovered check. This is possible only
570 // if the pawn is not on the same file as the enemy king, because we
571 // don't generate captures.
572 if (pawns & target) // For CHECK type target is dc bitboard
574 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
575 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
581 SERIALIZE_MOVES_D(b1, -TDELTA_N);
582 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
585 // En passant captures
586 if ((Type == CAPTURE || Type == EVASION) && pos.ep_square() != SQ_NONE)
588 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
589 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
591 // An en passant capture can be an evasion only if the checking piece
592 // is the double pushed pawn and so is in the target. Otherwise this
593 // is a discovery check and we are forced to do otherwise.
594 if (Type == EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
597 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
599 assert(b1 != EmptyBoardBB);
603 to = pop_1st_bit(&b1);
604 (*mlist++).move = make_ep_move(to, pos.ep_square());
610 template<PieceType Piece>
611 MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
613 assert(Piece != QUEEN);
615 Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
618 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
619 b &= ~QueenPseudoAttacks[ksq];
625 template<PieceType Piece>
626 MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
627 Bitboard dc, Square ksq) {
628 assert(Piece != KING);
630 Bitboard checkSqs, b;
632 const Square* ptr = pos.piece_list_begin(us, Piece);
634 if ((from = *ptr++) == SQ_NONE)
637 checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
641 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
642 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
643 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
646 if (dc && bit_is_set(dc, from))
649 b = pos.attacks_from<Piece>(from) & checkSqs;
652 } while ((from = *ptr++) != SQ_NONE);
657 template<CastlingSide Side>
658 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
660 Color us = pos.side_to_move();
662 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
663 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
665 Color them = opposite_color(us);
666 Square ksq = pos.king_square(us);
668 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
670 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
671 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
672 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
674 bool illegal = false;
676 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
678 // It is a bit complicated to correctly handle Chess960
679 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
680 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
681 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
684 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
685 if (s != ksq && s != rsq && pos.square_is_occupied(s))
688 if ( Side == QUEEN_SIDE
689 && square_file(rsq) == FILE_B
690 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
691 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
695 (*mlist++).move = make_castle_move(ksq, rsq);