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 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_SIZE]; /* [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 ChessSquare PromoPiece(moveType)
106 case WhitePromotionQueen:
108 case BlackPromotionQueen:
110 case WhitePromotionRook:
112 case BlackPromotionRook:
114 case WhitePromotionBishop:
116 case BlackPromotionBishop:
118 case WhitePromotionKnight:
120 case BlackPromotionKnight:
122 case WhitePromotionKing:
124 case BlackPromotionKing:
126 case WhitePromotionChancellor:
127 return WhiteMarshall;
128 case BlackPromotionChancellor:
129 return BlackMarshall;
130 case WhitePromotionArchbishop:
132 case BlackPromotionArchbishop:
134 case WhitePromotionCentaur:
136 case BlackPromotionCentaur:
141 char pieceToChar[] = {
142 'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
143 'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
144 'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
145 'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
151 if((int)p < 0 || (int)p >= (int)EmptySquare) return('x'); /* [HGM] for safety */
152 return pieceToChar[(int) p];
155 int PieceToNumber(p) /* [HGM] holdings: count piece type, ignoring non-participating piece types */
159 ChessSquare start = (int)p >= (int)BlackPawn ? BlackPawn : WhitePawn;
161 while(start++ != p) if(pieceToChar[(int)start-1] != '.') i++;
165 ChessSquare CharToPiece(c)
169 for(i=0; i< (int) EmptySquare; i++)
170 if(pieceToChar[i] == c) return (ChessSquare) i;
174 ChessMove PromoCharToMoveType(whiteOnMove, promoChar)
177 { /* [HGM] made dependent on CharToPiece to alow alternate piece letters */
178 ChessSquare piece = CharToPiece(whiteOnMove ? ToUpper(promoChar) : ToLower(promoChar) );
181 if(promoChar == NULLCHAR) return NormalMove;
185 return WhitePromotionQueen;
187 return WhitePromotionRook;
189 return WhitePromotionBishop;
191 return WhitePromotionKnight;
193 return WhitePromotionKing;
195 return WhitePromotionArchbishop;
197 return WhitePromotionChancellor;
199 return WhitePromotionCentaur;
201 return BlackPromotionQueen;
203 return BlackPromotionRook;
205 return BlackPromotionBishop;
207 return BlackPromotionKnight;
209 return BlackPromotionKing;
211 return BlackPromotionArchbishop;
213 return BlackPromotionChancellor;
215 return BlackPromotionCentaur;
217 // not all promotion implemented yet! Take Queen for those we don't know.
218 return (whiteOnMove ? WhitePromotionQueen : BlackPromotionQueen);
222 void CopyBoard(to, from)
227 for (i = 0; i < BOARD_HEIGHT; i++)
228 for (j = 0; j < BOARD_WIDTH; j++)
229 to[i][j] = from[i][j];
232 int CompareBoards(board1, board2)
233 Board board1, board2;
237 for (i = 0; i < BOARD_HEIGHT; i++)
238 for (j = 0; j < BOARD_WIDTH; j++) {
239 if (board1[i][j] != board2[i][j])
246 /* Call callback once for each pseudo-legal move in the given
247 position, except castling moves. A move is pseudo-legal if it is
248 legal, or if it would be legal except that it leaves the king in
249 check. In the arguments, epfile is EP_NONE if the previous move
250 was not a double pawn push, or the file 0..7 if it was, or
251 EP_UNKNOWN if we don't know and want to allow all e.p. captures.
252 Promotion moves generated are to Queen only.
254 void GenPseudoLegal(board, flags, epfile, callback, closure)
258 MoveCallback callback;
262 int i, j, d, s, fs, rs, rt, ft, m;
264 for (rf = 0; rf < BOARD_HEIGHT; rf++)
265 for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
268 if (flags & F_WHITE_ON_MOVE) {
269 if (!WhitePiece(board[rf][ff])) continue;
271 if (!BlackPiece(board[rf][ff])) continue;
273 m = 0; piece = board[rf][ff];
274 if(PieceToChar(piece) == '~')
275 piece = (ChessSquare) ( DEMOTED piece );
276 if(gameInfo.variant == VariantShogi)
277 piece = (ChessSquare) ( SHOGI piece );
280 /* case EmptySquare: [HGM] this is nonsense, and conflicts with Shogi cases */
282 /* can't happen ([HGM] except for faries...) */
286 if(gameInfo.variant == VariantXiangqi) {
287 /* [HGM] capture and move straight ahead in Xiangqi */
288 if (rf < BOARD_HEIGHT-1 &&
289 !SameColor(board[rf][ff], board[rf + 1][ff]) ) {
290 callback(board, flags, NormalMove,
291 rf, ff, rf + 1, ff, closure);
293 /* and move sideways when across the river */
294 for (s = -1; s <= 1; s += 2) {
295 if (rf >= BOARD_HEIGHT>>1 &&
296 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
297 !WhitePiece(board[rf][ff+s]) ) {
298 callback(board, flags, NormalMove,
299 rf, ff, rf, ff+s, closure);
304 if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
305 callback(board, flags,
306 rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
307 rf, ff, rf + 1, ff, closure);
309 if (rf == 1 && board[2][ff] == EmptySquare &&
310 gameInfo.variant != VariantShatranj && /* [HGM] */
311 gameInfo.variant != VariantCourier && /* [HGM] */
312 board[3][ff] == EmptySquare ) {
313 callback(board, flags, NormalMove,
314 rf, ff, 3, ff, closure);
316 for (s = -1; s <= 1; s += 2) {
317 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
318 ((flags & F_KRIEGSPIEL_CAPTURE) ||
319 BlackPiece(board[rf + 1][ff + s]))) {
320 callback(board, flags,
321 rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
322 rf, ff, rf + 1, ff + s, closure);
324 if (rf == BOARD_HEIGHT-4) {
325 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
326 (epfile == ff + s || epfile == EP_UNKNOWN) &&
327 board[BOARD_HEIGHT-4][ff + s] == BlackPawn &&
328 board[BOARD_HEIGHT-3][ff + s] == EmptySquare) {
329 callback(board, flags, WhiteCapturesEnPassant,
330 rf, ff, 5, ff + s, closure);
337 if(gameInfo.variant == VariantXiangqi) {
338 /* [HGM] capture straight ahead in Xiangqi */
339 if (rf > 0 && !SameColor(board[rf][ff], board[rf - 1][ff]) ) {
340 callback(board, flags, NormalMove,
341 rf, ff, rf - 1, ff, closure);
343 /* and move sideways when across the river */
344 for (s = -1; s <= 1; s += 2) {
345 if (rf < BOARD_HEIGHT>>1 &&
346 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
347 !BlackPiece(board[rf][ff+s]) ) {
348 callback(board, flags, NormalMove,
349 rf, ff, rf, ff+s, closure);
354 if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
355 callback(board, flags,
356 rf == 1 ? BlackPromotionQueen : NormalMove,
357 rf, ff, rf - 1, ff, closure);
359 if (rf == BOARD_HEIGHT-2 && board[BOARD_HEIGHT-3][ff] == EmptySquare &&
360 gameInfo.variant != VariantShatranj && /* [HGM] */
361 gameInfo.variant != VariantCourier && /* [HGM] */
362 board[BOARD_HEIGHT-4][ff] == EmptySquare) {
363 callback(board, flags, NormalMove,
364 rf, ff, BOARD_HEIGHT-4, ff, closure);
366 for (s = -1; s <= 1; s += 2) {
367 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
368 ((flags & F_KRIEGSPIEL_CAPTURE) ||
369 WhitePiece(board[rf - 1][ff + s]))) {
370 callback(board, flags,
371 rf == 1 ? BlackPromotionQueen : NormalMove,
372 rf, ff, rf - 1, ff + s, closure);
375 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
376 (epfile == ff + s || epfile == EP_UNKNOWN) &&
377 board[3][ff + s] == WhitePawn &&
378 board[2][ff + s] == EmptySquare) {
379 callback(board, flags, BlackCapturesEnPassant,
380 rf, ff, 2, ff + s, closure);
391 for (i = -1; i <= 1; i += 2)
392 for (j = -1; j <= 1; j += 2)
393 for (s = 1; s <= 2; s++) {
396 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
397 && ( gameInfo.variant != VariantXiangqi || board[rf+i*(s-1)][ff+j*(2-s)] == EmptySquare)
398 && !SameColor(board[rf][ff], board[rt][ft]))
399 callback(board, flags, NormalMove,
400 rf, ff, rt, ft, closure);
404 case SHOGI WhiteKnight:
405 for (s = -1; s <= 1; s += 2) {
406 if (rf < BOARD_HEIGHT-2 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
407 !SameColor(board[rf][ff], board[rf + 2][ff + s])) {
408 callback(board, flags, NormalMove,
409 rf, ff, rf + 2, ff + s, closure);
414 case SHOGI BlackKnight:
415 for (s = -1; s <= 1; s += 2) {
416 if (rf > 1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
417 !SameColor(board[rf][ff], board[rf - 2][ff + s])) {
418 callback(board, flags, NormalMove,
419 rf, ff, rf - 2, ff + s, closure);
426 for (d = 0; d <= 1; d++)
427 for (s = -1; s <= 1; s += 2) {
430 rt = rf + (i * s) * d;
431 ft = ff + (i * s) * (1 - d);
432 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
433 if (m == 0 && board[rt][ft] == EmptySquare)
434 callback(board, flags, NormalMove,
435 rf, ff, rt, ft, closure);
436 if (m == 1 && board[rt][ft] != EmptySquare &&
437 !SameColor(board[rf][ff], board[rt][ft]) )
438 callback(board, flags, NormalMove,
439 rf, ff, rt, ft, closure);
440 if (board[rt][ft] != EmptySquare && m++) break;
445 /* Gold General (and all its promoted versions) . First do the */
446 /* diagonal forward steps, then proceed as normal Wazir */
447 case SHOGI WhiteWazir:
448 case SHOGI (PROMOTED WhitePawn):
449 case SHOGI (PROMOTED WhiteKnight):
450 case SHOGI (PROMOTED WhiteQueen):
451 case SHOGI (PROMOTED WhiteFerz):
452 for (s = -1; s <= 1; s += 2) {
453 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
454 !SameColor(board[rf][ff], board[rf + 1][ff + s])) {
455 callback(board, flags, NormalMove,
456 rf, ff, rf + 1, ff + s, closure);
461 case SHOGI BlackWazir:
462 case SHOGI (PROMOTED BlackPawn):
463 case SHOGI (PROMOTED BlackKnight):
464 case SHOGI (PROMOTED BlackQueen):
465 case SHOGI (PROMOTED BlackFerz):
466 for (s = -1; s <= 1; s += 2) {
467 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
468 !SameColor(board[rf][ff], board[rf - 1][ff + s])) {
469 callback(board, flags, NormalMove,
470 rf, ff, rf - 1, ff + s, closure);
477 for (d = 0; d <= 1; d++)
478 for (s = -1; s <= 1; s += 2) {
480 ft = ff + s * (1 - d);
481 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
482 && !SameColor(board[rf][ff], board[rt][ft]) &&
483 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
484 callback(board, flags, NormalMove,
485 rf, ff, rt, ft, closure);
491 /* [HGM] support Shatranj pieces */
492 for (rs = -1; rs <= 1; rs += 2)
493 for (fs = -1; fs <= 1; fs += 2) {
496 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
497 && ( gameInfo.variant != VariantXiangqi ||
498 board[rf+rs][ff+fs] == EmptySquare && (2*rf < BOARD_HEIGHT) == (2*rt < BOARD_HEIGHT) )
500 && !SameColor(board[rf][ff], board[rt][ft]))
501 callback(board, flags, NormalMove,
502 rf, ff, rt, ft, closure);
506 /* Shogi Dragon Horse has to continue with Wazir after Bishop */
507 case SHOGI WhiteCardinal:
508 case SHOGI BlackCardinal:
511 /* Capablanca Archbishop continues as Knight */
516 /* Shogi Bishops are ordinary Bishops */
517 case SHOGI WhiteBishop:
518 case SHOGI BlackBishop:
521 for (rs = -1; rs <= 1; rs += 2)
522 for (fs = -1; fs <= 1; fs += 2)
526 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
527 if (SameColor(board[rf][ff], board[rt][ft])) break;
528 callback(board, flags, NormalMove,
529 rf, ff, rt, ft, closure);
530 if (board[rt][ft] != EmptySquare) break;
532 if(m==1) goto mounted;
533 if(m==2) goto finishGold;
534 /* Bishop falls through */
537 /* Shogi Lance is unlike anything, and asymmetric at that */
538 case SHOGI WhiteQueen:
542 if (rt >= BOARD_HEIGHT) 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) break;
550 case SHOGI BlackQueen:
555 if (SameColor(board[rf][ff], board[rt][ft])) break;
556 callback(board, flags, NormalMove,
557 rf, ff, rt, ft, closure);
558 if (board[rt][ft] != EmptySquare) break;
562 /* Shogi Dragon King has to continue as Ferz after Rook moves */
563 case SHOGI WhiteDragon:
564 case SHOGI BlackDragon:
567 /* Capablanca Chancellor sets flag to continue as Knight */
572 /* Shogi Rooks are ordinary Rooks */
573 case SHOGI WhiteRook:
574 case SHOGI BlackRook:
577 for (d = 0; d <= 1; d++)
578 for (s = -1; s <= 1; s += 2)
580 rt = rf + (i * s) * d;
581 ft = ff + (i * s) * (1 - d);
582 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
583 if (SameColor(board[rf][ff], board[rt][ft])) break;
584 callback(board, flags, NormalMove,
585 rf, ff, rt, ft, closure);
586 if (board[rt][ft] != EmptySquare) break;
588 if(m==1) goto mounted;
589 if(m==2) goto finishSilver;
594 for (rs = -1; rs <= 1; rs++)
595 for (fs = -1; fs <= 1; fs++) {
596 if (rs == 0 && fs == 0) continue;
600 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
601 if (SameColor(board[rf][ff], board[rt][ft])) break;
602 callback(board, flags, NormalMove,
603 rf, ff, rt, ft, closure);
604 if (board[rt][ft] != EmptySquare) break;
609 /* Shogi Pawn and Silver General: first the Pawn move, */
610 /* then the General continues like a Ferz */
611 case SHOGI WhitePawn:
612 case SHOGI WhiteFerz:
613 if (rf < BOARD_HEIGHT-1 &&
614 !SameColor(board[rf][ff], board[rf + 1][ff]) )
615 callback(board, flags, NormalMove,
616 rf, ff, rf + 1, ff, closure);
617 if(piece != SHOGI WhitePawn) goto finishSilver;
620 case SHOGI BlackPawn:
621 case SHOGI BlackFerz:
623 !SameColor(board[rf][ff], board[rf - 1][ff]) )
624 callback(board, flags, NormalMove,
625 rf, ff, rf - 1, ff, closure);
626 if(piece == SHOGI BlackPawn) break;
631 /* [HGM] support Shatranj pieces */
632 for (rs = -1; rs <= 1; rs += 2)
633 for (fs = -1; fs <= 1; fs += 2) {
636 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
637 if (!SameColor(board[rf][ff], board[rt][ft]) &&
638 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
639 callback(board, flags, NormalMove,
640 rf, ff, rt, ft, closure);
646 m++; // [HGM] superchess: use for Centaur
649 case SHOGI WhiteKing:
650 case SHOGI BlackKing:
654 for (i = -1; i <= 1; i++)
655 for (j = -1; j <= 1; j++) {
656 if (i == 0 && j == 0) continue;
659 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
660 if (SameColor(board[rf][ff], board[rt][ft])) continue;
661 callback(board, flags, NormalMove,
662 rf, ff, rt, ft, closure);
664 if(m==1) goto mounted;
667 case WhiteNightrider:
668 case BlackNightrider:
669 for (i = -1; i <= 1; i += 2)
670 for (j = -1; j <= 1; j += 2)
671 for (s = 1; s <= 2; s++) { int k;
675 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
676 if (SameColor(board[rf][ff], board[rt][ft])) break;
677 callback(board, flags, NormalMove,
678 rf, ff, rt, ft, closure);
679 if (board[rt][ft] != EmptySquare) break;
694 extern void GenLegalCallback P((Board board, int flags, ChessMove kind,
695 int rf, int ff, int rt, int ft,
698 void GenLegalCallback(board, flags, kind, rf, ff, rt, ft, closure)
705 register GenLegalClosure *cl = (GenLegalClosure *) closure;
707 if (!(flags & F_IGNORE_CHECK) &&
708 CheckTest(board, flags, rf, ff, rt, ft,
709 kind == WhiteCapturesEnPassant ||
710 kind == BlackCapturesEnPassant)) return;
711 if (flags & F_ATOMIC_CAPTURE) {
712 if (board[rt][ft] != EmptySquare ||
713 kind == WhiteCapturesEnPassant || kind == BlackCapturesEnPassant) {
715 ChessSquare king = (flags & F_WHITE_ON_MOVE) ? WhiteKing : BlackKing;
716 if (board[rf][ff] == king) return;
717 for (r = rt-1; r <= rt+1; r++) {
718 for (f = ft-1; f <= ft+1; f++) {
719 if (r >= 0 && r < BOARD_HEIGHT && f >= BOARD_LEFT && f < BOARD_RGHT &&
720 board[r][f] == king) return;
725 cl->cb(board, flags, kind, rf, ff, rt, ft, cl->cl);
732 int captures; // [HGM] losers
733 } LegalityTestClosure;
736 /* Like GenPseudoLegal, but (1) include castling moves, (2) unless
737 F_IGNORE_CHECK is set in the flags, omit moves that would leave the
738 king in check, and (3) if F_ATOMIC_CAPTURE is set in the flags, omit
739 moves that would destroy your own king. The CASTLE_OK flags are
740 true if castling is not yet ruled out by a move of the king or
741 rook. Return TRUE if the player on move is currently in check and
742 F_IGNORE_CHECK is not set. [HGM] add castlingRights parameter */
743 int GenLegal(board, flags, epfile, castlingRights, callback, closure)
747 char castlingRights[];
748 MoveCallback callback;
752 int ff, ft, k, left, right;
753 int ignoreCheck = (flags & F_IGNORE_CHECK) != 0;
754 ChessSquare wKing = WhiteKing, bKing = BlackKing;
758 GenPseudoLegal(board, flags, epfile, GenLegalCallback, (VOIDSTAR) &cl);
761 CheckTest(board, flags, -1, -1, -1, -1, FALSE)) return TRUE;
763 /* Generate castling moves */
764 if(gameInfo.variant == VariantKnightmate) { /* [HGM] Knightmate */
765 wKing = WhiteUnicorn; bKing = BlackUnicorn;
768 for (ff = BOARD_WIDTH>>1; ff >= (BOARD_WIDTH-1)>>1; ff-- /*ics wild 1*/) {
769 if ((flags & F_WHITE_ON_MOVE) &&
770 (flags & F_WHITE_KCASTLE_OK) &&
771 board[0][ff] == wKing &&
772 board[0][ff + 1] == EmptySquare &&
773 board[0][ff + 2] == EmptySquare &&
774 board[0][BOARD_RGHT-3] == EmptySquare &&
775 board[0][BOARD_RGHT-2] == EmptySquare &&
776 board[0][BOARD_RGHT-1] == WhiteRook &&
777 castlingRights[0] >= 0 && /* [HGM] check rights */
778 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
780 (!CheckTest(board, flags, 0, ff, 0, ff + 1, FALSE) &&
781 !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-3, FALSE) &&
782 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-2, FALSE)) &&
783 !CheckTest(board, flags, 0, ff, 0, ff + 2, FALSE)))) {
785 callback(board, flags,
786 ff==BOARD_WIDTH>>1 ? WhiteKingSideCastle : WhiteKingSideCastleWild,
787 0, ff, 0, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
789 if ((flags & F_WHITE_ON_MOVE) &&
790 (flags & F_WHITE_QCASTLE_OK) &&
791 board[0][ff] == wKing &&
792 board[0][ff - 1] == EmptySquare &&
793 board[0][ff - 2] == EmptySquare &&
794 board[0][BOARD_LEFT+2] == EmptySquare &&
795 board[0][BOARD_LEFT+1] == EmptySquare &&
796 board[0][BOARD_LEFT+0] == WhiteRook &&
797 castlingRights[1] >= 0 && /* [HGM] check rights */
798 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
800 (!CheckTest(board, flags, 0, ff, 0, ff - 1, FALSE) &&
801 !CheckTest(board, flags, 0, ff, 0, BOARD_LEFT+3, FALSE) &&
802 !CheckTest(board, flags, 0, ff, 0, ff - 2, FALSE)))) {
804 callback(board, flags,
805 ff==BOARD_WIDTH>>1 ? WhiteQueenSideCastle : WhiteQueenSideCastleWild,
806 0, ff, 0, ff - ((gameInfo.boardWidth+2)>>2), closure);
808 if (!(flags & F_WHITE_ON_MOVE) &&
809 (flags & F_BLACK_KCASTLE_OK) &&
810 board[BOARD_HEIGHT-1][ff] == bKing &&
811 board[BOARD_HEIGHT-1][ff + 1] == EmptySquare &&
812 board[BOARD_HEIGHT-1][ff + 2] == EmptySquare &&
813 board[BOARD_HEIGHT-1][BOARD_RGHT-3] == EmptySquare &&
814 board[BOARD_HEIGHT-1][BOARD_RGHT-2] == EmptySquare &&
815 board[BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook &&
816 castlingRights[3] >= 0 && /* [HGM] check rights */
817 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
819 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 1, FALSE) &&
820 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-3, FALSE) &&
821 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-2, FALSE)) &&
822 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 2, FALSE)))) {
824 callback(board, flags,
825 ff==BOARD_WIDTH>>1 ? BlackKingSideCastle : BlackKingSideCastleWild,
826 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
828 if (!(flags & F_WHITE_ON_MOVE) &&
829 (flags & F_BLACK_QCASTLE_OK) &&
830 board[BOARD_HEIGHT-1][ff] == bKing &&
831 board[BOARD_HEIGHT-1][ff - 1] == EmptySquare &&
832 board[BOARD_HEIGHT-1][ff - 2] == EmptySquare &&
833 board[BOARD_HEIGHT-1][BOARD_LEFT+2] == EmptySquare &&
834 board[BOARD_HEIGHT-1][BOARD_LEFT+1] == EmptySquare &&
835 board[BOARD_HEIGHT-1][BOARD_LEFT+0] == BlackRook &&
836 castlingRights[4] >= 0 && /* [HGM] check rights */
837 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
839 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 1, FALSE) &&
840 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_LEFT+3, FALSE) &&
841 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 2, FALSE)))) {
843 callback(board, flags,
844 ff==BOARD_WIDTH>>1 ? BlackQueenSideCastle : BlackQueenSideCastleWild,
845 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - ((gameInfo.boardWidth+2)>>2), closure);
849 if(gameInfo.variant == VariantFischeRandom) {
851 /* generate all potential FRC castling moves (KxR), ignoring flags */
852 /* [HGM] test if the Rooks we find have castling rights */
855 if ((flags & F_WHITE_ON_MOVE) != 0) {
856 ff = castlingRights[2]; /* King file if we have any rights */
857 if(ff > 0 && board[0][ff] == WhiteKing) {
858 if (appData.debugMode) {
859 fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
860 castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
862 ft = castlingRights[0]; /* Rook file if we have H-side rights */
864 right = BOARD_RGHT-2;
865 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
866 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
867 if(k != ft && board[0][k] != EmptySquare) ft = -1;
868 for(k=left; k<right && ft >= 0; k++) /* then if not checked */
869 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = -1;
870 if(ft >= 0 && board[0][ft] == WhiteRook)
871 callback(board, flags, WhiteHSideCastleFR, 0, ff, 0, ft, closure);
873 ft = castlingRights[1]; /* Rook file if we have A-side rights */
876 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
877 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
878 if(k != ft && board[0][k] != EmptySquare) ft = -1;
879 if(ff > BOARD_LEFT+2)
880 for(k=left+1; k<=right && ft >= 0; k++) /* then if not checked */
881 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = -1;
882 if(ft >= 0 && board[0][ft] == WhiteRook)
883 callback(board, flags, WhiteASideCastleFR, 0, ff, 0, ft, closure);
886 ff = castlingRights[5]; /* King file if we have any rights */
887 if(ff > 0 && board[BOARD_HEIGHT-1][ff] == BlackKing) {
888 ft = castlingRights[3]; /* Rook file if we have H-side rights */
890 right = BOARD_RGHT-2;
891 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
892 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
893 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = -1;
894 for(k=left; k<right && ft >= 0; k++) /* then if not checked */
895 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = -1;
896 if(ft >= 0 && board[BOARD_HEIGHT-1][ft] == BlackRook)
897 callback(board, flags, BlackHSideCastleFR, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ft, closure);
899 ft = castlingRights[4]; /* Rook file if we have A-side rights */
902 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
903 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
904 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = -1;
905 if(ff > BOARD_LEFT+2)
906 for(k=left+1; k<=right && ft >= 0; k++) /* then if not checked */
907 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = -1;
908 if(ft >= 0 && board[BOARD_HEIGHT-1][ft] == BlackRook)
909 callback(board, flags, BlackASideCastleFR, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ft, closure);
925 extern void CheckTestCallback P((Board board, int flags, ChessMove kind,
926 int rf, int ff, int rt, int ft,
930 void CheckTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
937 register CheckTestClosure *cl = (CheckTestClosure *) closure;
939 if (rt == cl->rking && ft == cl->fking) cl->check++;
943 /* If the player on move were to move from (rf, ff) to (rt, ft), would
944 he leave himself in check? Or if rf == -1, is the player on move
945 in check now? enPassant must be TRUE if the indicated move is an
946 e.p. capture. The possibility of castling out of a check along the
947 back rank is not accounted for (i.e., we still return nonzero), as
948 this is illegal anyway. Return value is the number of times the
950 int CheckTest(board, flags, rf, ff, rt, ft, enPassant)
953 int rf, ff, rt, ft, enPassant;
956 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
957 ChessSquare captured = EmptySquare;
958 /* Suppress warnings on uninitialized variables */
960 if(gameInfo.variant == VariantXiangqi)
961 king = flags & F_WHITE_ON_MOVE ? WhiteWazir : BlackWazir;
962 if(gameInfo.variant == VariantKnightmate)
963 king = flags & F_WHITE_ON_MOVE ? WhiteUnicorn : BlackUnicorn;
967 captured = board[rf][ft];
968 board[rf][ft] = EmptySquare;
970 captured = board[rt][ft];
972 board[rt][ft] = board[rf][ff];
973 board[rf][ff] = EmptySquare;
976 /* For compatibility with ICS wild 9, we scan the board in the
977 order a1, a2, a3, ... b1, b2, ..., h8 to find the first king,
978 and we test only whether that one is in check. */
980 for (cl.fking = BOARD_LEFT+0; cl.fking < BOARD_RGHT; cl.fking++)
981 for (cl.rking = 0; cl.rking < BOARD_HEIGHT; cl.rking++) {
982 if (board[cl.rking][cl.fking] == king) {
983 if(gameInfo.variant == VariantXiangqi) {
984 /* [HGM] In Xiangqi opposing Kings means check as well */
986 dir = (king >= BlackPawn) ? -1 : 1;
987 for( i=cl.rking+dir; i>=0 && i<BOARD_HEIGHT &&
988 board[i][cl.fking] == EmptySquare; i+=dir );
989 if(i>=0 && i<BOARD_HEIGHT &&
990 board[i][cl.fking] == (dir>0 ? BlackWazir : WhiteWazir) )
994 GenPseudoLegal(board, flags ^ F_WHITE_ON_MOVE, -1,
995 CheckTestCallback, (VOIDSTAR) &cl);
996 goto undo_move; /* 2-level break */
1003 board[rf][ff] = board[rt][ft];
1005 board[rf][ft] = captured;
1006 board[rt][ft] = EmptySquare;
1008 board[rt][ft] = captured;
1012 return cl.fking < BOARD_RGHT ? cl.check : 1000; // [HGM] atomic: return 1000 if we have no king
1016 extern void LegalityTestCallback P((Board board, int flags, ChessMove kind,
1017 int rf, int ff, int rt, int ft,
1020 void LegalityTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1027 register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
1029 // if (appData.debugMode) {
1030 // fprintf(debugFP, "Legality test: %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1032 if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
1033 cl->captures++; // [HGM] losers: count legal captures
1034 if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
1038 ChessMove LegalityTest(board, flags, epfile, castlingRights, rf, ff, rt, ft, promoChar)
1041 int rf, ff, rt, ft, promoChar;
1042 char castlingRights[];
1044 LegalityTestClosure cl; ChessSquare piece = board[rf][ff];
1046 if (appData.debugMode) {
1048 for(i=0; i<6; i++) fprintf(debugFP, "%d ", castlingRights[i]);
1049 fprintf(debugFP, "Legality test? %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1051 /* [HGM] Lance, Cobra and Falcon are wildcard pieces; consider all their moves legal */
1052 /* (perhaps we should disallow moves that obviously leave us in check?) */
1053 if(piece == WhiteFalcon || piece == BlackFalcon ||
1054 piece == WhiteCobra || piece == BlackCobra ||
1055 piece == WhiteLance || piece == BlackLance)
1062 cl.kind = IllegalMove;
1063 cl.captures = 0; // [HGM] losers: prepare to count legal captures.
1064 GenLegal(board, flags, epfile, castlingRights, LegalityTestCallback, (VOIDSTAR) &cl);
1065 if((flags & F_MANDATORY_CAPTURE) && cl.captures && board[rt][ft] == EmptySquare
1066 && cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant)
1067 return(IllegalMove); // [HGM] losers: if there are legal captures, non-capts are illegal
1069 if(gameInfo.variant == VariantShogi) {
1070 /* [HGM] Shogi promotions. '=' means defer */
1071 if(rf != DROP_RANK && cl.kind == NormalMove) {
1072 ChessSquare piece = board[rf][ff];
1074 if(promoChar == PieceToChar(BlackQueen)) promoChar = NULLCHAR; /* [HGM] Kludge */
1075 if(promoChar != NULLCHAR && promoChar != 'x' &&
1076 promoChar != '+' && promoChar != '=' &&
1077 ToUpper(PieceToChar(PROMOTED piece)) != ToUpper(promoChar) )
1078 cl.kind = IllegalMove;
1079 else if(flags & F_WHITE_ON_MOVE) {
1080 if( (int) piece < (int) WhiteWazir &&
1081 (rf > BOARD_HEIGHT-4 || rt > BOARD_HEIGHT-4) ) {
1082 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1083 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1084 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotionKnight;
1085 else /* promotion optional, default is promote */
1086 cl.kind = promoChar == '=' ? NormalMove : WhitePromotionQueen;
1088 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1089 NormalMove : IllegalMove;
1091 if( (int) piece < (int) BlackWazir && (rf < 3 || rt < 3) ) {
1092 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1093 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1094 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotionKnight;
1095 else /* promotion optional, default is promote */
1096 cl.kind = promoChar == '=' ? NormalMove : BlackPromotionQueen;
1098 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1099 NormalMove : IllegalMove;
1103 if (promoChar != NULLCHAR && promoChar != 'x') {
1104 if(promoChar == '=') cl.kind = IllegalMove; else // [HGM] shogi: no deferred promotion outside Shogi
1105 if (cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen) {
1107 PromoCharToMoveType((flags & F_WHITE_ON_MOVE) != 0, promoChar);
1109 cl.kind = IllegalMove;
1112 /* [HGM] For promotions, 'ToQueen' = optional, 'ToKnight' = mandatory */
1120 extern void MateTestCallback P((Board board, int flags, ChessMove kind,
1121 int rf, int ff, int rt, int ft,
1124 void MateTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1131 register MateTestClosure *cl = (MateTestClosure *) closure;
1136 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */
1137 int MateTest(board, flags, epfile, castlingRights)
1140 char castlingRights[];
1143 int inCheck, r, f, myPieces=0, hisPieces=0, nrKing=0;
1144 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
1146 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
1147 // [HGM] losers: Count pieces and kings, to detect other unorthodox winning conditions
1148 nrKing += (board[r][f] == king); // stm has king
1149 if( board[r][f] != EmptySquare ) {
1150 if((int)board[r][f] <= (int)king && (int)board[r][f] >= (int)king - (int)WhiteKing + (int)WhitePawn)
1155 if(appData.debugMode) fprintf(debugFP, "MateTest: K=%d, my=%d, his=%d\n", nrKing, myPieces, hisPieces);
1156 switch(gameInfo.variant) { // [HGM] losers: extinction wins
1157 case VariantShatranj:
1158 if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
1162 if(nrKing == 0) return MT_NOKING;
1165 if(myPieces == 1) return MT_BARE;
1168 inCheck = GenLegal(board, flags, epfile, castlingRights, MateTestCallback, (VOIDSTAR) &cl);
1169 // [HGM] 3check: yet to do!
1171 return inCheck ? MT_CHECK : MT_NONE;
1173 if(gameInfo.variant == VariantSuicide) // [HGM] losers: always stalemate, since no check, but result varies
1174 return myPieces == hisPieces ? MT_STALEMATE :
1175 myPieces > hisPieces ? MT_STAINMATE : MT_STEALMATE;
1176 else if(gameInfo.variant == VariantLosers) return inCheck ? MT_TRICKMATE : MT_STEALMATE;
1177 else if(gameInfo.variant == VariantGiveaway) return MT_STEALMATE; // no check exists, stalemated = win
1179 return inCheck ? MT_CHECKMATE
1180 : (gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj) ?
1181 MT_STAINMATE : MT_STALEMATE;
1186 extern void DisambiguateCallback P((Board board, int flags, ChessMove kind,
1187 int rf, int ff, int rt, int ft,
1190 void DisambiguateCallback(board, flags, kind, rf, ff, rt, ft, closure)
1197 register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
1199 if ((cl->pieceIn == EmptySquare || cl->pieceIn == board[rf][ff]
1200 || PieceToChar(board[rf][ff]) == '~'
1201 && cl->pieceIn == (ChessSquare)(DEMOTED board[rf][ff])
1203 (cl->rfIn == -1 || cl->rfIn == rf) &&
1204 (cl->ffIn == -1 || cl->ffIn == ff) &&
1205 (cl->rtIn == -1 || cl->rtIn == rt) &&
1206 (cl->ftIn == -1 || cl->ftIn == ft)) {
1209 cl->piece = board[rf][ff];
1218 void Disambiguate(board, flags, epfile, closure)
1221 DisambiguateClosure *closure;
1223 int illegal = 0; char c = closure->promoCharIn;
1225 closure->rf = closure->ff = closure->rt = closure->ft = 0;
1226 closure->kind = ImpossibleMove;
1227 if (appData.debugMode) {
1228 fprintf(debugFP, "Disambiguate in: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1229 closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
1230 closure->promoCharIn, closure->promoCharIn >= ' ' ? closure->promoCharIn : '-');
1232 GenLegal(board, flags, epfile, initialRights, DisambiguateCallback, (VOIDSTAR) closure);
1233 if (closure->count == 0) {
1234 /* See if it's an illegal move due to check */
1236 GenLegal(board, flags|F_IGNORE_CHECK, epfile, initialRights, DisambiguateCallback,
1237 (VOIDSTAR) closure);
1238 if (closure->count == 0) {
1239 /* No, it's not even that */
1240 if (appData.debugMode) { int i, j;
1241 for(i=BOARD_HEIGHT-1; i>=0; i--) {
1242 for(j=0; j<BOARD_WIDTH; j++)
1243 fprintf(debugFP, "%3d", (int) board[i][j]);
1244 fprintf(debugFP, "\n");
1251 if(gameInfo.variant == VariantShogi) {
1252 /* [HGM] Shogi promotions. '=' means defer */
1253 if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
1254 ChessSquare piece = closure->piece;
1255 if(c != NULLCHAR && c != 'x' && c != '+' && c != '=' &&
1256 ToUpper(PieceToChar(PROMOTED piece)) != ToUpper(c) )
1257 closure->kind = IllegalMove;
1258 else if(flags & F_WHITE_ON_MOVE) {
1259 if( (int) piece < (int) WhiteWazir &&
1260 (closure->rf > BOARD_HEIGHT-4 || closure->rt > BOARD_HEIGHT-4) ) {
1261 if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
1262 piece == WhiteKnight && closure->rt > BOARD_HEIGHT-3) /* promotion mandatory */
1263 closure->kind = c == '=' ? IllegalMove : WhitePromotionKnight;
1264 else /* promotion optional, default is promote */
1265 closure->kind = c == '=' ? NormalMove : WhitePromotionQueen;
1267 } else closure->kind = (c == NULLCHAR || c == 'x' || c == '=') ?
1268 NormalMove : IllegalMove;
1270 if( (int) piece < (int) BlackWazir && (closure->rf < 3 || closure->rt < 3) ) {
1271 if( (piece == BlackPawn || piece == BlackQueen) && closure->rt < 1 ||
1272 piece == BlackKnight && closure->rt < 2 ) /* promotion obligatory */
1273 closure->kind = c == '=' ? IllegalMove : BlackPromotionKnight;
1274 else /* promotion optional, default is promote */
1275 closure->kind = c == '=' ? NormalMove : BlackPromotionQueen;
1277 } else closure->kind = (c == NULLCHAR || c == 'x' || c == '=') ?
1278 NormalMove : IllegalMove;
1282 if (closure->promoCharIn != NULLCHAR && closure->promoCharIn != 'x') {
1283 if (closure->kind == WhitePromotionQueen
1284 || closure->kind == BlackPromotionQueen) {
1286 PromoCharToMoveType((flags & F_WHITE_ON_MOVE) != 0,
1287 closure->promoCharIn);
1289 closure->kind = IllegalMove;
1292 /* [HGM] returns 'q' for optional promotion, 'n' for mandatory */
1293 if(closure->promoCharIn != '=')
1294 closure->promoChar = ToLower(closure->promoCharIn);
1295 else closure->promoChar = '=';
1296 if (closure->promoChar == 'x') closure->promoChar = NULLCHAR;
1297 if (closure->count > 1) {
1298 closure->kind = AmbiguousMove;
1301 /* Note: If more than one illegal move matches, but no legal
1302 moves, we return IllegalMove, not AmbiguousMove. Caller
1303 can look at closure->count to detect this.
1305 closure->kind = IllegalMove;
1307 if(closure->kind == IllegalMove)
1308 /* [HGM] might be a variant we don't understand, pass on promotion info */
1309 closure->promoChar = ToLower(closure->promoCharIn);
1310 if (appData.debugMode) {
1311 fprintf(debugFP, "Disambiguate out: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1312 closure->piece,closure->ff,closure->rf,closure->ft,closure->rt,closure->promoChar,
1313 closure->promoChar >= ' ' ? closure->promoChar:'-');
1327 } CoordsToAlgebraicClosure;
1329 extern void CoordsToAlgebraicCallback P((Board board, int flags,
1330 ChessMove kind, int rf, int ff,
1331 int rt, int ft, VOIDSTAR closure));
1333 void CoordsToAlgebraicCallback(board, flags, kind, rf, ff, rt, ft, closure)
1340 register CoordsToAlgebraicClosure *cl =
1341 (CoordsToAlgebraicClosure *) closure;
1343 if (rt == cl->rt && ft == cl->ft &&
1344 (board[rf][ff] == cl->piece
1345 || PieceToChar(board[rf][ff]) == '~' &&
1346 (ChessSquare) (DEMOTED board[rf][ff]) == cl->piece)
1350 cl->kind = kind; /* this is the move we want */
1352 cl->file++; /* need file to rule out this move */
1356 cl->rank++; /* need rank to rule out this move */
1358 cl->either++; /* rank or file will rule out this move */
1364 /* Convert coordinates to normal algebraic notation.
1365 promoChar must be NULLCHAR or 'x' if not a promotion.
1367 ChessMove CoordsToAlgebraic(board, flags, epfile,
1368 rf, ff, rt, ft, promoChar, out)
1377 char *outp = out, c;
1378 CoordsToAlgebraicClosure cl;
1380 if (rf == DROP_RANK) {
1381 /* Bughouse piece drop */
1382 *outp++ = ToUpper(PieceToChar((ChessSquare) ff));
1387 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1389 return (flags & F_WHITE_ON_MOVE) ? WhiteDrop : BlackDrop;
1392 if (promoChar == 'x') promoChar = NULLCHAR;
1393 piece = board[rf][ff];
1394 if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
1396 if (appData.debugMode)
1397 fprintf(debugFP, "CoordsToAlgebraic, piece=%d (%d,%d)-(%d,%d) %c\n", (int)piece,ff,rf,ft,rt,promoChar >= ' ' ? promoChar : '-');
1401 kind = LegalityTest(board, flags, epfile, initialRights, rf, ff, rt, ft, promoChar);
1402 if (kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1403 /* Keep short notation if move is illegal only because it
1404 leaves the player in check, but still return IllegalMove */
1405 kind = LegalityTest(board, flags|F_IGNORE_CHECK, epfile, initialRights,
1406 rf, ff, rt, ft, promoChar);
1407 if (kind == IllegalMove) break;
1412 if (ff == ft && board[rt][ft] == EmptySquare) { /* [HGM] Xiangqi has straight noncapts! */
1413 /* Non-capture; use style "e5" */
1416 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1418 /* Capture; use style "exd5" */
1419 if(gameInfo.variant != VariantXiangqi || board[rt][ft] != EmptySquare )
1420 *outp++ = 'x'; /* [HGM] Xiangqi has sideway noncaptures across river! */
1424 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1426 /* Use promotion suffix style "=Q" */
1428 if (appData.debugMode)
1429 fprintf(debugFP, "movetype=%d, promochar=%d=%c\n", (int)kind, promoChar, promoChar >= ' ' ? promoChar : '-');
1430 if (promoChar != NULLCHAR) {
1431 if(gameInfo.variant == VariantShogi) {
1432 /* [HGM] ... but not in Shogi! */
1433 *outp++ = promoChar == '=' ? '=' : '+';
1436 *outp++ = ToUpper(promoChar);
1445 /* Fabien moved code: FRC castling first (if KxR), wild castling second */
1446 /* Code added by Tord: FRC castling. */
1447 if((piece == WhiteKing && board[rt][ft] == WhiteRook) ||
1448 (piece == BlackKing && board[rt][ft] == BlackRook)) {
1449 if(ft > ff) strcpy(out, "O-O"); else strcpy(out, "O-O-O");
1450 return LegalityTest(board, flags, epfile, initialRights,
1451 rf, ff, rt, ft, promoChar);
1453 /* End of code added by Tord */
1454 /* Test for castling or ICS wild castling */
1455 /* Use style "O-O" (oh-oh) for PGN compatibility */
1456 else if (rf == rt &&
1457 rf == ((piece == WhiteKing) ? 0 : BOARD_HEIGHT-1) &&
1458 ((ff == BOARD_WIDTH>>1 && (ft == BOARD_LEFT+2 || ft == BOARD_RGHT-2)) ||
1459 (ff == (BOARD_WIDTH-1)>>1 && (ft == BOARD_LEFT+1 || ft == BOARD_RGHT-3)))) {
1460 if(ft==BOARD_LEFT+1 || ft==BOARD_RGHT-2)
1463 strcpy(out, "O-O-O");
1465 /* This notation is always unambiguous, unless there are
1466 kings on both the d and e files, with "wild castling"
1467 possible for the king on the d file and normal castling
1468 possible for the other. ICS rules for wild 9
1469 effectively make castling illegal for either king in
1470 this situation. So I am not going to worry about it;
1471 I'll just generate an ambiguous O-O in this case.
1473 return LegalityTest(board, flags, epfile, initialRights,
1474 rf, ff, rt, ft, promoChar);
1477 /* else fall through */
1485 cl.kind = IllegalMove;
1486 cl.rank = cl.file = cl.either = 0;
1487 GenLegal(board, flags, epfile, initialRights,
1488 CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1490 if (cl.kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1491 /* Generate pretty moves for moving into check, but
1492 still return IllegalMove.
1494 GenLegal(board, flags|F_IGNORE_CHECK, epfile, initialRights,
1495 CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1496 if (cl.kind == IllegalMove) break;
1497 cl.kind = IllegalMove;
1500 /* Style is "Nf3" or "Nxf7" if this is unambiguous,
1501 else "Ngf3" or "Ngxf7",
1502 else "N1f3" or "N5xf7",
1503 else "Ng1f3" or "Ng5xf7".
1505 c = PieceToChar(piece) ;
1506 if( c == '~' || c == '+') {
1507 /* [HGM] print nonexistent piece as its demoted version */
1508 piece = (ChessSquare) (DEMOTED piece);
1510 if(c=='+') *outp++ = c;
1511 *outp++ = ToUpper(PieceToChar(piece));
1513 if (cl.file || (cl.either && !cl.rank)) {
1519 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1522 if(board[rt][ft] != EmptySquare)
1528 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1530 if (gameInfo.variant == VariantShogi) {
1531 /* [HGM] in Shogi non-pawns can promote */
1532 if(flags & F_WHITE_ON_MOVE) {
1533 if( (int) cl.piece < (int) WhiteWazir &&
1534 (rf > BOARD_HEIGHT-4 || rt > BOARD_HEIGHT-4) ) {
1535 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1536 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1537 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotionKnight;
1538 else cl.kind = WhitePromotionQueen; /* promotion optional */
1540 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1541 NormalMove : IllegalMove;
1543 if( (int) cl.piece < (int) BlackWazir && (rf < 3 || rt < 3) ) {
1544 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1545 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1546 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotionKnight;
1547 else cl.kind = BlackPromotionQueen; /* promotion optional */
1548 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1549 NormalMove : IllegalMove;
1551 if(cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen) {
1552 /* for optional promotions append '+' or '=' */
1553 if(promoChar == '=') {
1555 cl.kind = NormalMove;
1556 } else *outp++ = '+';
1558 } else if(cl.kind == IllegalMove) {
1559 /* Illegal move specifies any given promotion */
1560 if(promoChar != NULLCHAR && promoChar != 'x') {
1562 *outp++ = ToUpper(promoChar);
1569 /* [HGM] Always long notation for fairies we don't know */
1574 case WhiteGrasshopper:
1575 case BlackGrasshopper:
1577 /* Moving a nonexistent piece */
1581 /* Not a legal move, even ignoring check.
1582 If there was a piece on the from square,
1583 use style "Ng1g3" or "Ng1xe8";
1584 if there was a pawn or nothing (!),
1585 use style "g1g3" or "g1xe8". Use "x"
1586 if a piece was on the to square, even
1587 a piece of the same color.
1590 if (piece != EmptySquare && piece != WhitePawn && piece != BlackPawn) {
1591 *outp++ = ToUpper(PieceToChar(piece));
1596 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1597 if (board[rt][ft] != EmptySquare) *outp++ = 'x';
1601 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1602 /* Use promotion suffix style "=Q" */
1603 if (promoChar != NULLCHAR && promoChar != 'x') {
1605 *outp++ = ToUpper(promoChar);
1612 // [HGM] XQ: the following code serves to detect perpetual chasing (Asian rules)
1621 // I guess the following variables logically belong in the closure too, but I was too lazy and used globals
1623 int preyStackPointer, chaseStackPointer;
1626 char rf, ff, rt, ft;
1636 // there are three new callbacks for use with GenLegal: for adding captures, deleting them, and finding a recapture
1638 extern void AtacksCallback P((Board board, int flags, ChessMove kind,
1639 int rf, int ff, int rt, int ft,
1642 void AttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1648 { // For adding captures that can lead to chase indictment to the chaseStack
1649 if(board[rt][ft] == EmptySquare) return; // non-capture
1650 if(board[rt][ft] == WhitePawn && rt < BOARD_HEIGHT/2) return; // Pawn before river can be chased
1651 if(board[rt][ft] == BlackPawn && rt >= BOARD_HEIGHT/2) return; // Pawn before river can be chased
1652 if(board[rf][ff] == WhitePawn || board[rf][ff] == BlackPawn) return; // Pawns are allowed to chase
1653 if(board[rf][ff] == WhiteWazir || board[rf][ff] == BlackWazir) return; // King is allowed to chase
1654 // move cannot be excluded from being a chase trivially (based on attacker and victim); save it on chaseStack
1655 chaseStack[chaseStackPointer].rf = rf;
1656 chaseStack[chaseStackPointer].ff = ff;
1657 chaseStack[chaseStackPointer].rt = rt;
1658 chaseStack[chaseStackPointer].ft = ft;
1659 chaseStackPointer++;
1662 extern void ExistingAtacksCallback P((Board board, int flags, ChessMove kind,
1663 int rf, int ff, int rt, int ft,
1666 void ExistingAttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1672 { // for removing pre-exsting captures from the chaseStack, to be left with newly created ones
1674 register ChaseClosure *cl = (ChaseClosure *) closure; //closure tells us the move played in the repeat loop
1676 if(board[rt][ft] == EmptySquare) return; // no capture
1677 if(rf == cl->rf && ff == cl->ff) { // attacks with same piece from new position are not considered new
1678 rf = cl->rt; ff = cl->ft; // doctor their fromSquare so they will be recognized in chaseStack
1680 // search move in chaseStack, and delete it if it occurred there (as we know now it is not a new capture)
1681 for(i=0; i<chaseStackPointer; i++) {
1682 if(chaseStack[i].rf == rf && chaseStack[i].ff == ff &&
1683 chaseStack[i].rt == rt && chaseStack[i].ft == ft ) {
1684 // move found on chaseStack, delete it by overwriting with move popped from top of chaseStack
1685 chaseStack[i] = chaseStack[--chaseStackPointer];
1691 extern void ProtectedCallback P((Board board, int flags, ChessMove kind,
1692 int rf, int ff, int rt, int ft,
1695 void ProtectedCallback(board, flags, kind, rf, ff, rt, ft, closure)
1701 { // for determining if a piece (given through the closure) is protected
1702 register ChaseClosure *cl = (ChaseClosure *) closure; // closure tells us where to recapture
1704 if(rt == cl->rt && ft == cl->ft) cl->recaptures++; // count legal recaptures to this square
1705 if(appData.debugMode && board[rt][ft] != EmptySquare)
1706 fprintf(debugFP, "try %c%c%c%c=%d\n", ff+AAA, rf+ONE,ft+AAA, rt+ONE, cl->recaptures);
1709 extern char moveList[MAX_MOVES][MOVE_LEN];
1711 int PerpetualChase(int first, int last)
1712 { // this routine detects if the side to move in the 'first' position is perpetually chasing (when not checking)
1715 ChessSquare captured;
1717 preyStackPointer = 0; // clear stack of chased pieces
1718 for(i=first; i<last; i+=2) { // for all positions with same side to move
1719 if(appData.debugMode) fprintf(debugFP, "judge position %i\n", i);
1720 chaseStackPointer = 0; // clear stack that is going to hold possible chases
1721 // determine all captures possible after the move, and put them on chaseStack
1722 GenLegal(boards[i+1], PosFlags(i), EP_NONE, initialRights, AttacksCallback, &cl);
1723 if(appData.debugMode) { int n;
1724 for(n=0; n<chaseStackPointer; n++)
1725 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1726 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1727 fprintf(debugFP, ": all capts\n");
1729 // determine all captures possible before the move, and delete them from chaseStack
1730 cl.rf = moveList[i][1]-ONE; // prepare closure to pass move that led from i to i+1
1731 cl.ff = moveList[i][0]-AAA+BOARD_LEFT;
1732 cl.rt = moveList[i][3]-ONE;
1733 cl.ft = moveList[i][2]-AAA+BOARD_LEFT;
1734 GenLegal(boards[i], PosFlags(i), EP_NONE, initialRights, ExistingAttacksCallback, &cl);
1735 if(appData.debugMode) { int n;
1736 for(n=0; n<chaseStackPointer; n++)
1737 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1738 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1739 fprintf(debugFP, ": new capts after %c%c%c%c\n", cl.ff+AAA, cl.rf+ONE, cl.ft+AAA, cl.rt+ONE);
1741 // chaseSack now contains all captures made possible by the move
1742 for(j=0; j<chaseStackPointer; j++) { // run through chaseStack to identify true chases
1743 int attacker = (int)boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1744 int victim = (int)boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1746 if(attacker >= (int) BlackPawn) attacker = BLACK_TO_WHITE attacker; // convert to white, as piecee type
1747 if(victim >= (int) BlackPawn) victim = BLACK_TO_WHITE victim;
1749 if((attacker == WhiteKnight || attacker == WhiteCannon) && victim == WhiteRook)
1750 continue; // C or H attack on R is always chase; leave on chaseStack
1752 if(attacker == victim) {
1753 if(LegalityTest(boards[i+1], PosFlags(i+1), EP_NONE, initialRights, chaseStack[j].rt,
1754 chaseStack[j].ft, chaseStack[j].rf, chaseStack[j].ff, NULLCHAR) == NormalMove) {
1755 // we can capture back with equal piece, so this is no chase but a sacrifice
1756 chaseStack[j] = chaseStack[--chaseStackPointer]; // delete the capture from the chaseStack
1757 j--; /* ! */ continue;
1762 // the attack is on a lower piece, or on a pinned or blocked equal one
1763 // test if the victim is protected by a true protector. First make the capture.
1764 captured = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1765 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1766 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = EmptySquare;
1767 // Then test if the opponent can recapture
1768 cl.recaptures = 0; // prepare closure to pass recapture square and count moves to it
1769 cl.rt = chaseStack[j].rt;
1770 cl.ft = chaseStack[j].ft;
1771 if(appData.debugMode) {
1772 fprintf(debugFP, "test if we can recapture %c%c\n", cl.ft+AAA, cl.rt+ONE);
1774 GenLegal(boards[i+1], PosFlags(i+1), EP_NONE, initialRights, ProtectedCallback, &cl); // try all moves
1775 // unmake the capture
1776 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1777 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = captured;
1778 // if a recapture was found, piece is protected, and we are not chasing it.
1779 if(cl.recaptures) { // attacked piece was defended by true protector, no chase
1780 chaseStack[j] = chaseStack[--chaseStackPointer]; // so delete from chaseStack
1784 // chaseStack now contains all moves that chased
1785 if(appData.debugMode) { int n;
1786 for(n=0; n<chaseStackPointer; n++)
1787 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1788 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1789 fprintf(debugFP, ": chases\n");
1791 if(i == first) { // copy all people chased by first move of repeat cycle to preyStack
1792 for(j=0; j<chaseStackPointer; j++) {
1793 preyStack[j].rank = chaseStack[j].rt;
1794 preyStack[j].file = chaseStack[j].ft;
1796 preyStackPointer = chaseStackPointer;
1799 for(j=0; j<chaseStackPointer; j++) {
1800 for(k=0; k<preyStackPointer; k++) {
1801 // search the victim of each chase move on the preyStack (first occurrence)
1802 if(chaseStack[j].ft == preyStack[k].file && chaseStack[j].rt == preyStack[k].rank ) {
1803 if(k < tail) break; // piece was already identified as still being chased
1804 preyStack[preyStackPointer] = preyStack[tail]; // move chased piece to bottom part of preyStack
1805 preyStack[tail] = preyStack[k]; // by swapping
1806 preyStack[k] = preyStack[preyStackPointer];
1812 preyStackPointer = tail; // keep bottom part of preyStack, popping pieces unchased on move i.
1813 if(appData.debugMode) { int n;
1814 for(n=0; n<preyStackPointer; n++)
1815 fprintf(debugFP, "%c%c ", preyStack[n].file+AAA, preyStack[n].rank+ONE);
1816 fprintf(debugFP, "always chased upto ply %d\n", i);
1818 // now adjust the location of the chased pieces according to opponent move
1819 for(j=0; j<preyStackPointer; j++) {
1820 if(preyStack[j].rank == moveList[i+1][1]-ONE &&
1821 preyStack[j].file == moveList[i+1][0]-AAA+BOARD_LEFT) {
1822 preyStack[j].rank = moveList[i+1][3]-ONE;
1823 preyStack[j].file = moveList[i+1][2]-AAA+BOARD_LEFT;
1828 return preyStackPointer; // if any piece was left on preyStack, it has been perpetually chased