changes by H.G. Muller; version 4.3.4
[xboard.git] / moves.h
1 /*\r
2  * moves.h - Move generation and checking\r
3  * $Id: moves.h,v 2.1 2003/10/27 19:21:00 mann Exp $\r
4  *\r
5  * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.\r
6  * Enhancements Copyright 1992-95 Free Software Foundation, Inc.\r
7  *\r
8  * The following terms apply to Digital Equipment Corporation's copyright\r
9  * interest in XBoard:\r
10  * ------------------------------------------------------------------------\r
11  * All Rights Reserved\r
12  *\r
13  * Permission to use, copy, modify, and distribute this software and its\r
14  * documentation for any purpose and without fee is hereby granted,\r
15  * provided that the above copyright notice appear in all copies and that\r
16  * both that copyright notice and this permission notice appear in\r
17  * supporting documentation, and that the name of Digital not be\r
18  * used in advertising or publicity pertaining to distribution of the\r
19  * software without specific, written prior permission.\r
20  *\r
21  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING\r
22  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL\r
23  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR\r
24  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\r
25  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,\r
26  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
27  * SOFTWARE.\r
28  * ------------------------------------------------------------------------\r
29  *\r
30  * The following terms apply to the enhanced version of XBoard distributed\r
31  * by the Free Software Foundation:\r
32  * ------------------------------------------------------------------------\r
33  * This program is free software; you can redistribute it and/or modify\r
34  * it under the terms of the GNU General Public License as published by\r
35  * the Free Software Foundation; either version 2 of the License, or\r
36  * (at your option) any later version.\r
37  *\r
38  * This program is distributed in the hope that it will be useful,\r
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
41  * GNU General Public License for more details.\r
42  *\r
43  * You should have received a copy of the GNU General Public License\r
44  * along with this program; if not, write to the Free Software\r
45  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
46  * ------------------------------------------------------------------------\r
47  */\r
48 \r
49 extern ChessSquare PromoPiece P((ChessMove moveType));\r
50 extern ChessMove PromoCharToMoveType P((int whiteOnMove, int promoChar));\r
51 extern char PieceToChar P((ChessSquare p));\r
52 extern ChessSquare CharToPiece P((int c));\r
53 \r
54 extern void CopyBoard P((Board to, Board from));\r
55 extern int CompareBoards P((Board board1, Board board2));\r
56 extern char pieceToChar[(int)EmptySquare+1];\r
57 \r
58 typedef void (*MoveCallback) P((Board board, int flags, ChessMove kind,\r
59                                 int rf, int ff, int rt, int ft,\r
60                                 VOIDSTAR closure));\r
61 \r
62 /* Values for flags arguments */\r
63 #define F_WHITE_ON_MOVE 1\r
64 #define F_WHITE_KCASTLE_OK 2\r
65 #define F_WHITE_QCASTLE_OK 4\r
66 #define F_BLACK_KCASTLE_OK 8\r
67 #define F_BLACK_QCASTLE_OK 16\r
68 #define F_ALL_CASTLE_OK (F_WHITE_KCASTLE_OK | F_WHITE_QCASTLE_OK | \\r
69                          F_BLACK_KCASTLE_OK | F_BLACK_QCASTLE_OK)\r
70 #define F_IGNORE_CHECK 32\r
71 #define F_KRIEGSPIEL_CAPTURE 64 /* pawns can try to capture invisible pieces */\r
72 #define F_ATOMIC_CAPTURE 128    /* capturing piece explodes, destroying itself\r
73                                    and all non-pawns on adjacent squares; \r
74                                    destroying your own king is illegal */\r
75 \r
76 /* Special epfile values. [HGM] positive values are non-reversible moves! */\r
77 #define EP_NONE (-4)           /* [HGM] Tricky! order matters:            */\r
78 #define EP_UNKNOWN (-1)        /*       >= EP_UNKNOWN spils rep-draw      */\r
79 #define EP_CAPTURE (-2)        /*       <= EP_NONE is reversible move     */\r
80 #define EP_PAWN_MOVE (-3)\r
81 #define EP_REP_DRAW   (-15)\r
82 #define EP_RULE_DRAW  (-14)\r
83 #define EP_INSUF_DRAW  (-13)\r
84 #define EP_DRAWS (-10)\r
85 \r
86 /* Call callback once for each pseudo-legal move in the given\r
87    position, except castling moves.  A move is pseudo-legal if it is\r
88    legal, or if it would be legal except that it leaves the king in\r
89    check.  In the arguments, epfile is EP_NONE if the previous move\r
90    was not a double pawn push, or the file 0..7 if it was, or\r
91    EP_UNKNOWN if we don't know and want to allow all e.p. captures.\r
92    Promotion moves generated are to Queen only.\r
93 */\r
94 extern void GenPseudoLegal P((Board board, int flags, int epfile,\r
95                               MoveCallback callback, VOIDSTAR closure));\r
96 \r
97 /* Like GenPseudoLegal, but include castling moves and (unless \r
98    F_IGNORE_CHECK is set in the flags) omit moves that would leave the\r
99    king in check.  The CASTLE_OK flags are true if castling is not yet\r
100    ruled out by a move of the king or rook.  Return TRUE if the player\r
101    on move is currently in check and F_IGNORE_CHECK is not set.\r
102 */\r
103 extern int GenLegal P((Board board, int flags, int epfile,\r
104                         char castlingRights[], /* [HGM] */\r
105                         MoveCallback callback, VOIDSTAR closure));\r
106 \r
107 /* If the player on move were to move from (rf, ff) to (rt, ft), would\r
108    he leave himself in check?  Or if rf == -1, is the player on move\r
109    in check now?  enPassant must be TRUE if the indicated move is an\r
110    e.p. capture.  The possibility of castling out of a check along the\r
111    back rank is not accounted for (i.e., we still return nonzero), as\r
112    this is illegal anyway.  Return value is the number of times the\r
113    king is in check. */ \r
114 extern int CheckTest P((Board board, int flags,\r
115                         int rf, int ff, int rt, int ft, int enPassant));\r
116 \r
117 /* Is a move from (rf, ff) to (rt, ft) legal for the player whom the\r
118    flags say is on move?  Other arguments as in GenPseudoLegal.\r
119    Returns the type of move made, taking promoChar into account. */\r
120 extern ChessMove LegalityTest P((Board board, int flags, int epfile,\r
121                                  char castlingRights[], /* [HGM] */\r
122                                  int rf, int ff, int rt, int ft,\r
123                                  int promoChar));\r
124 \r
125 #define MT_NONE 0\r
126 #define MT_CHECK 1\r
127 #define MT_CHECKMATE 2\r
128 #define MT_STALEMATE 3\r
129 \r
130 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */\r
131 extern int MateTest P((Board board, int flags, int epfile,\r
132                                         char castlingRights[])); /* [HGM] */\r
133 \r
134 typedef struct {\r
135     /* Input data */\r
136     ChessSquare pieceIn;        /* EmptySquare if unknown */\r
137     int rfIn, ffIn, rtIn, ftIn; /* -1 if unknown */\r
138     int promoCharIn;            /* NULLCHAR if unknown */\r
139     /* Output data for matched move */\r
140     ChessMove kind;\r
141     ChessSquare piece;\r
142     int rf, ff, rt, ft;\r
143     int promoChar; /* 'q' if a promotion and promoCharIn was NULLCHAR */\r
144     int count;     /* Number of possibilities found */\r
145 } DisambiguateClosure;\r
146 \r
147 /* Disambiguate a partially-known move */\r
148 void Disambiguate P((Board board, int flags, int epfile,\r
149                      DisambiguateClosure *closure));\r
150 \r
151 \r
152 /* Convert coordinates to normal algebraic notation.\r
153    promoChar must be NULLCHAR or '.' if not a promotion.\r
154 */\r
155 ChessMove CoordsToAlgebraic P((Board board, int flags, int epfile,\r
156                                int rf, int ff, int rt, int ft,\r
157                                int promoChar, char out[MOVE_LEN]));\r