2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008 Marco Costalba
5 Stockfish is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Stockfish is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 // Simple macro to wrap a very common while loop, no facny, no flexibility,
29 // hardcoded list name 'mlist' and from square 'from'.
30 #define SERIALIZE_MOVES(b) while (b) (*mlist++).move = make_move(from, pop_1st_bit(&b))
33 //// Local definitions
49 bool castling_is_check(const Position&, CastlingSide);
52 template<CastlingSide Side>
53 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
55 template<Color Us, Rank, Bitboard, SquareDelta>
56 MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
58 template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
59 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
61 template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
62 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
64 template<Color, Color, Bitboard, Bitboard, SquareDelta>
65 MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
67 template<Color Us, SquareDelta Direction>
68 inline Bitboard move_pawns(Bitboard p) {
70 if (Direction == DELTA_N)
71 return Us == WHITE ? p << 8 : p >> 8;
72 else if (Direction == DELTA_NE)
73 return Us == WHITE ? p << 9 : p >> 7;
74 else if (Direction == DELTA_NW)
75 return Us == WHITE ? p << 7 : p >> 9;
81 // Template generate_piece_checks() with specializations
83 MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
86 inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
89 return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
91 return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
95 // Template generate_piece_moves() with specializations and overloads
97 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
100 MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
102 template<PieceType Piece, MoveType Type>
103 inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
105 assert(Piece == PAWN);
108 return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
109 : generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
111 return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
112 : generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
116 MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
119 inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
120 Color us, Bitboard t, Bitboard pnd) {
122 return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, t, m);
124 return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, t, m);
134 /// generate_captures generates() all pseudo-legal captures and queen
135 /// promotions. The return value is the number of moves generated.
137 int generate_captures(const Position& pos, MoveStack* mlist) {
140 assert(!pos.is_check());
142 Color us = pos.side_to_move();
143 Bitboard target = pos.pieces_of_color(opposite_color(us));
144 MoveStack* mlist_start = mlist;
146 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
147 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
148 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
149 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
150 mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
151 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
152 return int(mlist - mlist_start);
156 /// generate_noncaptures() generates all pseudo-legal non-captures and
157 /// underpromotions. The return value is the number of moves generated.
159 int generate_noncaptures(const Position& pos, MoveStack* mlist) {
162 assert(!pos.is_check());
164 Color us = pos.side_to_move();
165 Bitboard target = pos.empty_squares();
166 MoveStack* mlist_start = mlist;
168 mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
169 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
170 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
171 mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
172 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
173 mlist = generate_piece_moves<KING>(pos, mlist, us, target);
174 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
175 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
176 return int(mlist - mlist_start);
180 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
181 /// checks. It returns the number of generated moves.
183 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
186 assert(!pos.is_check());
188 Color us = pos.side_to_move();
189 Square ksq = pos.king_square(opposite_color(us));
190 MoveStack* mlist_start = mlist;
192 assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
195 mlist = generate_piece_checks<PAWN>(pos, mlist, us, dc, ksq);
196 mlist = generate_piece_checks<KNIGHT>(pos, mlist, us, dc, ksq);
197 mlist = generate_piece_checks<BISHOP>(pos, mlist, us, dc, ksq);
198 mlist = generate_piece_checks<ROOK>(pos, mlist, us, dc, ksq);
199 mlist = generate_piece_checks<QUEEN>(pos, mlist, us, dc, ksq);
200 mlist = generate_piece_checks<KING>(pos, mlist, us, dc, ksq);
202 // Castling moves that give check. Very rare but nice to have!
203 if ( pos.can_castle_queenside(us)
204 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_D)
205 && castling_is_check(pos, QUEEN_SIDE))
206 mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
208 if ( pos.can_castle_kingside(us)
209 && (square_rank(ksq) == square_rank(pos.king_square(us)) || square_file(ksq) == FILE_F)
210 && castling_is_check(pos, KING_SIDE))
211 mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
213 return int(mlist - mlist_start);
217 /// generate_evasions() generates all check evasions when the side to move is
218 /// in check. Unlike the other move generation functions, this one generates
219 /// only legal moves. It returns the number of generated moves.
221 int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
224 assert(pos.is_check());
227 Color us = pos.side_to_move();
228 Color them = opposite_color(us);
229 Square ksq = pos.king_square(us);
230 MoveStack* mlist_start = mlist;
232 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
234 // The bitboard of occupied pieces without our king
235 Bitboard b_noKing = pos.occupied_squares();
236 clear_bit(&b_noKing, ksq);
238 // Find squares attacked by slider checkers, we will
239 // remove them from king evasions set so to avoid a couple
240 // of cycles in the slow king evasions legality check loop
241 // and to be able to use square_is_attacked().
242 Bitboard checkers = pos.checkers();
243 Bitboard checkersAttacks = EmptyBoardBB;
244 Bitboard b = checkers & (pos.queens() | pos.bishops());
247 from = pop_1st_bit(&b);
248 checkersAttacks |= bishop_attacks_bb(from, b_noKing);
251 b = checkers & (pos.queens() | pos.rooks());
254 from = pop_1st_bit(&b);
255 checkersAttacks |= rook_attacks_bb(from, b_noKing);
258 // Generate evasions for king
259 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
262 to = pop_1st_bit(&b1);
263 // Note that we can use square_is_attacked() only because we
264 // have already removed slider checkers.
265 if (!pos.square_is_attacked(to, them))
266 (*mlist++).move = make_move(ksq, to);
269 // Generate evasions for other pieces only if not double check. We use a
270 // simple bit twiddling hack here rather than calling count_1s in order to
271 // save some time (we know that pos.checkers() has at most two nonzero bits).
272 if (!(checkers & (checkers - 1))) // Only one bit set?
274 Square checksq = first_1(checkers);
276 assert(pos.color_of_piece_on(checksq) == them);
278 // Generate captures of the checking piece
281 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & ~pinned;
284 from = pop_1st_bit(&b1);
285 if (relative_rank(us, checksq) == RANK_8)
287 (*mlist++).move = make_promotion_move(from, checksq, QUEEN);
288 (*mlist++).move = make_promotion_move(from, checksq, ROOK);
289 (*mlist++).move = make_promotion_move(from, checksq, BISHOP);
290 (*mlist++).move = make_promotion_move(from, checksq, KNIGHT);
292 (*mlist++).move = make_move(from, checksq);
296 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
297 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
298 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & ~pinned;
302 from = pop_1st_bit(&b1);
303 (*mlist++).move = make_move(from, checksq);
306 // Blocking check evasions are possible only if the checking piece is
308 if (checkers & pos.sliders())
310 Bitboard blockSquares = squares_between(checksq, ksq);
312 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
314 if (blockSquares != EmptyBoardBB)
316 mlist = generate_piece_moves<PAWN>(pos, mlist, us, blockSquares, pinned);
317 mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, blockSquares, pinned);
318 mlist = generate_piece_moves<BISHOP>(pos, mlist, us, blockSquares, pinned);
319 mlist = generate_piece_moves<ROOK>(pos, mlist, us, blockSquares, pinned);
320 mlist = generate_piece_moves<QUEEN>(pos, mlist, us, blockSquares, pinned);
324 // Finally, the special case of en passant captures. An en passant
325 // capture can only be a check evasion if the check is not a discovered
326 // check. If pos.ep_square() is set, the last move made must have been
327 // a double pawn push. If, furthermore, the checking piece is a pawn,
328 // an en passant check evasion may be possible.
329 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
331 to = pos.ep_square();
332 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
334 // The checking pawn cannot be a discovered (bishop) check candidate
335 // otherwise we were in check also before last double push move.
336 assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
337 assert(count_1s(b1) == 1 || count_1s(b1) == 2);
342 from = pop_1st_bit(&b1);
343 // Move is always legal because checking pawn is not a discovered
344 // check candidate and our capturing pawn has been already tested
345 // against pinned pieces.
346 (*mlist++).move = make_ep_move(from, to);
350 return int(mlist - mlist_start);
354 /// generate_legal_moves() computes a complete list of legal moves in the
355 /// current position. This function is not very fast, and should be used
356 /// only in situations where performance is unimportant. It wouldn't be
357 /// very hard to write an efficient legal move generator, but for the moment
358 /// we don't need it.
360 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
364 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
367 return generate_evasions(pos, mlist, pinned);
369 // Generate pseudo-legal moves
370 int n = generate_captures(pos, mlist);
371 n += generate_noncaptures(pos, mlist + n);
373 // Remove illegal moves from the list
374 for (int i = 0; i < n; i++)
375 if (!pos.pl_move_is_legal(mlist[i].move, pinned))
376 mlist[i--].move = mlist[--n].move;
382 /// move_is_legal() takes a position and a (not necessarily pseudo-legal)
383 /// move and a pinned pieces bitboard as input, and tests whether
384 /// the move is legal. If the move is legal, the move itself is
385 /// returned. If not, the function returns false. This function must
386 /// only be used when the side to move is not in check.
388 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
391 assert(!pos.is_check());
392 assert(move_is_ok(m));
393 assert(pinned == pos.pinned_pieces(pos.side_to_move()));
395 Color us = pos.side_to_move();
396 Color them = opposite_color(us);
397 Square from = move_from(m);
398 Piece pc = pos.piece_on(from);
400 // If the from square is not occupied by a piece belonging to the side to
401 // move, the move is obviously not legal.
402 if (color_of_piece(pc) != us)
405 Square to = move_to(m);
410 // The piece must be a pawn and destination square must be the
411 // en passant square.
412 if ( type_of_piece(pc) != PAWN
413 || to != pos.ep_square())
416 assert(pos.square_is_empty(to));
417 assert(pos.piece_on(to - pawn_push(us)) == piece_of_color_and_type(them, PAWN));
419 // The move is pseudo-legal, check if it is also legal
420 return pos.pl_move_is_legal(m, pinned);
424 if (move_is_short_castle(m))
426 // The piece must be a king and side to move must still have
427 // the right to castle kingside.
428 if ( type_of_piece(pc) != KING
429 ||!pos.can_castle_kingside(us))
432 assert(from == pos.king_square(us));
433 assert(to == pos.initial_kr_square(us));
434 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
436 Square g1 = relative_square(us, SQ_G1);
437 Square f1 = relative_square(us, SQ_F1);
439 bool illegal = false;
441 // Check if any of the squares between king and rook
442 // is occupied or under attack.
443 for (s = Min(from, g1); s <= Max(from, g1); s++)
444 if ( (s != from && s != to && !pos.square_is_empty(s))
445 || pos.square_is_attacked(s, them))
448 // Check if any of the squares between king and rook
450 for (s = Min(to, f1); s <= Max(to, f1); s++)
451 if (s != from && s != to && !pos.square_is_empty(s))
457 if (move_is_long_castle(m))
459 // The piece must be a king and side to move must still have
460 // the right to castle kingside.
461 if ( type_of_piece(pc) != KING
462 ||!pos.can_castle_queenside(us))
465 assert(from == pos.king_square(us));
466 assert(to == pos.initial_qr_square(us));
467 assert(pos.piece_on(to) == piece_of_color_and_type(us, ROOK));
469 Square c1 = relative_square(us, SQ_C1);
470 Square d1 = relative_square(us, SQ_D1);
472 bool illegal = false;
474 for (s = Min(from, c1); s <= Max(from, c1); s++)
475 if( (s != from && s != to && !pos.square_is_empty(s))
476 || pos.square_is_attacked(s, them))
479 for (s = Min(to, d1); s <= Max(to, d1); s++)
480 if(s != from && s != to && !pos.square_is_empty(s))
483 if ( square_file(to) == FILE_B
484 && ( pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, ROOK)
485 || pos.piece_on(to + DELTA_W) == piece_of_color_and_type(them, QUEEN)))
493 // The destination square cannot be occupied by a friendly piece
494 if (pos.color_of_piece_on(to) == us)
497 // Proceed according to the type of the moving piece.
498 if (type_of_piece(pc) == PAWN)
500 // If the destination square is on the 8/1th rank, the move must
502 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
503 ||(square_rank(to) == RANK_1 && us != WHITE))
504 && !move_promotion(m))
507 // Proceed according to the square delta between the source and
508 // destionation squares.
515 // Capture. The destination square must be occupied by an enemy
516 // piece (en passant captures was handled earlier).
517 if (pos.color_of_piece_on(to) != them)
523 // Pawn push. The destination square must be empty.
524 if (!pos.square_is_empty(to))
529 // Double white pawn push. The destination square must be on the fourth
530 // rank, and both the destination square and the square between the
531 // source and destination squares must be empty.
532 if ( square_rank(to) != RANK_4
533 || !pos.square_is_empty(to)
534 || !pos.square_is_empty(from + DELTA_N))
539 // Double black pawn push. The destination square must be on the fifth
540 // rank, and both the destination square and the square between the
541 // source and destination squares must be empty.
542 if ( square_rank(to) != RANK_5
543 || !pos.square_is_empty(to)
544 || !pos.square_is_empty(from + DELTA_S))
551 // The move is pseudo-legal, check if it is also legal
552 return pos.pl_move_is_legal(m, pinned);
555 // Luckly we can handle all the other pieces in one go
556 return ( pos.piece_attacks_square(pos.piece_on(from), from, to)
557 && pos.pl_move_is_legal(m, pinned)
558 && !move_promotion(m));
564 template<PieceType Piece>
565 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
570 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
572 from = pos.piece_list(us, Piece, i);
573 b = pos.piece_attacks<Piece>(from) & target;
579 template<PieceType Piece>
580 MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist,
581 Color us, Bitboard target, Bitboard pinned) {
585 for (int i = 0, e = pos.piece_count(us, Piece); i < e; i++)
587 from = pos.piece_list(us, Piece, i);
588 if (pinned && bit_is_set(pinned, from))
591 b = pos.piece_attacks<Piece>(from) & target;
598 MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
601 Square from = pos.king_square(us);
603 b = pos.piece_attacks<KING>(from) & target;
608 template<Color Us, Color Them, Bitboard TRank8BB, SquareDelta TDELTA_NE,
609 SquareDelta TDELTA_NW, SquareDelta TDELTA_N
611 MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist) {
614 Bitboard pawns = pos.pawns(Us);
615 Bitboard enemyPieces = pos.pieces_of_color(Them);
617 // Captures in the a1-h8 (a8-h1 for black) direction
618 Bitboard b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces;
620 // Capturing promotions
621 Bitboard b2 = b1 & TRank8BB;
624 to = pop_1st_bit(&b2);
625 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, QUEEN);
628 // Capturing non-promotions
632 to = pop_1st_bit(&b2);
633 (*mlist++).move = make_move(to - TDELTA_NE, to);
636 // Captures in the h1-a8 (h8-a1 for black) direction
637 b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces;
639 // Capturing promotions
643 to = pop_1st_bit(&b2);
644 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, QUEEN);
647 // Capturing non-promotions
651 to = pop_1st_bit(&b2);
652 (*mlist++).move = make_move(to - TDELTA_NW, to);
655 // Non-capturing promotions
656 b1 = move_pawns<Us, DELTA_N>(pawns) & pos.empty_squares() & TRank8BB;
659 to = pop_1st_bit(&b1);
660 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
663 // En passant captures
664 if (pos.ep_square() != SQ_NONE)
666 assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
667 assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
669 b1 = pawns & pos.pawn_attacks(Them, pos.ep_square());
670 assert(b1 != EmptyBoardBB);
674 to = pop_1st_bit(&b1);
675 (*mlist++).move = make_ep_move(to, pos.ep_square());
681 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB,
682 SquareDelta TDELTA_NE, SquareDelta TDELTA_NW, SquareDelta TDELTA_N
684 MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
686 Bitboard pawns = pos.pawns(Us);
687 Bitboard enemyPieces = pos.pieces_of_color(Them);
688 Bitboard emptySquares = pos.empty_squares();
692 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
693 b1 = move_pawns<Us, DELTA_NE>(pawns) & ~FileABB & enemyPieces & TRank8BB;
696 to = pop_1st_bit(&b1);
697 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, ROOK);
698 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, BISHOP);
699 (*mlist++).move = make_promotion_move(to - TDELTA_NE, to, KNIGHT);
702 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
703 b1 = move_pawns<Us, DELTA_NW>(pawns) & ~FileHBB & enemyPieces & TRank8BB;
706 to = pop_1st_bit(&b1);
707 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, ROOK);
708 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, BISHOP);
709 (*mlist++).move = make_promotion_move(to - TDELTA_NW, to, KNIGHT);
712 // Single pawn pushes
713 b1 = move_pawns<Us, DELTA_N>(pawns) & emptySquares;
717 to = pop_1st_bit(&b2);
718 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
719 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
720 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
725 to = pop_1st_bit(&b2);
726 (*mlist++).move = make_move(to - TDELTA_N, to);
729 // Double pawn pushes
730 b2 = move_pawns<Us, DELTA_N>(b1 & TRank3BB) & emptySquares;
733 to = pop_1st_bit(&b2);
734 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
740 template<Color Us, Color Them, Bitboard TRank8BB, Bitboard TRank3BB, SquareDelta TDELTA_N>
741 MoveStack* generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
743 // Find all friendly pawns not on the enemy king's file
745 Bitboard empty = pos.empty_squares();
747 if (dc != EmptyBoardBB)
749 // Pawn moves which gives discovered check. This is possible only if the
750 // pawn is not on the same file as the enemy king, because we don't
751 // generate captures.
752 b1 = pos.pawns(Us) & ~file_bb(ksq);
754 // Discovered checks, single pawn pushes, no promotions
755 b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
758 Square to = pop_1st_bit(&b3);
759 (*mlist++).move = make_move(to - TDELTA_N, to);
762 // Discovered checks, double pawn pushes
763 b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
766 Square to = pop_1st_bit(&b3);
767 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
771 // Direct checks. These are possible only for pawns on neighboring files
772 // of the enemy king.
773 b1 = pos.pawns(Us) & neighboring_files_bb(ksq) & ~dc;
775 // Direct checks, single pawn pushes
776 b2 = move_pawns<Us, DELTA_N>(b1) & empty;
777 b3 = b2 & pos.pawn_attacks(Them, ksq);
780 Square to = pop_1st_bit(&b3);
781 (*mlist++).move = make_move(to - TDELTA_N, to);
784 // Direct checks, double pawn pushes
785 b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.pawn_attacks(Them, ksq);
788 Square to = pop_1st_bit(&b3);
789 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
794 template<PieceType Piece>
795 MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
796 Bitboard dc, Square ksq) {
798 Bitboard target = pos.pieces_of_color_and_type(us, Piece);
801 Bitboard b = target & dc;
804 Square from = pop_1st_bit(&b);
805 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
807 bb &= ~QueenPseudoAttacks[ksq];
814 if (Piece == KING || !b)
817 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
820 Square from = pop_1st_bit(&b);
821 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
827 template<Color Us, Rank TRANK_8, Bitboard TRank3BB, SquareDelta TDELTA_N>
828 MoveStack* generate_pawn_blocking_evasions(const Position& pos, Bitboard pinned,
829 Bitboard blockSquares, MoveStack* mlist) {
832 // Find non-pinned pawns and push them one square
833 Bitboard b1 = move_pawns<Us, DELTA_N>(pos.pawns(Us) & ~pinned);
835 // We don't have to AND with empty squares here,
836 // because the blocking squares will always be empty.
837 Bitboard b2 = b1 & blockSquares;
840 to = pop_1st_bit(&b2);
842 assert(pos.piece_on(to) == EMPTY);
844 if (square_rank(to) == TRANK_8)
846 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, QUEEN);
847 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, ROOK);
848 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, BISHOP);
849 (*mlist++).move = make_promotion_move(to - TDELTA_N, to, KNIGHT);
851 (*mlist++).move = make_move(to - TDELTA_N, to);
854 // Double pawn pushes
855 b2 = b1 & pos.empty_squares() & TRank3BB;
856 b2 = move_pawns<Us, DELTA_N>(b2) & blockSquares;
859 to = pop_1st_bit(&b2);
861 assert(pos.piece_on(to) == EMPTY);
862 assert(Us != WHITE || square_rank(to) == RANK_4);
863 assert(Us != BLACK || square_rank(to) == RANK_5);
865 (*mlist++).move = make_move(to - TDELTA_N - TDELTA_N, to);
870 template<CastlingSide Side>
871 MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
873 Color us = pos.side_to_move();
875 if ( (Side == KING_SIDE && pos.can_castle_kingside(us))
876 ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
878 Color them = opposite_color(us);
879 Square ksq = pos.king_square(us);
881 assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
883 Square rsq = (Side == KING_SIDE ? pos.initial_kr_square(us) : pos.initial_qr_square(us));
884 Square s1 = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
885 Square s2 = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
887 bool illegal = false;
889 assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
891 // It is a bit complicated to correctly handle Chess960
892 for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
893 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
894 || pos.square_is_attacked(s, them))
897 for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
898 if (s != ksq && s != rsq && pos.square_is_occupied(s))
901 if ( Side == QUEEN_SIDE
902 && square_file(rsq) == FILE_B
903 && ( pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, ROOK)
904 || pos.piece_on(relative_square(us, SQ_A1)) == piece_of_color_and_type(them, QUEEN)))
908 (*mlist++).move = make_castle_move(ksq, rsq);
913 bool castling_is_check(const Position& pos, CastlingSide side) {
915 // After castling opponent king is attacked by the castled rook?
916 File rookFile = (side == QUEEN_SIDE ? FILE_D : FILE_F);
917 Color us = pos.side_to_move();
918 Square ksq = pos.king_square(us);
919 Bitboard occ = pos.occupied_squares();
921 clear_bit(&occ, ksq); // Remove our king from the board
922 Square rsq = make_square(rookFile, square_rank(ksq));
923 return bit_is_set(rook_attacks_bb(rsq, occ), pos.king_square(opposite_color(us)));