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
48 template<CastlingSide Side>
49 MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
51 template<Color Us, MoveType Type>
52 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
54 template<PieceType Piece>
55 inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
57 assert(Piece != QUEEN);
59 Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
62 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
63 b &= ~QueenPseudoAttacks[ksq];
69 template<PieceType Piece>
70 inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
71 Bitboard dc, Square ksq) {
72 assert(Piece != KING);
76 const Square* ptr = pos.piece_list_begin(us, Piece);
78 if ((from = *ptr++) == SQ_NONE)
81 checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
85 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
86 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
87 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
90 if (dc && bit_is_set(dc, from))
93 b = pos.attacks_from<Piece>(from) & checkSqs;
96 } while ((from = *ptr++) != SQ_NONE);
102 inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
104 return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
105 : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
108 template<PieceType Piece, MoveType Type>
109 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
111 assert(Piece == PAWN);
112 assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
114 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
115 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
118 template<PieceType Piece>
119 inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
123 const Square* ptr = pos.piece_list_begin(us, Piece);
129 b = pos.attacks_from<Piece>(from) & target;
131 } while (*++ptr != SQ_NONE);
137 inline MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
140 Square from = pos.king_square(us);
142 b = pos.attacks_from<KING>(from) & target;
154 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
155 /// promotions. Returns a pointer to the end of the move list.
157 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
158 /// underpromotions. Returns a pointer to the end of the move list.
160 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
161 /// non-captures. Returns a pointer to the end of the move list.
164 MoveStack* generate(const Position& pos, MoveStack* mlist) {
167 assert(!pos.is_check());
169 Color us = pos.side_to_move();
172 if (T == MV_CAPTURE || T == MV_NON_EVASION)
173 target = pos.pieces_of_color(opposite_color(us));
174 else if (T == MV_NON_CAPTURE)
175 target = pos.empty_squares();
179 if (T == MV_NON_EVASION)
181 mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
182 mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
183 target |= pos.empty_squares();
186 mlist = generate_piece_moves<PAWN, T>(pos, mlist, us, target);
188 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
189 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
190 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
191 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
192 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
196 if (pos.can_castle_kingside(us))
197 mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
199 if (pos.can_castle_queenside(us))
200 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
206 // Explicit template instantiation
207 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
208 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
209 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
212 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
213 /// underpromotions that give check. Returns a pointer to the end of the move list.
215 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
218 assert(!pos.is_check());
222 Color us = pos.side_to_move();
223 Square ksq = pos.king_square(opposite_color(us));
225 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
227 // Discovered non-capture checks
228 b = dc = pos.discovered_check_candidates(us);
232 from = pop_1st_bit(&b);
233 switch (pos.type_of_piece_on(from))
235 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
236 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
237 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
238 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
239 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
240 default: assert(false); break;
244 // Direct non-capture checks
245 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
246 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
247 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
248 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
249 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
253 /// generate_evasions() generates all pseudo-legal check evasions when
254 /// the side to move is in check. Returns a pointer to the end of the move list.
256 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
259 assert(pos.is_check());
262 Square from, checksq;
264 Color us = pos.side_to_move();
265 Square ksq = pos.king_square(us);
266 Bitboard checkers = pos.checkers();
267 Bitboard sliderAttacks = EmptyBoardBB;
269 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
272 // Find squares attacked by slider checkers, we will remove
273 // them from the king evasions set so to early skip known
274 // illegal moves and avoid an useless legality check later.
279 checksq = pop_1st_bit(&b);
281 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
283 switch (pos.type_of_piece_on(checksq))
285 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
286 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
288 // In case of a queen remove also squares attacked in the other direction to
289 // avoid possible illegal moves when queen and king are on adjacent squares.
290 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
291 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
293 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
299 // Generate evasions for king, capture and non capture moves
300 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
304 // Generate evasions for other pieces only if not double check
308 // Find squares where a blocking evasion or a capture of the
309 // checker piece is possible.
310 target = squares_between(checksq, ksq) | checkers;
312 mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
313 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
314 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
315 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
316 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
320 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
321 /// or pseudo-legal moves in the current position.
323 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
327 return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
328 : generate<MV_NON_EVASION>(pos, mlist);
332 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
336 MoveStack *last, *cur = mlist;
337 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
339 last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
341 // Remove illegal moves from the list
343 if (pos.pl_move_is_legal(cur->move, pinned))
346 cur->move = (--last)->move;
352 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
353 /// move and tests whether the move is legal. This version is not very fast
354 /// and should be used only in non time-critical paths.
356 bool move_is_legal(const Position& pos, const Move m) {
358 MoveStack mlist[MOVES_MAX];
359 MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
361 for (cur = mlist; cur != last; cur++)
363 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
369 /// Fast version of move_is_legal() that takes a position a move and a
370 /// bitboard of pinned pieces as input, and tests whether the move is legal.
372 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
375 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
377 Color us = pos.side_to_move();
378 Color them = opposite_color(us);
379 Square from = move_from(m);
380 Square to = move_to(m);
381 Piece pc = pos.piece_on(from);
383 // Use a slower but simpler function for uncommon cases
384 if (move_is_special(m))
385 return move_is_legal(pos, m);
387 // If the from square is not occupied by a piece belonging to the side to
388 // move, the move is obviously not legal.
389 if (color_of_piece(pc) != us)
392 // The destination square cannot be occupied by a friendly piece
393 if (pos.color_of_piece_on(to) == us)
396 // Handle the special case of a pawn move
397 if (type_of_piece(pc) == PAWN)
399 // Move direction must be compatible with pawn color
400 int direction = to - from;
401 if ((us == WHITE) != (direction > 0))
404 // We have already handled promotion moves, so destination
405 // cannot be on the 8/1th rank.
406 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
409 // Proceed according to the square delta between the origin and
410 // destination squares.
417 // Capture. The destination square must be occupied by an enemy
418 // piece (en passant captures was handled earlier).
419 if (pos.color_of_piece_on(to) != them)
425 // Pawn push. The destination square must be empty.
426 if (!pos.square_is_empty(to))
431 // Double white pawn push. The destination square must be on the fourth
432 // rank, and both the destination square and the square between the
433 // source and destination squares must be empty.
434 if ( square_rank(to) != RANK_4
435 || !pos.square_is_empty(to)
436 || !pos.square_is_empty(from + DELTA_N))
441 // Double black pawn push. The destination square must be on the fifth
442 // rank, and both the destination square and the square between the
443 // source and destination squares must be empty.
444 if ( square_rank(to) != RANK_5
445 || !pos.square_is_empty(to)
446 || !pos.square_is_empty(from + DELTA_S))
454 else if (!bit_is_set(pos.attacks_from(pc, from), to))
457 // The move is pseudo-legal, check if it is also legal
458 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
464 template<SquareDelta Delta>
465 inline Bitboard move_pawns(Bitboard p) {
467 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
468 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
469 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
472 template<MoveType Type, SquareDelta Delta>
473 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
475 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
480 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
481 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
482 SERIALIZE_MOVES_D(b, -Delta);
486 template<Color Us, MoveType Type, SquareDelta Delta>
487 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
489 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
494 // Promotions and under-promotions, both captures and non-captures
495 b = move_pawns<Delta>(pawnsOn7) & target;
497 if (Delta != DELTA_N && Delta != DELTA_S)
502 to = pop_1st_bit(&b);
504 if (Type == MV_CAPTURE || Type == MV_EVASION)
505 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
507 if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
509 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
510 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
511 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
514 // This is the only possible under promotion that can give a check
515 // not already included in the queen-promotion.
516 if ( Type == MV_CHECK
517 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
518 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
519 else (void)pos; // Silence a warning under MSVC
524 template<Color Us, MoveType Type>
525 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
527 // Calculate our parametrized parameters at compile time, named
528 // according to the point of view of white side.
529 const Color Them = (Us == WHITE ? BLACK : WHITE);
530 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
531 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
532 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
533 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
534 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
537 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
538 Bitboard pawns = pos.pieces(PAWN, Us);
539 Bitboard pawnsOn7 = pawns & TRank7BB;
540 Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
542 // Pre-calculate pawn pushes before changing emptySquares definition
543 if (Type != MV_CAPTURE)
545 emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
546 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
549 if (Type == MV_EVASION)
551 emptySquares &= target; // Only blocking squares
552 enemyPieces &= target; // Capture only the checker piece
555 // Promotions and underpromotions
558 if (Type == MV_CAPTURE)
559 emptySquares = pos.empty_squares();
562 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
563 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
564 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
568 if (Type == MV_CAPTURE || Type == MV_EVASION)
570 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
571 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
574 // Single and double pawn pushes
575 if (Type != MV_CAPTURE)
577 b1 = pawnPushes & emptySquares;
578 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
580 if (Type == MV_CHECK)
582 // Consider only pawn moves which give direct checks
583 b1 &= pos.attacks_from<PAWN>(ksq, Them);
584 b2 &= pos.attacks_from<PAWN>(ksq, Them);
586 // Add pawn moves which gives discovered check. This is possible only
587 // if the pawn is not on the same file as the enemy king, because we
588 // don't generate captures.
589 if (pawns & target) // For CHECK type target is dc bitboard
591 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
592 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
598 SERIALIZE_MOVES_D(b1, -TDELTA_N);
599 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
602 // En passant captures
603 if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
605 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
606 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
608 // An en passant capture can be an evasion only if the checking piece
609 // is the double pushed pawn and so is in the target. Otherwise this
610 // is a discovery check and we are forced to do otherwise.
611 if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
614 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
616 assert(b1 != EmptyBoardBB);
620 to = pop_1st_bit(&b1);
621 (*mlist++).move = make_ep_move(to, pos.ep_square());
627 template<CastlingSide Side>
628 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
630 Color them = opposite_color(us);
631 Square ksq = pos.king_square(us);
633 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
635 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
636 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
637 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
639 bool illegal = false;
641 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
643 // It is a bit complicated to correctly handle Chess960
644 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
645 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
646 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
649 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
650 if (s != ksq && s != rsq && pos.square_is_occupied(s))
653 if ( Side == QUEEN_SIDE
654 && square_file(rsq) == FILE_B
655 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
656 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
660 (*mlist++).move = make_castle_move(ksq, rsq);