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*);
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 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
197 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
203 // Explicit template instantiation
204 template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
205 template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist);
206 template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
209 /// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
210 /// underpromotions that give check. Returns a pointer to the end of the move list.
212 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
215 assert(!pos.is_check());
219 Color us = pos.side_to_move();
220 Square ksq = pos.king_square(opposite_color(us));
222 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
224 // Discovered non-capture checks
225 b = dc = pos.discovered_check_candidates(us);
229 from = pop_1st_bit(&b);
230 switch (pos.type_of_piece_on(from))
232 case PAWN: /* Will be generated togheter with pawns direct checks */ break;
233 case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
234 case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
235 case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
236 case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
237 default: assert(false); break;
241 // Direct non-capture checks
242 mlist = generate_direct_checks<PAWN>(pos, mlist, us, dc, ksq);
243 mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, dc, ksq);
244 mlist = generate_direct_checks<BISHOP>(pos, mlist, us, dc, ksq);
245 mlist = generate_direct_checks<ROOK>(pos, mlist, us, dc, ksq);
246 return generate_direct_checks<QUEEN>(pos, mlist, us, dc, ksq);
250 /// generate_evasions() generates all pseudo-legal check evasions when
251 /// the side to move is in check. Returns a pointer to the end of the move list.
253 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
256 assert(pos.is_check());
259 Square from, checksq;
261 Color us = pos.side_to_move();
262 Square ksq = pos.king_square(us);
263 Bitboard checkers = pos.checkers();
264 Bitboard sliderAttacks = EmptyBoardBB;
266 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
269 // Find squares attacked by slider checkers, we will remove
270 // them from the king evasions set so to early skip known
271 // illegal moves and avoid an useless legality check later.
276 checksq = pop_1st_bit(&b);
278 assert(pos.color_of_piece_on(checksq) == opposite_color(us));
280 switch (pos.type_of_piece_on(checksq))
282 case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
283 case ROOK: sliderAttacks |= RookPseudoAttacks[checksq]; break;
285 // In case of a queen remove also squares attacked in the other direction to
286 // avoid possible illegal moves when queen and king are on adjacent squares.
287 if (RookPseudoAttacks[checksq] & (1ULL << ksq))
288 sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
290 sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
296 // Generate evasions for king, capture and non capture moves
297 b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
301 // Generate evasions for other pieces only if not double check
305 // Find squares where a blocking evasion or a capture of the
306 // checker piece is possible.
307 target = squares_between(checksq, ksq) | checkers;
309 mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target);
310 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
311 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
312 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
313 return generate_piece_moves<QUEEN>(pos, mlist, us, target);
317 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
318 /// or pseudo-legal moves in the current position.
320 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
324 return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
325 : generate<MV_NON_EVASION>(pos, mlist);
329 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
333 MoveStack *last, *cur = mlist;
334 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
336 last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
338 // Remove illegal moves from the list
340 if (pos.pl_move_is_legal(cur->move, pinned))
343 cur->move = (--last)->move;
349 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
350 /// move and tests whether the move is legal. This version is not very fast
351 /// and should be used only in non time-critical paths.
353 bool move_is_legal(const Position& pos, const Move m) {
355 MoveStack mlist[MOVES_MAX];
356 MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
358 for (cur = mlist; cur != last; cur++)
360 return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
366 /// Fast version of move_is_legal() that takes a position a move and a
367 /// bitboard of pinned pieces as input, and tests whether the move is legal.
369 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
372 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
374 Color us = pos.side_to_move();
375 Color them = opposite_color(us);
376 Square from = move_from(m);
377 Square to = move_to(m);
378 Piece pc = pos.piece_on(from);
380 // Use a slower but simpler function for uncommon cases
381 if (move_is_special(m))
382 return move_is_legal(pos, m);
384 // If the from square is not occupied by a piece belonging to the side to
385 // move, the move is obviously not legal.
386 if (color_of_piece(pc) != us)
389 // The destination square cannot be occupied by a friendly piece
390 if (pos.color_of_piece_on(to) == us)
393 // Handle the special case of a pawn move
394 if (type_of_piece(pc) == PAWN)
396 // Move direction must be compatible with pawn color
397 int direction = to - from;
398 if ((us == WHITE) != (direction > 0))
401 // We have already handled promotion moves, so destination
402 // cannot be on the 8/1th rank.
403 if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
406 // Proceed according to the square delta between the origin and
407 // destination squares.
414 // Capture. The destination square must be occupied by an enemy
415 // piece (en passant captures was handled earlier).
416 if (pos.color_of_piece_on(to) != them)
422 // Pawn push. The destination square must be empty.
423 if (!pos.square_is_empty(to))
428 // Double white pawn push. The destination square must be on the fourth
429 // rank, and both the destination square and the square between the
430 // source and destination squares must be empty.
431 if ( square_rank(to) != RANK_4
432 || !pos.square_is_empty(to)
433 || !pos.square_is_empty(from + DELTA_N))
438 // Double black pawn push. The destination square must be on the fifth
439 // rank, and both the destination square and the square between the
440 // source and destination squares must be empty.
441 if ( square_rank(to) != RANK_5
442 || !pos.square_is_empty(to)
443 || !pos.square_is_empty(from + DELTA_S))
451 else if (!bit_is_set(pos.attacks_from(pc, from), to))
454 // The move is pseudo-legal, check if it is also legal
455 return pos.is_check() ? pos.pl_move_is_evasion(m, pinned) : pos.pl_move_is_legal(m, pinned);
461 template<SquareDelta Delta>
462 inline Bitboard move_pawns(Bitboard p) {
464 return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 :
465 Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 :
466 Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p;
469 template<MoveType Type, SquareDelta Delta>
470 inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
472 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
477 // Captures in the a1-h8 (a8-h1 for black) diagonal or in the h1-a8 (h8-a1 for black)
478 b = move_pawns<Delta>(pawns) & target & ~TFileABB;
479 SERIALIZE_MOVES_D(b, -Delta);
483 template<Color Us, MoveType Type, SquareDelta Delta>
484 inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
486 const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
491 // Promotions and under-promotions, both captures and non-captures
492 b = move_pawns<Delta>(pawnsOn7) & target;
494 if (Delta != DELTA_N && Delta != DELTA_S)
499 to = pop_1st_bit(&b);
501 if (Type == MV_CAPTURE || Type == MV_EVASION)
502 (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
504 if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
506 (*mlist++).move = make_promotion_move(to - Delta, to, ROOK);
507 (*mlist++).move = make_promotion_move(to - Delta, to, BISHOP);
508 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
511 // This is the only possible under promotion that can give a check
512 // not already included in the queen-promotion.
513 if ( Type == MV_CHECK
514 && bit_is_set(pos.attacks_from<KNIGHT>(to), pos.king_square(opposite_color(Us))))
515 (*mlist++).move = make_promotion_move(to - Delta, to, KNIGHT);
516 else (void)pos; // Silence a warning under MSVC
521 template<Color Us, MoveType Type>
522 MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
524 // Calculate our parametrized parameters at compile time, named
525 // according to the point of view of white side.
526 const Color Them = (Us == WHITE ? BLACK : WHITE);
527 const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
528 const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
529 const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
530 const SquareDelta TDELTA_NE = (Us == WHITE ? DELTA_NE : DELTA_SE);
531 const SquareDelta TDELTA_NW = (Us == WHITE ? DELTA_NW : DELTA_SW);
534 Bitboard b1, b2, dc1, dc2, pawnPushes, emptySquares;
535 Bitboard pawns = pos.pieces(PAWN, Us);
536 Bitboard pawnsOn7 = pawns & TRank7BB;
537 Bitboard enemyPieces = (Type == MV_CAPTURE ? target : pos.pieces_of_color(Them));
539 // Pre-calculate pawn pushes before changing emptySquares definition
540 if (Type != MV_CAPTURE)
542 emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
543 pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
546 if (Type == MV_EVASION)
548 emptySquares &= target; // Only blocking squares
549 enemyPieces &= target; // Capture only the checker piece
552 // Promotions and underpromotions
555 if (Type == MV_CAPTURE)
556 emptySquares = pos.empty_squares();
559 mlist = generate_promotions<Us, Type, TDELTA_NE>(pos, mlist, pawnsOn7, enemyPieces);
560 mlist = generate_promotions<Us, Type, TDELTA_NW>(pos, mlist, pawnsOn7, enemyPieces);
561 mlist = generate_promotions<Us, Type, TDELTA_N >(pos, mlist, pawnsOn7, emptySquares);
565 if (Type == MV_CAPTURE || Type == MV_EVASION)
567 mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
568 mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
571 // Single and double pawn pushes
572 if (Type != MV_CAPTURE)
574 b1 = pawnPushes & emptySquares;
575 b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
577 if (Type == MV_CHECK)
579 // Consider only pawn moves which give direct checks
580 b1 &= pos.attacks_from<PAWN>(ksq, Them);
581 b2 &= pos.attacks_from<PAWN>(ksq, Them);
583 // Add pawn moves which gives discovered check. This is possible only
584 // if the pawn is not on the same file as the enemy king, because we
585 // don't generate captures.
586 if (pawns & target) // For CHECK type target is dc bitboard
588 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
589 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
595 SERIALIZE_MOVES_D(b1, -TDELTA_N);
596 SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
599 // En passant captures
600 if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
602 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
603 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
605 // An en passant capture can be an evasion only if the checking piece
606 // is the double pushed pawn and so is in the target. Otherwise this
607 // is a discovery check and we are forced to do otherwise.
608 if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - TDELTA_N))
611 b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
613 assert(b1 != EmptyBoardBB);
617 to = pop_1st_bit(&b1);
618 (*mlist++).move = make_ep_move(to, pos.ep_square());
624 template<CastlingSide Side>
625 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
627 Color us = pos.side_to_move();
629 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
630 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
632 Color them = opposite_color(us);
633 Square ksq = pos.king_square(us);
635 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
637 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
638 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
639 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
641 bool illegal = false;
643 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
645 // It is a bit complicated to correctly handle Chess960
646 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
647 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
648 ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
651 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
652 if (s != ksq && s != rsq && pos.square_is_occupied(s))
655 if ( Side == QUEEN_SIDE
656 && square_file(rsq) == FILE_B
657 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
658 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
662 (*mlist++).move = make_castle_move(ksq, rsq);