Legality testing (mandatory capture) in giveaway variants
authorH.G. Muller <h.g.muller@hccnet.nl>
Sat, 13 Jun 2009 16:16:47 +0000 (09:16 -0700)
committerArun Persaud <arun@nubati.net>
Sat, 13 Jun 2009 16:16:47 +0000 (09:16 -0700)
moves.c
moves.h
parser.l
winboard/parser.c

diff --git a/moves.c b/moves.c
index 56b4cb1..c11a8f3 100644 (file)
--- a/moves.c
+++ b/moves.c
@@ -727,6 +727,7 @@ void GenLegalCallback(board, flags, kind, rf, ff, rt, ft, closure)
 typedef struct {
     int rf, ff, rt, ft;
     ChessMove kind;
+    int captures; // [HGM] losers
 } LegalityTestClosure;
 
 
@@ -1026,6 +1027,8 @@ void LegalityTestCallback(board, flags, kind, rf, ff, rt, ft, 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)
       cl->kind = kind;
 }
@@ -1055,7 +1058,11 @@ ChessMove LegalityTest(board, flags, epfile, castlingRights, rf, ff, rt, ft, pro
     cl.rt = rt;
     cl.ft = ft;
     cl.kind = IllegalMove;
+    cl.captures = 0; // [HGM] losers: prepare to count legal captures.
     GenLegal(board, flags, epfile, castlingRights, LegalityTestCallback, (VOIDSTAR) &cl);
+    if((flags & F_MANDATORY_CAPTURE) && cl.captures && board[rt][ft] == EmptySquare
+               && cl.kind != WhiteCapturesEnPassant && cl.kind != BlackCapturesEnPassant)
+       return(IllegalMove); // [HGM] losers: if there are legal captures, non-capts are illegal
 
     if(gameInfo.variant == VariantShogi) {
         /* [HGM] Shogi promotions. '=' means defer */
diff --git a/moves.h b/moves.h
index e81b105..50e6c17 100644 (file)
--- a/moves.h
+++ b/moves.h
@@ -77,6 +77,7 @@ typedef void (*MoveCallback) P((Board board, int flags, ChessMove kind,
                                   and all non-pawns on adjacent squares; 
                                   destroying your own king is illegal */
 #define F_FRC_TYPE_CASTLING 256 /* generate castlings as captures of own Rook */
+#define F_MANDATORY_CAPTURE 0x100
 
 /* Special epfile values. [HGM] positive values are non-reversible moves! */
 #define EP_NONE (-4)           /* [HGM] Tricky! order matters:            */
index ca41c63..29a2fa9 100755 (executable)
--- a/parser.l
+++ b/parser.l
@@ -243,7 +243,8 @@ extern void CopyBoard P((Board to, Board from));
       return (int) IllegalMove;\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\r
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+                         EP_UNKNOWN,\r
                           initialRights, /* [HGM] assume all castlings allowed */\r
                           currentMoveString[1] - ONE,\r
                           currentMoveString[0] - AAA,\r
@@ -304,7 +305,8 @@ extern void CopyBoard P((Board to, Board from));
       return 0;\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\r
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+                         EP_UNKNOWN,\r
                           initialRights, /* [HGM] assume all castlings allowed */\r
                           currentMoveString[1] - ONE,\r
                           currentMoveString[0] - AAA,\r
@@ -485,7 +487,8 @@ extern void CopyBoard P((Board to, Board from));
     }\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\r
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+                         EP_UNKNOWN,\r
                           initialRights, /* [HGM] assume all castlings allowed */\r
                           currentMoveString[1] - ONE,\r
                           currentMoveString[0] - AAA,\r
@@ -525,7 +528,8 @@ extern void CopyBoard P((Board to, Board from));
     }\r
 \r
     result = LegalityTest(boards[yyboardindex],\r
-                         PosFlags(yyboardindex), EP_UNKNOWN,\r
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!\r
+                         EP_UNKNOWN,\r
                           initialRights, /* [HGM] assume all castlings allowed */\r
                           currentMoveString[1] - ONE,\r
                           currentMoveString[0] - AAA,\r
@@ -706,7 +710,8 @@ extern void CopyBoard P((Board to, Board from));
         fprintf(debugFP, "long castling %d %d\n", ff, ft);\r
     }\r
     return (int) LegalityTest(boards[yyboardindex],\r
-                             PosFlags(yyboardindex), EP_UNKNOWN,\r
+                             PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
+                             EP_UNKNOWN,\r
                               castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
                              rf, ff, rt, ft, NULLCHAR);\r
 }\r
@@ -762,7 +767,8 @@ extern void CopyBoard P((Board to, Board from));
     }\r
 \r
     return (int) LegalityTest(boards[yyboardindex],\r
-                              PosFlags(yyboardindex), EP_UNKNOWN,\r
+                             PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!\r
+                             EP_UNKNOWN,\r
                               castlingRights[yyboardindex], /* [HGM] use true castling rights */\r
                              rf, ff, rt, ft, NULLCHAR);\r
 }\r
