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/>.
25 // Simple macro to wrap a very common while loop, no facny, no flexibility,
26 // hardcoded list name 'mlist' and from square 'from'.
27 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
29 // Version used for pawns, where the 'from' square is given as a delta from the 'to' square
30 #define SERIALIZE_MOVES_D(b, d) while (b) { to = pop_1st_bit(&b); (*mlist++).move = make_move(to + (d), to); }
39 template<CastlingSide Side>
40 MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
42 template<Color Us, MoveType Type>
43 MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
45 template<PieceType Piece>
46 inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
48 assert(Piece != QUEEN);
50 Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
53 Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
54 b &= ~QueenPseudoAttacks[ksq];
60 template<PieceType Piece>
61 inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us,
62 Bitboard dc, Square ksq) {
63 assert(Piece != KING);
67 const Square* ptr = pos.piece_list_begin(us, Piece);
69 if ((from = *ptr++) == SQ_NONE)
72 checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
76 if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
77 || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
78 || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
81 if (dc && bit_is_set(dc, from))
84 b = pos.attacks_from<Piece>(from) & checkSqs;
87 } while ((from = *ptr++) != SQ_NONE);
93 FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
95 return (us == WHITE ? generate_pawn_moves<WHITE, MV_CHECK>(p, m, dc, ksq)
96 : generate_pawn_moves<BLACK, MV_CHECK>(p, m, dc, ksq));
99 template<PieceType Piece, MoveType Type>
100 FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
102 assert(Piece == PAWN);
103 assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
105 return (us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
106 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
109 template<PieceType Piece>
110 FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
114 const Square* ptr = pos.piece_list_begin(us, Piece);
120 b = pos.attacks_from<Piece>(from) & target;
122 } while (*++ptr != SQ_NONE);
128 FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
131 Square from = pos.king_square(us);
133 b = pos.attacks_from<KING>(from) & target;
145 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
146 /// promotions. Returns a pointer to the end of the move list.
148 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
149 /// underpromotions. Returns a pointer to the end of the move list.
151 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
152 /// non-captures. Returns a pointer to the end of the move list.
154 template<MoveType Type>
155 MoveStack* generate(const Position& pos, MoveStack* mlist) {
158 assert(!pos.is_check());
160 Color us = pos.side_to_move();
163 if (Type == MV_CAPTURE || Type == MV_NON_EVASION)
164 target = pos.pieces_of_color(opposite_color(us));
165 else if (Type == MV_NON_CAPTURE)
166 target = pos.empty_squares();
170 if (Type == MV_NON_EVASION)
172 mlist = generate_piece_moves<PAWN, MV_CAPTURE>(pos, mlist, us, target);
173 mlist = generate_piece_moves<PAWN, MV_NON_CAPTURE>(pos, mlist, us, pos.empty_squares());
174 target |= pos.empty_squares();
177 mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target);
179 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
180 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
181 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
182 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
183 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
185 if (Type != MV_CAPTURE)
187 if (pos.can_castle_kingside(us))
188 mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
190 if (pos.can_castle_queenside(us))
191 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
197 // Explicit template instantiation
198 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
199 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
200 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
203 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
204 /// underpromotions that give check. Returns a pointer to the end of the move list.
206 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
209 assert(!pos.is_check());
213 Color us = pos.side_to_move();
214 Square ksq = pos.king_square(opposite_color(us));
216 assert(pos.piece_on(ksq) == make_piece(opposite_color(us), KING));
218 // Discovered non-capture checks
219 b = dc = pos.discovered_check_candidates(us);
223 from = pop_1st_bit(&b);
224 switch (pos.type_of_piece_on(from))
226 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
227 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
228 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
229 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
230 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
231 default: assert(false); break;
235 // Direct non-capture checks
236 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
237 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
238 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
239 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
240 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
244 /// generate_evasions() generates all pseudo-legal check evasions when
245 /// the side to move is in check. Returns a pointer to the end of the move list.
247 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
250 assert(pos.is_check());
253 Square from, checksq;
255 Color us = pos.side_to_move();
256 Square ksq = pos.king_square(us);
257 Bitboard checkers = pos.checkers();
258 Bitboard sliderAttacks = EmptyBoardBB;
260 assert(pos.piece_on(ksq) == make_piece(us, KING));
263 // Find squares attacked by slider checkers, we will remove
264 // them from the king evasions set so to early skip known
265 // illegal moves and avoid an useless legality check later.
270 checksq = pop_1st_bit(&b);
272 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
274 switch (pos.type_of_piece_on(checksq))
276 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
277 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
279 // In case of a queen remove also squares attacked in the other direction to
280 // avoid possible illegal moves when queen and king are on adjacent squares.
281 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
282 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
284 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
290 // Generate evasions for king, capture and non capture moves
291 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
295 // Generate evasions for other pieces only if not double check
299 // Find squares where a blocking evasion or a capture of the
300 // checker piece is possible.
301 target = squares_between(checksq, ksq) | checkers;
303 mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
304 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
305 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
306 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
307 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
311 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
312 /// or pseudo-legal moves in the current position.
314 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
318 return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
319 : generate<MV_NON_EVASION>(pos, mlist);
323 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
327 MoveStack *last, *cur = mlist;
328 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
330 last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
332 // Remove illegal moves from the list
334 if (pos.pl_move_is_legal(cur->move, pinned))
337 cur->move = (--last)->move;
343 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
344 /// move and tests whether the move is legal. This version is not very fast
345 /// and should be used only in non time-critical paths.
347 bool move_is_legal(const Position& pos, const Move m) {
349 MoveStack mlist[MOVES_MAX];
350 MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
352 for (cur = mlist; cur != last; cur++)
354 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
360 /// Fast version of move_is_legal() that takes a position a move and a
361 /// bitboard of pinned pieces as input, and tests whether the move is legal.
363 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
366 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
368 Color us = pos.side_to_move();
369 Color them = opposite_color(us);
370 Square from = move_from(m);
371 Square to = move_to(m);
372 Piece pc = pos.piece_on(from);
374 // Use a slower but simpler function for uncommon cases
375 if (move_is_special(m))
376 return move_is_legal(pos, m);
378 // If the from square is not occupied by a piece belonging to the side to
379 // move, the move is obviously not legal.
380 if (color_of_piece(pc) != us)
383 // The destination square cannot be occupied by a friendly piece
384 if (pos.color_of_piece_on(to) == us)
387 // Handle the special case of a pawn move
388 if (type_of_piece(pc) == PAWN)
390 // Move direction must be compatible with pawn color
391 int direction = to - from;
392 if ((us == WHITE) != (direction > 0))
395 // We have already handled promotion moves, so destination
396 // cannot be on the 8/1th rank.
397 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
400 // Proceed according to the square delta between the origin and
401 // destination squares.
408 // Capture. The destination square must be occupied by an enemy
409 // piece (en passant captures was handled earlier).
410 if (pos.color_of_piece_on(to) != them)
416 // Pawn push. The destination square must be empty.
417 if (!pos.square_is_empty(to))
422 // Double white pawn push. The destination square must be on the fourth
423 // rank, and both the destination square and the square between the
424 // source and destination squares must be empty.
425 if ( square_rank(to) != RANK_4
426 || !pos.square_is_empty(to)
427 || !pos.square_is_empty(from + DELTA_N))
432 // Double black pawn push. The destination square must be on the fifth
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_5
436 || !pos.square_is_empty(to)
437 || !pos.square_is_empty(from + DELTA_S))
445 else if (!bit_is_set(pos.attacks_from(pc, from), to))
448 // The move is pseudo-legal, check if it is also legal
449 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
455 template<Square Delta>
456 inline Bitboard move_pawns(Bitboard p) {
458 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
459 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
460 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
463 template<MoveType Type, Square Delta>
464 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
466 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
471 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
472 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
473 SERIALIZE_MOVES_D(b, -Delta);
477 template<Color Us, MoveType Type, Square Delta>
478 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
480 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
485 // Promotions and under-promotions, both captures and non-captures
486 b = move_pawns<Delta>(pawnsOn7) & target;
488 if (Delta != DELTA_N && Delta != DELTA_S)
493 to = pop_1st_bit(&b);
495 if (Type == MV_CAPTURE || Type == MV_EVASION)
496 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
498 if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
500 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
501 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
502 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
505 // This is the only possible under promotion that can give a check
506 // not already included in the queen-promotion.
507 if ( Type == MV_CHECK
508 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
509 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
510 else (void)pos; // Silence a warning under MSVC
515 template<Color Us, MoveType Type>
516 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
518 // Calculate our parametrized parameters at compile time, named
519 // according to the point of view of white side.
520 const Color Them = (Us == WHITE ? BLACK : WHITE);
521 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
522 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
523 const Square TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
524 const Square TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
525 const Square TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
528 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
529 Bitboard pawns = pos.pieces(PAWN, Us);
530 Bitboard pawnsOn7 = pawns & TRank7BB;
531 Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
533 // Pre-calculate pawn pushes before changing emptySquares definition
534 if (Type != MV_CAPTURE)
536 emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
537 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
540 if (Type == MV_EVASION)
542 emptySquares &= target; // Only blocking squares
543 enemyPieces &= target; // Capture only the checker piece
546 // Promotions and underpromotions
549 if (Type == MV_CAPTURE)
550 emptySquares = pos.empty_squares();
553 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
554 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
555 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
559 if (Type == MV_CAPTURE || Type == MV_EVASION)
561 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
562 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
565 // Single and double pawn pushes
566 if (Type != MV_CAPTURE)
568 b1 = pawnPushes & emptySquares;
569 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
571 if (Type == MV_CHECK)
573 // Consider only pawn moves which give direct checks
574 b1 &= pos.attacks_from<PAWN>(ksq, Them);
575 b2 &= pos.attacks_from<PAWN>(ksq, Them);
577 // Add pawn moves which gives discovered check. This is possible only
578 // if the pawn is not on the same file as the enemy king, because we
579 // don't generate captures.
580 if (pawns & target) // For CHECK type target is dc bitboard
582 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
583 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
589 SERIALIZE_MOVES_D(b1, -TDELTA_N);
590 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
593 // En passant captures
594 if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
596 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
597 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
599 // An en passant capture can be an evasion only if the checking piece
600 // is the double pushed pawn and so is in the target. Otherwise this
601 // is a discovery check and we are forced to do otherwise.
602 if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
605 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
607 assert(b1 != EmptyBoardBB);
611 to = pop_1st_bit(&b1);
612 (*mlist++).move = make_ep_move(to, pos.ep_square());
618 template<CastlingSide Side>
619 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
621 Color them = opposite_color(us);
622 Square ksq = pos.king_square(us);
624 assert(pos.piece_on(ksq) == make_piece(us, KING));
626 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
627 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
628 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
630 bool illegal = false;
632 assert(pos.piece_on(rsq) == make_piece(us, ROOK));
634 // It is a bit complicated to correctly handle Chess960
635 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
636 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
637 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
640 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
641 if (s != ksq && s != rsq && pos.square_is_occupied(s))
644 if ( Side == QUEEN_SIDE
645 && square_file(rsq) == FILE_B
646 && ( pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, ROOK)
647 || pos.piece_on(relative_square(us, SQ_A1)) == make_piece(them, QUEEN)))
651 (*mlist++).move = make_castle_move(ksq, rsq);