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