index ad2369e..81e4c76 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <stdio.h>
 
+
 /* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */
 #ifdef c_plusplus
 #ifndef __cplusplus
@@ -1708,8 +1709,10 @@ char *yytext;
  * parser.l -- lex parser of algebraic chess moves for XBoard
  * $Id: parser.l,v 2.1 2003/10/27 19:21:00 mann Exp $
  *
- * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
- * Enhancements Copyright 1992-95 Free Software Foundation, Inc.
+ * Copyright 1991 by Digital Equipment Corporation, Maynard,
+ * Massachusetts.  Enhancements Copyright
+ * 1992-2001,2002,2003,2004,2005,2006,2007,2008,2009 Free Software
+ * Foundation, Inc.
  *
  * The following terms apply to Digital Equipment Corporation's copyright
  * interest in XBoard:
@@ -1733,24 +1736,25 @@ char *yytext;
  * SOFTWARE.
  * ------------------------------------------------------------------------
  *
- * The following terms apply to the enhanced version of XBoard distributed
- * by the Free Software Foundation:
+ * The following terms apply to the enhanced version of XBoard
+ * distributed by the Free Software Foundation:
  * ------------------------------------------------------------------------
- * This program is free software; you can redistribute it and/or modify
+ *
+ * GNU XBoard is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU XBoard is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- * ------------------------------------------------------------------------
- */
+ * along with this program. If not, see http://www.gnu.org/licenses/.  
+ *
+ *------------------------------------------------------------------------
+ ** See the file ChangeLog for a revision history.  */
 
 /* This parser handles all forms of promotion.
  * The parser resolves ambiguous moves by searching and check-testing.
@@ -2197,7 +2201,8 @@ YY_RULE_SETUP
       return (int) IllegalMove;
 
     result = LegalityTest(boards[yyboardindex],
-                         PosFlags(yyboardindex), EP_UNKNOWN,
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
+                         EP_UNKNOWN,
                           initialRights, /* [HGM] assume all castlings allowed */
                           currentMoveString[1] - ONE,
                           currentMoveString[0] - AAA,
@@ -2260,7 +2265,8 @@ YY_RULE_SETUP
       return 0;
 
     result = LegalityTest(boards[yyboardindex],
-                         PosFlags(yyboardindex), EP_UNKNOWN,
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
+                         EP_UNKNOWN,
                           initialRights, /* [HGM] assume all castlings allowed */
                           currentMoveString[1] - ONE,
                           currentMoveString[0] - AAA,
@@ -2446,7 +2452,8 @@ YY_RULE_SETUP
     }
 
     result = LegalityTest(boards[yyboardindex],
-                         PosFlags(yyboardindex), EP_UNKNOWN,
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
+                         EP_UNKNOWN,
                           initialRights, /* [HGM] assume all castlings allowed */
                           currentMoveString[1] - ONE,
                           currentMoveString[0] - AAA,
@@ -2486,7 +2493,8 @@ YY_RULE_SETUP
     }
 
     result = LegalityTest(boards[yyboardindex],
-                         PosFlags(yyboardindex), EP_UNKNOWN,
+                         PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: might think we can e.p.!
+                         EP_UNKNOWN,
                           initialRights, /* [HGM] assume all castlings allowed */
                           currentMoveString[1] - ONE,
                           currentMoveString[0] - AAA,
@@ -2673,7 +2681,8 @@ YY_RULE_SETUP
         fprintf(debugFP, "long castling %d %d\n", ff, ft);
     }
     return (int) LegalityTest(boards[yyboardindex],
-                             PosFlags(yyboardindex), EP_UNKNOWN,
+                             PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
+                             EP_UNKNOWN,
                               castlingRights[yyboardindex], /* [HGM] use true castling rights */
                              rf, ff, rt, ft, NULLCHAR);
 }
@@ -2731,7 +2740,8 @@ YY_RULE_SETUP
     }
 
     return (int) LegalityTest(boards[yyboardindex],
-                              PosFlags(yyboardindex), EP_UNKNOWN,
+                             PosFlags(yyboardindex)&~F_MANDATORY_CAPTURE, // [HGM] losers: e.p.!
+                             EP_UNKNOWN,
                               castlingRights[yyboardindex], /* [HGM] use true castling rights */
                              rf, ff, rt, ft, NULLCHAR);
 }