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(pinned == pos.pinned_pieces(pos.side_to_move()));
391 Color us = pos.side_to_move();
392 Color them = opposite_color(us);
393 Square from = move_from(m);
394 Square to = move_to(m);
395 Piece pc = pos.piece_on(from);
397 // Use a slower but simpler function for uncommon cases
398 if (move_is_special(m))
399 return move_is_legal(pos, m);
401 // If the from square is not occupied by a piece belonging to the side to
402 // move, the move is obviously not legal.
403 if (color_of_piece(pc) != us)
406 // The destination square cannot be occupied by a friendly piece
407 if (pos.color_of_piece_on(to) == us)
410 // Handle the special case of a pawn move
411 if (type_of_piece(pc) == PAWN)
413 // Move direction must be compatible with pawn color
414 int direction = to - from;
415 if ((us == WHITE) != (direction > 0))
418 // We have already handled promotion moves, so destination
419 // cannot be on the 8/1th rank.
420 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
423 // Proceed according to the square delta between the origin and
424 // destination squares.
431 // Capture. The destination square must be occupied by an enemy
432 // piece (en passant captures was handled earlier).
433 if (pos.color_of_piece_on(to) != them)
439 // Pawn push. The destination square must be empty.
440 if (!pos.square_is_empty(to))
445 // Double white pawn push. The destination square must be on the fourth
446 // rank, and both the destination square and the square between the
447 // source and destination squares must be empty.
448 if ( square_rank(to) != RANK_4
449 || !pos.square_is_empty(to)
450 || !pos.square_is_empty(from + DELTA_N))
455 // Double black pawn push. The destination square must be on the fifth
456 // rank, and both the destination square and the square between the
457 // source and destination squares must be empty.
458 if ( square_rank(to) != RANK_5
459 || !pos.square_is_empty(to)
460 || !pos.square_is_empty(from + DELTA_S))
468 else if (!bit_is_set(pos.attacks_from(pc, from), to))
471 // The move is pseudo-legal, check if it is also legal
472 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
478 template<SquareDelta Delta>
479 inline Bitboard move_pawns(Bitboard p) {
481 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
482 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
483 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
486 template<MoveType Type, SquareDelta Delta>
487 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
489 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
494 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
495 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
496 SERIALIZE_MOVES_D(b, -Delta);
500 template<Color Us, MoveType Type, SquareDelta Delta>
501 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
503 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
508 // Promotions and under-promotions, both captures and non-captures
509 b = move_pawns<Delta>(pawnsOn7) & target;
511 if (Delta != DELTA_N && Delta != DELTA_S)
516 to = pop_1st_bit(&b);
518 if (Type == CAPTURE || Type == EVASION)
519 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
521 if (Type == NON_CAPTURE || Type == EVASION)
523 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
524 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
525 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
528 // This is the only possible under promotion that can give a check
529 // not already included in the queen-promotion.
531 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
532 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
533 else (void)pos; // Silence a warning under MSVC
538 template<Color Us, MoveType Type>
539 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
541 // Calculate our parametrized parameters at compile time, named
542 // according to the point of view of white side.
543 const Color Them = (Us == WHITE ? BLACK : WHITE);
544 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
545 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
546 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
547 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
548 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
551 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
552 Bitboard pawns = pos.pieces(PAWN, Us);
553 Bitboard pawnsOn7 = pawns & TRank7BB;
554 Bitboard enemyPieces = (Type == CAPTURE ? target : pos.pieces_of_color(Them));
556 // Pre-calculate pawn pushes before changing emptySquares definition
559 emptySquares = (Type == NON_CAPTURE ? target : pos.empty_squares());
560 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
565 emptySquares &= target; // Only blocking squares
566 enemyPieces &= target; // Capture only the checker piece
569 // Promotions and underpromotions
573 emptySquares = pos.empty_squares();
576 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
577 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
578 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
582 if (Type == CAPTURE || Type == EVASION)
584 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
585 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
588 // Single and double pawn pushes
591 b1 = pawnPushes & emptySquares;
592 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
596 // Consider only pawn moves which give direct checks
597 b1 &= pos.attacks_from<PAWN>(ksq, Them);
598 b2 &= pos.attacks_from<PAWN>(ksq, Them);
600 // Add pawn moves which gives discovered check. This is possible only
601 // if the pawn is not on the same file as the enemy king, because we
602 // don't generate captures.
603 if (pawns & target) // For CHECK type target is dc bitboard
605 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
606 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
612 SERIALIZE_MOVES_D(b1, -TDELTA_N);
613 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
616 // En passant captures
617 if ((Type == CAPTURE || Type == EVASION) && pos.ep_square() != SQ_NONE)
619 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
620 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
622 // An en passant capture can be an evasion only if the checking piece
623 // is the double pushed pawn and so is in the target. Otherwise this
624 // is a discovery check and we are forced to do otherwise.
625 if (Type == EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
628 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
630 assert(b1 != EmptyBoardBB);
634 to = pop_1st_bit(&b1);
635 (*mlist++).move = make_ep_move(to, pos.ep_square());
641 template<CastlingSide Side>
642 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
644 Color us = pos.side_to_move();
646 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
647 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
649 Color them = opposite_color(us);
650 Square ksq = pos.king_square(us);
652 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
654 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
655 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
656 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
658 bool illegal = false;
660 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
662 // It is a bit complicated to correctly handle Chess960
663 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
664 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
665 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
668 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
669 if (s != ksq && s != rsq && pos.square_is_occupied(s))
672 if ( Side == QUEEN_SIDE
673 && square_file(rsq) == FILE_B
674 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
675 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
679 (*mlist++).move = make_castle_move(ksq, rsq);