changes from H.G. Muller; version 4.3.14
[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 extern int PieceToNumber P((ChessSquare p));\r
54 \r
55 extern void CopyBoard P((Board to, Board from));\r
56 extern int CompareBoards P((Board board1, Board board2));\r
57 extern char pieceToChar[(int)EmptySquare+1];\r
58 \r
59 typedef void (*MoveCallback) P((Board board, int flags, ChessMove kind,\r
60                                 int rf, int ff, int rt, int ft,\r
61                                 VOIDSTAR closure));\r
62 \r
63 /* Values for flags arguments */\r
64 #define F_WHITE_ON_MOVE 1\r
65 #define F_WHITE_KCASTLE_OK 2\r
66 #define F_WHITE_QCASTLE_OK 4\r
67 #define F_BLACK_KCASTLE_OK 8\r
68 #define F_BLACK_QCASTLE_OK 16\r
69 #define F_ALL_CASTLE_OK (F_WHITE_KCASTLE_OK | F_WHITE_QCASTLE_OK | \\r
70                          F_BLACK_KCASTLE_OK | F_BLACK_QCASTLE_OK)\r
71 #define F_IGNORE_CHECK 32\r
72 #define F_KRIEGSPIEL_CAPTURE 64 /* pawns can try to capture invisible pieces */\r
73 #define F_ATOMIC_CAPTURE 128    /* capturing piece explodes, destroying itself\r
74                                    and all non-pawns on adjacent squares; \r
75                                    destroying your own king is illegal */\r
76 #define F_FRC_TYPE_CASTLING 256 /* generate castlings as captures of own Rook */\r
77 \r
78 /* Special epfile values. [HGM] positive values are non-reversible moves! */\r
79 #define EP_NONE (-4)           /* [HGM] Tricky! order matters:            */\r
80 #define EP_UNKNOWN (-1)        /*       >= EP_UNKNOWN spils rep-draw      */\r
81 #define EP_CAPTURE (-2)        /*       <= EP_NONE is reversible move     */\r
82 #define EP_PAWN_MOVE (-3)\r
83 #define EP_REP_DRAW   (-15)\r
84 #define EP_RULE_DRAW  (-14)\r
85 #define EP_INSUF_DRAW  (-13)\r
86 #define EP_DRAWS (-10)\r
87 #define EP_BEROLIN_A 16        /* [HGM] berolina: add to file if pawn to be taken of a-side of e.p.file */\r
88 \r
89 /* Call callback once for each pseudo-legal move in the given\r
90    position, except castling moves.  A move is pseudo-legal if it is\r
91    legal, or if it would be legal except that it leaves the king in\r
92    check.  In the arguments, epfile is EP_NONE if the previous move\r
93    was not a double pawn push, or the file 0..7 if it was, or\r
94    EP_UNKNOWN if we don't know and want to allow all e.p. captures.\r
95    Promotion moves generated are to Queen only.\r
96 */\r
97 extern void GenPseudoLegal P((Board board, int flags, int epfile,\r
98                               MoveCallback callback, VOIDSTAR closure));\r
99 \r
100 /* Like GenPseudoLegal, but include castling moves and (unless \r
101    F_IGNORE_CHECK is set in the flags) omit moves that would leave the\r
102    king in check.  The CASTLE_OK flags are true if castling is not yet\r
103    ruled out by a move of the king or rook.  Return TRUE if the player\r
104    on move is currently in check and F_IGNORE_CHECK is not set.\r
105 */\r
106 extern int GenLegal P((Board board, int flags, int epfile,\r
107                         char castlingRights[], /* [HGM] */\r
108                         MoveCallback callback, VOIDSTAR closure));\r
109 \r
110 /* If the player on move were to move from (rf, ff) to (rt, ft), would\r
111    he leave himself in check?  Or if rf == -1, is the player on move\r
112    in check now?  enPassant must be TRUE if the indicated move is an\r
113    e.p. capture.  The possibility of castling out of a check along the\r
114    back rank is not accounted for (i.e., we still return nonzero), as\r
115    this is illegal anyway.  Return value is the number of times the\r
116    king is in check. */ \r
117 extern int CheckTest P((Board board, int flags,\r
118                         int rf, int ff, int rt, int ft, int enPassant));\r
119 \r
120 /* Is a move from (rf, ff) to (rt, ft) legal for the player whom the\r
121    flags say is on move?  Other arguments as in GenPseudoLegal.\r
122    Returns the type of move made, taking promoChar into account. */\r
123 extern ChessMove LegalityTest P((Board board, int flags, int epfile,\r
124                                  char castlingRights[], /* [HGM] */\r
125                                  int rf, int ff, int rt, int ft,\r
126                                  int promoChar));\r
127 \r
128 #define MT_NONE 0\r
129 #define MT_CHECK 1\r
130 #define MT_CHECKMATE 2\r
131 #define MT_STALEMATE 3\r
132 \r
133 /* Return MT_NONE, MT_CHECK, MT_CHECKMATE, or MT_STALEMATE */\r
134 extern int MateTest P((Board board, int flags, int epfile,\r
135                                         char castlingRights[])); /* [HGM] */\r
136 \r
137 typedef struct {\r
138     /* Input data */\r
139     ChessSquare pieceIn;        /* EmptySquare if unknown */\r
140     int rfIn, ffIn, rtIn, ftIn; /* -1 if unknown */\r
141     int promoCharIn;            /* NULLCHAR if unknown */\r
142     /* Output data for matched move */\r
143     ChessMove kind;\r
144     ChessSquare piece;\r
145     int rf, ff, rt, ft;\r
146     int promoChar; /* 'q' if a promotion and promoCharIn was NULLCHAR */\r
147     int count;     /* Number of possibilities found */\r
148 } DisambiguateClosure;\r
149 \r
150 /* Disambiguate a partially-known move */\r
151 void Disambiguate P((Board board, int flags, int epfile,\r
152                      DisambiguateClosure *closure));\r
153 \r
154 \r
155 /* Convert coordinates to normal algebraic notation.\r
156    promoChar must be NULLCHAR or '.' if not a promotion.\r
157 */\r
158 ChessMove CoordsToAlgebraic P((Board board, int flags, int epfile,\r
159                                int rf, int ff, int rt, int ft,\r
160                                int promoChar, char out[MOVE_LEN]));\r