]> git.sesse.net Git - stockfish/blob - src/movegen.cpp
b130247bea5f151cd810ec3e73ddd8b1a1204a83
[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*);
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       mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
197       mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist);
198   }
199
200   return mlist;
201 }
202
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);
207
208
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.
211 template<>
212 MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) {
213
214   assert(pos.is_ok());
215   assert(!pos.is_check());
216
217   Bitboard b, dc;
218   Square from;
219   Color us = pos.side_to_move();
220   Square ksq = pos.king_square(opposite_color(us));
221
222   assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
223
224   // Discovered non-capture checks
225   b = dc = pos.discovered_check_candidates(us);
226
227   while (b)
228   {
229      from = pop_1st_bit(&b);
230      switch (pos.type_of_piece_on(from))
231      {
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;
238      }
239   }
240
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);
247 }
248
249
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.
252 template<>
253 MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
254
255   assert(pos.is_ok());
256   assert(pos.is_check());
257
258   Bitboard b, target;
259   Square from, checksq;
260   int checkersCnt = 0;
261   Color us = pos.side_to_move();
262   Square ksq = pos.king_square(us);
263   Bitboard checkers = pos.checkers();
264   Bitboard sliderAttacks = EmptyBoardBB;
265
266   assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
267   assert(checkers);
268
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.
272   b = checkers;
273   do
274   {
275       checkersCnt++;
276       checksq = pop_1st_bit(&b);
277
278       assert(pos.color_of_piece_on(checksq) == opposite_color(us));
279
280       switch (pos.type_of_piece_on(checksq))
281       {
282       case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
283       case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
284       case QUEEN:
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);
289           else
290               sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
291       default:
292           break;
293       }
294   } while (b);
295
296   // Generate evasions for king, capture and non capture moves
297   b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
298   from = ksq;
299   SERIALIZE_MOVES(b);
300
301   // Generate evasions for other pieces only if not double check
302   if (checkersCnt > 1)
303       return mlist;
304
305   // Find squares where a blocking evasion or a capture of the
306   // checker piece is possible.
307   target = squares_between(checksq, ksq) | checkers;
308
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);
314 }
315
316
317 /// generate<MV_LEGAL / MV_PSEUDO_LEGAL> computes a complete list of legal
318 /// or pseudo-legal moves in the current position.
319 template<>
320 inline MoveStack* generate<MV_PSEUDO_LEGAL>(const Position& pos, MoveStack* mlist) {
321
322   assert(pos.is_ok());
323
324   return pos.is_check() ? generate<MV_EVASION>(pos, mlist)
325                         : generate<MV_NON_EVASION>(pos, mlist);
326 }
327
328 template<>
329 MoveStack* generate<MV_LEGAL>(const Position& pos, MoveStack* mlist) {
330
331   assert(pos.is_ok());
332
333   MoveStack *last, *cur = mlist;
334   Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
335
336   last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
337
338   // Remove illegal moves from the list
339   while (cur != last)
340       if (pos.pl_move_is_legal(cur->move, pinned))
341           cur++;
342       else
343           cur->move = (--last)->move;
344
345   return last;
346 }
347
348
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.
352
353 bool move_is_legal(const Position& pos, const Move m) {
354
355   MoveStack mlist[MOVES_MAX];
356   MoveStack *cur, *last = generate<MV_PSEUDO_LEGAL>(pos, mlist);
357
358    for (cur = mlist; cur != last; cur++)
359       if (cur->move == m)
360           return pos.pl_move_is_legal(m, pos.pinned_pieces(pos.side_to_move()));
361
362   return false;
363 }
364
365
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.
368
369 bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
370
371   assert(pos.is_ok());
372   assert(pinned == pos.pinned_pieces(pos.side_to_move()));
373
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);
379
380   // Use a slower but simpler function for uncommon cases
381   if (move_is_special(m))
382       return move_is_legal(pos, m);
383
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)
387       return false;
388
389   // The destination square cannot be occupied by a friendly piece
390   if (pos.color_of_piece_on(to) == us)
391       return false;
392
393   // Handle the special case of a pawn move
394   if (type_of_piece(pc) == PAWN)
395   {
396       // Move direction must be compatible with pawn color
397       int direction = to - from;
398       if ((us == WHITE) != (direction > 0))
399           return false;
400
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)
404           return false;
405
406       // Proceed according to the square delta between the origin and
407       // destination squares.
408       switch (direction)
409       {
410       case DELTA_NW:
411       case DELTA_NE:
412       case DELTA_SW:
413       case DELTA_SE:
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)
417               return false;
418           break;
419
420       case DELTA_N:
421       case DELTA_S:
422       // Pawn push. The destination square must be empty.
423           if (!pos.square_is_empty(to))
424               return false;
425           break;
426
427       case DELTA_NN:
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))
434           return false;
435           break;
436
437       case DELTA_SS:
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))
444               return false;
445           break;
446
447       default:
448           return false;
449       }
450   }
451   else if (!bit_is_set(pos.attacks_from(pc, from), to))
452       return false;
453
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);
456 }
457
458
459 namespace {
460
461   template<SquareDelta Delta>
462   inline Bitboard move_pawns(Bitboard p) {
463
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;
467   }
468
469   template<MoveType Type, SquareDelta Delta>
470   inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
471
472     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
473
474     Bitboard b;
475     Square to;
476
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);
480     return mlist;
481   }
482
483   template<Color Us, MoveType Type, SquareDelta Delta>
484   inline MoveStack* generate_promotions(const Position& pos, MoveStack* mlist, Bitboard pawnsOn7, Bitboard target) {
485
486     const Bitboard TFileABB = (Delta == DELTA_NE || Delta == DELTA_SE ? FileABB : FileHBB);
487
488     Bitboard b;
489     Square to;
490
491     // Promotions and under-promotions, both captures and non-captures
492     b = move_pawns<Delta>(pawnsOn7) & target;
493
494     if (Delta != DELTA_N && Delta != DELTA_S)
495         b &= ~TFileABB;
496
497     while (b)
498     {
499         to = pop_1st_bit(&b);
500
501         if (Type == MV_CAPTURE || Type == MV_EVASION)
502             (*mlist++).move = make_promotion_move(to - Delta, to, QUEEN);
503
504         if (Type == MV_NON_CAPTURE || Type == MV_EVASION)
505         {
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);
509         }
510
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
517     }
518     return mlist;
519   }
520
521   template<Color Us, MoveType Type>
522   MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) {
523
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);
532
533     Square to;
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));
538
539     // Pre-calculate pawn pushes before changing emptySquares definition
540     if (Type != MV_CAPTURE)
541     {
542         emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares());
543         pawnPushes = move_pawns<TDELTA_N>(pawns & ~TRank7BB) & emptySquares;
544     }
545
546     if (Type == MV_EVASION)
547     {
548         emptySquares &= target; // Only blocking squares
549         enemyPieces  &= target; // Capture only the checker piece
550     }
551
552     // Promotions and underpromotions
553     if (pawnsOn7)
554     {
555         if (Type == MV_CAPTURE)
556             emptySquares = pos.empty_squares();
557
558         pawns &= ~TRank7BB;
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);
562     }
563
564     // Standard captures
565     if (Type == MV_CAPTURE || Type == MV_EVASION)
566     {
567         mlist = generate_pawn_captures<Type, TDELTA_NE>(mlist, pawns, enemyPieces);
568         mlist = generate_pawn_captures<Type, TDELTA_NW>(mlist, pawns, enemyPieces);
569     }
570
571     // Single and double pawn pushes
572     if (Type != MV_CAPTURE)
573     {
574         b1 = pawnPushes & emptySquares;
575         b2 = move_pawns<TDELTA_N>(pawnPushes & TRank3BB) & emptySquares;
576
577         if (Type == MV_CHECK)
578         {
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);
582
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
587             {
588                 dc1 = move_pawns<TDELTA_N>(pawns & target & ~file_bb(ksq)) & emptySquares;
589                 dc2 = move_pawns<TDELTA_N>(dc1 & TRank3BB) & emptySquares;
590
591                 b1 |= dc1;
592                 b2 |= dc2;
593             }
594         }
595         SERIALIZE_MOVES_D(b1, -TDELTA_N);
596         SERIALIZE_MOVES_D(b2, -TDELTA_N -TDELTA_N);
597     }
598
599     // En passant captures
600     if ((Type == MV_CAPTURE || Type == MV_EVASION) && pos.ep_square() != SQ_NONE)
601     {
602         assert(Us != WHITE || square_rank(pos.ep_square()) == RANK_6);
603         assert(Us != BLACK || square_rank(pos.ep_square()) == RANK_3);
604
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))
609             return mlist;
610
611         b1 = pawns & pos.attacks_from<PAWN>(pos.ep_square(), Them);
612
613         assert(b1 != EmptyBoardBB);
614
615         while (b1)
616         {
617             to = pop_1st_bit(&b1);
618             (*mlist++).move = make_ep_move(to, pos.ep_square());
619         }
620     }
621     return mlist;
622   }
623
624   template<CastlingSide Side>
625   MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist) {
626
627     Color us = pos.side_to_move();
628
629     if (  (Side == KING_SIDE  && pos.can_castle_kingside(us))
630         ||(Side == QUEEN_SIDE && pos.can_castle_queenside(us)))
631     {
632         Color them = opposite_color(us);
633         Square ksq = pos.king_square(us);
634
635         assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
636
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);
640         Square s;
641         bool illegal = false;
642
643         assert(pos.piece_on(rsq) == piece_of_color_and_type(us, ROOK));
644
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)))
649                 illegal = true;
650
651         for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)
652             if (s != ksq && s != rsq && pos.square_is_occupied(s))
653                 illegal = true;
654
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)))
659             illegal = true;
660
661         if (!illegal)
662             (*mlist++).move = make_castle_move(ksq, rsq);
663     }
664     return mlist;
665   }
666 }