2 * moves.c - Move generation and checking
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
10 * Enhancements Copyright 2005 Alessandro Scotti
12 * The following terms apply to Digital Equipment Corporation's copyright
14 * ------------------------------------------------------------------------
17 * Permission to use, copy, modify, and distribute this software and its
18 * documentation for any purpose and without fee is hereby granted,
19 * provided that the above copyright notice appear in all copies and that
20 * both that copyright notice and this permission notice appear in
21 * supporting documentation, and that the name of Digital not be
22 * used in advertising or publicity pertaining to distribution of the
23 * software without specific, written prior permission.
25 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 * ------------------------------------------------------------------------
34 * The following terms apply to the enhanced version of XBoard
35 * distributed by the Free Software Foundation:
36 * ------------------------------------------------------------------------
38 * GNU XBoard is free software: you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, either version 3 of the License, or (at
41 * your option) any later version.
43 * GNU XBoard is distributed in the hope that it will be useful, but
44 * WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 * General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program. If not, see http://www.gnu.org/licenses/. *
51 *------------------------------------------------------------------------
52 ** See the file ChangeLog for a revision history. */
59 #else /* not HAVE_STRING_H */
61 #endif /* not HAVE_STRING_H */
67 int WhitePiece P((ChessSquare));
68 int BlackPiece P((ChessSquare));
69 int SameColor P((ChessSquare, ChessSquare));
70 int PosFlags(int index);
72 extern signed char initialRights[BOARD_FILES]; /* [HGM] all rights enabled, set in InitPosition */
78 return (int) piece >= (int) WhitePawn && (int) piece < (int) BlackPawn;
84 return (int) piece >= (int) BlackPawn && (int) piece < (int) EmptySquare;
87 int SameColor(piece1, piece2)
88 ChessSquare piece1, piece2;
90 return ((int) piece1 >= (int) WhitePawn && /* [HGM] can be > King ! */
91 (int) piece1 < (int) BlackPawn &&
92 (int) piece2 >= (int) WhitePawn &&
93 (int) piece2 < (int) BlackPawn)
94 || ((int) piece1 >= (int) BlackPawn &&
95 (int) piece1 < (int) EmptySquare &&
96 (int) piece2 >= (int) BlackPawn &&
97 (int) piece2 < (int) EmptySquare);
100 char pieceToChar[] = {
101 'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
102 'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
103 'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
104 'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
106 char pieceNickName[EmptySquare];
111 if((int)p < 0 || (int)p >= (int)EmptySquare) return('x'); /* [HGM] for safety */
112 return pieceToChar[(int) p];
115 int PieceToNumber(p) /* [HGM] holdings: count piece type, ignoring non-participating piece types */
119 ChessSquare start = (int)p >= (int)BlackPawn ? BlackPawn : WhitePawn;
121 while(start++ != p) if(pieceToChar[(int)start-1] != '.') i++;
125 ChessSquare CharToPiece(c)
129 for(i=0; i< (int) EmptySquare; i++)
130 if(pieceNickName[i] == c) return (ChessSquare) i;
131 for(i=0; i< (int) EmptySquare; i++)
132 if(pieceToChar[i] == c) return (ChessSquare) i;
136 void CopyBoard(to, from)
141 for (i = 0; i < BOARD_HEIGHT; i++)
142 for (j = 0; j < BOARD_WIDTH; j++)
143 to[i][j] = from[i][j];
144 for (j = 0; j < BOARD_FILES-1; j++) // [HGM] gamestate: copy castling rights and ep status
145 to[CASTLING][j] = from[CASTLING][j];
146 to[HOLDINGS_SET] = 0; // flag used in ICS play
149 int CompareBoards(board1, board2)
150 Board board1, board2;
154 for (i = 0; i < BOARD_HEIGHT; i++)
155 for (j = 0; j < BOARD_WIDTH; j++) {
156 if (board1[i][j] != board2[i][j])
163 /* Call callback once for each pseudo-legal move in the given
164 position, except castling moves. A move is pseudo-legal if it is
165 legal, or if it would be legal except that it leaves the king in
166 check. In the arguments, epfile is EP_NONE if the previous move
167 was not a double pawn push, or the file 0..7 if it was, or
168 EP_UNKNOWN if we don't know and want to allow all e.p. captures.
169 Promotion moves generated are to Queen only.
171 void GenPseudoLegal(board, flags, callback, closure)
174 MoveCallback callback;
178 int i, j, d, s, fs, rs, rt, ft, m;
179 int epfile = (signed char)board[EP_STATUS]; // [HGM] gamestate: extract ep status from board
180 int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
182 for (rf = 0; rf < BOARD_HEIGHT; rf++)
183 for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
185 int rookRange = 1000;
187 if (flags & F_WHITE_ON_MOVE) {
188 if (!WhitePiece(board[rf][ff])) continue;
190 if (!BlackPiece(board[rf][ff])) continue;
192 m = 0; piece = board[rf][ff];
193 if(PieceToChar(piece) == '~')
194 piece = (ChessSquare) ( DEMOTED piece );
195 if(gameInfo.variant == VariantShogi)
196 piece = (ChessSquare) ( SHOGI piece );
199 /* case EmptySquare: [HGM] this is nonsense, and conflicts with Shogi cases */
201 /* can't happen ([HGM] except for faries...) */
205 if(gameInfo.variant == VariantXiangqi) {
206 /* [HGM] capture and move straight ahead in Xiangqi */
207 if (rf < BOARD_HEIGHT-1 &&
208 !SameColor(board[rf][ff], board[rf + 1][ff]) ) {
209 callback(board, flags, NormalMove,
210 rf, ff, rf + 1, ff, closure);
212 /* and move sideways when across the river */
213 for (s = -1; s <= 1; s += 2) {
214 if (rf >= BOARD_HEIGHT>>1 &&
215 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
216 !WhitePiece(board[rf][ff+s]) ) {
217 callback(board, flags, NormalMove,
218 rf, ff, rf, ff+s, closure);
223 if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
224 callback(board, flags,
225 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
226 rf, ff, rf + 1, ff, closure);
228 if (rf == 1 && board[2][ff] == EmptySquare &&
229 gameInfo.variant != VariantShatranj && /* [HGM] */
230 gameInfo.variant != VariantCourier && /* [HGM] */
231 board[3][ff] == EmptySquare ) {
232 callback(board, flags, NormalMove,
233 rf, ff, 3, ff, closure);
235 for (s = -1; s <= 1; s += 2) {
236 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
237 ((flags & F_KRIEGSPIEL_CAPTURE) ||
238 BlackPiece(board[rf + 1][ff + s]))) {
239 callback(board, flags,
240 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
241 rf, ff, rf + 1, ff + s, closure);
243 if (rf == BOARD_HEIGHT-4) {
244 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
245 (epfile == ff + s || epfile == EP_UNKNOWN) &&
246 board[BOARD_HEIGHT-4][ff + s] == BlackPawn &&
247 board[BOARD_HEIGHT-3][ff + s] == EmptySquare) {
248 callback(board, flags, WhiteCapturesEnPassant,
249 rf, ff, 5, ff + s, closure);
256 if(gameInfo.variant == VariantXiangqi) {
257 /* [HGM] capture straight ahead in Xiangqi */
258 if (rf > 0 && !SameColor(board[rf][ff], board[rf - 1][ff]) ) {
259 callback(board, flags, NormalMove,
260 rf, ff, rf - 1, ff, closure);
262 /* and move sideways when across the river */
263 for (s = -1; s <= 1; s += 2) {
264 if (rf < BOARD_HEIGHT>>1 &&
265 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
266 !BlackPiece(board[rf][ff+s]) ) {
267 callback(board, flags, NormalMove,
268 rf, ff, rf, ff+s, closure);
273 if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
274 callback(board, flags,
275 rf <= promoRank ? BlackPromotion : NormalMove,
276 rf, ff, rf - 1, ff, closure);
278 if (rf == BOARD_HEIGHT-2 && board[BOARD_HEIGHT-3][ff] == EmptySquare &&
279 gameInfo.variant != VariantShatranj && /* [HGM] */
280 gameInfo.variant != VariantCourier && /* [HGM] */
281 board[BOARD_HEIGHT-4][ff] == EmptySquare) {
282 callback(board, flags, NormalMove,
283 rf, ff, BOARD_HEIGHT-4, ff, closure);
285 for (s = -1; s <= 1; s += 2) {
286 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
287 ((flags & F_KRIEGSPIEL_CAPTURE) ||
288 WhitePiece(board[rf - 1][ff + s]))) {
289 callback(board, flags,
290 rf <= promoRank ? BlackPromotion : NormalMove,
291 rf, ff, rf - 1, ff + s, closure);
294 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
295 (epfile == ff + s || epfile == EP_UNKNOWN) &&
296 board[3][ff + s] == WhitePawn &&
297 board[2][ff + s] == EmptySquare) {
298 callback(board, flags, BlackCapturesEnPassant,
299 rf, ff, 2, ff + s, closure);
310 for (i = -1; i <= 1; i += 2)
311 for (j = -1; j <= 1; j += 2)
312 for (s = 1; s <= 2; s++) {
315 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
316 && ( gameInfo.variant != VariantXiangqi || board[rf+i*(s-1)][ff+j*(2-s)] == EmptySquare)
317 && !SameColor(board[rf][ff], board[rt][ft]))
318 callback(board, flags, NormalMove,
319 rf, ff, rt, ft, closure);
323 case SHOGI WhiteKnight:
324 for (s = -1; s <= 1; s += 2) {
325 if (rf < BOARD_HEIGHT-2 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
326 !SameColor(board[rf][ff], board[rf + 2][ff + s])) {
327 callback(board, flags, NormalMove,
328 rf, ff, rf + 2, ff + s, closure);
333 case SHOGI BlackKnight:
334 for (s = -1; s <= 1; s += 2) {
335 if (rf > 1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
336 !SameColor(board[rf][ff], board[rf - 2][ff + s])) {
337 callback(board, flags, NormalMove,
338 rf, ff, rf - 2, ff + s, closure);
345 for (d = 0; d <= 1; d++)
346 for (s = -1; s <= 1; s += 2) {
349 rt = rf + (i * s) * d;
350 ft = ff + (i * s) * (1 - d);
351 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
352 if (m == 0 && board[rt][ft] == EmptySquare)
353 callback(board, flags, NormalMove,
354 rf, ff, rt, ft, closure);
355 if (m == 1 && board[rt][ft] != EmptySquare &&
356 !SameColor(board[rf][ff], board[rt][ft]) )
357 callback(board, flags, NormalMove,
358 rf, ff, rt, ft, closure);
359 if (board[rt][ft] != EmptySquare && m++) break;
364 /* Gold General (and all its promoted versions) . First do the */
365 /* diagonal forward steps, then proceed as normal Wazir */
366 case SHOGI WhiteWazir:
367 case SHOGI (PROMOTED WhitePawn):
368 case SHOGI (PROMOTED WhiteKnight):
369 case SHOGI (PROMOTED WhiteQueen):
370 case SHOGI (PROMOTED WhiteFerz):
371 for (s = -1; s <= 1; s += 2) {
372 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
373 !SameColor(board[rf][ff], board[rf + 1][ff + s])) {
374 callback(board, flags, NormalMove,
375 rf, ff, rf + 1, ff + s, closure);
380 case SHOGI BlackWazir:
381 case SHOGI (PROMOTED BlackPawn):
382 case SHOGI (PROMOTED BlackKnight):
383 case SHOGI (PROMOTED BlackQueen):
384 case SHOGI (PROMOTED BlackFerz):
385 for (s = -1; s <= 1; s += 2) {
386 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
387 !SameColor(board[rf][ff], board[rf - 1][ff + s])) {
388 callback(board, flags, NormalMove,
389 rf, ff, rf - 1, ff + s, closure);
396 for (d = 0; d <= 1; d++)
397 for (s = -1; s <= 1; s += 2) {
399 ft = ff + s * (1 - d);
400 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
401 && !SameColor(board[rf][ff], board[rt][ft]) &&
402 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
403 callback(board, flags, NormalMove,
404 rf, ff, rt, ft, closure);
410 /* [HGM] support Shatranj pieces */
411 for (rs = -1; rs <= 1; rs += 2)
412 for (fs = -1; fs <= 1; fs += 2) {
415 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
416 && ( gameInfo.variant != VariantXiangqi ||
417 board[rf+rs][ff+fs] == EmptySquare && (2*rf < BOARD_HEIGHT) == (2*rt < BOARD_HEIGHT) )
419 && !SameColor(board[rf][ff], board[rt][ft]))
420 callback(board, flags, NormalMove,
421 rf, ff, rt, ft, closure);
422 if(gameInfo.variant == VariantShatranj && gameInfo.variant == VariantCourier) continue; // classical Alfil
423 rt = rf + rs; // in unknown variant we assume Modern Elephant, which can also do one step
425 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
426 && !SameColor(board[rf][ff], board[rt][ft]))
427 callback(board, flags, NormalMove,
428 rf, ff, rt, ft, closure);
430 if(gameInfo.variant == VariantSpartan)
431 for(fs = -1; fs <= 1; fs += 2) {
433 if (!(ft < BOARD_LEFT || ft >= BOARD_RGHT) && board[rf][ft] == EmptySquare)
434 callback(board, flags, NormalMove, rf, ff, rf, ft, closure);
438 /* Make Dragon-Horse also do Dababba moves outside Shogi, for better disambiguation in variant Fairy */
441 for (d = 0; d <= 1; d++) // Dababba moves that Rook cannot do
442 for (s = -2; s <= 2; s += 4) {
444 ft = ff + s * (1 - d);
445 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
446 if (SameColor(board[rf][ff], board[rt][ft])) continue;
447 callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
450 /* Shogi Dragon Horse has to continue with Wazir after Bishop */
451 case SHOGI WhiteCardinal:
452 case SHOGI BlackCardinal:
455 /* Capablanca Archbishop continues as Knight */
460 /* Shogi Bishops are ordinary Bishops */
461 case SHOGI WhiteBishop:
462 case SHOGI BlackBishop:
465 for (rs = -1; rs <= 1; rs += 2)
466 for (fs = -1; fs <= 1; fs += 2)
470 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
471 if (SameColor(board[rf][ff], board[rt][ft])) break;
472 callback(board, flags, NormalMove,
473 rf, ff, rt, ft, closure);
474 if (board[rt][ft] != EmptySquare) break;
476 if(m==1) goto mounted;
477 if(m==2) goto finishGold;
478 /* Bishop falls through */
481 /* Shogi Lance is unlike anything, and asymmetric at that */
482 case SHOGI WhiteQueen:
486 if (rt >= BOARD_HEIGHT) break;
487 if (SameColor(board[rf][ff], board[rt][ft])) break;
488 callback(board, flags, NormalMove,
489 rf, ff, rt, ft, closure);
490 if (board[rt][ft] != EmptySquare) break;
494 case SHOGI BlackQueen:
499 if (SameColor(board[rf][ff], board[rt][ft])) break;
500 callback(board, flags, NormalMove,
501 rf, ff, rt, ft, closure);
502 if (board[rt][ft] != EmptySquare) break;
506 /* Make Dragon-King Dababba & Rook-like outside Shogi, for better disambiguation in variant Fairy */
509 for (d = 0; d <= 1; d++) // Dababba moves that Rook cannot do
510 for (s = -2; s <= 2; s += 4) {
512 ft = ff + s * (1 - d);
513 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT || board[rf+rt>>1][ff+ft>>1] == EmptySquare) continue;
514 if (SameColor(board[rf][ff], board[rt][ft])) continue;
515 callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
517 if(gameInfo.variant == VariantSpartan) rookRange = 2; // in Spartan Chess restrict range to modern Dababba
520 /* Shogi Dragon King has to continue as Ferz after Rook moves */
521 case SHOGI WhiteDragon:
522 case SHOGI BlackDragon:
525 /* Capablanca Chancellor sets flag to continue as Knight */
529 m += (gameInfo.variant == VariantSpartan); // in Spartan Chess Chancellor is used for Dragon King.
531 /* Shogi Rooks are ordinary Rooks */
532 case SHOGI WhiteRook:
533 case SHOGI BlackRook:
537 for (d = 0; d <= 1; d++)
538 for (s = -1; s <= 1; s += 2)
540 rt = rf + (i * s) * d;
541 ft = ff + (i * s) * (1 - d);
542 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
543 if (SameColor(board[rf][ff], board[rt][ft])) break;
544 callback(board, flags, NormalMove,
545 rf, ff, rt, ft, closure);
546 if (board[rt][ft] != EmptySquare || i == rookRange) break;
548 if(m==1) goto mounted;
549 if(m==2) goto finishSilver;
554 for (rs = -1; rs <= 1; rs++)
555 for (fs = -1; fs <= 1; fs++) {
556 if (rs == 0 && fs == 0) continue;
560 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
561 if (SameColor(board[rf][ff], board[rt][ft])) break;
562 callback(board, flags, NormalMove,
563 rf, ff, rt, ft, closure);
564 if (board[rt][ft] != EmptySquare) break;
569 /* Shogi Pawn and Silver General: first the Pawn move, */
570 /* then the General continues like a Ferz */
572 if(gameInfo.variant != VariantMakruk) goto commoner;
573 case SHOGI WhitePawn:
574 case SHOGI WhiteFerz:
575 if (rf < BOARD_HEIGHT-1 &&
576 !SameColor(board[rf][ff], board[rf + 1][ff]) )
577 callback(board, flags, NormalMove,
578 rf, ff, rf + 1, ff, closure);
579 if(piece != SHOGI WhitePawn) goto finishSilver;
583 if(gameInfo.variant != VariantMakruk) goto commoner;
584 case SHOGI BlackPawn:
585 case SHOGI BlackFerz:
587 !SameColor(board[rf][ff], board[rf - 1][ff]) )
588 callback(board, flags, NormalMove,
589 rf, ff, rf - 1, ff, closure);
590 if(piece == SHOGI BlackPawn) break;
595 /* [HGM] support Shatranj pieces */
596 for (rs = -1; rs <= 1; rs += 2)
597 for (fs = -1; fs <= 1; fs += 2) {
600 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
601 if (!SameColor(board[rf][ff], board[rt][ft]) &&
602 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
603 callback(board, flags, NormalMove,
604 rf, ff, rt, ft, closure);
610 m++; // [HGM] superchess: use for Centaur
612 case SHOGI WhiteKing:
613 case SHOGI BlackKing:
617 for (i = -1; i <= 1; i++)
618 for (j = -1; j <= 1; j++) {
619 if (i == 0 && j == 0) continue;
622 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
623 if (SameColor(board[rf][ff], board[rt][ft])) continue;
624 callback(board, flags, NormalMove,
625 rf, ff, rt, ft, closure);
627 if(m==1) goto mounted;
630 case WhiteNightrider:
631 case BlackNightrider:
632 for (i = -1; i <= 1; i += 2)
633 for (j = -1; j <= 1; j += 2)
634 for (s = 1; s <= 2; s++) { int k;
638 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
639 if (SameColor(board[rf][ff], board[rt][ft])) break;
640 callback(board, flags, NormalMove,
641 rf, ff, rt, ft, closure);
642 if (board[rt][ft] != EmptySquare) break;
648 /* First do Bishop,then continue like Chancellor */
649 for (rs = -1; rs <= 1; rs += 2)
650 for (fs = -1; fs <= 1; fs += 2)
654 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
655 if (SameColor(board[rf][ff], board[rt][ft])) break;
656 callback(board, flags, NormalMove,
657 rf, ff, rt, ft, closure);
658 if (board[rt][ft] != EmptySquare) break;
663 // Use Lance as Berolina / Spartan Pawn.
665 if(gameInfo.variant == VariantSuper) goto Amazon;
666 if (rf < BOARD_HEIGHT-1 && BlackPiece(board[rf + 1][ff]))
667 callback(board, flags,
668 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
669 rf, ff, rf + 1, ff, closure);
670 for (s = -1; s <= 1; s += 2) {
671 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT && board[rf + 1][ff + s] == EmptySquare)
672 callback(board, flags,
673 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
674 rf, ff, rf + 1, ff + s, closure);
675 if (rf == 1 && ff + 2*s >= BOARD_LEFT && ff + 2*s < BOARD_RGHT && board[3][ff + 2*s] == EmptySquare )
676 callback(board, flags, NormalMove, rf, ff, 3, ff + 2*s, closure);
681 if(gameInfo.variant == VariantSuper) goto Amazon;
682 if (rf > 0 && WhitePiece(board[rf - 1][ff]))
683 callback(board, flags,
684 rf <= promoRank ? BlackPromotion : NormalMove,
685 rf, ff, rf - 1, ff, closure);
686 for (s = -1; s <= 1; s += 2) {
687 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT && board[rf - 1][ff + s] == EmptySquare)
688 callback(board, flags,
689 rf <= promoRank ? BlackPromotion : NormalMove,
690 rf, ff, rf - 1, ff + s, closure);
691 if (rf == BOARD_HEIGHT-2 && ff + 2*s >= BOARD_LEFT && ff + 2*s < BOARD_RGHT && board[rf-2][ff + 2*s] == EmptySquare )
692 callback(board, flags, NormalMove, rf, ff, rf-2, ff + 2*s, closure);
696 case WhiteFalcon: // [HGM] wild: for wildcards, self-capture symbolizes move to anywhere
700 callback(board, flags, NormalMove, rf, ff, rf, ff, closure);
713 extern void GenLegalCallback P((Board board, int flags, ChessMove kind,
714 int rf, int ff, int rt, int ft,
717 void GenLegalCallback(board, flags, kind, rf, ff, rt, ft, closure)
724 register GenLegalClosure *cl = (GenLegalClosure *) closure;
726 if (!(flags & F_IGNORE_CHECK) ) {
727 int check, promo = (gameInfo.variant == VariantSpartan && kind == BlackPromotion);
728 if(promo) board[rf][ff] = BlackKing; // [HGM] spartan: promote to King before check-test
729 check = CheckTest(board, flags, rf, ff, rt, ft,
730 kind == WhiteCapturesEnPassant ||
731 kind == BlackCapturesEnPassant);
732 if(promo) board[rf][ff] = BlackLance;
735 if (flags & F_ATOMIC_CAPTURE) {
736 if (board[rt][ft] != EmptySquare ||
737 kind == WhiteCapturesEnPassant || kind == BlackCapturesEnPassant) {
739 ChessSquare king = (flags & F_WHITE_ON_MOVE) ? WhiteKing : BlackKing;
740 if (board[rf][ff] == king) return;
741 for (r = rt-1; r <= rt+1; r++) {
742 for (f = ft-1; f <= ft+1; f++) {
743 if (r >= 0 && r < BOARD_HEIGHT && f >= BOARD_LEFT && f < BOARD_RGHT &&
744 board[r][f] == king) return;
749 cl->cb(board, flags, kind, rf, ff, rt, ft, cl->cl);
756 int captures; // [HGM] losers
757 } LegalityTestClosure;
760 /* Like GenPseudoLegal, but (1) include castling moves, (2) unless
761 F_IGNORE_CHECK is set in the flags, omit moves that would leave the
762 king in check, and (3) if F_ATOMIC_CAPTURE is set in the flags, omit
763 moves that would destroy your own king. The CASTLE_OK flags are
764 true if castling is not yet ruled out by a move of the king or
765 rook. Return TRUE if the player on move is currently in check and
766 F_IGNORE_CHECK is not set. [HGM] add castlingRights parameter */
767 int GenLegal(board, flags, callback, closure)
770 MoveCallback callback;
774 int ff, ft, k, left, right, swap;
775 int ignoreCheck = (flags & F_IGNORE_CHECK) != 0;
776 ChessSquare wKing = WhiteKing, bKing = BlackKing, *castlingRights = board[CASTLING];
780 GenPseudoLegal(board, flags, GenLegalCallback, (VOIDSTAR) &cl);
783 CheckTest(board, flags, -1, -1, -1, -1, FALSE)) return TRUE;
785 /* Generate castling moves */
786 if(gameInfo.variant == VariantKnightmate) { /* [HGM] Knightmate */
787 wKing = WhiteUnicorn; bKing = BlackUnicorn;
790 for (ff = BOARD_WIDTH>>1; ff >= (BOARD_WIDTH-1)>>1; ff-- /*ics wild 1*/) {
791 if ((flags & F_WHITE_ON_MOVE) &&
792 (flags & F_WHITE_KCASTLE_OK) &&
793 board[0][ff] == wKing &&
794 board[0][ff + 1] == EmptySquare &&
795 board[0][ff + 2] == EmptySquare &&
796 board[0][BOARD_RGHT-3] == EmptySquare &&
797 board[0][BOARD_RGHT-2] == EmptySquare &&
798 board[0][BOARD_RGHT-1] == WhiteRook &&
799 castlingRights[0] != NoRights && /* [HGM] check rights */
800 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
802 (!CheckTest(board, flags, 0, ff, 0, ff + 1, FALSE) &&
803 !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-3, FALSE) &&
804 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-2, FALSE)) &&
805 !CheckTest(board, flags, 0, ff, 0, ff + 2, FALSE)))) {
807 callback(board, flags,
808 ff==BOARD_WIDTH>>1 ? WhiteKingSideCastle : WhiteKingSideCastleWild,
809 0, ff, 0, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
811 if ((flags & F_WHITE_ON_MOVE) &&
812 (flags & F_WHITE_QCASTLE_OK) &&
813 board[0][ff] == wKing &&
814 board[0][ff - 1] == EmptySquare &&
815 board[0][ff - 2] == EmptySquare &&
816 board[0][BOARD_LEFT+2] == EmptySquare &&
817 board[0][BOARD_LEFT+1] == EmptySquare &&
818 board[0][BOARD_LEFT+0] == WhiteRook &&
819 castlingRights[1] != NoRights && /* [HGM] check rights */
820 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
822 (!CheckTest(board, flags, 0, ff, 0, ff - 1, FALSE) &&
823 !CheckTest(board, flags, 0, ff, 0, BOARD_LEFT+3, FALSE) &&
824 !CheckTest(board, flags, 0, ff, 0, ff - 2, FALSE)))) {
826 callback(board, flags,
827 ff==BOARD_WIDTH>>1 ? WhiteQueenSideCastle : WhiteQueenSideCastleWild,
828 0, ff, 0, ff - ((gameInfo.boardWidth+2)>>2), closure);
830 if (!(flags & F_WHITE_ON_MOVE) &&
831 (flags & F_BLACK_KCASTLE_OK) &&
832 board[BOARD_HEIGHT-1][ff] == bKing &&
833 board[BOARD_HEIGHT-1][ff + 1] == EmptySquare &&
834 board[BOARD_HEIGHT-1][ff + 2] == EmptySquare &&
835 board[BOARD_HEIGHT-1][BOARD_RGHT-3] == EmptySquare &&
836 board[BOARD_HEIGHT-1][BOARD_RGHT-2] == EmptySquare &&
837 board[BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook &&
838 castlingRights[3] != NoRights && /* [HGM] check rights */
839 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
841 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 1, FALSE) &&
842 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-3, FALSE) &&
843 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-2, FALSE)) &&
844 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 2, FALSE)))) {
846 callback(board, flags,
847 ff==BOARD_WIDTH>>1 ? BlackKingSideCastle : BlackKingSideCastleWild,
848 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
850 if (!(flags & F_WHITE_ON_MOVE) &&
851 (flags & F_BLACK_QCASTLE_OK) &&
852 board[BOARD_HEIGHT-1][ff] == bKing &&
853 board[BOARD_HEIGHT-1][ff - 1] == EmptySquare &&
854 board[BOARD_HEIGHT-1][ff - 2] == EmptySquare &&
855 board[BOARD_HEIGHT-1][BOARD_LEFT+2] == EmptySquare &&
856 board[BOARD_HEIGHT-1][BOARD_LEFT+1] == EmptySquare &&
857 board[BOARD_HEIGHT-1][BOARD_LEFT+0] == BlackRook &&
858 castlingRights[4] != NoRights && /* [HGM] check rights */
859 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
861 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 1, FALSE) &&
862 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_LEFT+3, FALSE) &&
863 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 2, FALSE)))) {
865 callback(board, flags,
866 ff==BOARD_WIDTH>>1 ? BlackQueenSideCastle : BlackQueenSideCastleWild,
867 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - ((gameInfo.boardWidth+2)>>2), closure);
871 if((swap = gameInfo.variant == VariantSChess) || flags & F_FRC_TYPE_CASTLING) {
873 /* generate all potential FRC castling moves (KxR), ignoring flags */
874 /* [HGM] test if the Rooks we find have castling rights */
875 /* In S-Chess we generate RxK for allowed castlings, for gating at Rook square */
878 if ((flags & F_WHITE_ON_MOVE) != 0) {
879 ff = castlingRights[2]; /* King file if we have any rights */
880 if(ff != NoRights && board[0][ff] == WhiteKing) {
881 if (appData.debugMode) {
882 fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
883 castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
885 ft = castlingRights[0]; /* Rook file if we have H-side rights */
887 right = BOARD_RGHT-2;
888 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
889 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
890 if(k != ft && board[0][k] != EmptySquare) ft = NoRights;
891 for(k=left; k<right && ft != NoRights; k++) /* then if not checked */
892 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = NoRights;
893 if(ft != NoRights && board[0][ft] == WhiteRook)
894 callback(board, flags, WhiteHSideCastleFR, 0, swap ? ft : ff, 0, swap ? ff : ft, closure);
896 ft = castlingRights[1]; /* Rook file if we have A-side rights */
899 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
900 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
901 if(k != ft && board[0][k] != EmptySquare) ft = NoRights;
902 if(ff > BOARD_LEFT+2)
903 for(k=left+1; k<=right && ft != NoRights; k++) /* then if not checked */
904 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = NoRights;
905 if(ft != NoRights && board[0][ft] == WhiteRook)
906 callback(board, flags, WhiteASideCastleFR, 0, swap ? ft : ff, 0, swap ? ff : ft, closure);
909 ff = castlingRights[5]; /* King file if we have any rights */
910 if(ff != NoRights && board[BOARD_HEIGHT-1][ff] == BlackKing) {
911 ft = castlingRights[3]; /* Rook file if we have H-side rights */
913 right = BOARD_RGHT-2;
914 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
915 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
916 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = NoRights;
917 for(k=left; k<right && ft != NoRights; k++) /* then if not checked */
918 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = NoRights;
919 if(ft != NoRights && board[BOARD_HEIGHT-1][ft] == BlackRook)
920 callback(board, flags, BlackHSideCastleFR, BOARD_HEIGHT-1, swap ? ft : ff, BOARD_HEIGHT-1, swap ? ff : ft, closure);
922 ft = castlingRights[4]; /* Rook file if we have A-side rights */
925 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
926 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
927 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = NoRights;
928 if(ff > BOARD_LEFT+2)
929 for(k=left+1; k<=right && ft != NoRights; k++) /* then if not checked */
930 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = NoRights;
931 if(ft != NoRights && board[BOARD_HEIGHT-1][ft] == BlackRook)
932 callback(board, flags, BlackASideCastleFR, BOARD_HEIGHT-1, swap ? ft : ff, BOARD_HEIGHT-1, swap ? ff : ft, closure);
948 extern void CheckTestCallback P((Board board, int flags, ChessMove kind,
949 int rf, int ff, int rt, int ft,
953 void CheckTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
960 register CheckTestClosure *cl = (CheckTestClosure *) closure;
962 if (rt == cl->rking && ft == cl->fking) cl->check++;
966 /* If the player on move were to move from (rf, ff) to (rt, ft), would
967 he leave himself in check? Or if rf == -1, is the player on move
968 in check now? enPassant must be TRUE if the indicated move is an
969 e.p. capture. The possibility of castling out of a check along the
970 back rank is not accounted for (i.e., we still return nonzero), as
971 this is illegal anyway. Return value is the number of times the
973 int CheckTest(board, flags, rf, ff, rt, ft, enPassant)
976 int rf, ff, rt, ft, enPassant;
979 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
980 ChessSquare captured = EmptySquare;
981 /* Suppress warnings on uninitialized variables */
983 if(gameInfo.variant == VariantXiangqi)
984 king = flags & F_WHITE_ON_MOVE ? WhiteWazir : BlackWazir;
985 if(gameInfo.variant == VariantKnightmate)
986 king = flags & F_WHITE_ON_MOVE ? WhiteUnicorn : BlackUnicorn;
990 captured = board[rf][ft];
991 board[rf][ft] = EmptySquare;
993 captured = board[rt][ft];
995 board[rt][ft] = board[rf][ff];
996 board[rf][ff] = EmptySquare;
997 } else board[rt][ft] = ff; // [HGM] drop
999 /* For compatibility with ICS wild 9, we scan the board in the
1000 order a1, a2, a3, ... b1, b2, ..., h8 to find the first king,
1001 and we test only whether that one is in check. */
1002 for (cl.fking = BOARD_LEFT+0; cl.fking < BOARD_RGHT; cl.fking++)
1003 for (cl.rking = 0; cl.rking < BOARD_HEIGHT; cl.rking++) {
1004 if (board[cl.rking][cl.fking] == king) {
1005 if(gameInfo.variant == VariantXiangqi) {
1006 /* [HGM] In Xiangqi opposing Kings means check as well */
1008 dir = (king >= BlackPawn) ? -1 : 1;
1009 for( i=cl.rking+dir; i>=0 && i<BOARD_HEIGHT &&
1010 board[i][cl.fking] == EmptySquare; i+=dir );
1011 if(i>=0 && i<BOARD_HEIGHT &&
1012 board[i][cl.fking] == (dir>0 ? BlackWazir : WhiteWazir) )
1016 GenPseudoLegal(board, flags ^ F_WHITE_ON_MOVE, CheckTestCallback, (VOIDSTAR) &cl);
1017 if(gameInfo.variant != VariantSpartan || cl.check == 0) // in Spartan Chess go on to test if other King is checked too
1018 goto undo_move; /* 2-level break */
1025 board[rf][ff] = board[rt][ft];
1027 board[rf][ft] = captured;
1028 board[rt][ft] = EmptySquare;
1030 board[rt][ft] = captured;
1032 } else board[rt][ft] = EmptySquare; // [HGM] drop
1034 return cl.fking < BOARD_RGHT ? cl.check : 1000; // [HGM] atomic: return 1000 if we have no king
1037 ChessMove LegalDrop(board, flags, piece, rt, ft)
1042 { // [HGM] put drop legality testing in separate routine for clarity
1044 if(appData.debugMode) fprintf(debugFP, "LegalDrop: %d @ %d,%d)\n", piece, ft, rt);
1045 if(board[rt][ft] != EmptySquare) return ImpossibleMove; // must drop to empty square
1046 n = PieceToNumber(piece);
1047 if(gameInfo.holdingsWidth == 0 || (flags & F_WHITE_ON_MOVE ? board[n][BOARD_WIDTH-1] : board[BOARD_HEIGHT-1-n][0]) != piece)
1048 return ImpossibleMove; // piece not available
1049 if(gameInfo.variant == VariantShogi) { // in Shogi lots of drops are forbidden!
1050 if((piece == WhitePawn || piece == WhiteQueen) && rt == BOARD_HEIGHT-1 ||
1051 (piece == BlackPawn || piece == BlackQueen) && rt == 0 ||
1052 piece == WhiteKnight && rt > BOARD_HEIGHT-3 ||
1053 piece == BlackKnight && rt < 2 ) return IllegalMove; // e.g. where dropped piece has no moves
1054 if(piece == WhitePawn || piece == BlackPawn) {
1056 for(r=1; r<BOARD_HEIGHT-1; r++)
1057 if(board[r][ft] == piece) return IllegalMove; // or there already is a Pawn in file
1058 // should still test if we mate with this Pawn
1060 } else if(gameInfo.variant == VariantSChess) { // only back-rank drops
1061 if (rt != (piece < BlackPawn ? 0 : BOARD_HEIGHT-1)) return IllegalMove;
1063 if( (piece == WhitePawn || piece == BlackPawn) &&
1064 (rt == 0 || rt == BOARD_HEIGHT -1 ) )
1065 return IllegalMove; /* no pawn drops on 1st/8th */
1067 if(appData.debugMode) fprintf(debugFP, "LegalDrop: %d @ %d,%d)\n", piece, ft, rt);
1068 if (!(flags & F_IGNORE_CHECK) &&
1069 CheckTest(board, flags, DROP_RANK, piece, rt, ft, FALSE) ) return IllegalMove;
1070 return flags & F_WHITE_ON_MOVE ? WhiteDrop : BlackDrop;
1073 extern void LegalityTestCallback P((Board board, int flags, ChessMove kind,
1074 int rf, int ff, int rt, int ft,
1077 void LegalityTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1084 register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
1086 // if (appData.debugMode) {
1087 // fprintf(debugFP, "Legality test: %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1089 if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
1090 cl->captures++; // [HGM] losers: count legal captures
1091 if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
1095 ChessMove LegalityTest(board, flags, rf, ff, rt, ft, promoChar)
1098 int rf, ff, rt, ft, promoChar;
1100 LegalityTestClosure cl; ChessSquare piece, *castlingRights = board[CASTLING];
1102 if(rf == DROP_RANK) return LegalDrop(board, flags, ff, rt, ft);
1103 piece = board[rf][ff];
1105 if (appData.debugMode) {
1107 for(i=0; i<6; i++) fprintf(debugFP, "%d ", castlingRights[i]);
1108 fprintf(debugFP, "Legality test? %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1110 /* [HGM] Cobra and Falcon are wildcard pieces; consider all their moves legal */
1111 /* (perhaps we should disallow moves that obviously leave us in check?) */
1112 if(piece == WhiteFalcon || piece == BlackFalcon ||
1113 piece == WhiteCobra || piece == BlackCobra)
1114 return CheckTest(board, flags, rf, ff, rt, ft, FALSE) ? IllegalMove : NormalMove;
1120 cl.kind = IllegalMove;
1121 cl.captures = 0; // [HGM] losers: prepare to count legal captures.
1122 GenLegal(board, flags, LegalityTestCallback, (VOIDSTAR) &cl);
1123 if((flags & F_MANDATORY_CAPTURE) && cl.captures && board[rt][ft] == EmptySquare
1124 && cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant)
1125 return(IllegalMove); // [HGM] losers: if there are legal captures, non-capts are illegal
1127 if(promoChar == 'x') promoChar = NULLCHAR; // [HGM] is this ever the case?
1128 if(gameInfo.variant == VariantSChess && promoChar && promoChar != '=' && board[rf][ff] != WhitePawn && board[rf][ff] != BlackPawn) {
1129 if(board[rf][ff] < BlackPawn) { // white
1130 if(rf != 0) return IllegalMove; // must be on back rank
1131 if(board[PieceToNumber(CharToPiece(ToUpper(promoChar)))][BOARD_WIDTH-2] == 0) return ImpossibleMove;// must be in stock
1133 if(rf != BOARD_HEIGHT-1) return IllegalMove;
1134 if(board[BOARD_HEIGHT-1-PieceToNumber(CharToPiece(ToLower(promoChar)))][1] == 0) return ImpossibleMove;
1137 if(gameInfo.variant == VariantShogi) {
1138 /* [HGM] Shogi promotions. '=' means defer */
1139 if(rf != DROP_RANK && cl.kind == NormalMove) {
1140 ChessSquare piece = board[rf][ff];
1142 if(promoChar == PieceToChar(BlackQueen)) promoChar = NULLCHAR; /* [HGM] Kludge */
1143 if(promoChar == 'd' && (piece == WhiteRook || piece == BlackRook) ||
1144 promoChar == 'h' && (piece == WhiteBishop || piece == BlackBishop) ||
1145 promoChar == 'g' && (piece <= WhiteFerz || piece <= BlackFerz && piece >= BlackPawn) )
1146 promoChar = '+'; // allowed ICS notations
1147 if(appData.debugMode)fprintf(debugFP,"SHOGI promoChar = %c\n", promoChar ? promoChar : '-');
1148 if(promoChar != NULLCHAR && promoChar != '+' && promoChar != '=')
1149 return CharToPiece(promoChar) == EmptySquare ? ImpossibleMove : IllegalMove;
1150 else if(flags & F_WHITE_ON_MOVE) {
1151 if( (int) piece < (int) WhiteWazir &&
1152 (rf >= BOARD_HEIGHT*2/3 || rt >= BOARD_HEIGHT*2/3) ) {
1153 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1154 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1155 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotion;
1156 else /* promotion optional, default is defer */
1157 cl.kind = promoChar == '+' ? WhitePromotion : WhiteNonPromotion;
1158 } else cl.kind = promoChar == '+' ? IllegalMove : NormalMove;
1160 if( (int) piece < (int) BlackWazir && (rf < BOARD_HEIGHT/3 || rt < BOARD_HEIGHT/3) ) {
1161 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1162 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1163 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotion;
1164 else /* promotion optional, default is defer */
1165 cl.kind = promoChar == '+' ? BlackPromotion : BlackNonPromotion;
1166 } else cl.kind = promoChar == '+' ? IllegalMove : NormalMove;
1170 if (promoChar != NULLCHAR) {
1171 if(promoChar == '=') cl.kind = IllegalMove; else // [HGM] shogi: no deferred promotion outside Shogi
1172 if (cl.kind == WhitePromotion || cl.kind == BlackPromotion) {
1173 ChessSquare piece = CharToPiece(flags & F_WHITE_ON_MOVE ? ToUpper(promoChar) : ToLower(promoChar));
1174 if(piece == EmptySquare)
1175 cl.kind = ImpossibleMove; // non-existing piece
1176 if(gameInfo.variant == VariantSpartan && cl.kind == BlackPromotion ) {
1177 if(promoChar != PieceToChar(BlackKing)) {
1178 if(CheckTest(board, flags, rf, ff, rt, ft, FALSE)) cl.kind = IllegalMove; // [HGM] spartan: only promotion to King was possible
1179 if(piece == BlackLance) cl.kind = ImpossibleMove;
1180 } else { // promotion to King allowed only if we do not haave two yet
1181 int r, f, kings = 0;
1182 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) kings += (board[r][f] == BlackKing);
1183 if(kings == 2) cl.kind = IllegalMove;
1185 } else if(piece == WhitePawn || piece == BlackPawn) cl.kind = ImpossibleMove; // cannot stay Pawn in any variant
1186 else if((piece == WhiteUnicorn || piece == BlackUnicorn) && gameInfo.variant == VariantKnightmate)
1187 cl.kind = IllegalMove; // promotion to Royal Knight not allowed
1188 else if((piece == WhiteKing || piece == BlackKing) && gameInfo.variant != VariantSuicide && gameInfo.variant != VariantGiveaway)
1189 cl.kind = IllegalMove; // promotion to King usually not allowed
1191 cl.kind = IllegalMove;
1201 extern void MateTestCallback P((Board board, int flags, ChessMove kind,
1202 int rf, int ff, int rt, int ft,
1205 void MateTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1212 register MateTestClosure *cl = (MateTestClosure *) closure;
1217 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */
1218 int MateTest(board, flags)
1223 int inCheck, r, f, myPieces=0, hisPieces=0, nrKing=0;
1224 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
1226 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
1227 // [HGM] losers: Count pieces and kings, to detect other unorthodox winning conditions
1228 nrKing += (board[r][f] == king); // stm has king
1229 if( board[r][f] != EmptySquare ) {
1230 if((int)board[r][f] <= (int)king && (int)board[r][f] >= (int)king - (int)WhiteKing + (int)WhitePawn)
1235 if(appData.debugMode) fprintf(debugFP, "MateTest: K=%d, my=%d, his=%d\n", nrKing, myPieces, hisPieces);
1236 switch(gameInfo.variant) { // [HGM] losers: extinction wins
1237 case VariantShatranj:
1238 if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
1242 if(nrKing == 0) return MT_NOKING;
1245 if(myPieces == 1) return MT_BARE;
1248 inCheck = GenLegal(board, flags, MateTestCallback, (VOIDSTAR) &cl);
1249 // [HGM] 3check: yet to do!
1251 return inCheck ? MT_CHECK : MT_NONE;
1253 if(gameInfo.holdingsWidth && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat) { // drop game
1254 int r, f, n, holdings = flags & F_WHITE_ON_MOVE ? BOARD_WIDTH-1 : 0;
1255 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) if(board[r][f] == EmptySquare) // all empty squares
1256 for(n=0; n<BOARD_HEIGHT; n++) // all pieces in hand
1257 if(board[n][holdings] != EmptySquare) {
1258 int moveType = LegalDrop(board, flags, board[n][holdings], r, f);
1259 if(moveType == WhiteDrop || moveType == BlackDrop) return (inCheck ? MT_CHECK : MT_NONE); // we have legal drop
1262 if(gameInfo.variant == VariantSuicide) // [HGM] losers: always stalemate, since no check, but result varies
1263 return myPieces == hisPieces ? MT_STALEMATE :
1264 myPieces > hisPieces ? MT_STAINMATE : MT_STEALMATE;
1265 else if(gameInfo.variant == VariantLosers) return inCheck ? MT_TRICKMATE : MT_STEALMATE;
1266 else if(gameInfo.variant == VariantGiveaway) return MT_STEALMATE; // no check exists, stalemated = win
1268 return inCheck ? MT_CHECKMATE
1269 : (gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj) ?
1270 MT_STAINMATE : MT_STALEMATE;
1275 extern void DisambiguateCallback P((Board board, int flags, ChessMove kind,
1276 int rf, int ff, int rt, int ft,
1279 void DisambiguateCallback(board, flags, kind, rf, ff, rt, ft, closure)
1286 register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
1287 int wildCard = FALSE; ChessSquare piece = board[rf][ff];
1289 // [HGM] wild: for wild-card pieces rt and rf are dummies
1290 if(piece == WhiteFalcon || piece == BlackFalcon ||
1291 piece == WhiteCobra || piece == BlackCobra)
1294 if ((cl->pieceIn == EmptySquare || cl->pieceIn == board[rf][ff]
1295 || PieceToChar(board[rf][ff]) == '~'
1296 && cl->pieceIn == (ChessSquare)(DEMOTED board[rf][ff])
1298 (cl->rfIn == -1 || cl->rfIn == rf) &&
1299 (cl->ffIn == -1 || cl->ffIn == ff) &&
1300 (cl->rtIn == -1 || cl->rtIn == rt || wildCard) &&
1301 (cl->ftIn == -1 || cl->ftIn == ft || wildCard)) {
1304 if(cl->count == 1 || board[rt][ft] != EmptySquare) {
1305 // [HGM] oneclick: if multiple moves, be sure we remember capture
1306 cl->piece = board[rf][ff];
1309 cl->rt = wildCard ? cl->rtIn : rt;
1310 cl->ft = wildCard ? cl->ftIn : ft;
1313 cl->captures += (board[rt][ft] != EmptySquare); // [HGM] oneclick: count captures
1317 void Disambiguate(board, flags, closure)
1320 DisambiguateClosure *closure;
1322 int illegal = 0; char c = closure->promoCharIn;
1324 closure->count = closure->captures = 0;
1325 closure->rf = closure->ff = closure->rt = closure->ft = 0;
1326 closure->kind = ImpossibleMove;
1327 if (appData.debugMode) {
1328 fprintf(debugFP, "Disambiguate in: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1329 closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
1330 closure->promoCharIn, closure->promoCharIn >= ' ' ? closure->promoCharIn : '-');
1332 GenLegal(board, flags, DisambiguateCallback, (VOIDSTAR) closure);
1333 if (closure->count == 0) {
1334 /* See if it's an illegal move due to check */
1336 GenLegal(board, flags|F_IGNORE_CHECK, DisambiguateCallback, (VOIDSTAR) closure);
1337 if (closure->count == 0) {
1338 /* No, it's not even that */
1339 if (appData.debugMode) { int i, j;
1340 for(i=BOARD_HEIGHT-1; i>=0; i--) {
1341 for(j=0; j<BOARD_WIDTH; j++)
1342 fprintf(debugFP, "%3d", (int) board[i][j]);
1343 fprintf(debugFP, "\n");
1350 if (c == 'x') c = NULLCHAR; // get rid of any 'x' (which should never happen?)
1351 if(gameInfo.variant == VariantSChess && c && c != '=' && closure->piece != WhitePawn && closure->piece != BlackPawn) {
1352 if(closure->piece < BlackPawn) { // white
1353 if(closure->rf != 0) closure->kind = IllegalMove; // must be on back rank
1354 if(board[PieceToNumber(CharToPiece(ToUpper(c)))][BOARD_WIDTH-2] == 0) closure->kind = ImpossibleMove;// must be in stock
1356 if(closure->rf != BOARD_HEIGHT-1) closure->kind = IllegalMove;
1357 if(board[BOARD_HEIGHT-1-PieceToNumber(CharToPiece(ToLower(c)))][1] == 0) closure->kind = ImpossibleMove;
1360 if(gameInfo.variant == VariantShogi) {
1361 /* [HGM] Shogi promotions. On input, '=' means defer, '+' promote. Afterwards, c is set to '+' for promotions, NULL other */
1362 if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
1363 ChessSquare piece = closure->piece;
1364 if (c == 'd' && (piece == WhiteRook || piece == BlackRook) ||
1365 c == 'h' && (piece == WhiteBishop || piece == BlackBishop) ||
1366 c == 'g' && (piece <= WhiteFerz || piece <= BlackFerz && piece >= BlackPawn) )
1367 c = '+'; // allowed ICS notations
1368 if(c != NULLCHAR && c != '+' && c != '=') closure->kind = IllegalMove; // otherwise specifying a piece is illegal
1369 else if(flags & F_WHITE_ON_MOVE) {
1370 if( (int) piece < (int) WhiteWazir &&
1371 (closure->rf >= BOARD_HEIGHT-(BOARD_HEIGHT/3) || closure->rt >= BOARD_HEIGHT-(BOARD_HEIGHT/3)) ) {
1372 if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
1373 piece == WhiteKnight && closure->rt > BOARD_HEIGHT-3) /* promotion mandatory */
1374 closure->kind = c == '=' ? IllegalMove : WhitePromotion;
1375 else /* promotion optional, default is defer */
1376 closure->kind = c == '+' ? WhitePromotion : WhiteNonPromotion;
1377 } else closure->kind = c == '+' ? IllegalMove : NormalMove;
1379 if( (int) piece < (int) BlackWazir && (closure->rf < BOARD_HEIGHT/3 || closure->rt < BOARD_HEIGHT/3) ) {
1380 if( (piece == BlackPawn || piece == BlackQueen) && closure->rt < 1 ||
1381 piece == BlackKnight && closure->rt < 2 ) /* promotion obligatory */
1382 closure->kind = c == '=' ? IllegalMove : BlackPromotion;
1383 else /* promotion optional, default is defer */
1384 closure->kind = c == '+' ? BlackPromotion : BlackNonPromotion;
1385 } else closure->kind = c == '+' ? IllegalMove : NormalMove;
1388 if(closure->kind == WhitePromotion || closure->kind == BlackPromotion) c = '+'; else
1389 if(closure->kind == WhiteNonPromotion || closure->kind == BlackNonPromotion) c = '=';
1391 if (closure->kind == WhitePromotion || closure->kind == BlackPromotion) {
1392 if(c == NULLCHAR) { // missing promoChar on mandatory promotion; use default for variant
1393 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
1394 c = PieceToChar(BlackFerz);
1395 else if(gameInfo.variant == VariantGreat)
1396 c = PieceToChar(BlackMan);
1398 c = PieceToChar(BlackQueen);
1399 } else if(c == '=') closure->kind = IllegalMove; // no deferral outside Shogi
1400 } else if (c != NULLCHAR) closure->kind = IllegalMove;
1402 closure->promoChar = ToLower(c); // this can be NULLCHAR! Note we keep original promoChar even if illegal.
1403 if(c != '+' && c != '=' && c != NULLCHAR && CharToPiece(flags & F_WHITE_ON_MOVE ? ToUpper(c) : ToLower(c)) == EmptySquare)
1404 closure->kind = ImpossibleMove; // but we cannot handle non-existing piece types!
1405 if (closure->count > 1) {
1406 closure->kind = AmbiguousMove;
1409 /* Note: If more than one illegal move matches, but no legal
1410 moves, we return IllegalMove, not AmbiguousMove. Caller
1411 can look at closure->count to detect this.
1413 closure->kind = IllegalMove;
1415 if (appData.debugMode) {
1416 fprintf(debugFP, "Disambiguate out: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1417 closure->piece,closure->ff,closure->rf,closure->ft,closure->rt,closure->promoChar,
1418 closure->promoChar >= ' ' ? closure->promoChar:'-');
1432 } CoordsToAlgebraicClosure;
1434 extern void CoordsToAlgebraicCallback P((Board board, int flags,
1435 ChessMove kind, int rf, int ff,
1436 int rt, int ft, VOIDSTAR closure));
1438 void CoordsToAlgebraicCallback(board, flags, kind, rf, ff, rt, ft, closure)
1445 register CoordsToAlgebraicClosure *cl =
1446 (CoordsToAlgebraicClosure *) closure;
1448 if ((rt == cl->rt && ft == cl->ft || rt == rf && ft == ff) && // [HGM] null move matches any toSquare
1449 (board[rf][ff] == cl->piece
1450 || PieceToChar(board[rf][ff]) == '~' &&
1451 (ChessSquare) (DEMOTED board[rf][ff]) == cl->piece)
1455 cl->kind = kind; /* this is the move we want */
1457 cl->file++; /* need file to rule out this move */
1461 cl->rank++; /* need rank to rule out this move */
1463 cl->either++; /* rank or file will rule out this move */
1469 /* Convert coordinates to normal algebraic notation.
1470 promoChar must be NULLCHAR or 'x' if not a promotion.
1472 ChessMove CoordsToAlgebraic(board, flags, rf, ff, rt, ft, promoChar, out)
1481 char *outp = out, c;
1482 CoordsToAlgebraicClosure cl;
1484 if (rf == DROP_RANK) {
1485 /* Bughouse piece drop */
1486 *outp++ = ToUpper(PieceToChar((ChessSquare) ff));
1491 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1493 return (flags & F_WHITE_ON_MOVE) ? WhiteDrop : BlackDrop;
1496 if (promoChar == 'x') promoChar = NULLCHAR;
1497 piece = board[rf][ff];
1498 if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
1500 if (appData.debugMode)
1501 fprintf(debugFP, "CoordsToAlgebraic, piece=%d (%d,%d)-(%d,%d) %c\n", (int)piece,ff,rf,ft,rt,promoChar >= ' ' ? promoChar : '-');
1505 kind = LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1506 if (kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1507 /* Keep short notation if move is illegal only because it
1508 leaves the player in check, but still return IllegalMove */
1509 kind = LegalityTest(board, flags|F_IGNORE_CHECK, rf, ff, rt, ft, promoChar);
1510 if (kind == IllegalMove) break;
1515 if (ff == ft && board[rt][ft] == EmptySquare) { /* [HGM] Xiangqi has straight noncapts! */
1516 /* Non-capture; use style "e5" */
1519 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1521 /* Capture; use style "exd5" */
1522 if(gameInfo.variant != VariantXiangqi || board[rt][ft] != EmptySquare )
1523 *outp++ = 'x'; /* [HGM] Xiangqi has sideway noncaptures across river! */
1527 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1529 /* Use promotion suffix style "=Q" */
1531 if (appData.debugMode)
1532 fprintf(debugFP, "movetype=%d, promochar=%d=%c\n", (int)kind, promoChar, promoChar >= ' ' ? promoChar : '-');
1533 if (promoChar != NULLCHAR) {
1534 if(gameInfo.variant == VariantShogi) {
1535 /* [HGM] ... but not in Shogi! */
1536 *outp++ = promoChar == '=' ? '=' : '+';
1539 *outp++ = ToUpper(promoChar);
1548 /* Fabien moved code: FRC castling first (if KxR), wild castling second */
1549 /* Code added by Tord: FRC castling. */
1550 if((piece == WhiteKing && board[rt][ft] == WhiteRook) ||
1551 (piece == BlackKing && board[rt][ft] == BlackRook)) {
1553 safeStrCpy(out, "O-O", MOVE_LEN);
1555 safeStrCpy(out, "O-O-O", MOVE_LEN);
1556 return LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1558 /* End of code added by Tord */
1559 /* Test for castling or ICS wild castling */
1560 /* Use style "O-O" (oh-oh) for PGN compatibility */
1561 else if (rf == rt &&
1562 rf == ((piece == WhiteKing) ? 0 : BOARD_HEIGHT-1) &&
1563 (ft - ff > 1 || ff - ft > 1) && // No castling if legal King move (on narrow boards!)
1564 ((ff == BOARD_WIDTH>>1 && (ft == BOARD_LEFT+2 || ft == BOARD_RGHT-2)) ||
1565 (ff == (BOARD_WIDTH-1)>>1 && (ft == BOARD_LEFT+1 || ft == BOARD_RGHT-3)))) {
1566 if(ft==BOARD_LEFT+1 || ft==BOARD_RGHT-2)
1567 snprintf(out, MOVE_LEN, "O-O%c%c", promoChar ? '/' : 0, ToUpper(promoChar));
1569 snprintf(out, MOVE_LEN, "O-O-O%c%c", promoChar ? '/' : 0, ToUpper(promoChar));
1571 /* This notation is always unambiguous, unless there are
1572 kings on both the d and e files, with "wild castling"
1573 possible for the king on the d file and normal castling
1574 possible for the other. ICS rules for wild 9
1575 effectively make castling illegal for either king in
1576 this situation. So I am not going to worry about it;
1577 I'll just generate an ambiguous O-O in this case.
1579 return LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1582 /* else fall through */
1590 cl.kind = IllegalMove;
1591 cl.rank = cl.file = cl.either = 0;
1592 GenLegal(board, flags, CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1594 if (cl.kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1595 /* Generate pretty moves for moving into check, but
1596 still return IllegalMove.
1598 GenLegal(board, flags|F_IGNORE_CHECK, CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1599 if (cl.kind == IllegalMove) break;
1600 cl.kind = IllegalMove;
1603 /* Style is "Nf3" or "Nxf7" if this is unambiguous,
1604 else "Ngf3" or "Ngxf7",
1605 else "N1f3" or "N5xf7",
1606 else "Ng1f3" or "Ng5xf7".
1608 c = PieceToChar(piece) ;
1609 if( c == '~' || c == '+') {
1610 /* [HGM] print nonexistent piece as its demoted version */
1611 piece = (ChessSquare) (DEMOTED piece);
1613 if(c=='+') *outp++ = c;
1614 *outp++ = ToUpper(PieceToChar(piece));
1616 if (cl.file || (cl.either && !cl.rank)) {
1622 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1625 if(board[rt][ft] != EmptySquare)
1631 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1632 if (gameInfo.variant == VariantShogi) {
1633 /* [HGM] in Shogi non-pawns can promote */
1634 *outp++ = promoChar; // Don't bother to correct move type, return value is never used!
1636 else if (gameInfo.variant != VariantSuper && promoChar &&
1637 (piece == WhiteLance || piece == BlackLance) ) { // Lance sometimes represents Pawn
1639 *outp++ = ToUpper(promoChar);
1641 else if (gameInfo.variant == VariantSChess && promoChar) { // and in S-Chess we have gating
1643 *outp++ = ToUpper(promoChar);
1649 /* Moving a nonexistent piece */
1653 /* Not a legal move, even ignoring check.
1654 If there was a piece on the from square,
1655 use style "Ng1g3" or "Ng1xe8";
1656 if there was a pawn or nothing (!),
1657 use style "g1g3" or "g1xe8". Use "x"
1658 if a piece was on the to square, even
1659 a piece of the same color.
1663 if (piece != EmptySquare && piece != WhitePawn && piece != BlackPawn) {
1665 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<=BOARD_RGHT; f++)
1666 c += (board[r][f] == piece); // count on-board pieces of given type
1667 *outp++ = ToUpper(PieceToChar(piece));
1669 if(c != 1) { // [HGM] but if there is only one piece of the mentioned type, no from-square, thank you!
1673 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1675 if (board[rt][ft] != EmptySquare) *outp++ = 'x';
1679 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1680 /* Use promotion suffix style "=Q" */
1681 if (promoChar != NULLCHAR && promoChar != 'x') {
1683 *outp++ = ToUpper(promoChar);
1690 // [HGM] XQ: the following code serves to detect perpetual chasing (Asian rules)
1699 // I guess the following variables logically belong in the closure too, but I was too lazy and used globals
1701 int preyStackPointer, chaseStackPointer;
1704 char rf, ff, rt, ft;
1714 // there are three new callbacks for use with GenLegal: for adding captures, deleting them, and finding a recapture
1716 extern void AtacksCallback P((Board board, int flags, ChessMove kind,
1717 int rf, int ff, int rt, int ft,
1720 void AttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1726 { // For adding captures that can lead to chase indictment to the chaseStack
1727 if(board[rt][ft] == EmptySquare) return; // non-capture
1728 if(board[rt][ft] == WhitePawn && rt < BOARD_HEIGHT/2) return; // Pawn before river can be chased
1729 if(board[rt][ft] == BlackPawn && rt >= BOARD_HEIGHT/2) return; // Pawn before river can be chased
1730 if(board[rf][ff] == WhitePawn || board[rf][ff] == BlackPawn) return; // Pawns are allowed to chase
1731 if(board[rf][ff] == WhiteWazir || board[rf][ff] == BlackWazir) return; // King is allowed to chase
1732 // move cannot be excluded from being a chase trivially (based on attacker and victim); save it on chaseStack
1733 chaseStack[chaseStackPointer].rf = rf;
1734 chaseStack[chaseStackPointer].ff = ff;
1735 chaseStack[chaseStackPointer].rt = rt;
1736 chaseStack[chaseStackPointer].ft = ft;
1737 chaseStackPointer++;
1740 extern void ExistingAtacksCallback P((Board board, int flags, ChessMove kind,
1741 int rf, int ff, int rt, int ft,
1744 void ExistingAttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1750 { // for removing pre-exsting captures from the chaseStack, to be left with newly created ones
1752 register ChaseClosure *cl = (ChaseClosure *) closure; //closure tells us the move played in the repeat loop
1754 if(board[rt][ft] == EmptySquare) return; // no capture
1755 if(rf == cl->rf && ff == cl->ff) { // attacks with same piece from new position are not considered new
1756 rf = cl->rt; ff = cl->ft; // doctor their fromSquare so they will be recognized in chaseStack
1758 // search move in chaseStack, and delete it if it occurred there (as we know now it is not a new capture)
1759 for(i=0; i<chaseStackPointer; i++) {
1760 if(chaseStack[i].rf == rf && chaseStack[i].ff == ff &&
1761 chaseStack[i].rt == rt && chaseStack[i].ft == ft ) {
1762 // move found on chaseStack, delete it by overwriting with move popped from top of chaseStack
1763 chaseStack[i] = chaseStack[--chaseStackPointer];
1769 extern void ProtectedCallback P((Board board, int flags, ChessMove kind,
1770 int rf, int ff, int rt, int ft,
1773 void ProtectedCallback(board, flags, kind, rf, ff, rt, ft, closure)
1779 { // for determining if a piece (given through the closure) is protected
1780 register ChaseClosure *cl = (ChaseClosure *) closure; // closure tells us where to recapture
1782 if(rt == cl->rt && ft == cl->ft) cl->recaptures++; // count legal recaptures to this square
1783 if(appData.debugMode && board[rt][ft] != EmptySquare)
1784 fprintf(debugFP, "try %c%c%c%c=%d\n", ff+AAA, rf+ONE,ft+AAA, rt+ONE, cl->recaptures);
1787 extern char moveList[MAX_MOVES][MOVE_LEN];
1789 int PerpetualChase(int first, int last)
1790 { // this routine detects if the side to move in the 'first' position is perpetually chasing (when not checking)
1793 ChessSquare captured;
1795 preyStackPointer = 0; // clear stack of chased pieces
1796 for(i=first; i<last; i+=2) { // for all positions with same side to move
1797 if(appData.debugMode) fprintf(debugFP, "judge position %i\n", i);
1798 chaseStackPointer = 0; // clear stack that is going to hold possible chases
1799 // determine all captures possible after the move, and put them on chaseStack
1800 GenLegal(boards[i+1], PosFlags(i), AttacksCallback, &cl);
1801 if(appData.debugMode) { int n;
1802 for(n=0; n<chaseStackPointer; n++)
1803 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1804 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1805 fprintf(debugFP, ": all capts\n");
1807 // determine all captures possible before the move, and delete them from chaseStack
1808 cl.rf = moveList[i][1]-ONE; // prepare closure to pass move that led from i to i+1
1809 cl.ff = moveList[i][0]-AAA+BOARD_LEFT;
1810 cl.rt = moveList[i][3]-ONE;
1811 cl.ft = moveList[i][2]-AAA+BOARD_LEFT;
1812 GenLegal(boards[i], PosFlags(i), ExistingAttacksCallback, &cl);
1813 if(appData.debugMode) { int n;
1814 for(n=0; n<chaseStackPointer; n++)
1815 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1816 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1817 fprintf(debugFP, ": new capts after %c%c%c%c\n", cl.ff+AAA, cl.rf+ONE, cl.ft+AAA, cl.rt+ONE);
1819 // chaseSack now contains all captures made possible by the move
1820 for(j=0; j<chaseStackPointer; j++) { // run through chaseStack to identify true chases
1821 int attacker = (int)boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1822 int victim = (int)boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1824 if(attacker >= (int) BlackPawn) attacker = BLACK_TO_WHITE attacker; // convert to white, as piecee type
1825 if(victim >= (int) BlackPawn) victim = BLACK_TO_WHITE victim;
1827 if((attacker == WhiteKnight || attacker == WhiteCannon) && victim == WhiteRook)
1828 continue; // C or H attack on R is always chase; leave on chaseStack
1830 if(attacker == victim) {
1831 if(LegalityTest(boards[i+1], PosFlags(i+1), chaseStack[j].rt,
1832 chaseStack[j].ft, chaseStack[j].rf, chaseStack[j].ff, NULLCHAR) == NormalMove) {
1833 // we can capture back with equal piece, so this is no chase but a sacrifice
1834 chaseStack[j] = chaseStack[--chaseStackPointer]; // delete the capture from the chaseStack
1835 j--; /* ! */ continue;
1840 // the attack is on a lower piece, or on a pinned or blocked equal one
1841 // test if the victim is protected by a true protector. First make the capture.
1842 captured = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1843 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1844 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = EmptySquare;
1845 // Then test if the opponent can recapture
1846 cl.recaptures = 0; // prepare closure to pass recapture square and count moves to it
1847 cl.rt = chaseStack[j].rt;
1848 cl.ft = chaseStack[j].ft;
1849 if(appData.debugMode) {
1850 fprintf(debugFP, "test if we can recapture %c%c\n", cl.ft+AAA, cl.rt+ONE);
1852 GenLegal(boards[i+1], PosFlags(i+1), ProtectedCallback, &cl); // try all moves
1853 // unmake the capture
1854 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1855 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = captured;
1856 // if a recapture was found, piece is protected, and we are not chasing it.
1857 if(cl.recaptures) { // attacked piece was defended by true protector, no chase
1858 chaseStack[j] = chaseStack[--chaseStackPointer]; // so delete from chaseStack
1862 // chaseStack now contains all moves that chased
1863 if(appData.debugMode) { int n;
1864 for(n=0; n<chaseStackPointer; n++)
1865 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1866 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1867 fprintf(debugFP, ": chases\n");
1869 if(i == first) { // copy all people chased by first move of repeat cycle to preyStack
1870 for(j=0; j<chaseStackPointer; j++) {
1871 preyStack[j].rank = chaseStack[j].rt;
1872 preyStack[j].file = chaseStack[j].ft;
1874 preyStackPointer = chaseStackPointer;
1877 for(j=0; j<chaseStackPointer; j++) {
1878 for(k=0; k<preyStackPointer; k++) {
1879 // search the victim of each chase move on the preyStack (first occurrence)
1880 if(chaseStack[j].ft == preyStack[k].file && chaseStack[j].rt == preyStack[k].rank ) {
1881 if(k < tail) break; // piece was already identified as still being chased
1882 preyStack[preyStackPointer] = preyStack[tail]; // move chased piece to bottom part of preyStack
1883 preyStack[tail] = preyStack[k]; // by swapping
1884 preyStack[k] = preyStack[preyStackPointer];
1890 preyStackPointer = tail; // keep bottom part of preyStack, popping pieces unchased on move i.
1891 if(appData.debugMode) { int n;
1892 for(n=0; n<preyStackPointer; n++)
1893 fprintf(debugFP, "%c%c ", preyStack[n].file+AAA, preyStack[n].rank+ONE);
1894 fprintf(debugFP, "always chased upto ply %d\n", i);
1896 // now adjust the location of the chased pieces according to opponent move
1897 for(j=0; j<preyStackPointer; j++) {
1898 if(preyStack[j].rank == moveList[i+1][1]-ONE &&
1899 preyStack[j].file == moveList[i+1][0]-AAA+BOARD_LEFT) {
1900 preyStack[j].rank = moveList[i+1][3]-ONE;
1901 preyStack[j].file = moveList[i+1][2]-AAA+BOARD_LEFT;
1906 return preyStackPointer; // if any piece was left on preyStack, it has been perpetually chased