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;
683 case WhiteFalcon: // [HGM] wild: for wildcards, self-capture symbolizes move to anywhere
689 callback(board, flags, NormalMove, rf, ff, rf, ff, closure);
702 extern void GenLegalCallback P((Board board, int flags, ChessMove kind,
703 int rf, int ff, int rt, int ft,
706 void GenLegalCallback(board, flags, kind, rf, ff, rt, ft, closure)
713 register GenLegalClosure *cl = (GenLegalClosure *) closure;
715 if (!(flags & F_IGNORE_CHECK) &&
716 CheckTest(board, flags, rf, ff, rt, ft,
717 kind == WhiteCapturesEnPassant ||
718 kind == BlackCapturesEnPassant)) return;
719 if (flags & F_ATOMIC_CAPTURE) {
720 if (board[rt][ft] != EmptySquare ||
721 kind == WhiteCapturesEnPassant || kind == BlackCapturesEnPassant) {
723 ChessSquare king = (flags & F_WHITE_ON_MOVE) ? WhiteKing : BlackKing;
724 if (board[rf][ff] == king) return;
725 for (r = rt-1; r <= rt+1; r++) {
726 for (f = ft-1; f <= ft+1; f++) {
727 if (r >= 0 && r < BOARD_HEIGHT && f >= BOARD_LEFT && f < BOARD_RGHT &&
728 board[r][f] == king) return;
733 cl->cb(board, flags, kind, rf, ff, rt, ft, cl->cl);
740 int captures; // [HGM] losers
741 } LegalityTestClosure;
744 /* Like GenPseudoLegal, but (1) include castling moves, (2) unless
745 F_IGNORE_CHECK is set in the flags, omit moves that would leave the
746 king in check, and (3) if F_ATOMIC_CAPTURE is set in the flags, omit
747 moves that would destroy your own king. The CASTLE_OK flags are
748 true if castling is not yet ruled out by a move of the king or
749 rook. Return TRUE if the player on move is currently in check and
750 F_IGNORE_CHECK is not set. [HGM] add castlingRights parameter */
751 int GenLegal(board, flags, epfile, castlingRights, callback, closure)
755 char castlingRights[];
756 MoveCallback callback;
760 int ff, ft, k, left, right;
761 int ignoreCheck = (flags & F_IGNORE_CHECK) != 0;
762 ChessSquare wKing = WhiteKing, bKing = BlackKing;
766 GenPseudoLegal(board, flags, epfile, GenLegalCallback, (VOIDSTAR) &cl);
769 CheckTest(board, flags, -1, -1, -1, -1, FALSE)) return TRUE;
771 /* Generate castling moves */
772 if(gameInfo.variant == VariantKnightmate) { /* [HGM] Knightmate */
773 wKing = WhiteUnicorn; bKing = BlackUnicorn;
776 for (ff = BOARD_WIDTH>>1; ff >= (BOARD_WIDTH-1)>>1; ff-- /*ics wild 1*/) {
777 if ((flags & F_WHITE_ON_MOVE) &&
778 (flags & F_WHITE_KCASTLE_OK) &&
779 board[0][ff] == wKing &&
780 board[0][ff + 1] == EmptySquare &&
781 board[0][ff + 2] == EmptySquare &&
782 board[0][BOARD_RGHT-3] == EmptySquare &&
783 board[0][BOARD_RGHT-2] == EmptySquare &&
784 board[0][BOARD_RGHT-1] == WhiteRook &&
785 castlingRights[0] >= 0 && /* [HGM] check rights */
786 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
788 (!CheckTest(board, flags, 0, ff, 0, ff + 1, FALSE) &&
789 !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-3, FALSE) &&
790 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-2, FALSE)) &&
791 !CheckTest(board, flags, 0, ff, 0, ff + 2, FALSE)))) {
793 callback(board, flags,
794 ff==BOARD_WIDTH>>1 ? WhiteKingSideCastle : WhiteKingSideCastleWild,
795 0, ff, 0, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
797 if ((flags & F_WHITE_ON_MOVE) &&
798 (flags & F_WHITE_QCASTLE_OK) &&
799 board[0][ff] == wKing &&
800 board[0][ff - 1] == EmptySquare &&
801 board[0][ff - 2] == EmptySquare &&
802 board[0][BOARD_LEFT+2] == EmptySquare &&
803 board[0][BOARD_LEFT+1] == EmptySquare &&
804 board[0][BOARD_LEFT+0] == WhiteRook &&
805 castlingRights[1] >= 0 && /* [HGM] check rights */
806 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
808 (!CheckTest(board, flags, 0, ff, 0, ff - 1, FALSE) &&
809 !CheckTest(board, flags, 0, ff, 0, BOARD_LEFT+3, FALSE) &&
810 !CheckTest(board, flags, 0, ff, 0, ff - 2, FALSE)))) {
812 callback(board, flags,
813 ff==BOARD_WIDTH>>1 ? WhiteQueenSideCastle : WhiteQueenSideCastleWild,
814 0, ff, 0, ff - ((gameInfo.boardWidth+2)>>2), closure);
816 if (!(flags & F_WHITE_ON_MOVE) &&
817 (flags & F_BLACK_KCASTLE_OK) &&
818 board[BOARD_HEIGHT-1][ff] == bKing &&
819 board[BOARD_HEIGHT-1][ff + 1] == EmptySquare &&
820 board[BOARD_HEIGHT-1][ff + 2] == EmptySquare &&
821 board[BOARD_HEIGHT-1][BOARD_RGHT-3] == EmptySquare &&
822 board[BOARD_HEIGHT-1][BOARD_RGHT-2] == EmptySquare &&
823 board[BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook &&
824 castlingRights[3] >= 0 && /* [HGM] check rights */
825 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
827 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 1, FALSE) &&
828 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-3, FALSE) &&
829 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-2, FALSE)) &&
830 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 2, FALSE)))) {
832 callback(board, flags,
833 ff==BOARD_WIDTH>>1 ? BlackKingSideCastle : BlackKingSideCastleWild,
834 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
836 if (!(flags & F_WHITE_ON_MOVE) &&
837 (flags & F_BLACK_QCASTLE_OK) &&
838 board[BOARD_HEIGHT-1][ff] == bKing &&
839 board[BOARD_HEIGHT-1][ff - 1] == EmptySquare &&
840 board[BOARD_HEIGHT-1][ff - 2] == EmptySquare &&
841 board[BOARD_HEIGHT-1][BOARD_LEFT+2] == EmptySquare &&
842 board[BOARD_HEIGHT-1][BOARD_LEFT+1] == EmptySquare &&
843 board[BOARD_HEIGHT-1][BOARD_LEFT+0] == BlackRook &&
844 castlingRights[4] >= 0 && /* [HGM] check rights */
845 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
847 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 1, FALSE) &&
848 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_LEFT+3, FALSE) &&
849 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 2, FALSE)))) {
851 callback(board, flags,
852 ff==BOARD_WIDTH>>1 ? BlackQueenSideCastle : BlackQueenSideCastleWild,
853 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - ((gameInfo.boardWidth+2)>>2), closure);
857 if(gameInfo.variant == VariantFischeRandom) {
859 /* generate all potential FRC castling moves (KxR), ignoring flags */
860 /* [HGM] test if the Rooks we find have castling rights */
863 if ((flags & F_WHITE_ON_MOVE) != 0) {
864 ff = castlingRights[2]; /* King file if we have any rights */
865 if(ff > 0 && board[0][ff] == WhiteKing) {
866 if (appData.debugMode) {
867 fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
868 castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
870 ft = castlingRights[0]; /* Rook file if we have H-side rights */
872 right = BOARD_RGHT-2;
873 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
874 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
875 if(k != ft && board[0][k] != EmptySquare) ft = -1;
876 for(k=left; k<right && ft >= 0; k++) /* then if not checked */
877 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = -1;
878 if(ft >= 0 && board[0][ft] == WhiteRook)
879 callback(board, flags, WhiteHSideCastleFR, 0, ff, 0, ft, closure);
881 ft = castlingRights[1]; /* Rook file if we have A-side rights */
884 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
885 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
886 if(k != ft && board[0][k] != EmptySquare) ft = -1;
887 if(ff > BOARD_LEFT+2)
888 for(k=left+1; k<=right && ft >= 0; k++) /* then if not checked */
889 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = -1;
890 if(ft >= 0 && board[0][ft] == WhiteRook)
891 callback(board, flags, WhiteASideCastleFR, 0, ff, 0, ft, closure);
894 ff = castlingRights[5]; /* King file if we have any rights */
895 if(ff > 0 && board[BOARD_HEIGHT-1][ff] == BlackKing) {
896 ft = castlingRights[3]; /* Rook file if we have H-side rights */
898 right = BOARD_RGHT-2;
899 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
900 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
901 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = -1;
902 for(k=left; k<right && ft >= 0; k++) /* then if not checked */
903 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = -1;
904 if(ft >= 0 && board[BOARD_HEIGHT-1][ft] == BlackRook)
905 callback(board, flags, BlackHSideCastleFR, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ft, closure);
907 ft = castlingRights[4]; /* Rook file if we have A-side rights */
910 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
911 for(k=left; k<=right && ft >= 0; k++) /* first test if blocked */
912 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = -1;
913 if(ff > BOARD_LEFT+2)
914 for(k=left+1; k<=right && ft >= 0; k++) /* then if not checked */
915 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = -1;
916 if(ft >= 0 && board[BOARD_HEIGHT-1][ft] == BlackRook)
917 callback(board, flags, BlackASideCastleFR, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ft, closure);
933 extern void CheckTestCallback P((Board board, int flags, ChessMove kind,
934 int rf, int ff, int rt, int ft,
938 void CheckTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
945 register CheckTestClosure *cl = (CheckTestClosure *) closure;
947 if (rt == cl->rking && ft == cl->fking) cl->check++;
951 /* If the player on move were to move from (rf, ff) to (rt, ft), would
952 he leave himself in check? Or if rf == -1, is the player on move
953 in check now? enPassant must be TRUE if the indicated move is an
954 e.p. capture. The possibility of castling out of a check along the
955 back rank is not accounted for (i.e., we still return nonzero), as
956 this is illegal anyway. Return value is the number of times the
958 int CheckTest(board, flags, rf, ff, rt, ft, enPassant)
961 int rf, ff, rt, ft, enPassant;
964 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
965 ChessSquare captured = EmptySquare;
966 /* Suppress warnings on uninitialized variables */
968 if(gameInfo.variant == VariantXiangqi)
969 king = flags & F_WHITE_ON_MOVE ? WhiteWazir : BlackWazir;
970 if(gameInfo.variant == VariantKnightmate)
971 king = flags & F_WHITE_ON_MOVE ? WhiteUnicorn : BlackUnicorn;
975 captured = board[rf][ft];
976 board[rf][ft] = EmptySquare;
978 captured = board[rt][ft];
980 board[rt][ft] = board[rf][ff];
981 board[rf][ff] = EmptySquare;
984 /* For compatibility with ICS wild 9, we scan the board in the
985 order a1, a2, a3, ... b1, b2, ..., h8 to find the first king,
986 and we test only whether that one is in check. */
988 for (cl.fking = BOARD_LEFT+0; cl.fking < BOARD_RGHT; cl.fking++)
989 for (cl.rking = 0; cl.rking < BOARD_HEIGHT; cl.rking++) {
990 if (board[cl.rking][cl.fking] == king) {
991 if(gameInfo.variant == VariantXiangqi) {
992 /* [HGM] In Xiangqi opposing Kings means check as well */
994 dir = (king >= BlackPawn) ? -1 : 1;
995 for( i=cl.rking+dir; i>=0 && i<BOARD_HEIGHT &&
996 board[i][cl.fking] == EmptySquare; i+=dir );
997 if(i>=0 && i<BOARD_HEIGHT &&
998 board[i][cl.fking] == (dir>0 ? BlackWazir : WhiteWazir) )
1002 GenPseudoLegal(board, flags ^ F_WHITE_ON_MOVE, -1,
1003 CheckTestCallback, (VOIDSTAR) &cl);
1004 goto undo_move; /* 2-level break */
1011 board[rf][ff] = board[rt][ft];
1013 board[rf][ft] = captured;
1014 board[rt][ft] = EmptySquare;
1016 board[rt][ft] = captured;
1020 return cl.fking < BOARD_RGHT ? cl.check : 1000; // [HGM] atomic: return 1000 if we have no king
1024 extern void LegalityTestCallback P((Board board, int flags, ChessMove kind,
1025 int rf, int ff, int rt, int ft,
1028 void LegalityTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1035 register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
1037 // if (appData.debugMode) {
1038 // fprintf(debugFP, "Legality test: %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1040 if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
1041 cl->captures++; // [HGM] losers: count legal captures
1042 if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
1046 ChessMove LegalityTest(board, flags, epfile, castlingRights, rf, ff, rt, ft, promoChar)
1049 int rf, ff, rt, ft, promoChar;
1050 char castlingRights[];
1052 LegalityTestClosure cl; ChessSquare piece = board[rf][ff];
1054 if (appData.debugMode) {
1056 for(i=0; i<6; i++) fprintf(debugFP, "%d ", castlingRights[i]);
1057 fprintf(debugFP, "Legality test? %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
1059 /* [HGM] Lance, Cobra and Falcon are wildcard pieces; consider all their moves legal */
1060 /* (perhaps we should disallow moves that obviously leave us in check?) */
1061 if(piece == WhiteFalcon || piece == BlackFalcon ||
1062 piece == WhiteCobra || piece == BlackCobra ||
1063 piece == WhiteLance || piece == BlackLance)
1064 return CheckTest(board, flags, rf, ff, rt, ft, FALSE) ? IllegalMove : NormalMove;
1070 cl.kind = IllegalMove;
1071 cl.captures = 0; // [HGM] losers: prepare to count legal captures.
1072 GenLegal(board, flags, epfile, castlingRights, LegalityTestCallback, (VOIDSTAR) &cl);
1073 if((flags & F_MANDATORY_CAPTURE) && cl.captures && board[rt][ft] == EmptySquare
1074 && cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant)
1075 return(IllegalMove); // [HGM] losers: if there are legal captures, non-capts are illegal
1077 if(gameInfo.variant == VariantShogi) {
1078 /* [HGM] Shogi promotions. '=' means defer */
1079 if(rf != DROP_RANK && cl.kind == NormalMove) {
1080 ChessSquare piece = board[rf][ff];
1082 if(promoChar == PieceToChar(BlackQueen)) promoChar = NULLCHAR; /* [HGM] Kludge */
1083 if(promoChar != NULLCHAR && promoChar != 'x' &&
1084 promoChar != '+' && promoChar != '=' &&
1085 ToUpper(PieceToChar(PROMOTED piece)) != ToUpper(promoChar) )
1086 cl.kind = IllegalMove;
1087 else if(flags & F_WHITE_ON_MOVE) {
1088 if( (int) piece < (int) WhiteWazir &&
1089 (rf > BOARD_HEIGHT-4 || rt > BOARD_HEIGHT-4) ) {
1090 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1091 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1092 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotionKnight;
1093 else /* promotion optional, default is promote */
1094 cl.kind = promoChar == '=' ? NormalMove : WhitePromotionQueen;
1096 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1097 NormalMove : IllegalMove;
1099 if( (int) piece < (int) BlackWazir && (rf < 3 || rt < 3) ) {
1100 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1101 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1102 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotionKnight;
1103 else /* promotion optional, default is promote */
1104 cl.kind = promoChar == '=' ? NormalMove : BlackPromotionQueen;
1106 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1107 NormalMove : IllegalMove;
1111 if (promoChar != NULLCHAR && promoChar != 'x') {
1112 if(promoChar == '=') cl.kind = IllegalMove; else // [HGM] shogi: no deferred promotion outside Shogi
1113 if (cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen) {
1115 PromoCharToMoveType((flags & F_WHITE_ON_MOVE) != 0, promoChar);
1117 cl.kind = IllegalMove;
1120 /* [HGM] For promotions, 'ToQueen' = optional, 'ToKnight' = mandatory */
1128 extern void MateTestCallback P((Board board, int flags, ChessMove kind,
1129 int rf, int ff, int rt, int ft,
1132 void MateTestCallback(board, flags, kind, rf, ff, rt, ft, closure)
1139 register MateTestClosure *cl = (MateTestClosure *) closure;
1144 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */
1145 int MateTest(board, flags, epfile, castlingRights)
1148 char castlingRights[];
1151 int inCheck, r, f, myPieces=0, hisPieces=0, nrKing=0;
1152 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
1154 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
1155 // [HGM] losers: Count pieces and kings, to detect other unorthodox winning conditions
1156 nrKing += (board[r][f] == king); // stm has king
1157 if( board[r][f] != EmptySquare ) {
1158 if((int)board[r][f] <= (int)king && (int)board[r][f] >= (int)king - (int)WhiteKing + (int)WhitePawn)
1163 if(appData.debugMode) fprintf(debugFP, "MateTest: K=%d, my=%d, his=%d\n", nrKing, myPieces, hisPieces);
1164 switch(gameInfo.variant) { // [HGM] losers: extinction wins
1165 case VariantShatranj:
1166 if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
1170 if(nrKing == 0) return MT_NOKING;
1173 if(myPieces == 1) return MT_BARE;
1176 inCheck = GenLegal(board, flags, epfile, castlingRights, MateTestCallback, (VOIDSTAR) &cl);
1177 // [HGM] 3check: yet to do!
1179 return inCheck ? MT_CHECK : MT_NONE;
1181 if(gameInfo.variant == VariantSuicide) // [HGM] losers: always stalemate, since no check, but result varies
1182 return myPieces == hisPieces ? MT_STALEMATE :
1183 myPieces > hisPieces ? MT_STAINMATE : MT_STEALMATE;
1184 else if(gameInfo.variant == VariantLosers) return inCheck ? MT_TRICKMATE : MT_STEALMATE;
1185 else if(gameInfo.variant == VariantGiveaway) return MT_STEALMATE; // no check exists, stalemated = win
1187 return inCheck ? MT_CHECKMATE
1188 : (gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj) ?
1189 MT_STAINMATE : MT_STALEMATE;
1194 extern void DisambiguateCallback P((Board board, int flags, ChessMove kind,
1195 int rf, int ff, int rt, int ft,
1198 void DisambiguateCallback(board, flags, kind, rf, ff, rt, ft, closure)
1205 register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
1206 int wildCard = FALSE; ChessSquare piece = board[rf][ff];
1208 // [HGM] wild: for wild-card pieces rt and rf are dummies
1209 if(piece == WhiteFalcon || piece == BlackFalcon ||
1210 piece == WhiteCobra || piece == BlackCobra ||
1211 piece == WhiteLance || piece == BlackLance)
1214 if ((cl->pieceIn == EmptySquare || cl->pieceIn == board[rf][ff]
1215 || PieceToChar(board[rf][ff]) == '~'
1216 && cl->pieceIn == (ChessSquare)(DEMOTED board[rf][ff])
1218 (cl->rfIn == -1 || cl->rfIn == rf) &&
1219 (cl->ffIn == -1 || cl->ffIn == ff) &&
1220 (cl->rtIn == -1 || cl->rtIn == rt || wildCard) &&
1221 (cl->ftIn == -1 || cl->ftIn == ft || wildCard)) {
1224 cl->piece = board[rf][ff];
1227 cl->rt = wildCard ? cl->rtIn : rt;
1228 cl->ft = wildCard ? cl->ftIn : ft;
1233 void Disambiguate(board, flags, epfile, closure)
1236 DisambiguateClosure *closure;
1238 int illegal = 0; char c = closure->promoCharIn;
1241 closure->rf = closure->ff = closure->rt = closure->ft = 0;
1242 closure->kind = ImpossibleMove;
1243 if (appData.debugMode) {
1244 fprintf(debugFP, "Disambiguate in: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1245 closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
1246 closure->promoCharIn, closure->promoCharIn >= ' ' ? closure->promoCharIn : '-');
1248 GenLegal(board, flags, epfile, initialRights, DisambiguateCallback, (VOIDSTAR) closure);
1249 if (closure->count == 0) {
1250 /* See if it's an illegal move due to check */
1252 GenLegal(board, flags|F_IGNORE_CHECK, epfile, initialRights, DisambiguateCallback,
1253 (VOIDSTAR) closure);
1254 if (closure->count == 0) {
1255 /* No, it's not even that */
1256 if (appData.debugMode) { int i, j;
1257 for(i=BOARD_HEIGHT-1; i>=0; i--) {
1258 for(j=0; j<BOARD_WIDTH; j++)
1259 fprintf(debugFP, "%3d", (int) board[i][j]);
1260 fprintf(debugFP, "\n");
1267 if(gameInfo.variant == VariantShogi) {
1268 /* [HGM] Shogi promotions. '=' means defer */
1269 if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
1270 ChessSquare piece = closure->piece;
1271 if(c != NULLCHAR && c != 'x' && c != '+' && c != '=' &&
1272 ToUpper(PieceToChar(PROMOTED piece)) != ToUpper(c) )
1273 closure->kind = IllegalMove;
1274 else if(flags & F_WHITE_ON_MOVE) {
1275 if( (int) piece < (int) WhiteWazir &&
1276 (closure->rf > BOARD_HEIGHT-4 || closure->rt > BOARD_HEIGHT-4) ) {
1277 if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
1278 piece == WhiteKnight && closure->rt > BOARD_HEIGHT-3) /* promotion mandatory */
1279 closure->kind = c == '=' ? IllegalMove : WhitePromotionKnight;
1280 else /* promotion optional, default is promote */
1281 closure->kind = c == '=' ? NormalMove : WhitePromotionQueen;
1283 } else closure->kind = (c == NULLCHAR || c == 'x' || c == '=') ?
1284 NormalMove : IllegalMove;
1286 if( (int) piece < (int) BlackWazir && (closure->rf < 3 || closure->rt < 3) ) {
1287 if( (piece == BlackPawn || piece == BlackQueen) && closure->rt < 1 ||
1288 piece == BlackKnight && closure->rt < 2 ) /* promotion obligatory */
1289 closure->kind = c == '=' ? IllegalMove : BlackPromotionKnight;
1290 else /* promotion optional, default is promote */
1291 closure->kind = c == '=' ? NormalMove : BlackPromotionQueen;
1293 } else closure->kind = (c == NULLCHAR || c == 'x' || c == '=') ?
1294 NormalMove : IllegalMove;
1298 if (closure->promoCharIn != NULLCHAR && closure->promoCharIn != 'x') {
1299 if (closure->kind == WhitePromotionQueen
1300 || closure->kind == BlackPromotionQueen) {
1302 PromoCharToMoveType((flags & F_WHITE_ON_MOVE) != 0,
1303 closure->promoCharIn);
1305 closure->kind = IllegalMove;
1308 /* [HGM] returns 'q' for optional promotion, 'n' for mandatory */
1309 if(closure->promoCharIn != '=')
1310 closure->promoChar = ToLower(closure->promoCharIn);
1311 else closure->promoChar = '=';
1312 if (closure->promoChar == 'x') closure->promoChar = NULLCHAR;
1313 if (closure->count > 1) {
1314 closure->kind = AmbiguousMove;
1317 /* Note: If more than one illegal move matches, but no legal
1318 moves, we return IllegalMove, not AmbiguousMove. Caller
1319 can look at closure->count to detect this.
1321 closure->kind = IllegalMove;
1323 if(closure->kind == IllegalMove)
1324 /* [HGM] might be a variant we don't understand, pass on promotion info */
1325 closure->promoChar = ToLower(closure->promoCharIn);
1326 if (appData.debugMode) {
1327 fprintf(debugFP, "Disambiguate out: %d(%d,%d)-(%d,%d) = %d (%c)\n",
1328 closure->piece,closure->ff,closure->rf,closure->ft,closure->rt,closure->promoChar,
1329 closure->promoChar >= ' ' ? closure->promoChar:'-');
1343 } CoordsToAlgebraicClosure;
1345 extern void CoordsToAlgebraicCallback P((Board board, int flags,
1346 ChessMove kind, int rf, int ff,
1347 int rt, int ft, VOIDSTAR closure));
1349 void CoordsToAlgebraicCallback(board, flags, kind, rf, ff, rt, ft, closure)
1356 register CoordsToAlgebraicClosure *cl =
1357 (CoordsToAlgebraicClosure *) closure;
1359 if (rt == cl->rt && ft == cl->ft &&
1360 (board[rf][ff] == cl->piece
1361 || PieceToChar(board[rf][ff]) == '~' &&
1362 (ChessSquare) (DEMOTED board[rf][ff]) == cl->piece)
1366 cl->kind = kind; /* this is the move we want */
1368 cl->file++; /* need file to rule out this move */
1372 cl->rank++; /* need rank to rule out this move */
1374 cl->either++; /* rank or file will rule out this move */
1380 /* Convert coordinates to normal algebraic notation.
1381 promoChar must be NULLCHAR or 'x' if not a promotion.
1383 ChessMove CoordsToAlgebraic(board, flags, epfile,
1384 rf, ff, rt, ft, promoChar, out)
1393 char *outp = out, c;
1394 CoordsToAlgebraicClosure cl;
1396 if (rf == DROP_RANK) {
1397 /* Bughouse piece drop */
1398 *outp++ = ToUpper(PieceToChar((ChessSquare) ff));
1403 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1405 return (flags & F_WHITE_ON_MOVE) ? WhiteDrop : BlackDrop;
1408 if (promoChar == 'x') promoChar = NULLCHAR;
1409 piece = board[rf][ff];
1410 if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
1412 if (appData.debugMode)
1413 fprintf(debugFP, "CoordsToAlgebraic, piece=%d (%d,%d)-(%d,%d) %c\n", (int)piece,ff,rf,ft,rt,promoChar >= ' ' ? promoChar : '-');
1417 kind = LegalityTest(board, flags, epfile, initialRights, rf, ff, rt, ft, promoChar);
1418 if (kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1419 /* Keep short notation if move is illegal only because it
1420 leaves the player in check, but still return IllegalMove */
1421 kind = LegalityTest(board, flags|F_IGNORE_CHECK, epfile, initialRights,
1422 rf, ff, rt, ft, promoChar);
1423 if (kind == IllegalMove) break;
1428 if (ff == ft && board[rt][ft] == EmptySquare) { /* [HGM] Xiangqi has straight noncapts! */
1429 /* Non-capture; use style "e5" */
1432 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1434 /* Capture; use style "exd5" */
1435 if(gameInfo.variant != VariantXiangqi || board[rt][ft] != EmptySquare )
1436 *outp++ = 'x'; /* [HGM] Xiangqi has sideway noncaptures across river! */
1440 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1442 /* Use promotion suffix style "=Q" */
1444 if (appData.debugMode)
1445 fprintf(debugFP, "movetype=%d, promochar=%d=%c\n", (int)kind, promoChar, promoChar >= ' ' ? promoChar : '-');
1446 if (promoChar != NULLCHAR) {
1447 if(gameInfo.variant == VariantShogi) {
1448 /* [HGM] ... but not in Shogi! */
1449 *outp++ = promoChar == '=' ? '=' : '+';
1452 *outp++ = ToUpper(promoChar);
1461 /* Fabien moved code: FRC castling first (if KxR), wild castling second */
1462 /* Code added by Tord: FRC castling. */
1463 if((piece == WhiteKing && board[rt][ft] == WhiteRook) ||
1464 (piece == BlackKing && board[rt][ft] == BlackRook)) {
1465 if(ft > ff) strcpy(out, "O-O"); else strcpy(out, "O-O-O");
1466 return LegalityTest(board, flags, epfile, initialRights,
1467 rf, ff, rt, ft, promoChar);
1469 /* End of code added by Tord */
1470 /* Test for castling or ICS wild castling */
1471 /* Use style "O-O" (oh-oh) for PGN compatibility */
1472 else if (rf == rt &&
1473 rf == ((piece == WhiteKing) ? 0 : BOARD_HEIGHT-1) &&
1474 ((ff == BOARD_WIDTH>>1 && (ft == BOARD_LEFT+2 || ft == BOARD_RGHT-2)) ||
1475 (ff == (BOARD_WIDTH-1)>>1 && (ft == BOARD_LEFT+1 || ft == BOARD_RGHT-3)))) {
1476 if(ft==BOARD_LEFT+1 || ft==BOARD_RGHT-2)
1479 strcpy(out, "O-O-O");
1481 /* This notation is always unambiguous, unless there are
1482 kings on both the d and e files, with "wild castling"
1483 possible for the king on the d file and normal castling
1484 possible for the other. ICS rules for wild 9
1485 effectively make castling illegal for either king in
1486 this situation. So I am not going to worry about it;
1487 I'll just generate an ambiguous O-O in this case.
1489 return LegalityTest(board, flags, epfile, initialRights,
1490 rf, ff, rt, ft, promoChar);
1493 /* else fall through */
1501 cl.kind = IllegalMove;
1502 cl.rank = cl.file = cl.either = 0;
1503 GenLegal(board, flags, epfile, initialRights,
1504 CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1506 if (cl.kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1507 /* Generate pretty moves for moving into check, but
1508 still return IllegalMove.
1510 GenLegal(board, flags|F_IGNORE_CHECK, epfile, initialRights,
1511 CoordsToAlgebraicCallback, (VOIDSTAR) &cl);
1512 if (cl.kind == IllegalMove) break;
1513 cl.kind = IllegalMove;
1516 /* Style is "Nf3" or "Nxf7" if this is unambiguous,
1517 else "Ngf3" or "Ngxf7",
1518 else "N1f3" or "N5xf7",
1519 else "Ng1f3" or "Ng5xf7".
1521 c = PieceToChar(piece) ;
1522 if( c == '~' || c == '+') {
1523 /* [HGM] print nonexistent piece as its demoted version */
1524 piece = (ChessSquare) (DEMOTED piece);
1526 if(c=='+') *outp++ = c;
1527 *outp++ = ToUpper(PieceToChar(piece));
1529 if (cl.file || (cl.either && !cl.rank)) {
1535 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1538 if(board[rt][ft] != EmptySquare)
1544 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1546 if (gameInfo.variant == VariantShogi) {
1547 /* [HGM] in Shogi non-pawns can promote */
1548 if(flags & F_WHITE_ON_MOVE) {
1549 if( (int) cl.piece < (int) WhiteWazir &&
1550 (rf > BOARD_HEIGHT-4 || rt > BOARD_HEIGHT-4) ) {
1551 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1552 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1553 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotionKnight;
1554 else cl.kind = WhitePromotionQueen; /* promotion optional */
1556 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1557 NormalMove : IllegalMove;
1559 if( (int) cl.piece < (int) BlackWazir && (rf < 3 || rt < 3) ) {
1560 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1561 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1562 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotionKnight;
1563 else cl.kind = BlackPromotionQueen; /* promotion optional */
1564 } else cl.kind = (promoChar == NULLCHAR || promoChar == 'x' || promoChar == '=') ?
1565 NormalMove : IllegalMove;
1567 if(cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen) {
1568 /* for optional promotions append '+' or '=' */
1569 if(promoChar == '=') {
1571 cl.kind = NormalMove;
1572 } else *outp++ = '+';
1574 } else if(cl.kind == IllegalMove) {
1575 /* Illegal move specifies any given promotion */
1576 if(promoChar != NULLCHAR && promoChar != 'x') {
1578 *outp++ = ToUpper(promoChar);
1585 /* [HGM] Always long notation for fairies we don't know */
1590 case WhiteGrasshopper:
1591 case BlackGrasshopper:
1593 /* Moving a nonexistent piece */
1597 /* Not a legal move, even ignoring check.
1598 If there was a piece on the from square,
1599 use style "Ng1g3" or "Ng1xe8";
1600 if there was a pawn or nothing (!),
1601 use style "g1g3" or "g1xe8". Use "x"
1602 if a piece was on the to square, even
1603 a piece of the same color.
1606 if (piece != EmptySquare && piece != WhitePawn && piece != BlackPawn) {
1607 *outp++ = ToUpper(PieceToChar(piece));
1612 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1613 if (board[rt][ft] != EmptySquare) *outp++ = 'x';
1617 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1618 /* Use promotion suffix style "=Q" */
1619 if (promoChar != NULLCHAR && promoChar != 'x') {
1621 *outp++ = ToUpper(promoChar);
1628 // [HGM] XQ: the following code serves to detect perpetual chasing (Asian rules)
1637 // I guess the following variables logically belong in the closure too, but I was too lazy and used globals
1639 int preyStackPointer, chaseStackPointer;
1642 char rf, ff, rt, ft;
1652 // there are three new callbacks for use with GenLegal: for adding captures, deleting them, and finding a recapture
1654 extern void AtacksCallback P((Board board, int flags, ChessMove kind,
1655 int rf, int ff, int rt, int ft,
1658 void AttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1664 { // For adding captures that can lead to chase indictment to the chaseStack
1665 if(board[rt][ft] == EmptySquare) return; // non-capture
1666 if(board[rt][ft] == WhitePawn && rt < BOARD_HEIGHT/2) return; // Pawn before river can be chased
1667 if(board[rt][ft] == BlackPawn && rt >= BOARD_HEIGHT/2) return; // Pawn before river can be chased
1668 if(board[rf][ff] == WhitePawn || board[rf][ff] == BlackPawn) return; // Pawns are allowed to chase
1669 if(board[rf][ff] == WhiteWazir || board[rf][ff] == BlackWazir) return; // King is allowed to chase
1670 // move cannot be excluded from being a chase trivially (based on attacker and victim); save it on chaseStack
1671 chaseStack[chaseStackPointer].rf = rf;
1672 chaseStack[chaseStackPointer].ff = ff;
1673 chaseStack[chaseStackPointer].rt = rt;
1674 chaseStack[chaseStackPointer].ft = ft;
1675 chaseStackPointer++;
1678 extern void ExistingAtacksCallback P((Board board, int flags, ChessMove kind,
1679 int rf, int ff, int rt, int ft,
1682 void ExistingAttacksCallback(board, flags, kind, rf, ff, rt, ft, closure)
1688 { // for removing pre-exsting captures from the chaseStack, to be left with newly created ones
1690 register ChaseClosure *cl = (ChaseClosure *) closure; //closure tells us the move played in the repeat loop
1692 if(board[rt][ft] == EmptySquare) return; // no capture
1693 if(rf == cl->rf && ff == cl->ff) { // attacks with same piece from new position are not considered new
1694 rf = cl->rt; ff = cl->ft; // doctor their fromSquare so they will be recognized in chaseStack
1696 // search move in chaseStack, and delete it if it occurred there (as we know now it is not a new capture)
1697 for(i=0; i<chaseStackPointer; i++) {
1698 if(chaseStack[i].rf == rf && chaseStack[i].ff == ff &&
1699 chaseStack[i].rt == rt && chaseStack[i].ft == ft ) {
1700 // move found on chaseStack, delete it by overwriting with move popped from top of chaseStack
1701 chaseStack[i] = chaseStack[--chaseStackPointer];
1707 extern void ProtectedCallback P((Board board, int flags, ChessMove kind,
1708 int rf, int ff, int rt, int ft,
1711 void ProtectedCallback(board, flags, kind, rf, ff, rt, ft, closure)
1717 { // for determining if a piece (given through the closure) is protected
1718 register ChaseClosure *cl = (ChaseClosure *) closure; // closure tells us where to recapture
1720 if(rt == cl->rt && ft == cl->ft) cl->recaptures++; // count legal recaptures to this square
1721 if(appData.debugMode && board[rt][ft] != EmptySquare)
1722 fprintf(debugFP, "try %c%c%c%c=%d\n", ff+AAA, rf+ONE,ft+AAA, rt+ONE, cl->recaptures);
1725 extern char moveList[MAX_MOVES][MOVE_LEN];
1727 int PerpetualChase(int first, int last)
1728 { // this routine detects if the side to move in the 'first' position is perpetually chasing (when not checking)
1731 ChessSquare captured;
1733 preyStackPointer = 0; // clear stack of chased pieces
1734 for(i=first; i<last; i+=2) { // for all positions with same side to move
1735 if(appData.debugMode) fprintf(debugFP, "judge position %i\n", i);
1736 chaseStackPointer = 0; // clear stack that is going to hold possible chases
1737 // determine all captures possible after the move, and put them on chaseStack
1738 GenLegal(boards[i+1], PosFlags(i), EP_NONE, initialRights, AttacksCallback, &cl);
1739 if(appData.debugMode) { int n;
1740 for(n=0; n<chaseStackPointer; n++)
1741 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1742 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1743 fprintf(debugFP, ": all capts\n");
1745 // determine all captures possible before the move, and delete them from chaseStack
1746 cl.rf = moveList[i][1]-ONE; // prepare closure to pass move that led from i to i+1
1747 cl.ff = moveList[i][0]-AAA+BOARD_LEFT;
1748 cl.rt = moveList[i][3]-ONE;
1749 cl.ft = moveList[i][2]-AAA+BOARD_LEFT;
1750 GenLegal(boards[i], PosFlags(i), EP_NONE, initialRights, ExistingAttacksCallback, &cl);
1751 if(appData.debugMode) { int n;
1752 for(n=0; n<chaseStackPointer; n++)
1753 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1754 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1755 fprintf(debugFP, ": new capts after %c%c%c%c\n", cl.ff+AAA, cl.rf+ONE, cl.ft+AAA, cl.rt+ONE);
1757 // chaseSack now contains all captures made possible by the move
1758 for(j=0; j<chaseStackPointer; j++) { // run through chaseStack to identify true chases
1759 int attacker = (int)boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1760 int victim = (int)boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1762 if(attacker >= (int) BlackPawn) attacker = BLACK_TO_WHITE attacker; // convert to white, as piecee type
1763 if(victim >= (int) BlackPawn) victim = BLACK_TO_WHITE victim;
1765 if((attacker == WhiteKnight || attacker == WhiteCannon) && victim == WhiteRook)
1766 continue; // C or H attack on R is always chase; leave on chaseStack
1768 if(attacker == victim) {
1769 if(LegalityTest(boards[i+1], PosFlags(i+1), EP_NONE, initialRights, chaseStack[j].rt,
1770 chaseStack[j].ft, chaseStack[j].rf, chaseStack[j].ff, NULLCHAR) == NormalMove) {
1771 // we can capture back with equal piece, so this is no chase but a sacrifice
1772 chaseStack[j] = chaseStack[--chaseStackPointer]; // delete the capture from the chaseStack
1773 j--; /* ! */ continue;
1778 // the attack is on a lower piece, or on a pinned or blocked equal one
1779 // test if the victim is protected by a true protector. First make the capture.
1780 captured = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1781 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1782 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = EmptySquare;
1783 // Then test if the opponent can recapture
1784 cl.recaptures = 0; // prepare closure to pass recapture square and count moves to it
1785 cl.rt = chaseStack[j].rt;
1786 cl.ft = chaseStack[j].ft;
1787 if(appData.debugMode) {
1788 fprintf(debugFP, "test if we can recapture %c%c\n", cl.ft+AAA, cl.rt+ONE);
1790 GenLegal(boards[i+1], PosFlags(i+1), EP_NONE, initialRights, ProtectedCallback, &cl); // try all moves
1791 // unmake the capture
1792 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1793 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = captured;
1794 // if a recapture was found, piece is protected, and we are not chasing it.
1795 if(cl.recaptures) { // attacked piece was defended by true protector, no chase
1796 chaseStack[j] = chaseStack[--chaseStackPointer]; // so delete from chaseStack
1800 // chaseStack now contains all moves that chased
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, ": chases\n");
1807 if(i == first) { // copy all people chased by first move of repeat cycle to preyStack
1808 for(j=0; j<chaseStackPointer; j++) {
1809 preyStack[j].rank = chaseStack[j].rt;
1810 preyStack[j].file = chaseStack[j].ft;
1812 preyStackPointer = chaseStackPointer;
1815 for(j=0; j<chaseStackPointer; j++) {
1816 for(k=0; k<preyStackPointer; k++) {
1817 // search the victim of each chase move on the preyStack (first occurrence)
1818 if(chaseStack[j].ft == preyStack[k].file && chaseStack[j].rt == preyStack[k].rank ) {
1819 if(k < tail) break; // piece was already identified as still being chased
1820 preyStack[preyStackPointer] = preyStack[tail]; // move chased piece to bottom part of preyStack
1821 preyStack[tail] = preyStack[k]; // by swapping
1822 preyStack[k] = preyStack[preyStackPointer];
1828 preyStackPointer = tail; // keep bottom part of preyStack, popping pieces unchased on move i.
1829 if(appData.debugMode) { int n;
1830 for(n=0; n<preyStackPointer; n++)
1831 fprintf(debugFP, "%c%c ", preyStack[n].file+AAA, preyStack[n].rank+ONE);
1832 fprintf(debugFP, "always chased upto ply %d\n", i);
1834 // now adjust the location of the chased pieces according to opponent move
1835 for(j=0; j<preyStackPointer; j++) {
1836 if(preyStack[j].rank == moveList[i+1][1]-ONE &&
1837 preyStack[j].file == moveList[i+1][0]-AAA+BOARD_LEFT) {
1838 preyStack[j].rank = moveList[i+1][3]-ONE;
1839 preyStack[j].file = moveList[i+1][2]-AAA+BOARD_LEFT;
1844 return preyStackPointer; // if any piece was left on preyStack, it has been perpetually chased