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/>.
30 //// Local definitions
36 Bitboard Rank3BB, Rank8BB;
38 SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
41 const PawnParams WhitePawnParams = { Rank3BB, Rank8BB, RANK_8, DELTA_N, DELTA_NE, DELTA_NW, WHITE, BLACK };
42 const PawnParams BlackPawnParams = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE };
44 int generate_castle_moves(const Position&, MoveStack*);
47 int generate_pawn_captures(const Position&, MoveStack*);
50 int generate_pawn_noncaptures(const Position&, MoveStack*);
53 int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
56 int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
59 int generate_piece_moves(const Position&, MoveStack*, Bitboard);
62 int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*);
63 int generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*);
66 int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
75 /// generate_captures generates() all pseudo-legal captures and queen
76 /// promotions. The return value is the number of moves generated.
78 int generate_captures(const Position& pos, MoveStack* mlist) {
81 assert(!pos.is_check());
83 Color us = pos.side_to_move();
84 Bitboard target = pos.pieces_of_color(opposite_color(us));
88 n = generate_pawn_captures<WHITE>(pos, mlist);
90 n = generate_pawn_captures<BLACK>(pos, mlist);
92 n += generate_piece_moves<KNIGHT>(pos, mlist+n, target);
93 n += generate_piece_moves<BISHOP>(pos, mlist+n, target);
94 n += generate_piece_moves<ROOK>(pos, mlist+n, target);
95 n += generate_piece_moves<QUEEN>(pos, mlist+n, target);
96 n += generate_piece_moves<KING>(pos, mlist+n, target);
101 /// generate_noncaptures() generates all pseudo-legal non-captures and
102 /// underpromotions. The return value is the number of moves generated.
104 int generate_noncaptures(const Position& pos, MoveStack *mlist) {
107 assert(!pos.is_check());
109 Color us = pos.side_to_move();
110 Bitboard target = pos.empty_squares();
114 n = generate_pawn_noncaptures<WHITE>(pos, mlist);
116 n = generate_pawn_noncaptures<BLACK>(pos, mlist);
118 n += generate_piece_moves<KNIGHT>(pos, mlist+n, target);
119 n += generate_piece_moves<BISHOP>(pos, mlist+n, target);
120 n += generate_piece_moves<ROOK>(pos, mlist+n, target);
121 n += generate_piece_moves<QUEEN>(pos, mlist+n, target);
122 n += generate_piece_moves<KING>(pos, mlist+n, target);
123 n += generate_castle_moves(pos, mlist+n);
128 /// generate_checks() generates all pseudo-legal non-capturing, non-promoting
129 /// checks, except castling moves (will add this later). It returns the
130 /// number of generated moves.
132 int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
135 assert(!pos.is_check());
138 Color us = pos.side_to_move();
139 Square ksq = pos.king_square(opposite_color(us));
141 assert(pos.piece_on(ksq) == king_of_color(opposite_color(us)));
143 dc = pos.discovered_check_candidates(us);
147 n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist);
149 n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist);
152 Bitboard b = pos.knights(us);
154 n += generate_piece_checks<KNIGHT>(pos, b, dc, ksq, mlist+n);
158 n += generate_piece_checks<BISHOP>(pos, b, dc, ksq, mlist+n);
162 n += generate_piece_checks<ROOK>(pos, b, dc, ksq, mlist+n);
166 n += generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist+n);
168 // Hopefully we always have a king ;-)
169 n += generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist+n);
171 // TODO: Castling moves!
177 /// generate_evasions() generates all check evasions when the side to move is
178 /// in check. Unlike the other move generation functions, this one generates
179 /// only legal moves. It returns the number of generated moves. This
180 /// function is very ugly, and needs cleaning up some time later. FIXME
182 int generate_evasions(const Position& pos, MoveStack* mlist) {
185 assert(pos.is_check());
187 Color us = pos.side_to_move();
188 Color them = opposite_color(us);
189 Square ksq = pos.king_square(us);
193 assert(pos.piece_on(ksq) == king_of_color(us));
195 // Generate evasions for king
196 Bitboard b1 = pos.piece_attacks<KING>(ksq) & ~pos.pieces_of_color(us);
197 Bitboard b2 = pos.occupied_squares();
202 to = pop_1st_bit(&b1);
204 // Make sure to is not attacked by the other side. This is a bit ugly,
205 // because we can't use Position::square_is_attacked. Instead we use
206 // the low-level bishop_attacks_bb and rook_attacks_bb with the bitboard
207 // b2 (the occupied squares with the king removed) in order to test whether
208 // the king will remain in check on the destination square.
209 if (!( (bishop_attacks_bb(to, b2) & pos.bishops_and_queens(them))
210 || (rook_attacks_bb(to, b2) & pos.rooks_and_queens(them))
211 || (pos.piece_attacks<KNIGHT>(to) & pos.knights(them))
212 || (pos.pawn_attacks(us, to) & pos.pawns(them))
213 || (pos.piece_attacks<KING>(to) & pos.kings(them))))
215 mlist[n++].move = make_move(ksq, to);
218 // Generate evasions for other pieces only if not double check. We use a
219 // simple bit twiddling hack here rather than calling count_1s in order to
220 // save some time (we know that pos.checkers() has at most two nonzero bits).
221 Bitboard checkers = pos.checkers();
223 if (!(checkers & (checkers - 1))) // Only one bit set?
225 Square checksq = first_1(checkers);
227 assert(pos.color_of_piece_on(checksq) == them);
229 // Find pinned pieces
230 Bitboard not_pinned = ~pos.pinned_pieces(us);
232 // Generate captures of the checking piece
235 b1 = pos.pawn_attacks(them, checksq) & pos.pawns(us) & not_pinned;
238 from = pop_1st_bit(&b1);
239 if (relative_rank(us, checksq) == RANK_8)
241 mlist[n++].move = make_promotion_move(from, checksq, QUEEN);
242 mlist[n++].move = make_promotion_move(from, checksq, ROOK);
243 mlist[n++].move = make_promotion_move(from, checksq, BISHOP);
244 mlist[n++].move = make_promotion_move(from, checksq, KNIGHT);
246 mlist[n++].move = make_move(from, checksq);
250 b1 = ( (pos.piece_attacks<KNIGHT>(checksq) & pos.knights(us))
251 | (pos.piece_attacks<BISHOP>(checksq) & pos.bishops_and_queens(us))
252 | (pos.piece_attacks<ROOK>(checksq) & pos.rooks_and_queens(us)) ) & not_pinned;
256 from = pop_1st_bit(&b1);
257 mlist[n++].move = make_move(from, checksq);
260 // Blocking check evasions are possible only if the checking piece is
262 if (checkers & pos.sliders())
264 Bitboard blockSquares = squares_between(checksq, ksq);
266 assert((pos.occupied_squares() & blockSquares) == EmptyBoardBB);
268 // Pawn moves. Because a blocking evasion can never be a capture, we
269 // only generate pawn pushes.
271 n += generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist+n);
273 n += generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist+n);
276 b1 = pos.knights(us) & not_pinned;
278 n += generate_piece_blocking_evasions<KNIGHT>(pos, b1, blockSquares, mlist+n);
280 b1 = pos.bishops(us) & not_pinned;
282 n += generate_piece_blocking_evasions<BISHOP>(pos, b1, blockSquares, mlist+n);
284 b1 = pos.rooks(us) & not_pinned;
286 n += generate_piece_blocking_evasions<ROOK>(pos, b1, blockSquares, mlist+n);
288 b1 = pos.queens(us) & not_pinned;
290 n += generate_piece_blocking_evasions<QUEEN>(pos, b1, blockSquares, mlist+n);
293 // Finally, the ugly special case of en passant captures. An en passant
294 // capture can only be a check evasion if the check is not a discovered
295 // check. If pos.ep_square() is set, the last move made must have been
296 // a double pawn push. If, furthermore, the checking piece is a pawn,
297 // an en passant check evasion may be possible.
298 if (pos.ep_square() != SQ_NONE && (checkers & pos.pawns(them)))
300 to = pos.ep_square();
301 b1 = pos.pawn_attacks(them, to) & pos.pawns(us);
303 assert(b1 != EmptyBoardBB);
308 from = pop_1st_bit(&b1);
310 // Before generating the move, we have to make sure it is legal.
311 // This is somewhat tricky, because the two disappearing pawns may
312 // cause new "discovered checks". We test this by removing the
313 // two relevant bits from the occupied squares bitboard, and using
314 // the low-level bitboard functions for bishop and rook attacks.
315 b2 = pos.occupied_squares();
316 clear_bit(&b2, from);
317 clear_bit(&b2, checksq);
318 if (!( (bishop_attacks_bb(ksq, b2) & pos.bishops_and_queens(them))
319 ||(rook_attacks_bb(ksq, b2) & pos.rooks_and_queens(them))))
321 mlist[n++].move = make_ep_move(from, to);
329 /// generate_legal_moves() computes a complete list of legal moves in the
330 /// current position. This function is not very fast, and should be used
331 /// only in situations where performance is unimportant. It wouldn't be
332 /// very hard to write an efficient legal move generator, but for the moment
333 /// we don't need it.
335 int generate_legal_moves(const Position& pos, MoveStack* mlist) {
340 return generate_evasions(pos, mlist);
342 // Generate pseudo-legal moves:
343 int n = generate_captures(pos, mlist);
344 n += generate_noncaptures(pos, mlist + n);
346 Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
348 // Remove illegal moves from the list:
349 for (int i = 0; i < n; i++)
350 if (!pos.move_is_legal(mlist[i].move, pinned))
351 mlist[i--].move = mlist[--n].move;
357 /// generate_move_if_legal() takes a position and a (not necessarily
358 /// pseudo-legal) move and a pinned pieces bitboard as input, and tests
359 /// whether the move is legal. If the move is legal, the move itself is
360 /// returned. If not, the function returns MOVE_NONE. This function must
361 /// only be used when the side to move is not in check.
363 Move generate_move_if_legal(const Position& pos, Move m, Bitboard pinned) {
366 assert(!pos.is_check());
367 assert(move_is_ok(m));
369 Color us = pos.side_to_move();
370 Color them = opposite_color(us);
371 Square from = move_from(m);
372 Piece pc = pos.piece_on(from);
374 // If the from square is not occupied by a piece belonging to the side to
375 // move, the move is obviously not legal.
376 if (color_of_piece(pc) != us)
379 Square to = move_to(m);
384 // The piece must be a pawn and destination square must be the
385 // en passant square.
386 if ( type_of_piece(pc) != PAWN
387 || to != pos.ep_square())
390 assert(pos.square_is_empty(to));
391 assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
393 // The move is pseudo-legal. If it is legal, return it.
394 return (pos.move_is_legal(m) ? m : MOVE_NONE);
398 if (move_is_short_castle(m))
400 // The piece must be a king and side to move must still have
401 // the right to castle kingside.
402 if ( type_of_piece(pc) != KING
403 ||!pos.can_castle_kingside(us))
406 assert(from == pos.king_square(us));
407 assert(to == pos.initial_kr_square(us));
408 assert(pos.piece_on(to) == rook_of_color(us));
410 Square g1 = relative_square(us, SQ_G1);
411 Square f1 = relative_square(us, SQ_F1);
413 bool illegal = false;
415 // Check if any of the squares between king and rook
416 // is occupied or under attack.
417 for (s = Min(from, g1); s <= Max(from, g1); s++)
418 if ( (s != from && s != to && !pos.square_is_empty(s))
419 || pos.square_is_attacked(s, them))
422 // Check if any of the squares between king and rook
424 for (s = Min(to, f1); s <= Max(to, f1); s++)
425 if (s != from && s != to && !pos.square_is_empty(s))
428 return (!illegal ? m : MOVE_NONE);
431 if (move_is_long_castle(m))
433 // The piece must be a king and side to move must still have
434 // the right to castle kingside.
435 if ( type_of_piece(pc) != KING
436 ||!pos.can_castle_queenside(us))
439 assert(from == pos.king_square(us));
440 assert(to == pos.initial_qr_square(us));
441 assert(pos.piece_on(to) == rook_of_color(us));
443 Square c1 = relative_square(us, SQ_C1);
444 Square d1 = relative_square(us, SQ_D1);
446 bool illegal = false;
448 for (s = Min(from, c1); s <= Max(from, c1); s++)
449 if( (s != from && s != to && !pos.square_is_empty(s))
450 || pos.square_is_attacked(s, them))
453 for (s = Min(to, d1); s <= Max(to, d1); s++)
454 if(s != from && s != to && !pos.square_is_empty(s))
457 if ( square_file(to) == FILE_B
458 && ( pos.piece_on(to + DELTA_W) == rook_of_color(them)
459 || pos.piece_on(to + DELTA_W) == queen_of_color(them)))
462 return (!illegal ? m : MOVE_NONE);
467 // The destination square cannot be occupied by a friendly piece
468 if (pos.color_of_piece_on(to) == us)
471 // Proceed according to the type of the moving piece.
472 if (type_of_piece(pc) == PAWN)
474 // If the destination square is on the 8/1th rank, the move must
476 if ( ( (square_rank(to) == RANK_8 && us == WHITE)
477 ||(square_rank(to) == RANK_1 && us != WHITE))
478 && !move_promotion(m))
481 // Proceed according to the square delta between the source and
482 // destionation squares.
489 // Capture. The destination square must be occupied by an enemy
490 // piece (en passant captures was handled earlier).
491 if (pos.color_of_piece_on(to) != them)
497 // Pawn push. The destination square must be empty.
498 if (!pos.square_is_empty(to))
503 // Double white pawn push. The destination square must be on the fourth
504 // rank, and both the destination square and the square between the
505 // source and destination squares must be empty.
506 if ( square_rank(to) != RANK_4
507 || !pos.square_is_empty(to)
508 || !pos.square_is_empty(from + DELTA_N))
513 // Double black pawn push. The destination square must be on the fifth
514 // rank, and both the destination square and the square between the
515 // source and destination squares must be empty.
516 if ( square_rank(to) != RANK_5
517 || !pos.square_is_empty(to)
518 || !pos.square_is_empty(from + DELTA_S))
525 // The move is pseudo-legal. Return it if it is legal.
526 return (pos.move_is_legal(m) ? m : MOVE_NONE);
529 // Luckly we can handle all the other pieces in one go
530 return ( pos.piece_attacks_square(from, to)
531 && pos.move_is_legal(m)
532 && !move_promotion(m) ? m : MOVE_NONE);
538 template<PieceType Piece>
539 int generate_piece_moves(const Position& pos, MoveStack* mlist, Bitboard target) {
542 Color us = pos.side_to_move();
544 for (int i = 0; i < pos.piece_count(us, Piece); i++)
546 Square from = pos.piece_list(us, Piece, i);
547 Bitboard b = pos.piece_attacks<Piece>(from) & target;
550 Square to = pop_1st_bit(&b);
551 mlist[n++].move = make_move(from, to);
558 template<PieceType Piece>
559 int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
560 Bitboard blockSquares, MoveStack* mlist) {
564 Square from = pop_1st_bit(&b);
565 Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
568 Square to = pop_1st_bit(&bb);
569 mlist[n++].move = make_move(from, to);
577 int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
579 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
581 Bitboard pawns = pos.pawns(PP.us);
582 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
586 // Captures in the a1-h8 (a8-h1 for black) direction
587 Bitboard b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces;
589 // Capturing promotions
590 Bitboard b2 = b1 & PP.Rank8BB;
593 sq = pop_1st_bit(&b2);
594 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, QUEEN);
597 // Capturing non-promotions
598 b2 = b1 & ~PP.Rank8BB;
601 sq = pop_1st_bit(&b2);
602 mlist[n++].move = make_move(sq - PP.DELTA_NE, sq);
605 // Captures in the h1-a8 (h8-a1 for black) direction
606 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces;
608 // Capturing promotions
609 b2 = b1 & PP.Rank8BB;
612 sq = pop_1st_bit(&b2);
613 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, QUEEN);
616 // Capturing non-promotions
617 b2 = b1 & ~PP.Rank8BB;
620 sq = pop_1st_bit(&b2);
621 mlist[n++].move = make_move(sq - PP.DELTA_NW, sq);
624 // Non-capturing promotions
625 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & pos.empty_squares() & Rank8BB;
628 sq = pop_1st_bit(&b1);
629 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, QUEEN);
632 // En passant captures
633 if (pos.ep_square() != SQ_NONE)
635 assert(PP.us != WHITE || square_rank(pos.ep_square()) == RANK_6);
636 assert(PP.us != BLACK || square_rank(pos.ep_square()) == RANK_3);
638 b1 = pawns & pos.pawn_attacks(PP.them, pos.ep_square());
639 assert(b1 != EmptyBoardBB);
643 sq = pop_1st_bit(&b1);
644 mlist[n++].move = make_ep_move(sq, pos.ep_square());
652 int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
654 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
656 Bitboard pawns = pos.pawns(PP.us);
657 Bitboard enemyPieces = pos.pieces_of_color(PP.them);
658 Bitboard emptySquares = pos.empty_squares();
663 // Underpromotion captures in the a1-h8 (a8-h1 for black) direction
664 b1 = (C == WHITE ? pawns << 9 : pawns >> 7) & ~FileABB & enemyPieces & PP.Rank8BB;
667 sq = pop_1st_bit(&b1);
668 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, ROOK);
669 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, BISHOP);
670 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NE, sq, KNIGHT);
673 // Underpromotion captures in the h1-a8 (h8-a1 for black) direction
674 b1 = (C == WHITE ? pawns << 7 : pawns >> 9) & ~FileHBB & enemyPieces & PP.Rank8BB;
677 sq = pop_1st_bit(&b1);
678 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, ROOK);
679 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, BISHOP);
680 mlist[n++].move = make_promotion_move(sq - PP.DELTA_NW, sq, KNIGHT);
683 // Single pawn pushes
684 b1 = (C == WHITE ? pawns << 8 : pawns >> 8) & emptySquares;
685 b2 = b1 & PP.Rank8BB;
688 sq = pop_1st_bit(&b2);
689 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, ROOK);
690 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, BISHOP);
691 mlist[n++].move = make_promotion_move(sq - PP.DELTA_N, sq, KNIGHT);
693 b2 = b1 & ~PP.Rank8BB;
696 sq = pop_1st_bit(&b2);
697 mlist[n++].move = make_move(sq - PP.DELTA_N, sq);
700 // Double pawn pushes
701 b2 = (C == WHITE ? (b1 & PP.Rank3BB) << 8 : (b1 & PP.Rank3BB) >> 8) & emptySquares;
704 sq = pop_1st_bit(&b2);
705 mlist[n++].move = make_move(sq - PP.DELTA_N - PP.DELTA_N, sq);
712 int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist)
714 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
716 // Pawn moves which give discovered check. This is possible only if the
717 // pawn is not on the same file as the enemy king, because we don't
718 // generate captures.
720 Bitboard empty = pos.empty_squares();
722 // Find all friendly pawns not on the enemy king's file
723 Bitboard b1 = pos.pawns(pos.side_to_move()) & ~file_bb(ksq), b2, b3;
725 // Discovered checks, single pawn pushes
726 b2 = b3 = (C == WHITE ? (b1 & dc) << 8 : (b1 & dc) >> 8) & ~PP.Rank8BB & empty;
729 Square to = pop_1st_bit(&b3);
730 mlist[n++].move = make_move(to - PP.DELTA_N, to);
733 // Discovered checks, double pawn pushes
734 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8) & empty;
737 Square to = pop_1st_bit(&b3);
738 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
741 // Direct checks. These are possible only for pawns on neighboring files
744 b1 &= (~dc & neighboring_files_bb(ksq)); // FIXME why ~dc ??
746 // Direct checks, single pawn pushes
747 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & empty;
748 b3 = b2 & pos.pawn_attacks(PP.them, ksq);
751 Square to = pop_1st_bit(&b3);
752 mlist[n++].move = make_move(to - PP.DELTA_N, to);
755 // Direct checks, double pawn pushes
756 b3 = (C == WHITE ? (b2 & PP.Rank3BB) << 8 : (b2 & PP.Rank3BB) >> 8)
758 & pos.pawn_attacks(PP.them, ksq);
762 Square to = pop_1st_bit(&b3);
763 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
768 template<PieceType Piece>
769 int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
770 Square ksq, MoveStack* mlist) {
773 Bitboard b = target & dc;
776 Square from = pop_1st_bit(&b);
777 Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
780 Square to = pop_1st_bit(&bb);
781 mlist[n++].move = make_move(from, to);
786 Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
789 Square from = pop_1st_bit(&b);
790 Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
793 Square to = pop_1st_bit(&bb);
794 mlist[n++].move = make_move(from, to);
800 int generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
801 Square ksq, MoveStack* mlist) {
803 if (bit_is_set(dc, from))
805 Bitboard b = pos.piece_attacks<KING>(from)
806 & pos.empty_squares()
807 & ~QueenPseudoAttacks[ksq];
810 Square to = pop_1st_bit(&b);
811 mlist[n++].move = make_move(from, to);
819 int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
820 Bitboard blockSquares, MoveStack* mlist) {
822 static const PawnParams PP = (C == WHITE ? WhitePawnParams : BlackPawnParams);
824 // Find non-pinned pawns
826 Bitboard b1 = pos.pawns(PP.us) & not_pinned;
828 // Single pawn pushes. We don't have to AND with empty squares here,
829 // because the blocking squares will always be empty.
830 Bitboard b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & blockSquares;
833 Square to = pop_1st_bit(&b2);
835 assert(pos.piece_on(to) == EMPTY);
837 if (square_rank(to) == PP.RANK_8)
839 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, QUEEN);
840 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, ROOK);
841 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, BISHOP);
842 mlist[n++].move = make_promotion_move(to - PP.DELTA_N, to, KNIGHT);
844 mlist[n++].move = make_move(to - PP.DELTA_N, to);
847 // Double pawn pushes
848 b2 = (C == WHITE ? b1 << 8 : b1 >> 8) & pos.empty_squares() & PP.Rank3BB;
849 b2 = (C == WHITE ? b2 << 8 : b2 >> 8) & blockSquares;;
852 Square to = pop_1st_bit(&b2);
854 assert(pos.piece_on(to) == EMPTY);
855 assert(PP.us != WHITE || square_rank(to) == RANK_4);
856 assert(PP.us != BLACK || square_rank(to) == RANK_5);
858 mlist[n++].move = make_move(to - PP.DELTA_N - PP.DELTA_N, to);
864 int generate_castle_moves(const Position& pos, MoveStack* mlist) {
867 Color us = pos.side_to_move();
869 if (pos.can_castle(us))
871 Color them = opposite_color(us);
872 Square ksq = pos.king_square(us);
874 assert(pos.piece_on(ksq) == king_of_color(us));
876 if (pos.can_castle_kingside(us))
878 Square rsq = pos.initial_kr_square(us);
879 Square g1 = relative_square(us, SQ_G1);
880 Square f1 = relative_square(us, SQ_F1);
882 bool illegal = false;
884 assert(pos.piece_on(rsq) == rook_of_color(us));
886 for (s = Min(ksq, g1); s <= Max(ksq, g1); s++)
887 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
888 || pos.square_is_attacked(s, them))
891 for (s = Min(rsq, f1); s <= Max(rsq, f1); s++)
892 if (s != ksq && s != rsq && pos.square_is_occupied(s))
896 mlist[n++].move = make_castle_move(ksq, rsq);
899 if (pos.can_castle_queenside(us))
901 Square rsq = pos.initial_qr_square(us);
902 Square c1 = relative_square(us, SQ_C1);
903 Square d1 = relative_square(us, SQ_D1);
905 bool illegal = false;
907 assert(pos.piece_on(rsq) == rook_of_color(us));
909 for (s = Min(ksq, c1); s <= Max(ksq, c1); s++)
910 if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
911 || pos.square_is_attacked(s, them))
914 for (s = Min(rsq, d1); s <= Max(rsq, d1); s++)
915 if (s != ksq && s != rsq && pos.square_is_occupied(s))
918 if ( square_file(rsq) == FILE_B
919 && ( pos.piece_on(relative_square(us, SQ_A1)) == rook_of_color(them)
920 || pos.piece_on(relative_square(us, SQ_A1)) == queen_of_color(them)))
924 mlist[n++].move = make_castle_move(ksq, rsq);