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