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
55 template<CastlingSide Side>
56 MoveStack* generate_castle_moves(const Position&, MoveStack*);
58 template<Color Us, MoveType Type>
59 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
61 template<PieceType Piece>
62 inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
64 assert(Piece != QUEEN);
66 Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
69 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
70 b &= ~QueenPseudoAttacks[ksq];
76 template<PieceType Piece>
77 inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
78 Bitboard dc, Square ksq) {
79 assert(Piece != KING);
83 const Square* ptr = pos.piece_list_begin(us, Piece);
85 if ((from = *ptr++) == SQ_NONE)
88 checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
92 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
93 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
94 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
97 if (dc && bit_is_set(dc, from))
100 b = pos.attacks_from<Piece>(from) & checkSqs;
103 } while ((from = *ptr++) != SQ_NONE);
109 inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
111 return (us == WHITE ? generate_pawn_moves<WHITE, CHECK>(p, m, dc, ksq)
112 : generate_pawn_moves<BLACK, CHECK>(p, m, dc, ksq));
115 template<PieceType Piece, MoveType Type>
116 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
118 assert(Piece == PAWN);
119 assert(Type == CAPTURE || Type == NON_CAPTURE || Type == EVASION);
121 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
122 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
125 template<PieceType Piece>
126 inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
130 const Square* ptr = pos.piece_list_begin(us, Piece);
136 b = pos.attacks_from<Piece>(from) & target;
138 } while (*++ptr != SQ_NONE);
144 inline MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
147 Square from = pos.king_square(us);
149 b = pos.attacks_from<KING>(from) & target;
162 /// generate_captures() generates all pseudo-legal captures and queen
163 /// promotions. Returns a pointer to the end of the move list.
165 MoveStack* generate_captures(const Position& pos, MoveStack* mlist) {
168 assert(!pos.is_check());
170 Color us = pos.side_to_move();
171 Bitboard target = pos.pieces_of_color(opposite_color(us));
173 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
174 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
175 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
176 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
177 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us, target);
178 return generate_piece_moves<KING>(pos, mlist, us, target);
182 /// generate_noncaptures() generates all pseudo-legal non-captures and
183 /// underpromotions. Returns a pointer to the end of the move list.
185 MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist) {
188 assert(!pos.is_check());
190 Color us = pos.side_to_move();
191 Bitboard target = pos.empty_squares();
193 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us, target);
194 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
195 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
196 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
197 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
198 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
199 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
200 return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
204 /// generate_non_evasions() generates all pseudo-legal captures and
205 /// non-captures. Returns a pointer to the end of the move list.
207 MoveStack* generate_non_evasions(const Position& pos, MoveStack* mlist) {
210 assert(!pos.is_check());
212 Color us = pos.side_to_move();
213 Bitboard target = pos.pieces_of_color(opposite_color(us));
215 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us, target);
216 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
218 target |= pos.empty_squares();
220 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
221 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
222 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
223 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
224 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
225 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
226 return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
230 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
231 /// underpromotions that give check. Returns a pointer to the end of the move list.
233 MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist) {
236 assert(!pos.is_check());
240 Color us = pos.side_to_move();
241 Square ksq = pos.king_square(opposite_color(us));
243 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
245 // Discovered non-capture checks
246 b = dc = pos.discovered_check_candidates(us);
250 from = pop_1st_bit(&b);
251 switch (pos.type_of_piece_on(from))
253 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
254 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
255 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
256 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
257 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
258 default: assert(false); break;
262 // Direct non-capture checks
263 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
264 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
265 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
266 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
267 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
271 /// generate_evasions() generates all pseudo-legal check evasions when
272 /// the side to move is in check. Returns a pointer to the end of the move list.
274 MoveStack* generate_evasions(const Position& pos, MoveStack* mlist) {
277 assert(pos.is_check());
280 Square from, checksq;
282 Color us = pos.side_to_move();
283 Square ksq = pos.king_square(us);
284 Bitboard checkers = pos.checkers();
285 Bitboard sliderAttacks = EmptyBoardBB;
287 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
290 // Find squares attacked by slider checkers, we will remove
291 // them from the king evasions set so to early skip known
292 // illegal moves and avoid an useless legality check later.
297 checksq = pop_1st_bit(&b);
299 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
301 switch (pos.type_of_piece_on(checksq))
303 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
304 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
306 // In case of a queen remove also squares attacked in the other direction to
307 // avoid possible illegal moves when queen and king are on adjacent squares.
308 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
309 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
311 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
317 // Generate evasions for king, capture and non capture moves
318 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
322 // Generate evasions for other pieces only if not double check
326 // Find squares where a blocking evasion or a capture of the
327 // checker piece is possible.
328 target = squares_between(checksq, ksq) | checkers;
330 mlist = generate_piece_moves<PAWN, EVASION>(pos, mlist, us, target);
331 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
332 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
333 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
334 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
338 /// generate_moves() computes a complete list of legal or pseudo-legal moves in
339 /// the current position. This function is not very fast, and should be used
340 /// only in non time-critical paths.
342 MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLegal) {
346 MoveStack *last, *cur = mlist;
347 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
349 // Generate pseudo-legal moves
350 last = pos.is_check() ? generate_evasions(pos, mlist)
351 : generate_non_evasions(pos, mlist);
355 // Remove illegal moves from the list
357 if (pos.pl_move_is_legal(cur->move, pinned))
360 cur->move = (--last)->move;
366 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
367 /// move and tests whether the move is legal. This version is not very fast
368 /// and should be used only in non time-critical paths.
370 bool move_is_legal(const Position& pos, const Move m) {
372 MoveStack mlist[MOVES_MAX];
373 MoveStack *cur, *last = generate_moves(pos, mlist, true);
375 for (cur = mlist; cur != last; cur++)
377 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
383 /// Fast version of move_is_legal() that takes a position a move and a
384 /// bitboard of pinned pieces as input, and tests whether the move is legal.
386 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
389 assert(move_is_ok(m));
390 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
392 Color us = pos.side_to_move();
393 Color them = opposite_color(us);
394 Square from = move_from(m);
395 Square to = move_to(m);
396 Piece pc = pos.piece_on(from);
398 // Use a slower but simpler function for uncommon cases
399 if (move_is_special(m))
400 return move_is_legal(pos, m);
402 // If the from square is not occupied by a piece belonging to the side to
403 // move, the move is obviously not legal.
404 if (color_of_piece(pc) != us)
407 // The destination square cannot be occupied by a friendly piece
408 if (pos.color_of_piece_on(to) == us)
411 // Handle the special case of a pawn move
412 if (type_of_piece(pc) == PAWN)
414 // Move direction must be compatible with pawn color
415 int direction = to - from;
416 if ((us == WHITE) != (direction > 0))
419 // We have already handled promotion moves, so destination
420 // cannot be on the 8/1th rank.
421 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
424 // Proceed according to the square delta between the origin and
425 // destination squares.
432 // Capture. The destination square must be occupied by an enemy
433 // piece (en passant captures was handled earlier).
434 if (pos.color_of_piece_on(to) != them)
440 // Pawn push. The destination square must be empty.
441 if (!pos.square_is_empty(to))
446 // Double white pawn push. The destination square must be on the fourth
447 // rank, and both the destination square and the square between the
448 // source and destination squares must be empty.
449 if ( square_rank(to) != RANK_4
450 || !pos.square_is_empty(to)
451 || !pos.square_is_empty(from + DELTA_N))
456 // Double black pawn push. The destination square must be on the fifth
457 // rank, and both the destination square and the square between the
458 // source and destination squares must be empty.
459 if ( square_rank(to) != RANK_5
460 || !pos.square_is_empty(to)
461 || !pos.square_is_empty(from + DELTA_S))
469 else if (!bit_is_set(pos.attacks_from(pc, from), to))
472 // The move is pseudo-legal, check if it is also legal
473 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
479 template<SquareDelta Delta>
480 inline Bitboard move_pawns(Bitboard p) {
482 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
483 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
484 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
487 template<MoveType Type, SquareDelta Delta>
488 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
490 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
495 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
496 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
497 SERIALIZE_MOVES_D(b, -Delta);
501 template<Color Us, MoveType Type, SquareDelta Delta>
502 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
504 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
509 // Promotions and under-promotions, both captures and non-captures
510 b = move_pawns<Delta>(pawnsOn7) & target;
512 if (Delta != DELTA_N && Delta != DELTA_S)
517 to = pop_1st_bit(&b);
519 if (Type == CAPTURE || Type == EVASION)
520 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
522 if (Type == NON_CAPTURE || Type == EVASION)
524 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
525 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
526 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
529 // This is the only possible under promotion that can give a check
530 // not already included in the queen-promotion.
532 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
533 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
534 else (void)pos; // Silence a warning under MSVC
539 template<Color Us, MoveType Type>
540 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
542 // Calculate our parametrized parameters at compile time, named
543 // according to the point of view of white side.
544 const Color Them = (Us == WHITE ? BLACK : WHITE);
545 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
546 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
547 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
548 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
549 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
552 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
553 Bitboard pawns = pos.pieces(PAWN, Us);
554 Bitboard pawnsOn7 = pawns & TRank7BB;
555 Bitboard enemyPieces = (Type == CAPTURE ? target : pos.pieces_of_color(Them));
557 // Pre-calculate pawn pushes before changing emptySquares definition
560 emptySquares = (Type == NON_CAPTURE ? target : pos.empty_squares());
561 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
566 emptySquares &= target; // Only blocking squares
567 enemyPieces &= target; // Capture only the checker piece
570 // Promotions and underpromotions
574 emptySquares = pos.empty_squares();
577 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
578 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
579 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
583 if (Type == CAPTURE || Type == EVASION)
585 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
586 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
589 // Single and double pawn pushes
592 b1 = pawnPushes & emptySquares;
593 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
597 // Consider only pawn moves which give direct checks
598 b1 &= pos.attacks_from<PAWN>(ksq, Them);
599 b2 &= pos.attacks_from<PAWN>(ksq, Them);
601 // Add pawn moves which gives discovered check. This is possible only
602 // if the pawn is not on the same file as the enemy king, because we
603 // don't generate captures.
604 if (pawns & target) // For CHECK type target is dc bitboard
606 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
607 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
613 SERIALIZE_MOVES_D(b1, -TDELTA_N);
614 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
617 // En passant captures
618 if ((Type == CAPTURE || Type == EVASION) && pos.ep_square() != SQ_NONE)
620 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
621 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
623 // An en passant capture can be an evasion only if the checking piece
624 // is the double pushed pawn and so is in the target. Otherwise this
625 // is a discovery check and we are forced to do otherwise.
626 if (Type == EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
629 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
631 assert(b1 != EmptyBoardBB);
635 to = pop_1st_bit(&b1);
636 (*mlist++).move = make_ep_move(to, pos.ep_square());
642 template<CastlingSide Side>
643 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
645 Color us = pos.side_to_move();
647 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
648 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
650 Color them = opposite_color(us);
651 Square ksq = pos.king_square(us);
653 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
655 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
656 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
657 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
659 bool illegal = false;
661 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
663 // It is a bit complicated to correctly handle Chess960
664 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
665 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
666 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
669 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
670 if (s != ksq && s != rsq && pos.square_is_occupied(s))
673 if ( Side == QUEEN_SIDE
674 && square_file(rsq) == FILE_B
675 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
676 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
680 (*mlist++).move = make_castle_move(ksq, rsq);