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