/*
* moves.c - Move generation and checking
- * $Id: moves.c,v 2.1 2003/10/27 19:21:00 mann Exp $
*
* Copyright 1991 by Digital Equipment Corporation, Maynard,
- * Massachusetts. Enhancements Copyright
- * 1992-2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software
- * Foundation, Inc.
+ * Massachusetts.
+ *
+ * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
+ * 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+ *
+ * Enhancements Copyright 2005 Alessandro Scotti
*
* The following terms apply to Digital Equipment Corporation's copyright
* interest in XBoard:
int SameColor P((ChessSquare, ChessSquare));
int PosFlags(int index);
-extern char initialRights[BOARD_SIZE]; /* [HGM] all rights enabled, set in InitPosition */
+extern signed char initialRights[BOARD_SIZE]; /* [HGM] all rights enabled, set in InitPosition */
int WhitePiece(piece)
char pieceToChar[] = {
'P', 'N', 'B', 'R', 'Q', 'F', 'E', 'A', 'C', 'W', 'M',
- 'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 's', 'U', 'K',
+ 'O', 'H', 'I', 'J', 'G', 'D', 'V', 'L', 'S', 'U', 'K',
'p', 'n', 'b', 'r', 'q', 'f', 'e', 'a', 'c', 'w', 'm',
'o', 'h', 'i', 'j', 'g', 'd', 'v', 'l', 's', 'u', 'k',
'x' };
{
int rf, ff;
int i, j, d, s, fs, rs, rt, ft, m;
+ int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
for (rf = 0; rf < BOARD_HEIGHT; rf++)
for (ff = BOARD_LEFT; ff < BOARD_RGHT; ff++) {
}
if (rf < BOARD_HEIGHT-1 && board[rf + 1][ff] == EmptySquare) {
callback(board, flags,
- rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
+ rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotionQueen : NormalMove,
rf, ff, rf + 1, ff, closure);
}
if (rf == 1 && board[2][ff] == EmptySquare &&
((flags & F_KRIEGSPIEL_CAPTURE) ||
BlackPiece(board[rf + 1][ff + s]))) {
callback(board, flags,
- rf == BOARD_HEIGHT-2 ? WhitePromotionQueen : NormalMove,
+ rf >= BOARD_HEIGHT-1-promoRank ? WhitePromotionQueen : NormalMove,
rf, ff, rf + 1, ff + s, closure);
}
if (rf == BOARD_HEIGHT-4) {
}
if (rf > 0 && board[rf - 1][ff] == EmptySquare) {
callback(board, flags,
- rf == 1 ? BlackPromotionQueen : NormalMove,
+ rf <= promoRank ? BlackPromotionQueen : NormalMove,
rf, ff, rf - 1, ff, closure);
}
if (rf == BOARD_HEIGHT-2 && board[BOARD_HEIGHT-3][ff] == EmptySquare &&
((flags & F_KRIEGSPIEL_CAPTURE) ||
WhitePiece(board[rf - 1][ff + s]))) {
callback(board, flags,
- rf == 1 ? BlackPromotionQueen : NormalMove,
+ rf <= promoRank ? BlackPromotionQueen : NormalMove,
rf, ff, rf - 1, ff + s, closure);
}
if (rf == 3) {
if (board[rt][ft] != EmptySquare) break;
}
if(m==1) goto mounted;
- if(m==2) goto finishGold;
+ if(m==2) goto finishSilver;
break;
case WhiteQueen:
/* Shogi Pawn and Silver General: first the Pawn move, */
/* then the General continues like a Ferz */
+ case WhiteMan:
+ if(gameInfo.variant != VariantMakruk) goto commoner;
case SHOGI WhitePawn:
case SHOGI WhiteFerz:
if (rf < BOARD_HEIGHT-1 &&
if(piece != SHOGI WhitePawn) goto finishSilver;
break;
+ case BlackMan:
+ if(gameInfo.variant != VariantMakruk) goto commoner;
case SHOGI BlackPawn:
case SHOGI BlackFerz:
if (rf > 0 &&
case WhiteSilver:
case BlackSilver:
m++; // [HGM] superchess: use for Centaur
- case WhiteMan:
- case BlackMan:
+ commoner:
case SHOGI WhiteKing:
case SHOGI BlackKing:
case WhiteKing:
}
}
break;
+ case WhiteFalcon: // [HGM] wild: for wildcards, self-capture symbolizes move to anywhere
+ case BlackFalcon:
+ case WhiteCobra:
+ case BlackCobra:
+ case WhiteLance:
+ case BlackLance:
+ callback(board, flags, NormalMove, rf, ff, rf, ff, closure);
+ break;
}
}
}
}
- if(gameInfo.variant == VariantFischeRandom) {
+ if(flags & F_FRC_TYPE_CASTLING) {
/* generate all potential FRC castling moves (KxR), ignoring flags */
/* [HGM] test if the Rooks we find have castling rights */
if ((flags & F_WHITE_ON_MOVE) != 0) {
ff = castlingRights[2]; /* King file if we have any rights */
if(ff > 0 && board[0][ff] == WhiteKing) {
- if (appData.debugMode) {
- fprintf(debugFP, "FRC castling, %d %d %d %d %d %d\n",
- castlingRights[0],castlingRights[1],ff,castlingRights[3],castlingRights[4],castlingRights[5]);
- }
ft = castlingRights[0]; /* Rook file if we have H-side rights */
left = ff+1;
right = BOARD_RGHT-2;
}
}
- return cl.check;
+ return cl.fking < BOARD_RGHT ? cl.check : 1000; // [HGM] atomic: return 1000 if we have no king
}
{
register LegalityTestClosure *cl = (LegalityTestClosure *) closure;
-// if (appData.debugMode) {
-// fprintf(debugFP, "Legality test: %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
-// }
if(board[rt][ft] != EmptySquare || kind==WhiteCapturesEnPassant || kind==BlackCapturesEnPassant)
cl->captures++; // [HGM] losers: count legal captures
if (rf == cl->rf && ff == cl->ff && rt == cl->rt && ft == cl->ft)
{
LegalityTestClosure cl; ChessSquare piece = board[rf][ff];
- if (appData.debugMode) {
- int i;
- for(i=0; i<6; i++) fprintf(debugFP, "%d ", castlingRights[i]);
- fprintf(debugFP, "Legality test? %c%c%c%c\n", ff+AAA, rf+ONE, ft+AAA, rt+ONE);
- }
/* [HGM] Lance, Cobra and Falcon are wildcard pieces; consider all their moves legal */
/* (perhaps we should disallow moves that obviously leave us in check?) */
if(piece == WhiteFalcon || piece == BlackFalcon ||
piece == WhiteCobra || piece == BlackCobra ||
piece == WhiteLance || piece == BlackLance)
- return NormalMove;
+ return CheckTest(board, flags, rf, ff, rt, ft, FALSE) ? IllegalMove : NormalMove;
cl.rf = rf;
cl.ff = ff;
char castlingRights[];
{
MateTestClosure cl;
- int inCheck;
+ int inCheck, r, f, myPieces=0, hisPieces=0, nrKing=0;
+ ChessSquare king = flags & F_WHITE_ON_MOVE ? WhiteKing : BlackKing;
+
+ for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
+ // [HGM] losers: Count pieces and kings, to detect other unorthodox winning conditions
+ nrKing += (board[r][f] == king); // stm has king
+ if( board[r][f] != EmptySquare ) {
+ if((int)board[r][f] <= (int)king && (int)board[r][f] >= (int)king - (int)WhiteKing + (int)WhitePawn)
+ myPieces++;
+ else hisPieces++;
+ }
+ }
+ switch(gameInfo.variant) { // [HGM] losers: extinction wins
+ case VariantShatranj:
+ if(hisPieces == 1) return myPieces > 1 ? MT_BARE : MT_DRAW;
+ default:
+ break;
+ case VariantAtomic:
+ if(nrKing == 0) return MT_NOKING;
+ break;
+ case VariantLosers:
+ if(myPieces == 1) return MT_BARE;
+ }
cl.count = 0;
inCheck = GenLegal(board, flags, epfile, castlingRights, MateTestCallback, (VOIDSTAR) &cl);
+ // [HGM] 3check: yet to do!
if (cl.count > 0) {
return inCheck ? MT_CHECK : MT_NONE;
} else {
- return inCheck || gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj ?
- MT_CHECKMATE : MT_STALEMATE;
+ if(gameInfo.variant == VariantSuicide) // [HGM] losers: always stalemate, since no check, but result varies
+ return myPieces == hisPieces ? MT_STALEMATE :
+ myPieces > hisPieces ? MT_STAINMATE : MT_STEALMATE;
+ else if(gameInfo.variant == VariantLosers) return inCheck ? MT_TRICKMATE : MT_STEALMATE;
+ else if(gameInfo.variant == VariantGiveaway) return MT_STEALMATE; // no check exists, stalemated = win
+
+ return inCheck ? MT_CHECKMATE
+ : (gameInfo.variant == VariantXiangqi || gameInfo.variant == VariantShatranj) ?
+ MT_STAINMATE : MT_STALEMATE;
}
}
VOIDSTAR closure;
{
register DisambiguateClosure *cl = (DisambiguateClosure *) closure;
+ int wildCard = FALSE; ChessSquare piece = board[rf][ff];
+
+ // [HGM] wild: for wild-card pieces rt and rf are dummies
+ if(piece == WhiteFalcon || piece == BlackFalcon ||
+ piece == WhiteCobra || piece == BlackCobra ||
+ piece == WhiteLance || piece == BlackLance)
+ wildCard = TRUE;
if ((cl->pieceIn == EmptySquare || cl->pieceIn == board[rf][ff]
|| PieceToChar(board[rf][ff]) == '~'
) &&
(cl->rfIn == -1 || cl->rfIn == rf) &&
(cl->ffIn == -1 || cl->ffIn == ff) &&
- (cl->rtIn == -1 || cl->rtIn == rt) &&
- (cl->ftIn == -1 || cl->ftIn == ft)) {
+ (cl->rtIn == -1 || cl->rtIn == rt || wildCard) &&
+ (cl->ftIn == -1 || cl->ftIn == ft || wildCard)) {
cl->count++;
cl->piece = board[rf][ff];
cl->rf = rf;
cl->ff = ff;
- cl->rt = rt;
- cl->ft = ft;
+ cl->rt = wildCard ? cl->rtIn : rt;
+ cl->ft = wildCard ? cl->ftIn : ft;
cl->kind = kind;
}
}
DisambiguateClosure *closure;
{
int illegal = 0; char c = closure->promoCharIn;
+
closure->count = 0;
closure->rf = closure->ff = closure->rt = closure->ft = 0;
closure->kind = ImpossibleMove;
- if (appData.debugMode) {
- fprintf(debugFP, "Disambiguate in: %d(%d,%d)-(%d,%d) = %d (%c)\n",
- closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
- closure->promoCharIn, closure->promoCharIn >= ' ' ? closure->promoCharIn : '-');
- }
+
GenLegal(board, flags, epfile, initialRights, DisambiguateCallback, (VOIDSTAR) closure);
if (closure->count == 0) {
/* See if it's an illegal move due to check */
(VOIDSTAR) closure);
if (closure->count == 0) {
/* No, it's not even that */
- if (appData.debugMode) { int i, j;
- for(i=BOARD_HEIGHT-1; i>=0; i--) {
- for(j=0; j<BOARD_WIDTH; j++)
- fprintf(debugFP, "%3d", (int) board[i][j]);
- fprintf(debugFP, "\n");
- }
- }
return;
}
}
/* [HGM] Shogi promotions. '=' means defer */
if(closure->rfIn != DROP_RANK && closure->kind == NormalMove) {
ChessSquare piece = closure->piece;
-#if 0
- if (appData.debugMode) {
- fprintf(debugFP, "Disambiguate A: %d(%d,%d)-(%d,%d) = %d (%c)\n",
- closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
- closure->promoCharIn,closure->promoCharIn);
- }
-#endif
if(c != NULLCHAR && c != 'x' && c != '+' && c != '=' &&
ToUpper(PieceToChar(PROMOTED piece)) != ToUpper(c) )
closure->kind = IllegalMove;
else if(flags & F_WHITE_ON_MOVE) {
-#if 0
- if (appData.debugMode) {
- fprintf(debugFP, "Disambiguate B: %d(%d,%d)-(%d,%d) = %d (%c)\n",
- closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
- closure->promoCharIn,closure->promoCharIn);
- }
-#endif
if( (int) piece < (int) WhiteWazir &&
(closure->rf > BOARD_HEIGHT-4 || closure->rt > BOARD_HEIGHT-4) ) {
if( (piece == WhitePawn || piece == WhiteQueen) && closure->rt > BOARD_HEIGHT-2 ||
closure->kind = IllegalMove;
}
}
-#if 0
- if (appData.debugMode) {
- fprintf(debugFP, "Disambiguate C: %d(%d,%d)-(%d,%d) = %d (%c)\n",
- closure->pieceIn,closure->ffIn,closure->rfIn,closure->ftIn,closure->rtIn,
- closure->promoCharIn,closure->promoCharIn);
- }
-#endif
/* [HGM] returns 'q' for optional promotion, 'n' for mandatory */
if(closure->promoCharIn != '=')
closure->promoChar = ToLower(closure->promoCharIn);
if(closure->kind == IllegalMove)
/* [HGM] might be a variant we don't understand, pass on promotion info */
closure->promoChar = ToLower(closure->promoCharIn);
- if (appData.debugMode) {
- fprintf(debugFP, "Disambiguate out: %d(%d,%d)-(%d,%d) = %d (%c)\n",
- closure->piece,closure->ff,closure->rf,closure->ft,closure->rt,closure->promoChar,
- closure->promoChar >= ' ' ? closure->promoChar:'-');
- }
}
piece = board[rf][ff];
if(PieceToChar(piece)=='~') piece = (ChessSquare)(DEMOTED piece);
- if (appData.debugMode)
- fprintf(debugFP, "CoordsToAlgebraic, piece=%d (%d,%d)-(%d,%d) %c\n", (int)piece,ff,rf,ft,rt,promoChar >= ' ' ? promoChar : '-');
switch (piece) {
case WhitePawn:
case BlackPawn:
}
/* Use promotion suffix style "=Q" */
*outp = NULLCHAR;
- if (appData.debugMode)
- fprintf(debugFP, "movetype=%d, promochar=%d=%c\n", (int)kind, promoChar, promoChar >= ' ' ? promoChar : '-');
if (promoChar != NULLCHAR) {
if(gameInfo.variant == VariantShogi) {
/* [HGM] ... but not in Shogi! */