2 * moves.c - Move generation and checking
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
10 * Enhancements Copyright 2005 Alessandro Scotti
12 * The following terms apply to Digital Equipment Corporation's copyright
14 * ------------------------------------------------------------------------
17 * Permission to use, copy, modify, and distribute this software and its
18 * documentation for any purpose and without fee is hereby granted,
19 * provided that the above copyright notice appear in all copies and that
20 * both that copyright notice and this permission notice appear in
21 * supporting documentation, and that the name of Digital not be
22 * used in advertising or publicity pertaining to distribution of the
23 * software without specific, written prior permission.
25 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
32 * ------------------------------------------------------------------------
34 * The following terms apply to the enhanced version of XBoard
35 * distributed by the Free Software Foundation:
36 * ------------------------------------------------------------------------
38 * GNU XBoard is free software: you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, either version 3 of the License, or (at
41 * your option) any later version.
43 * GNU XBoard is distributed in the hope that it will be useful, but
44 * WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 * General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program. If not, see http://www.gnu.org/licenses/. *
51 *------------------------------------------------------------------------
52 ** See the file ChangeLog for a revision history. */
59 #else /* not HAVE_STRING_H */
61 #endif /* not HAVE_STRING_H */
67 int WhitePiece P((ChessSquare));
68 int BlackPiece P((ChessSquare));
69 int SameColor P((ChessSquare, ChessSquare));
70 int PosFlags(int index);
72 extern signed char initialRights[BOARD_FILES]; /* [HGM] all rights enabled, set in InitPosition */
76 WhitePiece (ChessSquare piece)
78 return (int) piece >= (int) WhitePawn && (int) piece < (int) BlackPawn;
82 BlackPiece (ChessSquare piece)
84 return (int) piece >= (int) BlackPawn && (int) piece < (int) EmptySquare;
89 SameColor (ChessSquare piece1, ChessSquare piece2)
91 return ((int) piece1 >= (int) WhitePawn && /* [HGM] can be > King ! */
92 (int) piece1 < (int) BlackPawn &&
93 (int) piece2 >= (int) WhitePawn &&
94 (int) piece2 < (int) BlackPawn)
95 || ((int) piece1 >= (int) BlackPawn &&
96 (int) piece1 < (int) EmptySquare &&
97 (int) piece2 >= (int) BlackPawn &&
98 (int) piece2 < (int) EmptySquare);
101 #define SameColor(piece1, piece2) (piece1 < EmptySquare && piece2 < EmptySquare && (piece1 < BlackPawn) == (piece2 < BlackPawn))
104 char pieceToChar[] = {
105 'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
106 'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
107 'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
108 'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
110 char pieceNickName[EmptySquare];
113 PieceToChar (ChessSquare p)
115 if((int)p < 0 || (int)p >= (int)EmptySquare) return('x'); /* [HGM] for safety */
116 return pieceToChar[(int) p];
120 PieceToNumber (ChessSquare p) /* [HGM] holdings: count piece type, ignoring non-participating piece types */
123 ChessSquare start = (int)p >= (int)BlackPawn ? BlackPawn : WhitePawn;
125 while(start++ != p) if(pieceToChar[(int)start-1] != '.') i++;
133 if(c == '.') return EmptySquare;
134 for(i=0; i< (int) EmptySquare; i++)
135 if(pieceNickName[i] == c) return (ChessSquare) i;
136 for(i=0; i< (int) EmptySquare; i++)
137 if(pieceToChar[i] == c) return (ChessSquare) i;
142 CopyBoard (Board to, Board from)
146 for (i = 0; i < BOARD_HEIGHT; i++)
147 for (j = 0; j < BOARD_WIDTH; j++)
148 to[i][j] = from[i][j];
149 for (j = 0; j < BOARD_FILES; j++) // [HGM] gamestate: copy castling rights and ep status
150 to[VIRGIN][j] = from[VIRGIN][j],
151 to[CASTLING][j] = from[CASTLING][j];
152 to[HOLDINGS_SET] = 0; // flag used in ICS play
156 CompareBoards (Board board1, Board board2)
160 for (i = 0; i < BOARD_HEIGHT; i++)
161 for (j = 0; j < BOARD_WIDTH; j++) {
162 if (board1[i][j] != board2[i][j])
169 /* Call callback once for each pseudo-legal move in the given
170 position, except castling moves. A move is pseudo-legal if it is
171 legal, or if it would be legal except that it leaves the king in
172 check. In the arguments, epfile is EP_NONE if the previous move
173 was not a double pawn push, or the file 0..7 if it was, or
174 EP_UNKNOWN if we don't know and want to allow all e.p. captures.
175 Promotion moves generated are to Queen only.
178 GenPseudoLegal (Board board, int flags, MoveCallback callback, VOIDSTAR closure, ChessSquare filter)
179 // speed: only do moves with this piece type
182 int i, j, d, s, fs, rs, rt, ft, m;
183 int epfile = (signed char)board[EP_STATUS]; // [HGM] gamestate: extract ep status from board
184 int promoRank = gameInfo.variant == VariantMakruk || gameInfo.variant == VariantGrand ? 3 : 1;
186 for (rf = 0; rf < BOARD_HEIGHT; rf++)
187 for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
191 if(board[rf][ff] == EmptySquare) continue;
192 if ((flags & F_WHITE_ON_MOVE) != (board[rf][ff] < BlackPawn)) continue; // [HGM] speed: wrong color
194 m = 0; piece = board[rf][ff];
195 if(PieceToChar(piece) == '~')
196 piece = (ChessSquare) ( DEMOTED piece );
197 if(filter != EmptySquare && piece != filter) continue;
198 if(gameInfo.variant == VariantShogi)
199 piece = (ChessSquare) ( SHOGI piece );
201 switch ((int)piece) {
202 /* case EmptySquare: [HGM] this is nonsense, and conflicts with Shogi cases */
204 /* can't happen ([HGM] except for faries...) */
208 if(gameInfo.variant == VariantXiangqi) {
209 /* [HGM] capture and move straight ahead in Xiangqi */
210 if (rf < BOARD_HEIGHT-1 &&
211 !SameColor(board[rf][ff], board[rf + 1][ff]) ) {
212 callback(board, flags, NormalMove,
213 rf, ff, rf + 1, ff, closure);
215 /* and move sideways when across the river */
216 for (s = -1; s <= 1; s += 2) {
217 if (rf >= BOARD_HEIGHT>>1 &&
218 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
219 !WhitePiece(board[rf][ff+s]) ) {
220 callback(board, flags, NormalMove,
221 rf, ff, rf, ff+s, closure);
226 if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
227 callback(board, flags,
228 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
229 rf, ff, rf + 1, ff, closure);
231 if (rf <= (BOARD_HEIGHT>>1)-3 && board[rf+1][ff] == EmptySquare && // [HGM] grand: also on 3rd rank on 10-board
232 gameInfo.variant != VariantShatranj && /* [HGM] */
233 gameInfo.variant != VariantCourier && /* [HGM] */
234 board[rf+2][ff] == EmptySquare ) {
235 callback(board, flags, NormalMove,
236 rf, ff, rf+2, ff, closure);
238 for (s = -1; s <= 1; s += 2) {
239 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
240 ((flags & F_KRIEGSPIEL_CAPTURE) ||
241 BlackPiece(board[rf + 1][ff + s]))) {
242 callback(board, flags,
243 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
244 rf, ff, rf + 1, ff + s, closure);
246 if (rf >= BOARD_HEIGHT+1>>1) {// [HGM] grand: 4th & 5th rank on 10-board
247 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
248 (epfile == ff + s || epfile == EP_UNKNOWN) && rf < BOARD_HEIGHT-3 &&
249 board[rf][ff + s] == BlackPawn &&
250 board[rf+1][ff + s] == EmptySquare) {
251 callback(board, flags, WhiteCapturesEnPassant,
252 rf, ff, rf+1, ff + s, closure);
259 if(gameInfo.variant == VariantXiangqi) {
260 /* [HGM] capture straight ahead in Xiangqi */
261 if (rf > 0 && !SameColor(board[rf][ff], board[rf - 1][ff]) ) {
262 callback(board, flags, NormalMove,
263 rf, ff, rf - 1, ff, closure);
265 /* and move sideways when across the river */
266 for (s = -1; s <= 1; s += 2) {
267 if (rf < BOARD_HEIGHT>>1 &&
268 ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
269 !BlackPiece(board[rf][ff+s]) ) {
270 callback(board, flags, NormalMove,
271 rf, ff, rf, ff+s, closure);
276 if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
277 callback(board, flags,
278 rf <= promoRank ? BlackPromotion : NormalMove,
279 rf, ff, rf - 1, ff, closure);
281 if (rf >= (BOARD_HEIGHT+1>>1)+2 && board[rf-1][ff] == EmptySquare && // [HGM] grand
282 gameInfo.variant != VariantShatranj && /* [HGM] */
283 gameInfo.variant != VariantCourier && /* [HGM] */
284 board[rf-2][ff] == EmptySquare) {
285 callback(board, flags, NormalMove,
286 rf, ff, rf-2, ff, closure);
288 for (s = -1; s <= 1; s += 2) {
289 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
290 ((flags & F_KRIEGSPIEL_CAPTURE) ||
291 WhitePiece(board[rf - 1][ff + s]))) {
292 callback(board, flags,
293 rf <= promoRank ? BlackPromotion : NormalMove,
294 rf, ff, rf - 1, ff + s, closure);
296 if (rf < BOARD_HEIGHT>>1) {
297 if (ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
298 (epfile == ff + s || epfile == EP_UNKNOWN) && rf > 2 &&
299 board[rf][ff + s] == WhitePawn &&
300 board[rf-1][ff + s] == EmptySquare) {
301 callback(board, flags, BlackCapturesEnPassant,
302 rf, ff, rf-1, ff + s, closure);
313 for (i = -1; i <= 1; i += 2)
314 for (j = -1; j <= 1; j += 2)
315 for (s = 1; s <= 2; s++) {
318 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
319 && ( gameInfo.variant != VariantXiangqi || board[rf+i*(s-1)][ff+j*(2-s)] == EmptySquare)
320 && !SameColor(board[rf][ff], board[rt][ft]))
321 callback(board, flags, NormalMove,
322 rf, ff, rt, ft, closure);
326 case SHOGI WhiteKnight:
327 for (s = -1; s <= 1; s += 2) {
328 if (rf < BOARD_HEIGHT-2 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
329 !SameColor(board[rf][ff], board[rf + 2][ff + s])) {
330 callback(board, flags, NormalMove,
331 rf, ff, rf + 2, ff + s, closure);
336 case SHOGI BlackKnight:
337 for (s = -1; s <= 1; s += 2) {
338 if (rf > 1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
339 !SameColor(board[rf][ff], board[rf - 2][ff + s])) {
340 callback(board, flags, NormalMove,
341 rf, ff, rf - 2, ff + s, closure);
348 for (d = 0; d <= 1; d++)
349 for (s = -1; s <= 1; s += 2) {
352 rt = rf + (i * s) * d;
353 ft = ff + (i * s) * (1 - d);
354 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
355 if (m == 0 && board[rt][ft] == EmptySquare)
356 callback(board, flags, NormalMove,
357 rf, ff, rt, ft, closure);
358 if (m == 1 && board[rt][ft] != EmptySquare &&
359 !SameColor(board[rf][ff], board[rt][ft]) )
360 callback(board, flags, NormalMove,
361 rf, ff, rt, ft, closure);
362 if (board[rt][ft] != EmptySquare && m++) break;
367 /* Gold General (and all its promoted versions) . First do the */
368 /* diagonal forward steps, then proceed as normal Wazir */
369 case SHOGI WhiteWazir:
370 case SHOGI (PROMOTED WhitePawn):
371 case SHOGI (PROMOTED WhiteKnight):
372 case SHOGI (PROMOTED WhiteQueen):
373 case SHOGI (PROMOTED WhiteFerz):
374 for (s = -1; s <= 1; s += 2) {
375 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
376 !SameColor(board[rf][ff], board[rf + 1][ff + s])) {
377 callback(board, flags, NormalMove,
378 rf, ff, rf + 1, ff + s, closure);
383 case SHOGI BlackWazir:
384 case SHOGI (PROMOTED BlackPawn):
385 case SHOGI (PROMOTED BlackKnight):
386 case SHOGI (PROMOTED BlackQueen):
387 case SHOGI (PROMOTED BlackFerz):
388 for (s = -1; s <= 1; s += 2) {
389 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT &&
390 !SameColor(board[rf][ff], board[rf - 1][ff + s])) {
391 callback(board, flags, NormalMove,
392 rf, ff, rf - 1, ff + s, closure);
399 for (d = 0; d <= 1; d++)
400 for (s = -1; s <= 1; s += 2) {
402 ft = ff + s * (1 - d);
403 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
404 && !SameColor(board[rf][ff], board[rt][ft]) &&
405 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
406 callback(board, flags, NormalMove,
407 rf, ff, rt, ft, closure);
413 /* [HGM] support Shatranj pieces */
414 for (rs = -1; rs <= 1; rs += 2)
415 for (fs = -1; fs <= 1; fs += 2) {
418 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
419 && ( gameInfo.variant != VariantXiangqi ||
420 board[rf+rs][ff+fs] == EmptySquare && (2*rf < BOARD_HEIGHT) == (2*rt < BOARD_HEIGHT) )
422 && !SameColor(board[rf][ff], board[rt][ft]))
423 callback(board, flags, NormalMove,
424 rf, ff, rt, ft, closure);
425 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier
426 || gameInfo.variant == VariantXiangqi) continue; // classical Alfil
427 rt = rf + rs; // in unknown variant we assume Modern Elephant, which can also do one step
429 if (!(rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT)
430 && !SameColor(board[rf][ff], board[rt][ft]))
431 callback(board, flags, NormalMove,
432 rf, ff, rt, ft, closure);
434 if(gameInfo.variant == VariantSpartan)
435 for(fs = -1; fs <= 1; fs += 2) {
437 if (!(ft < BOARD_LEFT || ft >= BOARD_RGHT) && board[rf][ft] == EmptySquare)
438 callback(board, flags, NormalMove, rf, ff, rf, ft, closure);
442 /* Make Dragon-Horse also do Dababba moves outside Shogi, for better disambiguation in variant Fairy */
445 for (d = 0; d <= 1; d++) // Dababba moves that Rook cannot do
446 for (s = -2; s <= 2; s += 4) {
448 ft = ff + s * (1 - d);
449 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
450 if (SameColor(board[rf][ff], board[rt][ft])) continue;
451 callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
454 /* Shogi Dragon Horse has to continue with Wazir after Bishop */
455 case SHOGI WhiteCardinal:
456 case SHOGI BlackCardinal:
459 /* Capablanca Archbishop continues as Knight */
464 /* Shogi Bishops are ordinary Bishops */
465 case SHOGI WhiteBishop:
466 case SHOGI BlackBishop:
469 for (rs = -1; rs <= 1; rs += 2)
470 for (fs = -1; fs <= 1; fs += 2)
474 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
475 if (SameColor(board[rf][ff], board[rt][ft])) break;
476 callback(board, flags, NormalMove,
477 rf, ff, rt, ft, closure);
478 if (board[rt][ft] != EmptySquare) break;
480 if(m==1) goto mounted;
481 if(m==2) goto finishGold;
482 /* Bishop falls through */
485 /* Shogi Lance is unlike anything, and asymmetric at that */
486 case SHOGI WhiteQueen:
490 if (rt >= BOARD_HEIGHT) break;
491 if (SameColor(board[rf][ff], board[rt][ft])) break;
492 callback(board, flags, NormalMove,
493 rf, ff, rt, ft, closure);
494 if (board[rt][ft] != EmptySquare) break;
498 case SHOGI BlackQueen:
503 if (SameColor(board[rf][ff], board[rt][ft])) break;
504 callback(board, flags, NormalMove,
505 rf, ff, rt, ft, closure);
506 if (board[rt][ft] != EmptySquare) break;
510 /* Make Dragon-King Dababba & Rook-like outside Shogi, for better disambiguation in variant Fairy */
513 for (d = 0; d <= 1; d++) // Dababba moves that Rook cannot do
514 for (s = -2; s <= 2; s += 4) {
516 ft = ff + s * (1 - d);
517 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT || board[rf+rt>>1][ff+ft>>1] == EmptySquare) continue;
518 if (SameColor(board[rf][ff], board[rt][ft])) continue;
519 callback(board, flags, NormalMove, rf, ff, rt, ft, closure);
521 if(gameInfo.variant == VariantSpartan) rookRange = 2; // in Spartan Chess restrict range to modern Dababba
524 /* Shogi Dragon King has to continue as Ferz after Rook moves */
525 case SHOGI WhiteDragon:
526 case SHOGI BlackDragon:
529 /* Capablanca Chancellor sets flag to continue as Knight */
533 m += (gameInfo.variant == VariantSpartan); // in Spartan Chess Chancellor is used for Dragon King.
535 /* Shogi Rooks are ordinary Rooks */
536 case SHOGI WhiteRook:
537 case SHOGI BlackRook:
541 for (d = 0; d <= 1; d++)
542 for (s = -1; s <= 1; s += 2)
544 rt = rf + (i * s) * d;
545 ft = ff + (i * s) * (1 - d);
546 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
547 if (SameColor(board[rf][ff], board[rt][ft])) break;
548 callback(board, flags, NormalMove,
549 rf, ff, rt, ft, closure);
550 if (board[rt][ft] != EmptySquare || i == rookRange) break;
552 if(m==1) goto mounted;
553 if(m==2) goto finishSilver;
558 for (rs = -1; rs <= 1; rs++)
559 for (fs = -1; fs <= 1; fs++) {
560 if (rs == 0 && fs == 0) continue;
564 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
565 if (SameColor(board[rf][ff], board[rt][ft])) break;
566 callback(board, flags, NormalMove,
567 rf, ff, rt, ft, closure);
568 if (board[rt][ft] != EmptySquare) break;
573 /* Shogi Pawn and Silver General: first the Pawn move, */
574 /* then the General continues like a Ferz */
576 if(gameInfo.variant != VariantMakruk) goto commoner;
577 case SHOGI WhitePawn:
578 case SHOGI WhiteFerz:
579 if (rf < BOARD_HEIGHT-1 &&
580 !SameColor(board[rf][ff], board[rf + 1][ff]) )
581 callback(board, flags, NormalMove,
582 rf, ff, rf + 1, ff, closure);
583 if(piece != SHOGI WhitePawn) goto finishSilver;
587 if(gameInfo.variant != VariantMakruk) goto commoner;
588 case SHOGI BlackPawn:
589 case SHOGI BlackFerz:
591 !SameColor(board[rf][ff], board[rf - 1][ff]) )
592 callback(board, flags, NormalMove,
593 rf, ff, rf - 1, ff, closure);
594 if(piece == SHOGI BlackPawn) break;
599 /* [HGM] support Shatranj pieces */
600 for (rs = -1; rs <= 1; rs += 2)
601 for (fs = -1; fs <= 1; fs += 2) {
604 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
605 if (!SameColor(board[rf][ff], board[rt][ft]) &&
606 (gameInfo.variant != VariantXiangqi || InPalace(rt, ft) ) )
607 callback(board, flags, NormalMove,
608 rf, ff, rt, ft, closure);
614 m++; // [HGM] superchess: use for Centaur
616 case SHOGI WhiteKing:
617 case SHOGI BlackKing:
621 for (i = -1; i <= 1; i++)
622 for (j = -1; j <= 1; j++) {
623 if (i == 0 && j == 0) continue;
626 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) continue;
627 if (SameColor(board[rf][ff], board[rt][ft])) continue;
628 callback(board, flags, NormalMove,
629 rf, ff, rt, ft, closure);
631 if(m==1) goto mounted;
634 case WhiteNightrider:
635 case BlackNightrider:
636 for (i = -1; i <= 1; i += 2)
637 for (j = -1; j <= 1; j += 2)
638 for (s = 1; s <= 2; s++) { int k;
642 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
643 if (SameColor(board[rf][ff], board[rt][ft])) break;
644 callback(board, flags, NormalMove,
645 rf, ff, rt, ft, closure);
646 if (board[rt][ft] != EmptySquare) break;
652 /* First do Bishop,then continue like Chancellor */
653 for (rs = -1; rs <= 1; rs += 2)
654 for (fs = -1; fs <= 1; fs += 2)
658 if (rt < 0 || rt >= BOARD_HEIGHT || ft < BOARD_LEFT || ft >= BOARD_RGHT) break;
659 if (SameColor(board[rf][ff], board[rt][ft])) break;
660 callback(board, flags, NormalMove,
661 rf, ff, rt, ft, closure);
662 if (board[rt][ft] != EmptySquare) break;
667 // Use Lance as Berolina / Spartan Pawn.
669 if(gameInfo.variant == VariantSuper) goto Amazon;
670 if (rf < BOARD_HEIGHT-1 && BlackPiece(board[rf + 1][ff]))
671 callback(board, flags,
672 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
673 rf, ff, rf + 1, ff, closure);
674 for (s = -1; s <= 1; s += 2) {
675 if (rf < BOARD_HEIGHT-1 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT && board[rf + 1][ff + s] == EmptySquare)
676 callback(board, flags,
677 rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotion : NormalMove,
678 rf, ff, rf + 1, ff + s, closure);
679 if (rf == 1 && ff + 2*s >= BOARD_LEFT && ff + 2*s < BOARD_RGHT && board[3][ff + 2*s] == EmptySquare )
680 callback(board, flags, NormalMove, rf, ff, 3, ff + 2*s, closure);
685 if(gameInfo.variant == VariantSuper) goto Amazon;
686 if (rf > 0 && WhitePiece(board[rf - 1][ff]))
687 callback(board, flags,
688 rf <= promoRank ? BlackPromotion : NormalMove,
689 rf, ff, rf - 1, ff, closure);
690 for (s = -1; s <= 1; s += 2) {
691 if (rf > 0 && ff + s >= BOARD_LEFT && ff + s < BOARD_RGHT && board[rf - 1][ff + s] == EmptySquare)
692 callback(board, flags,
693 rf <= promoRank ? BlackPromotion : NormalMove,
694 rf, ff, rf - 1, ff + s, closure);
695 if (rf == BOARD_HEIGHT-2 && ff + 2*s >= BOARD_LEFT && ff + 2*s < BOARD_RGHT && board[rf-2][ff + 2*s] == EmptySquare )
696 callback(board, flags, NormalMove, rf, ff, rf-2, ff + 2*s, closure);
700 case WhiteFalcon: // [HGM] wild: for wildcards, self-capture symbolizes move to anywhere
704 callback(board, flags, NormalMove, rf, ff, rf, ff, closure);
717 int rFilter, fFilter; // [HGM] speed: sorry, but I get a bit tired of this closure madness
718 Board xqCheckers, nullBoard;
720 extern void GenLegalCallback P((Board board, int flags, ChessMove kind,
721 int rf, int ff, int rt, int ft,
725 GenLegalCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
727 register GenLegalClosure *cl = (GenLegalClosure *) closure;
729 if(rFilter >= 0 && rFilter != rt || fFilter >= 0 && fFilter != ft) return; // [HGM] speed: ignore moves with wrong to-square
731 if (!(flags & F_IGNORE_CHECK) ) {
732 int check, promo = (gameInfo.variant == VariantSpartan && kind == BlackPromotion);
735 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
736 kings += (board[r][f] == BlackKing);
740 board[rf][ff] = BlackKing; // [HGM] spartan: promote to King before check-test
742 check = CheckTest(board, flags, rf, ff, rt, ft,
743 kind == WhiteCapturesEnPassant ||
744 kind == BlackCapturesEnPassant);
745 if(promo) board[rf][ff] = BlackLance;
748 if (flags & F_ATOMIC_CAPTURE) {
749 if (board[rt][ft] != EmptySquare ||
750 kind == WhiteCapturesEnPassant || kind == BlackCapturesEnPassant) {
752 ChessSquare king = (flags & F_WHITE_ON_MOVE) ? WhiteKing : BlackKing;
753 if (board[rf][ff] == king) return;
754 for (r = rt-1; r <= rt+1; r++) {
755 for (f = ft-1; f <= ft+1; f++) {
756 if (r >= 0 && r < BOARD_HEIGHT && f >= BOARD_LEFT && f < BOARD_RGHT &&
757 board[r][f] == king) return;
762 cl->cb(board, flags, kind, rf, ff, rt, ft, cl->cl);
769 int captures; // [HGM] losers
770 } LegalityTestClosure;
773 /* Like GenPseudoLegal, but (1) include castling moves, (2) unless
774 F_IGNORE_CHECK is set in the flags, omit moves that would leave the
775 king in check, and (3) if F_ATOMIC_CAPTURE is set in the flags, omit
776 moves that would destroy your own king. The CASTLE_OK flags are
777 true if castling is not yet ruled out by a move of the king or
778 rook. Return TRUE if the player on move is currently in check and
779 F_IGNORE_CHECK is not set. [HGM] add castlingRights parameter */
781 GenLegal (Board board, int flags, MoveCallback callback, VOIDSTAR closure, ChessSquare filter)
784 int ff, ft, k, left, right, swap;
785 int ignoreCheck = (flags & F_IGNORE_CHECK) != 0;
786 ChessSquare wKing = WhiteKing, bKing = BlackKing, *castlingRights = board[CASTLING];
787 int inCheck = !ignoreCheck && CheckTest(board, flags, -1, -1, -1, -1, FALSE); // kludge alert: this would mark pre-existing checkers if status==1
791 xqCheckers[EP_STATUS] *= 2; // quasi: if previous CheckTest has been marking, we now set flag for suspending same checkers
792 if(filter == EmptySquare) rFilter = fFilter = -1; // [HGM] speed: do not filter on square if we do not filter on piece
793 GenPseudoLegal(board, flags, GenLegalCallback, (VOIDSTAR) &cl, filter);
795 if (inCheck) return TRUE;
797 /* Generate castling moves */
798 if(gameInfo.variant == VariantKnightmate) { /* [HGM] Knightmate */
799 wKing = WhiteUnicorn; bKing = BlackUnicorn;
802 for (ff = BOARD_WIDTH>>1; ff >= (BOARD_WIDTH-1)>>1; ff-- /*ics wild 1*/) {
803 if ((flags & F_WHITE_ON_MOVE) &&
804 (flags & F_WHITE_KCASTLE_OK) &&
805 board[0][ff] == wKing &&
806 board[0][ff + 1] == EmptySquare &&
807 board[0][ff + 2] == EmptySquare &&
808 board[0][BOARD_RGHT-3] == EmptySquare &&
809 board[0][BOARD_RGHT-2] == EmptySquare &&
810 board[0][BOARD_RGHT-1] == WhiteRook &&
811 castlingRights[0] != NoRights && /* [HGM] check rights */
812 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
814 (!CheckTest(board, flags, 0, ff, 0, ff + 1, FALSE) &&
815 !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-3, FALSE) &&
816 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, 0, ff, 0, BOARD_RGHT-2, FALSE)) &&
817 !CheckTest(board, flags, 0, ff, 0, ff + 2, FALSE)))) {
819 callback(board, flags,
820 ff==BOARD_WIDTH>>1 ? WhiteKingSideCastle : WhiteKingSideCastleWild,
821 0, ff, 0, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
823 if ((flags & F_WHITE_ON_MOVE) &&
824 (flags & F_WHITE_QCASTLE_OK) &&
825 board[0][ff] == wKing &&
826 board[0][ff - 1] == EmptySquare &&
827 board[0][ff - 2] == EmptySquare &&
828 board[0][BOARD_LEFT+2] == EmptySquare &&
829 board[0][BOARD_LEFT+1] == EmptySquare &&
830 board[0][BOARD_LEFT+0] == WhiteRook &&
831 castlingRights[1] != NoRights && /* [HGM] check rights */
832 ( castlingRights[2] == ff || castlingRights[6] == ff ) &&
834 (!CheckTest(board, flags, 0, ff, 0, ff - 1, FALSE) &&
835 !CheckTest(board, flags, 0, ff, 0, BOARD_LEFT+3, FALSE) &&
836 !CheckTest(board, flags, 0, ff, 0, ff - 2, FALSE)))) {
838 callback(board, flags,
839 ff==BOARD_WIDTH>>1 ? WhiteQueenSideCastle : WhiteQueenSideCastleWild,
840 0, ff, 0, ff - ((gameInfo.boardWidth+2)>>2), closure);
842 if (!(flags & F_WHITE_ON_MOVE) &&
843 (flags & F_BLACK_KCASTLE_OK) &&
844 board[BOARD_HEIGHT-1][ff] == bKing &&
845 board[BOARD_HEIGHT-1][ff + 1] == EmptySquare &&
846 board[BOARD_HEIGHT-1][ff + 2] == EmptySquare &&
847 board[BOARD_HEIGHT-1][BOARD_RGHT-3] == EmptySquare &&
848 board[BOARD_HEIGHT-1][BOARD_RGHT-2] == EmptySquare &&
849 board[BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook &&
850 castlingRights[3] != NoRights && /* [HGM] check rights */
851 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
853 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 1, FALSE) &&
854 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-3, FALSE) &&
855 (gameInfo.variant != VariantJanus || !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_RGHT-2, FALSE)) &&
856 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + 2, FALSE)))) {
858 callback(board, flags,
859 ff==BOARD_WIDTH>>1 ? BlackKingSideCastle : BlackKingSideCastleWild,
860 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff + ((gameInfo.boardWidth+2)>>2) + (gameInfo.variant == VariantJanus), closure);
862 if (!(flags & F_WHITE_ON_MOVE) &&
863 (flags & F_BLACK_QCASTLE_OK) &&
864 board[BOARD_HEIGHT-1][ff] == bKing &&
865 board[BOARD_HEIGHT-1][ff - 1] == EmptySquare &&
866 board[BOARD_HEIGHT-1][ff - 2] == EmptySquare &&
867 board[BOARD_HEIGHT-1][BOARD_LEFT+2] == EmptySquare &&
868 board[BOARD_HEIGHT-1][BOARD_LEFT+1] == EmptySquare &&
869 board[BOARD_HEIGHT-1][BOARD_LEFT+0] == BlackRook &&
870 castlingRights[4] != NoRights && /* [HGM] check rights */
871 ( castlingRights[5] == ff || castlingRights[7] == ff ) &&
873 (!CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 1, FALSE) &&
874 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, BOARD_LEFT+3, FALSE) &&
875 !CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - 2, FALSE)))) {
877 callback(board, flags,
878 ff==BOARD_WIDTH>>1 ? BlackQueenSideCastle : BlackQueenSideCastleWild,
879 BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, ff - ((gameInfo.boardWidth+2)>>2), closure);
883 if((swap = gameInfo.variant == VariantSChess) || flags & F_FRC_TYPE_CASTLING) {
885 /* generate all potential FRC castling moves (KxR), ignoring flags */
886 /* [HGM] test if the Rooks we find have castling rights */
887 /* In S-Chess we generate RxK for allowed castlings, for gating at Rook square */
890 if ((flags & F_WHITE_ON_MOVE) != 0) {
891 ff = castlingRights[2]; /* King file if we have any rights */
892 if(ff != NoRights && board[0][ff] == WhiteKing) {
893 if (appData.debugMode) {
894 fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
895 castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
897 ft = castlingRights[0]; /* Rook file if we have H-side rights */
899 right = BOARD_RGHT-2;
900 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
901 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
902 if(k != ft && board[0][k] != EmptySquare) ft = NoRights;
903 for(k=left; k<right && ft != NoRights; k++) /* then if not checked */
904 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = NoRights;
905 if(ft != NoRights && board[0][ft] == WhiteRook)
906 callback(board, flags, WhiteHSideCastleFR, 0, swap ? ft : ff, 0, swap ? ff : ft, closure);
908 ft = castlingRights[1]; /* Rook file if we have A-side rights */
911 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
912 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
913 if(k != ft && board[0][k] != EmptySquare) ft = NoRights;
914 if(ff > BOARD_LEFT+2)
915 for(k=left+1; k<=right && ft != NoRights; k++) /* then if not checked */
916 if(!ignoreCheck && CheckTest(board, flags, 0, ff, 0, k, FALSE)) ft = NoRights;
917 if(ft != NoRights && board[0][ft] == WhiteRook)
918 callback(board, flags, WhiteASideCastleFR, 0, swap ? ft : ff, 0, swap ? ff : ft, closure);
921 ff = castlingRights[5]; /* King file if we have any rights */
922 if(ff != NoRights && board[BOARD_HEIGHT-1][ff] == BlackKing) {
923 ft = castlingRights[3]; /* Rook file if we have H-side rights */
925 right = BOARD_RGHT-2;
926 if(ff == BOARD_RGHT-2) left = right = ff-1; /* special case */
927 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
928 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = NoRights;
929 for(k=left; k<right && ft != NoRights; k++) /* then if not checked */
930 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = NoRights;
931 if(ft != NoRights && board[BOARD_HEIGHT-1][ft] == BlackRook)
932 callback(board, flags, BlackHSideCastleFR, BOARD_HEIGHT-1, swap ? ft : ff, BOARD_HEIGHT-1, swap ? ff : ft, closure);
934 ft = castlingRights[4]; /* Rook file if we have A-side rights */
937 if(ff <= BOARD_LEFT+2) { left = ff+1; right = BOARD_LEFT+3; }
938 for(k=left; k<=right && ft != NoRights; k++) /* first test if blocked */
939 if(k != ft && board[BOARD_HEIGHT-1][k] != EmptySquare) ft = NoRights;
940 if(ff > BOARD_LEFT+2)
941 for(k=left+1; k<=right && ft != NoRights; k++) /* then if not checked */
942 if(!ignoreCheck && CheckTest(board, flags, BOARD_HEIGHT-1, ff, BOARD_HEIGHT-1, k, FALSE)) ft = NoRights;
943 if(ft != NoRights && board[BOARD_HEIGHT-1][ft] == BlackRook)
944 callback(board, flags, BlackASideCastleFR, BOARD_HEIGHT-1, swap ? ft : ff, BOARD_HEIGHT-1, swap ? ff : ft, closure);
960 extern void CheckTestCallback P((Board board, int flags, ChessMove kind,
961 int rf, int ff, int rt, int ft,
966 CheckTestCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
968 register CheckTestClosure *cl = (CheckTestClosure *) closure;
970 if (rt == cl->rking && ft == cl->fking) {
971 if(xqCheckers[EP_STATUS] >= 2 && xqCheckers[rf][ff]) return; // checker is piece with suspended checking power
973 xqCheckers[rf][ff] = xqCheckers[EP_STATUS] & 1; // remember who is checking (if status == 1)
978 /* If the player on move were to move from (rf, ff) to (rt, ft), would
979 he leave himself in check? Or if rf == -1, is the player on move
980 in check now? enPassant must be TRUE if the indicated move is an
981 e.p. capture. The possibility of castling out of a check along the
982 back rank is not accounted for (i.e., we still return nonzero), as
983 this is illegal anyway. Return value is the number of times the
986 CheckTest (Board board, int flags, int rf, int ff, int rt, int ft, int enPassant)
989 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
990 ChessSquare captured = EmptySquare;
991 /* Suppress warnings on uninitialized variables */
993 if(gameInfo.variant == VariantXiangqi)
994 king = flags & F_WHITE_ON_MOVE ? WhiteWazir : BlackWazir;
995 if(gameInfo.variant == VariantKnightmate)
996 king = flags & F_WHITE_ON_MOVE ? WhiteUnicorn : BlackUnicorn;
1000 captured = board[rf][ft];
1001 board[rf][ft] = EmptySquare;
1003 captured = board[rt][ft];
1005 if(rf == DROP_RANK) board[rt][ft] = ff; else { // [HGM] drop
1006 board[rt][ft] = board[rf][ff];
1007 board[rf][ff] = EmptySquare;
1011 /* For compatibility with ICS wild 9, we scan the board in the
1012 order a1, a2, a3, ... b1, b2, ..., h8 to find the first king,
1013 and we test only whether that one is in check. */
1014 for (cl.fking = BOARD_LEFT+0; cl.fking < BOARD_RGHT; cl.fking++)
1015 for (cl.rking = 0; cl.rking < BOARD_HEIGHT; cl.rking++) {
1016 if (board[cl.rking][cl.fking] == king) {
1018 if(gameInfo.variant == VariantXiangqi) {
1019 /* [HGM] In Xiangqi opposing Kings means check as well */
1021 dir = (king >= BlackPawn) ? -1 : 1;
1022 for( i=cl.rking+dir; i>=0 && i<BOARD_HEIGHT &&
1023 board[i][cl.fking] == EmptySquare; i+=dir );
1024 if(i>=0 && i<BOARD_HEIGHT &&
1025 board[i][cl.fking] == (dir>0 ? BlackWazir : WhiteWazir) )
1028 GenPseudoLegal(board, flags ^ F_WHITE_ON_MOVE, CheckTestCallback, (VOIDSTAR) &cl, EmptySquare);
1029 if(gameInfo.variant != VariantSpartan || cl.check == 0) // in Spartan Chess go on to test if other King is checked too
1030 goto undo_move; /* 2-level break */
1037 if(rf != DROP_RANK) // [HGM] drop
1038 board[rf][ff] = board[rt][ft];
1040 board[rf][ft] = captured;
1041 board[rt][ft] = EmptySquare;
1043 board[rt][ft] = captured;
1047 return cl.fking < BOARD_RGHT ? cl.check : 1000; // [HGM] atomic: return 1000 if we have no king
1051 LegalDrop (Board board, int flags, ChessSquare piece, int rt, int ft)
1052 { // [HGM] put drop legality testing in separate routine for clarity
1054 if(appData.debugMode) fprintf(debugFP, "LegalDrop: %d @ %d,%d)\n", piece, ft, rt);
1055 if(board[rt][ft] != EmptySquare) return ImpossibleMove; // must drop to empty square
1056 n = PieceToNumber(piece);
1057 if((gameInfo.holdingsWidth == 0 || (flags & F_WHITE_ON_MOVE ? board[n][BOARD_WIDTH-1] : board[BOARD_HEIGHT-1-n][0]) != piece)
1058 && gameInfo.variant != VariantBughouse) // in bughouse we don't check for availability, because ICS doesn't always tell us
1059 return ImpossibleMove; // piece not available
1060 if(gameInfo.variant == VariantShogi) { // in Shogi lots of drops are forbidden!
1061 if((piece == WhitePawn || piece == WhiteQueen) && rt == BOARD_HEIGHT-1 ||
1062 (piece == BlackPawn || piece == BlackQueen) && rt == 0 ||
1063 piece == WhiteKnight && rt > BOARD_HEIGHT-3 ||
1064 piece == BlackKnight && rt < 2 ) return IllegalMove; // e.g. where dropped piece has no moves
1065 if(piece == WhitePawn || piece == BlackPawn) {
1067 for(r=1; r<BOARD_HEIGHT-1; r++)
1068 if(board[r][ft] == piece) return IllegalMove; // or there already is a Pawn in file
1069 // should still test if we mate with this Pawn
1071 } else if(gameInfo.variant == VariantSChess) { // only back-rank drops
1072 if (rt != (piece < BlackPawn ? 0 : BOARD_HEIGHT-1)) return IllegalMove;
1074 if( (piece == WhitePawn || piece == BlackPawn) &&
1075 (rt == 0 || rt == BOARD_HEIGHT -1 ) )
1076 return IllegalMove; /* no pawn drops on 1st/8th */
1078 if(appData.debugMode) fprintf(debugFP, "LegalDrop: %d @ %d,%d)\n", piece, ft, rt);
1079 if (!(flags & F_IGNORE_CHECK) &&
1080 CheckTest(board, flags, DROP_RANK, piece, rt, ft, FALSE) ) return IllegalMove;
1081 return flags & F_WHITE_ON_MOVE ? WhiteDrop : BlackDrop;
1084 extern void LegalityTestCallback P((Board board, int flags, ChessMove kind,
1085 int rf, int ff, int rt, int ft,
1089 LegalityTestCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1091 register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
1093 if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
1094 cl->captures++; // [HGM] losers: count legal captures
1095 if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
1100 LegalityTest (Board board, int flags, int rf, int ff, int rt, int ft, int promoChar)
1102 LegalityTestClosure cl; ChessSquare piece, filterPiece;
1104 if(quickFlag) flags = flags & ~1 | quickFlag & 1; // [HGM] speed: in quick mode quickFlag specifies side-to-move.
1105 if(rf == DROP_RANK) return LegalDrop(board, flags, ff, rt, ft);
1106 piece = filterPiece = board[rf][ff];
1107 if(PieceToChar(piece) == '~') filterPiece = DEMOTED piece;
1109 /* [HGM] Cobra and Falcon are wildcard pieces; consider all their moves legal */
1110 /* (perhaps we should disallow moves that obviously leave us in check?) */
1111 if(piece == WhiteFalcon || piece == BlackFalcon ||
1112 piece == WhiteCobra || piece == BlackCobra)
1113 return CheckTest(board, flags, rf, ff, rt, ft, FALSE) ? IllegalMove : NormalMove;
1117 cl.rt = rFilter = rt; // [HGM] speed: filter on to-square
1118 cl.ft = fFilter = ft;
1119 cl.kind = IllegalMove;
1120 cl.captures = 0; // [HGM] losers: prepare to count legal captures.
1121 if(flags & F_MANDATORY_CAPTURE) filterPiece = EmptySquare; // [HGM] speed: do not filter in suicide, to find all captures
1122 GenLegal(board, flags, LegalityTestCallback, (VOIDSTAR) &cl, filterPiece);
1123 if((flags & F_MANDATORY_CAPTURE) && cl.captures && board[rt][ft] == EmptySquare
1124 && cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant)
1125 return(IllegalMove); // [HGM] losers: if there are legal captures, non-capts are illegal
1127 if(promoChar == 'x') promoChar = NULLCHAR; // [HGM] is this ever the case?
1128 if(gameInfo.variant == VariantSChess && promoChar && promoChar != '=' && board[rf][ff] != WhitePawn && board[rf][ff] != BlackPawn) {
1129 if(board[rf][ff] < BlackPawn) { // white
1130 if(rf != 0) return IllegalMove; // must be on back rank
1131 if(!(board[VIRGIN][ff] & VIRGIN_W)) return IllegalMove; // non-virgin
1132 if(board[PieceToNumber(CharToPiece(ToUpper(promoChar)))][BOARD_WIDTH-2] == 0) return ImpossibleMove;// must be in stock
1134 if(rf != BOARD_HEIGHT-1) return IllegalMove;
1135 if(!(board[VIRGIN][ff] & VIRGIN_B)) return IllegalMove; // non-virgin
1136 if(board[BOARD_HEIGHT-1-PieceToNumber(CharToPiece(ToLower(promoChar)))][1] == 0) return ImpossibleMove;
1139 if(gameInfo.variant == VariantShogi) {
1140 /* [HGM] Shogi promotions. '=' means defer */
1141 if(rf != DROP_RANK && cl.kind == NormalMove) {
1142 ChessSquare piece = board[rf][ff];
1144 if(promoChar == PieceToChar(BlackQueen)) promoChar = NULLCHAR; /* [HGM] Kludge */
1145 if(promoChar == 'd' && (piece == WhiteRook || piece == BlackRook) ||
1146 promoChar == 'h' && (piece == WhiteBishop || piece == BlackBishop) ||
1147 promoChar == 'g' && (piece <= WhiteFerz || piece <= BlackFerz && piece >= BlackPawn) )
1148 promoChar = '+'; // allowed ICS notations
1149 if(appData.debugMode)fprintf(debugFP,"SHOGI promoChar = %c\n", promoChar ? promoChar : '-');
1150 if(promoChar != NULLCHAR && promoChar != '+' && promoChar != '=')
1151 return CharToPiece(promoChar) == EmptySquare ? ImpossibleMove : IllegalMove;
1152 else if(flags & F_WHITE_ON_MOVE) {
1153 if( (int) piece < (int) WhiteWazir &&
1154 (rf >= BOARD_HEIGHT*2/3 || rt >= BOARD_HEIGHT*2/3) ) {
1155 if( (piece == WhitePawn || piece == WhiteQueen) && rt > BOARD_HEIGHT-2 ||
1156 piece == WhiteKnight && rt > BOARD_HEIGHT-3) /* promotion mandatory */
1157 cl.kind = promoChar == '=' ? IllegalMove : WhitePromotion;
1158 else /* promotion optional, default is defer */
1159 cl.kind = promoChar == '+' ? WhitePromotion : WhiteNonPromotion;
1160 } else cl.kind = promoChar == '+' ? IllegalMove : NormalMove;
1162 if( (int) piece < (int) BlackWazir && (rf < BOARD_HEIGHT/3 || rt < BOARD_HEIGHT/3) ) {
1163 if( (piece == BlackPawn || piece == BlackQueen) && rt < 1 ||
1164 piece == BlackKnight && rt < 2 ) /* promotion obligatory */
1165 cl.kind = promoChar == '=' ? IllegalMove : BlackPromotion;
1166 else /* promotion optional, default is defer */
1167 cl.kind = promoChar == '+' ? BlackPromotion : BlackNonPromotion;
1168 } else cl.kind = promoChar == '+' ? IllegalMove : NormalMove;
1172 if (promoChar != NULLCHAR) {
1173 if(promoChar == '=') cl.kind = IllegalMove; else // [HGM] shogi: no deferred promotion outside Shogi
1174 if (cl.kind == WhitePromotion || cl.kind == BlackPromotion) {
1175 ChessSquare piece = CharToPiece(flags & F_WHITE_ON_MOVE ? ToUpper(promoChar) : ToLower(promoChar));
1176 if(piece == EmptySquare)
1177 cl.kind = ImpossibleMove; // non-existing piece
1178 if(gameInfo.variant == VariantSpartan && cl.kind == BlackPromotion ) {
1179 if(promoChar != PieceToChar(BlackKing)) {
1180 if(CheckTest(board, flags, rf, ff, rt, ft, FALSE)) cl.kind = IllegalMove; // [HGM] spartan: only promotion to King was possible
1181 if(piece == BlackLance) cl.kind = ImpossibleMove;
1182 } else { // promotion to King allowed only if we do not haave two yet
1183 int r, f, kings = 0;
1184 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) kings += (board[r][f] == BlackKing);
1185 if(kings == 2) cl.kind = IllegalMove;
1187 } else if(piece == WhitePawn && rt == BOARD_HEIGHT-1 ||
1188 piece == BlackPawn && rt == 0) cl.kind = IllegalMove; // cannot stay Pawn on last rank in any variant
1189 else if((piece == WhiteUnicorn || piece == BlackUnicorn) && gameInfo.variant == VariantKnightmate)
1190 cl.kind = IllegalMove; // promotion to Royal Knight not allowed
1191 else if((piece == WhiteKing || piece == BlackKing) && gameInfo.variant != VariantSuicide && gameInfo.variant != VariantGiveaway)
1192 cl.kind = IllegalMove; // promotion to King usually not allowed
1194 cl.kind = IllegalMove;
1204 extern void MateTestCallback P((Board board, int flags, ChessMove kind,
1205 int rf, int ff, int rt, int ft,
1209 MateTestCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1211 register MateTestClosure *cl = (MateTestClosure *) closure;
1216 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */
1218 MateTest (Board board, int flags)
1221 int inCheck, r, f, myPieces=0, hisPieces=0, nrKing=0;
1222 ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
1224 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
1225 // [HGM] losers: Count pieces and kings, to detect other unorthodox winning conditions
1226 nrKing += (board[r][f] == king); // stm has king
1227 if( board[r][f] != EmptySquare ) {
1228 if((int)board[r][f] <= (int)king && (int)board[r][f] >= (int)king - (int)WhiteKing + (int)WhitePawn)
1233 switch(gameInfo.variant) { // [HGM] losers: extinction wins
1234 case VariantShatranj:
1235 if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
1239 if(nrKing == 0) return MT_NOKING;
1242 if(myPieces == 1) return MT_BARE;
1245 inCheck = GenLegal(board, flags, MateTestCallback, (VOIDSTAR) &cl, EmptySquare);
1246 // [HGM] 3check: yet to do!
1248 return inCheck ? MT_CHECK : MT_NONE;
1250 if(gameInfo.holdingsWidth && gameInfo.variant != VariantSuper && gameInfo.variant != VariantGreat
1251 && gameInfo.variant != VariantSChess && gameInfo.variant != VariantGrand) { // drop game
1252 int r, f, n, holdings = flags & F_WHITE_ON_MOVE ? BOARD_WIDTH-1 : 0;
1253 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) if(board[r][f] == EmptySquare) // all empty squares
1254 for(n=0; n<BOARD_HEIGHT; n++) // all pieces in hand
1255 if(board[n][holdings] != EmptySquare) {
1256 int moveType = LegalDrop(board, flags, board[n][holdings], r, f);
1257 if(moveType == WhiteDrop || moveType == BlackDrop) return (inCheck ? MT_CHECK : MT_NONE); // we have legal drop
1260 if(gameInfo.variant == VariantSuicide) // [HGM] losers: always stalemate, since no check, but result varies
1261 return myPieces == hisPieces ? MT_STALEMATE :
1262 myPieces > hisPieces ? MT_STAINMATE : MT_STEALMATE;
1263 else if(gameInfo.variant == VariantLosers) return inCheck ? MT_TRICKMATE : MT_STEALMATE;
1264 else if(gameInfo.variant == VariantGiveaway) return MT_STEALMATE; // no check exists, stalemated = win
1266 return inCheck ? MT_CHECKMATE
1267 : (gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj) ?
1268 MT_STAINMATE : MT_STALEMATE;
1273 extern void DisambiguateCallback P((Board board, int flags, ChessMove kind,
1274 int rf, int ff, int rt, int ft,
1278 DisambiguateCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1280 register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
1281 int wildCard = FALSE; ChessSquare piece = board[rf][ff];
1283 // [HGM] wild: for wild-card pieces rt and rf are dummies
1284 if(piece == WhiteFalcon || piece == BlackFalcon ||
1285 piece == WhiteCobra || piece == BlackCobra)
1288 if ((cl->pieceIn == EmptySquare || cl->pieceIn == board[rf][ff]
1289 || PieceToChar(board[rf][ff]) == '~'
1290 && cl->pieceIn == (ChessSquare)(DEMOTED board[rf][ff])
1292 (cl->rfIn == -1 || cl->rfIn == rf) &&
1293 (cl->ffIn == -1 || cl->ffIn == ff) &&
1294 (cl->rtIn == -1 || cl->rtIn == rt || wildCard) &&
1295 (cl->ftIn == -1 || cl->ftIn == ft || wildCard)) {
1298 if(cl->count == 1 || board[rt][ft] != EmptySquare) {
1299 // [HGM] oneclick: if multiple moves, be sure we remember capture
1300 cl->piece = board[rf][ff];
1303 cl->rt = wildCard ? cl->rtIn : rt;
1304 cl->ft = wildCard ? cl->ftIn : ft;
1307 cl->captures += (board[rt][ft] != EmptySquare); // [HGM] oneclick: count captures
1312 Disambiguate (Board board, int flags, DisambiguateClosure *closure)
1314 int illegal = 0; char c = closure->promoCharIn;
1316 if(quickFlag) flags = flags & ~1 | quickFlag & 1; // [HGM] speed: in quick mode quickFlag specifies side-to-move.
1317 closure->count = closure->captures = 0;
1318 closure->rf = closure->ff = closure->rt = closure->ft = 0;
1319 closure->kind = ImpossibleMove;
1320 rFilter = closure->rtIn; // [HGM] speed: only consider moves to given to-square
1321 fFilter = closure->ftIn;
1322 if(quickFlag) { // [HGM] speed: try without check test first, because if that is not ambiguous, we are happy
1323 GenLegal(board, flags|F_IGNORE_CHECK, DisambiguateCallback, (VOIDSTAR) closure, closure->pieceIn);
1324 if(closure->count > 1) { // gamble did not pay off. retry with check test to resolve ambiguity
1325 closure->count = closure->captures = 0;
1326 closure->rf = closure->ff = closure->rt = closure->ft = 0;
1327 closure->kind = ImpossibleMove;
1328 GenLegal(board, flags, DisambiguateCallback, (VOIDSTAR) closure, closure->pieceIn); // [HGM] speed: only pieces of requested type
1331 GenLegal(board, flags, DisambiguateCallback, (VOIDSTAR) closure, closure->pieceIn); // [HGM] speed: only pieces of requested type
1332 if (closure->count == 0) {
1333 /* See if it's an illegal move due to check */
1335 GenLegal(board, flags|F_IGNORE_CHECK, DisambiguateCallback, (VOIDSTAR) closure, closure->pieceIn);
1336 if (closure->count == 0) {
1337 /* No, it's not even that */
1338 if(!appData.testLegality && closure->pieceIn != EmptySquare) {
1339 int f, r; // if there is only a single piece of the requested type on the board, use that
1340 closure->rt = closure->rtIn, closure->ft = closure->ftIn;
1341 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++)
1342 if(board[r][f] == closure->pieceIn) closure->count++, closure->rf = r, closure->ff = f;
1343 if(closure->count > 1) illegal = 0; // ambiguous
1345 if(closure->count == 0) {
1346 if (appData.debugMode) { int i, j;
1347 for(i=BOARD_HEIGHT-1; i>=0; i--) {
1348 for(j=0; j<BOARD_WIDTH; j++)
1349 fprintf(debugFP, "%3d", (int) board[i][j]);
1350 fprintf(debugFP, "\n");
1358 if (c == 'x') c = NULLCHAR; // get rid of any 'x' (which should never happen?)
1359 if(gameInfo.variant == VariantSChess && c && c != '=' && closure->piece != WhitePawn && closure->piece != BlackPawn) {
1360 if(closure->piece < BlackPawn) { // white
1361 if(closure->rf != 0) closure->kind = IllegalMove; // must be on back rank
1362 if(!(board[VIRGIN][closure->ff] & VIRGIN_W)) closure->kind = IllegalMove; // non-virgin
1363 if(board[PieceToNumber(CharToPiece(ToUpper(c)))][BOARD_WIDTH-2] == 0) closure->kind = ImpossibleMove;// must be in stock
1365 if(closure->rf != BOARD_HEIGHT-1) closure->kind = IllegalMove;
1366 if(!(board[VIRGIN][closure->ff] & VIRGIN_B)) closure->kind = IllegalMove; // non-virgin
1367 if(board[BOARD_HEIGHT-1-PieceToNumber(CharToPiece(ToLower(c)))][1] == 0) closure->kind = ImpossibleMove;
1370 if(gameInfo.variant == VariantShogi) {
1371 /* [HGM] Shogi promotions. On input, '=' means defer, '+' promote. Afterwards, c is set to '+' for promotions, NULL other */
1372 if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
1373 ChessSquare piece = closure->piece;
1374 if (c == 'd' && (piece == WhiteRook || piece == BlackRook) ||
1375 c == 'h' && (piece == WhiteBishop || piece == BlackBishop) ||
1376 c == 'g' && (piece <= WhiteFerz || piece <= BlackFerz && piece >= BlackPawn) )
1377 c = '+'; // allowed ICS notations
1378 if(c != NULLCHAR && c != '+' && c != '=') closure->kind = IllegalMove; // otherwise specifying a piece is illegal
1379 else if(flags & F_WHITE_ON_MOVE) {
1380 if( (int) piece < (int) WhiteWazir &&
1381 (closure->rf >= BOARD_HEIGHT-(BOARD_HEIGHT/3) || closure->rt >= BOARD_HEIGHT-(BOARD_HEIGHT/3)) ) {
1382 if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
1383 piece == WhiteKnight && closure->rt > BOARD_HEIGHT-3) /* promotion mandatory */
1384 closure->kind = c == '=' ? IllegalMove : WhitePromotion;
1385 else /* promotion optional, default is defer */
1386 closure->kind = c == '+' ? WhitePromotion : WhiteNonPromotion;
1387 } else closure->kind = c == '+' ? IllegalMove : NormalMove;
1389 if( (int) piece < (int) BlackWazir && (closure->rf < BOARD_HEIGHT/3 || closure->rt < BOARD_HEIGHT/3) ) {
1390 if( (piece == BlackPawn || piece == BlackQueen) && closure->rt < 1 ||
1391 piece == BlackKnight && closure->rt < 2 ) /* promotion obligatory */
1392 closure->kind = c == '=' ? IllegalMove : BlackPromotion;
1393 else /* promotion optional, default is defer */
1394 closure->kind = c == '+' ? BlackPromotion : BlackNonPromotion;
1395 } else closure->kind = c == '+' ? IllegalMove : NormalMove;
1398 if(closure->kind == WhitePromotion || closure->kind == BlackPromotion) c = '+'; else
1399 if(closure->kind == WhiteNonPromotion || closure->kind == BlackNonPromotion) c = '=';
1401 if (closure->kind == WhitePromotion || closure->kind == BlackPromotion) {
1402 if(c == NULLCHAR) { // missing promoChar on mandatory promotion; use default for variant
1403 if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
1404 c = PieceToChar(BlackFerz);
1405 else if(gameInfo.variant == VariantGreat)
1406 c = PieceToChar(BlackMan);
1407 else if(gameInfo.variant == VariantGrand)
1408 closure->kind = closure->rt != 0 && closure->rt != BOARD_HEIGHT-1 ? NormalMove : AmbiguousMove; // no default in Grand Chess
1410 c = PieceToChar(BlackQueen);
1411 } else if(c == '=') closure->kind = IllegalMove; // no deferral outside Shogi
1412 } else if (c != NULLCHAR) closure->kind = IllegalMove;
1414 closure->promoChar = ToLower(c); // this can be NULLCHAR! Note we keep original promoChar even if illegal.
1415 if(c != '+' && c != '=' && c != NULLCHAR && CharToPiece(flags & F_WHITE_ON_MOVE ? ToUpper(c) : ToLower(c)) == EmptySquare)
1416 closure->kind = ImpossibleMove; // but we cannot handle non-existing piece types!
1417 if (closure->count > 1) {
1418 closure->kind = AmbiguousMove;
1421 /* Note: If more than one illegal move matches, but no legal
1422 moves, we return IllegalMove, not AmbiguousMove. Caller
1423 can look at closure->count to detect this.
1425 closure->kind = IllegalMove;
1439 } CoordsToAlgebraicClosure;
1441 extern void CoordsToAlgebraicCallback P((Board board, int flags,
1442 ChessMove kind, int rf, int ff,
1443 int rt, int ft, VOIDSTAR closure));
1446 CoordsToAlgebraicCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1448 register CoordsToAlgebraicClosure *cl =
1449 (CoordsToAlgebraicClosure *) closure;
1451 if ((rt == cl->rt && ft == cl->ft || rt == rf && ft == ff) && // [HGM] null move matches any toSquare
1452 (board[rf][ff] == cl->piece
1453 || PieceToChar(board[rf][ff]) == '~' &&
1454 (ChessSquare) (DEMOTED board[rf][ff]) == cl->piece)
1458 cl->kind = kind; /* this is the move we want */
1460 cl->file++; /* need file to rule out this move */
1464 cl->rank++; /* need rank to rule out this move */
1466 cl->either++; /* rank or file will rule out this move */
1472 /* Convert coordinates to normal algebraic notation.
1473 promoChar must be NULLCHAR or 'x' if not a promotion.
1476 CoordsToAlgebraic (Board board, int flags, int rf, int ff, int rt, int ft, int promoChar, char out[MOVE_LEN])
1480 char *outp = out, c;
1481 CoordsToAlgebraicClosure cl;
1483 if (rf == DROP_RANK) {
1484 if(ff == EmptySquare) { strncpy(outp, "--",3); return NormalMove; } // [HGM] pass
1485 /* Bughouse piece drop */
1486 *outp++ = ToUpper(PieceToChar((ChessSquare) ff));
1491 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1493 return (flags & F_WHITE_ON_MOVE) ? WhiteDrop : BlackDrop;
1496 if (promoChar == 'x') promoChar = NULLCHAR;
1497 piece = board[rf][ff];
1498 if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
1503 kind = LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1504 if (kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1505 /* Keep short notation if move is illegal only because it
1506 leaves the player in check, but still return IllegalMove */
1507 kind = LegalityTest(board, flags|F_IGNORE_CHECK, rf, ff, rt, ft, promoChar);
1508 if (kind == IllegalMove) break;
1513 if (ff == ft && board[rt][ft] == EmptySquare) { /* [HGM] Xiangqi has straight noncapts! */
1514 /* Non-capture; use style "e5" */
1517 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1519 /* Capture; use style "exd5" */
1520 if(gameInfo.variant != VariantXiangqi || board[rt][ft] != EmptySquare )
1521 *outp++ = 'x'; /* [HGM] Xiangqi has sideway noncaptures across river! */
1525 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1527 /* Use promotion suffix style "=Q" */
1529 if (promoChar != NULLCHAR) {
1530 if(gameInfo.variant == VariantShogi) {
1531 /* [HGM] ... but not in Shogi! */
1532 *outp++ = promoChar == '=' ? '=' : '+';
1535 *outp++ = ToUpper(promoChar);
1544 /* Fabien moved code: FRC castling first (if KxR), wild castling second */
1545 /* Code added by Tord: FRC castling. */
1546 if((piece == WhiteKing && board[rt][ft] == WhiteRook) ||
1547 (piece == BlackKing && board[rt][ft] == BlackRook)) {
1549 safeStrCpy(out, "O-O", MOVE_LEN);
1551 safeStrCpy(out, "O-O-O", MOVE_LEN);
1552 return LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1554 /* End of code added by Tord */
1555 /* Test for castling or ICS wild castling */
1556 /* Use style "O-O" (oh-oh) for PGN compatibility */
1557 else if (rf == rt &&
1558 rf == ((piece == WhiteKing) ? 0 : BOARD_HEIGHT-1) &&
1559 (ft - ff > 1 || ff - ft > 1) && // No castling if legal King move (on narrow boards!)
1560 ((ff == BOARD_WIDTH>>1 && (ft == BOARD_LEFT+2 || ft == BOARD_RGHT-2)) ||
1561 (ff == (BOARD_WIDTH-1)>>1 && (ft == BOARD_LEFT+1 || ft == BOARD_RGHT-3)))) {
1562 if(ft==BOARD_LEFT+1 || ft==BOARD_RGHT-2)
1563 snprintf(out, MOVE_LEN, "O-O%c%c", promoChar ? '/' : 0, ToUpper(promoChar));
1565 snprintf(out, MOVE_LEN, "O-O-O%c%c", promoChar ? '/' : 0, ToUpper(promoChar));
1567 /* This notation is always unambiguous, unless there are
1568 kings on both the d and e files, with "wild castling"
1569 possible for the king on the d file and normal castling
1570 possible for the other. ICS rules for wild 9
1571 effectively make castling illegal for either king in
1572 this situation. So I am not going to worry about it;
1573 I'll just generate an ambiguous O-O in this case.
1575 return LegalityTest(board, flags, rf, ff, rt, ft, promoChar);
1578 /* else fall through */
1583 cl.rt = rFilter = rt; // [HGM] speed: filter on to-square
1584 cl.ft = fFilter = ft;
1586 cl.kind = IllegalMove;
1587 cl.rank = cl.file = cl.either = 0;
1588 c = PieceToChar(piece) ;
1589 GenLegal(board, flags, CoordsToAlgebraicCallback, (VOIDSTAR) &cl, c!='~' ? piece : (DEMOTED piece)); // [HGM] speed
1591 if (cl.kind == IllegalMove && !(flags&F_IGNORE_CHECK)) {
1592 /* Generate pretty moves for moving into check, but
1593 still return IllegalMove.
1595 GenLegal(board, flags|F_IGNORE_CHECK, CoordsToAlgebraicCallback, (VOIDSTAR) &cl, c!='~' ? piece : (DEMOTED piece));
1596 if (cl.kind == IllegalMove) break;
1597 cl.kind = IllegalMove;
1600 /* Style is "Nf3" or "Nxf7" if this is unambiguous,
1601 else "Ngf3" or "Ngxf7",
1602 else "N1f3" or "N5xf7",
1603 else "Ng1f3" or "Ng5xf7".
1605 if( c == '~' || c == '+') {
1606 /* [HGM] print nonexistent piece as its demoted version */
1607 piece = (ChessSquare) (DEMOTED piece);
1609 if(c=='+') *outp++ = c;
1610 *outp++ = ToUpper(PieceToChar(piece));
1612 if (cl.file || (cl.either && !cl.rank)) {
1618 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1621 if(board[rt][ft] != EmptySquare)
1627 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1628 if (gameInfo.variant == VariantShogi) {
1629 /* [HGM] in Shogi non-pawns can promote */
1630 *outp++ = promoChar; // Don't bother to correct move type, return value is never used!
1632 else if (gameInfo.variant != VariantSuper && promoChar &&
1633 (piece == WhiteLance || piece == BlackLance) ) { // Lance sometimes represents Pawn
1635 *outp++ = ToUpper(promoChar);
1637 else if (gameInfo.variant == VariantSChess && promoChar) { // and in S-Chess we have gating
1639 *outp++ = ToUpper(promoChar);
1645 /* Moving a nonexistent piece */
1649 /* Not a legal move, even ignoring check.
1650 If there was a piece on the from square,
1651 use style "Ng1g3" or "Ng1xe8";
1652 if there was a pawn or nothing (!),
1653 use style "g1g3" or "g1xe8". Use "x"
1654 if a piece was on the to square, even
1655 a piece of the same color.
1659 if (piece != EmptySquare && piece != WhitePawn && piece != BlackPawn) {
1661 for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<=BOARD_RGHT; f++)
1662 c += (board[r][f] == piece); // count on-board pieces of given type
1663 *outp++ = ToUpper(PieceToChar(piece));
1665 if(c != 1) { // [HGM] but if there is only one piece of the mentioned type, no from-square, thank you!
1669 else { *outp++ = (rf+ONE-'0')/10 + '0';*outp++ = (rf+ONE-'0')%10 + '0'; }
1671 if (board[rt][ft] != EmptySquare) *outp++ = 'x';
1675 else { *outp++ = (rt+ONE-'0')/10 + '0';*outp++ = (rt+ONE-'0')%10 + '0'; }
1676 /* Use promotion suffix style "=Q" */
1677 if (promoChar != NULLCHAR && promoChar != 'x') {
1679 *outp++ = ToUpper(promoChar);
1686 // [HGM] XQ: the following code serves to detect perpetual chasing (Asian rules)
1695 // I guess the following variables logically belong in the closure too, but I was too lazy and used globals
1697 int preyStackPointer, chaseStackPointer;
1700 unsigned char rf, ff, rt, ft;
1704 unsigned char rank, file;
1710 // there are three new callbacks for use with GenLegal: for adding captures, deleting them, and finding a recapture
1712 extern void AtacksCallback P((Board board, int flags, ChessMove kind,
1713 int rf, int ff, int rt, int ft,
1717 AttacksCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1718 { // For adding captures that can lead to chase indictment to the chaseStack
1719 if(board[rt][ft] == EmptySquare) return; // non-capture
1720 if(board[rt][ft] == WhitePawn && rt < BOARD_HEIGHT/2) return; // Pawn before river can be chased
1721 if(board[rt][ft] == BlackPawn && rt >= BOARD_HEIGHT/2) return; // Pawn before river can be chased
1722 if(board[rf][ff] == WhitePawn || board[rf][ff] == BlackPawn) return; // Pawns are allowed to chase
1723 if(board[rf][ff] == WhiteWazir || board[rf][ff] == BlackWazir) return; // King is allowed to chase
1724 // move cannot be excluded from being a chase trivially (based on attacker and victim); save it on chaseStack
1725 chaseStack[chaseStackPointer].rf = rf;
1726 chaseStack[chaseStackPointer].ff = ff;
1727 chaseStack[chaseStackPointer].rt = rt;
1728 chaseStack[chaseStackPointer].ft = ft;
1729 chaseStackPointer++;
1732 extern void ExistingAtacksCallback P((Board board, int flags, ChessMove kind,
1733 int rf, int ff, int rt, int ft,
1737 ExistingAttacksCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1738 { // for removing pre-exsting captures from the chaseStack, to be left with newly created ones
1740 register ChaseClosure *cl = (ChaseClosure *) closure; //closure tells us the move played in the repeat loop
1742 if(board[rt][ft] == EmptySquare) return; // no capture
1743 if(rf == cl->rf && ff == cl->ff) { // attacks with same piece from new position are not considered new
1744 rf = cl->rt; ff = cl->ft; // doctor their fromSquare so they will be recognized in chaseStack
1746 // search move in chaseStack, and delete it if it occurred there (as we know now it is not a new capture)
1747 for(i=0; i<chaseStackPointer; i++) {
1748 if(chaseStack[i].rf == rf && chaseStack[i].ff == ff &&
1749 chaseStack[i].rt == rt && chaseStack[i].ft == ft ) {
1750 // move found on chaseStack, delete it by overwriting with move popped from top of chaseStack
1751 chaseStack[i] = chaseStack[--chaseStackPointer];
1757 extern void ProtectedCallback P((Board board, int flags, ChessMove kind,
1758 int rf, int ff, int rt, int ft,
1762 ProtectedCallback (Board board, int flags, ChessMove kind, int rf, int ff, int rt, int ft, VOIDSTAR closure)
1763 { // for determining if a piece (given through the closure) is protected
1764 register ChaseClosure *cl = (ChaseClosure *) closure; // closure tells us where to recapture
1766 if(rt == cl->rt && ft == cl->ft) cl->recaptures++; // count legal recaptures to this square
1767 if(appData.debugMode && board[rt][ft] != EmptySquare)
1768 fprintf(debugFP, "try %c%c%c%c=%d\n", ff+AAA, rf+ONE,ft+AAA, rt+ONE, cl->recaptures);
1771 extern char moveList[MAX_MOVES][MOVE_LEN];
1774 PerpetualChase (int first, int last)
1775 { // this routine detects if the side to move in the 'first' position is perpetually chasing (when not checking)
1778 ChessSquare captured;
1780 preyStackPointer = 0; // clear stack of chased pieces
1781 for(i=first; i<last; i+=2) { // for all positions with same side to move
1782 if(appData.debugMode) fprintf(debugFP, "judge position %i\n", i);
1783 chaseStackPointer = 0; // clear stack that is going to hold possible chases
1784 // determine all captures possible after the move, and put them on chaseStack
1785 GenLegal(boards[i+1], PosFlags(i), AttacksCallback, &cl, EmptySquare);
1786 if(appData.debugMode) { int n;
1787 for(n=0; n<chaseStackPointer; n++)
1788 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1789 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1790 fprintf(debugFP, ": all capts\n");
1792 // determine all captures possible before the move, and delete them from chaseStack
1793 cl.rf = moveList[i][1]-ONE; // prepare closure to pass move that led from i to i+1
1794 cl.ff = moveList[i][0]-AAA+BOARD_LEFT;
1795 cl.rt = moveList[i][3]-ONE;
1796 cl.ft = moveList[i][2]-AAA+BOARD_LEFT;
1797 CopyBoard(xqCheckers, nullBoard); xqCheckers[EP_STATUS] = 1; // giant kludge to make GenLegal ignore pre-existing checks
1798 GenLegal(boards[i], PosFlags(i), ExistingAttacksCallback, &cl, EmptySquare);
1799 xqCheckers[EP_STATUS] = 0; // disable the generation of quasi-legal moves again
1800 if(appData.debugMode) { int n;
1801 for(n=0; n<chaseStackPointer; n++)
1802 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1803 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1804 fprintf(debugFP, ": new capts after %c%c%c%c\n", cl.ff+AAA, cl.rf+ONE, cl.ft+AAA, cl.rt+ONE);
1806 // chaseSack now contains all captures made possible by the move
1807 for(j=0; j<chaseStackPointer; j++) { // run through chaseStack to identify true chases
1808 int attacker = (int)boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1809 int victim = (int)boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1811 if(attacker >= (int) BlackPawn) attacker = BLACK_TO_WHITE attacker; // convert to white, as piecee type
1812 if(victim >= (int) BlackPawn) victim = BLACK_TO_WHITE victim;
1814 if((attacker == WhiteKnight || attacker == WhiteCannon) && victim == WhiteRook)
1815 continue; // C or H attack on R is always chase; leave on chaseStack
1817 if(attacker == victim) {
1818 if(LegalityTest(boards[i+1], PosFlags(i+1), chaseStack[j].rt,
1819 chaseStack[j].ft, chaseStack[j].rf, chaseStack[j].ff, NULLCHAR) == NormalMove) {
1820 // we can capture back with equal piece, so this is no chase but a sacrifice
1821 chaseStack[j] = chaseStack[--chaseStackPointer]; // delete the capture from the chaseStack
1822 j--; /* ! */ continue;
1827 // the attack is on a lower piece, or on a pinned or blocked equal one
1828 CopyBoard(xqCheckers, nullBoard); xqCheckers[EP_STATUS] = 1;
1829 CheckTest(boards[i+1], PosFlags(i+1), -1, -1, -1, -1, FALSE); // if we deliver check with our move, the checkers get marked
1830 // test if the victim is protected by a true protector. First make the capture.
1831 captured = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1832 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = boards[i+1][chaseStack[j].rf][chaseStack[j].ff];
1833 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = EmptySquare;
1834 // Then test if the opponent can recapture
1835 cl.recaptures = 0; // prepare closure to pass recapture square and count moves to it
1836 cl.rt = chaseStack[j].rt;
1837 cl.ft = chaseStack[j].ft;
1838 if(appData.debugMode) {
1839 fprintf(debugFP, "test if we can recapture %c%c\n", cl.ft+AAA, cl.rt+ONE);
1841 xqCheckers[EP_STATUS] = 2; // causes GenLegal to ignore the checks we delivered with the move, in real life evaded before we captured
1842 GenLegal(boards[i+1], PosFlags(i+1), ProtectedCallback, &cl, EmptySquare); // try all moves
1843 xqCheckers[EP_STATUS] = 0; // disable quasi-legal moves again
1844 // unmake the capture
1845 boards[i+1][chaseStack[j].rf][chaseStack[j].ff] = boards[i+1][chaseStack[j].rt][chaseStack[j].ft];
1846 boards[i+1][chaseStack[j].rt][chaseStack[j].ft] = captured;
1847 // if a recapture was found, piece is protected, and we are not chasing it.
1848 if(cl.recaptures) { // attacked piece was defended by true protector, no chase
1849 chaseStack[j] = chaseStack[--chaseStackPointer]; // so delete from chaseStack
1853 // chaseStack now contains all moves that chased
1854 if(appData.debugMode) { int n;
1855 for(n=0; n<chaseStackPointer; n++)
1856 fprintf(debugFP, "%c%c%c%c ", chaseStack[n].ff+AAA, chaseStack[n].rf+ONE,
1857 chaseStack[n].ft+AAA, chaseStack[n].rt+ONE);
1858 fprintf(debugFP, ": chases\n");
1860 if(i == first) { // copy all people chased by first move of repeat cycle to preyStack
1861 for(j=0; j<chaseStackPointer; j++) {
1862 preyStack[j].rank = chaseStack[j].rt;
1863 preyStack[j].file = chaseStack[j].ft;
1865 preyStackPointer = chaseStackPointer;
1868 for(j=0; j<chaseStackPointer; j++) {
1869 for(k=0; k<preyStackPointer; k++) {
1870 // search the victim of each chase move on the preyStack (first occurrence)
1871 if(chaseStack[j].ft == preyStack[k].file && chaseStack[j].rt == preyStack[k].rank ) {
1872 if(k < tail) break; // piece was already identified as still being chased
1873 preyStack[preyStackPointer] = preyStack[tail]; // move chased piece to bottom part of preyStack
1874 preyStack[tail] = preyStack[k]; // by swapping
1875 preyStack[k] = preyStack[preyStackPointer];
1881 preyStackPointer = tail; // keep bottom part of preyStack, popping pieces unchased on move i.
1882 if(appData.debugMode) { int n;
1883 for(n=0; n<preyStackPointer; n++)
1884 fprintf(debugFP, "%c%c ", preyStack[n].file+AAA, preyStack[n].rank+ONE);
1885 fprintf(debugFP, "always chased upto ply %d\n", i);
1887 // now adjust the location of the chased pieces according to opponent move
1888 for(j=0; j<preyStackPointer; j++) {
1889 if(preyStack[j].rank == moveList[i+1][1]-ONE &&
1890 preyStack[j].file == moveList[i+1][0]-AAA+BOARD_LEFT) {
1891 preyStack[j].rank = moveList[i+1][3]-ONE;
1892 preyStack[j].file = moveList[i+1][2]-AAA+BOARD_LEFT;
1897 return preyStackPointer ? 256*(preyStack[preyStackPointer].file - BOARD_LEFT + AAA) + (preyStack[preyStackPointer].rank + ONE)
1898 : 0; // if any piece was left on preyStack, it has been perpetually chased,and we return the