]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
5f7ffb3564e039cd8f25d9902d0e172c7cdf3b08
[stockfish] / src / movegen.cpp
1 /*
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
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26
27 #include "bitcount.h"
28 #include "movegen.h"
29
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))
33
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); }
36
37 ////
38 //// Local definitions
39 ////
40
41 namespace {
42
43   enum CastlingSide {
44     KING_SIDE,
45     QUEEN_SIDE
46   };
47
48   template<CastlingSide Side>
49   MoveStack* generate_castle_moves(const Position&, MoveStack*, Color us);
50
51   template<Color Us, MoveType Type>
52   MoveStack* generate_pawn_moves(const Position&, MoveStack*, Bitboard, Square);
53
54   template<PieceType Piece>
55   inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) {
56
57     assert(Piece != QUEEN);
58
59     Bitboard b = pos.attacks_from<Piece>(from) & pos.empty_squares();
60     if (Piece == KING)
61     {
62         Square ksq = pos.king_square(opposite_color(pos.side_to_move()));
63         b &= ~QueenPseudoAttacks[ksq];
64     }
65     SERIALIZE_MOVES(b);
66     return mlist;
67   }
68
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);
73
74     Bitboard checkSqs, b;
75     Square from;
76     const Square* ptr = pos.piece_list_begin(us, Piece);
77
78     if ((from = *ptr++) == SQ_NONE)
79         return mlist;
80
81     checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
82
83     do
84     {
85         if (   (Piece == QUEEN  && !(QueenPseudoAttacks[from]  & checkSqs))
86             || (Piece == ROOK   && !(RookPseudoAttacks[from]   & checkSqs))
87             || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
88             continue;
89
90         if (dc && bit_is_set(dc, from))
91             continue;
92
93         b = pos.attacks_from<Piece>(from) & checkSqs;
94         SERIALIZE_MOVES(b);
95
96     } while ((from = *ptr++) != SQ_NONE);
97
98     return mlist;
99   }
100
101   template<>
102   inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
103
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));
106   }
107
108   template<PieceType Piece, MoveType Type>
109   inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
110
111     assert(Piece == PAWN);
112     assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_EVASION);
113
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));
116   }
117
118   template<PieceType Piece>
119   inline MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
120
121     Bitboard b;
122     Square from;
123     const Square* ptr = pos.piece_list_begin(us, Piece);
124
125     if (*ptr != SQ_NONE)
126     {
127         do {
128             from = *ptr;
129             b = pos.attacks_from<Piece>(from) & target;
130             SERIALIZE_MOVES(b);
131         } while (*++ptr != SQ_NONE);
132     }
133     return mlist;
134   }
135
136   template<>
137   inline MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
138
139     Bitboard b;
140     Square from = pos.king_square(us);
141
142     b = pos.attacks_from<KING>(from) & target;
143     SERIALIZE_MOVES(b);
144     return mlist;
145   }
146
147 }
148
149 ////
150 //// Functions
151 ////
152
153
154 /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
155 /// promotions. Returns a pointer to the end of the move list.
156 ///
157 /// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and
158 /// underpromotions. Returns a pointer to the end of the move list.
159 ///
160 /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
161 /// non-captures. Returns a pointer to the end of the move list.
162
163 template<MoveType T>
164 MoveStack* generate(const Position& pos, MoveStack* mlist) {
165
166   assert(pos.is_ok());
167   assert(!pos.is_check());
168
169   Color us = pos.side_to_move();
170   Bitboard target;
171
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();
176   else
177       assert(false);
178
179   if (T == MV_NON_EVASION)
180   {
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();
184   }
185   else
186       mlist = generate_piece_moves<PAWN, T>(pos, mlist, us, target);
187
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);
193
194   if (T != MV_CAPTURE)
195   {
196       if (pos.can_castle_kingside(us))
197           mlist = generate_castle_moves<KING_SIDE>(pos, mlist, us);
198
199       if (pos.can_castle_queenside(us))
200           mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist, us);
201   }
202
203   return mlist;
204 }
205
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);
210
211
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.
214 template<>
215 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
216
217   assert(pos.is_ok());
218   assert(!pos.is_check());
219
220   Bitboard b, dc;
221   Square from;
222   Color us = pos.side_to_move();
223   Square ksq = pos.king_square(opposite_color(us));
224
225   assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
226
227   // Discovered non-capture checks
228   b = dc = pos.discovered_check_candidates(us);
229
230   while (b)
231   {
232      from = pop_1st_bit(&b);
233      switch (pos.type_of_piece_on(from))
234      {
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;
241      }
242   }
243
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);
250 }
251
252
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.
255 template<>
256 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
257
258   assert(pos.is_ok());
259   assert(pos.is_check());
260
261   Bitboard b, target;
262   Square from, checksq;
263   int checkersCnt = 0;
264   Color us = pos.side_to_move();
265   Square ksq = pos.king_square(us);
266   Bitboard checkers = pos.checkers();
267   Bitboard sliderAttacks = EmptyBoardBB;
268
269   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
270   assert(checkers);
271
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.
275   b = checkers;
276   do
277   {
278       checkersCnt++;
279       checksq = pop_1st_bit(&b);
280
281       assert(pos.color_of_piece_on(checksq) == opposite_color(us));
282
283       switch (pos.type_of_piece_on(checksq))
284       {
285       case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
286       case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
287       case QUEEN:
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);
292           else
293               sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
294       default:
295           break;
296       }
297   } while (b);
298
299   // Generate evasions for king, capture and non capture moves
300   b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
301   from = ksq;
302   SERIALIZE_MOVES(b);
303
304   // Generate evasions for other pieces only if not double check
305   if (checkersCnt > 1)
306       return mlist;
307
308   // Find squares where a blocking evasion or a capture of the
309   // checker piece is possible.
310   target = squares_between(checksq, ksq) | checkers;
311
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);
317 }
318
319
320 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
321 /// or pseudo-legal moves in the current position.
322 template<>
323 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
324
325   assert(pos.is_ok());
326
327   return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
328                         : generate<MV_NON_EVASION>(pos, mlist);
329 }
330
331 template<>
332 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
333
334   assert(pos.is_ok());
335
336   MoveStack *last, *cur = mlist;
337   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
338
339   last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
340
341   // Remove illegal moves from the list
342   while (cur != last)
343       if (pos.pl_move_is_legal(cur->move, pinned))
344           cur++;
345       else
346           cur->move = (--last)->move;
347
348   return last;
349 }
350
351
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.
355
356 bool move_is_legal(const Position& pos, const Move m) {
357
358   MoveStack mlist[MOVES_MAX];
359   MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
360
361    for (cur = mlist; cur != last; cur++)
362       if (cur->move == m)
363           return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
364
365   return false;
366 }
367
368
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.
371
372 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
373
374   assert(pos.is_ok());
375   assert(pinned == pos.pinned_pieces(pos.side_to_move()));
376
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);
382
383   // Use a slower but simpler function for uncommon cases
384   if (move_is_special(m))
385       return move_is_legal(pos, m);
386
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)
390       return false;
391
392   // The destination square cannot be occupied by a friendly piece
393   if (pos.color_of_piece_on(to) == us)
394       return false;
395
396   // Handle the special case of a pawn move
397   if (type_of_piece(pc) == PAWN)
398   {
399       // Move direction must be compatible with pawn color
400       int direction = to - from;
401       if ((us == WHITE) != (direction > 0))
402           return false;
403
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)
407           return false;
408
409       // Proceed according to the square delta between the origin and
410       // destination squares.
411       switch (direction)
412       {
413       case DELTA_NW:
414       case DELTA_NE:
415       case DELTA_SW:
416       case DELTA_SE:
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)
420               return false;
421           break;
422
423       case DELTA_N:
424       case DELTA_S:
425       // Pawn push. The destination square must be empty.
426           if (!pos.square_is_empty(to))
427               return false;
428           break;
429
430       case DELTA_NN:
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))
437           return false;
438           break;
439
440       case DELTA_SS:
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))
447               return false;
448           break;
449
450       default:
451           return false;
452       }
453   }
454   else if (!bit_is_set(pos.attacks_from(pc, from), to))
455       return false;
456
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);
459 }
460
461
462 namespace {
463
464   template<SquareDelta Delta>
465   inline Bitboard move_pawns(Bitboard p) {
466
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;
470   }
471
472   template<MoveType Type, SquareDelta Delta>
473   inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
474
475     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
476
477     Bitboard b;
478     Square to;
479
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);
483     return mlist;
484   }
485
486   template<Color Us, MoveType Type, SquareDelta Delta>
487   inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
488
489     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
490
491     Bitboard b;
492     Square to;
493
494     // Promotions and under-promotions, both captures and non-captures
495     b = move_pawns<Delta>(pawnsOn7) & target;
496
497     if (Delta != DELTA_N && Delta != DELTA_S)
498         b &= ~TFileABB;
499
500     while (b)
501     {
502         to = pop_1st_bit(&b);
503
504         if (Type == MV_CAPTURE || Type == MV_EVASION)
505             (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
506
507         if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
508         {
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);
512         }
513
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
520     }
521     return mlist;
522   }
523
524   template<Color Us, MoveType Type>
525   MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
526
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);
535
536     Square to;
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));
541
542     // Pre-calculate pawn pushes before changing emptySquares definition
543     if (Type != MV_CAPTURE)
544     {
545         emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
546         pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
547     }
548
549     if (Type == MV_EVASION)
550     {
551         emptySquares &= target; // Only blocking squares
552         enemyPieces  &= target; // Capture only the checker piece
553     }
554
555     // Promotions and underpromotions
556     if (pawnsOn7)
557     {
558         if (Type == MV_CAPTURE)
559             emptySquares = pos.empty_squares();
560
561         pawns &= ~TRank7BB;
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);
565     }
566
567     // Standard captures
568     if (Type == MV_CAPTURE || Type == MV_EVASION)
569     {
570         mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
571         mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
572     }
573
574     // Single and double pawn pushes
575     if (Type != MV_CAPTURE)
576     {
577         b1 = pawnPushes & emptySquares;
578         b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
579
580         if (Type == MV_CHECK)
581         {
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);
585
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
590             {
591                 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
592                 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
593
594                 b1 |= dc1;
595                 b2 |= dc2;
596             }
597         }
598         SERIALIZE_MOVES_D(b1, -TDELTA_N);
599         SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
600     }
601
602     // En passant captures
603     if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
604     {
605         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
606         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
607
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))
612             return mlist;
613
614         b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
615
616         assert(b1 != EmptyBoardBB);
617
618         while (b1)
619         {
620             to = pop_1st_bit(&b1);
621             (*mlist++).move = make_ep_move(to, pos.ep_square());
622         }
623     }
624     return mlist;
625   }
626
627   template<CastlingSide Side>
628   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) {
629
630     Color them = opposite_color(us);
631     Square ksq = pos.king_square(us);
632
633     assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
634
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);
638     Square s;
639     bool illegal = false;
640
641     assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
642
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)))
647             illegal = true;
648
649     for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
650         if (s != ksq && s != rsq && pos.square_is_occupied(s))
651             illegal = true;
652
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)))
657         illegal = true;
658
659     if (!illegal)
660         (*mlist++).move = make_castle_move(ksq, rsq);
661
662     return mlist;
663   }
664
665 } // namespace