Update copyright year to 2015.
[cboard.git] / libchess / chess.h
blobb0dfea749830e6de9b0cb189782a356f60d58390
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2015 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef CHESS_H
20 #define CHESS_H
22 #define PGN_TIME_FORMAT "%Y.%m.%d"
23 #define MAX_PGN_LINE_LEN 255
24 #define MAX_SAN_MOVE_LEN 7
25 #define MAX_PGN_NAG 8
27 #define VALIDRANK VALIDFILE
28 #define VALIDFILE(f) (f >= 1 && f <= 8)
29 #define RANKTOBOARD(r) (8 - r)
30 #define FILETOBOARD(c) (c - 1)
31 #define RANKTOINT(r) (r - '0')
32 #define FILETOINT(c) (c - ('a' - 1))
33 #define VALIDROW(r) (r >= '1' && r <= '8')
34 #define VALIDCOL(c) (c >= 'a' && c <= 'h')
35 #define INTTORANK(r) (r + '0')
36 #define INTTOFILE(f) (f + ('a' - 1))
38 #define SET_FLAG(var, f) (var |= f)
39 #define CLEAR_FLAG(var, f) (var &= ~(f))
40 #define TOGGLE_FLAG(var, f) (var ^= (f))
41 #define TEST_FLAG(var, f) (var & f)
43 enum {
44 OPEN_SQUARE, PAWN, BISHOP, ROOK, KNIGHT, QUEEN, KING
47 enum {
48 WHITE, BLACK
51 /* Game flags. */
52 #define GF_PERROR 0x0001 /* Parse error for this game. */
53 #define GF_ENPASSANT 0x0002 /* For En Passant validation. */
54 #define GF_GAMEOVER 0x0004 /* End of game. */
55 #define GF_WK_CASTLE 0x0008
56 #define GF_WQ_CASTLE 0x0010
57 #define GF_BK_CASTLE 0x0020
58 #define GF_BQ_CASTLE 0x0040
59 #define GF_BLACK_OPENING 0x0080
62 * The chess board.
64 typedef struct {
65 unsigned char icon; // The piece.
66 unsigned char valid: 1, // != 0 if this square is a valid move for the
67 // selected piece.
68 enpassant: 1;
69 } BOARD[8][8];
72 * PGN Roster tags.
74 typedef struct tags {
75 char *name; // Tag name.
76 char *value; // Tag value.
77 } TAG;
80 * Move history.
82 * g.hp is the pointer to the current history which may be .rav for
83 * Recursive Annotated Variations. The depth of recursion is kept track of in
84 * g.ravlevel.
86 typedef struct history {
87 char *move; // The SAN move text.
88 char *comment; // Annotation for this move.
89 unsigned char nag[MAX_PGN_NAG]; // Numeric Annotation Glyph.
90 char *fen; // Of the current board.
91 struct history **rav; // Variation of the current move.
92 } HISTORY;
95 * Keeps game state history for the root move.
97 typedef struct {
98 char *fen; // Game board state.
99 unsigned short flags;
100 unsigned short hindex;
101 HISTORY **hp; // Pointer to the root move.
102 } RAV;
105 * This is an array of 'games' structures. One for each game in a file, or
106 * the current game.
108 typedef struct game_s {
109 TAG **tag; // Roster tags.
110 HISTORY **history; // Move history for this game.
111 HISTORY **hp; // History pointer pointing to the location
112 // in *history used mainly for RAV.
113 RAV *rav; // Saved game states for the root move of RAV.
114 int ravlevel; // An index to *rav.
115 unsigned short hindex; // Current move in *hp.
116 unsigned short flags; // Game flags.
117 unsigned short oflags; // Game flags for the previous move.
118 unsigned char side: 1, // This playing side. BLACK or WHITE.
119 turn: 1; // BLACK or WHITE.
120 unsigned short ply; // Move count.
121 void *data; /* User data associated with this game. Must
122 * be freed by the user. */
123 // internal validation state
124 int validate;
125 int done_fen_tag;
126 int pgn_fen_tag;
127 int capture;
128 int castle;
129 int check;
130 int check_testing;
131 int kfile;
132 int krank;
133 int okfile;
134 int okrank;
135 int validate_find;
136 } *GAME;
139 * Global GAME array. pgn_new_game() appends to this array.
141 GAME *game;
144 * 'gindex' and 'gtotal' are the current and total number of games in 'game'.
146 int gindex, gtotal;
149 * Library configuration flags. These will affect all games.
151 typedef enum {
153 * When pgn_write() is called to write a game, write reduced PGN format.
154 * This will only write the seven tag roster and move text skipping any
155 * annotation. The type for this flag is an int.
157 PGN_REDUCED,
160 * The number of full moves to write per line. If 0 then pgn_write() will
161 * write as many as possible within 80 columns. The type for this flag
162 * is an int.
164 PGN_MPL,
167 * Normally when a parse error occurs in a game the game is flagged with
168 * GF_PERROR and the rest of the game is discarded and processing of the
169 * next game is done. When set and a parse error occurs the rest of the
170 * entire file will be discarded. The type for this flag is an int.
172 PGN_STOP_ON_ERROR,
174 #ifdef DEBUG
176 * If the following is set to a value > 0 and DEBUG was defined
177 * at compile time then debugging output will be written to
178 * "libchess.debug" in the current directory.
180 PGN_DEBUG,
181 #endif
184 * After PGN_PROGRESS amount of bytes have been read from a file, call
185 * PGN_PROGRESS_FUNC. The type for PGN_PROGRESS is a long.
186 * PGN_PROGRESS_FUNC is of type pgn_progress.
188 PGN_PROGRESS,
189 PGN_PROGRESS_FUNC,
192 * If set to 1 and an opponent can attack a castling square the castling
193 * move will not be a valid one.
195 PGN_STRICT_CASTLING,
196 } pgn_config_flag;
199 * Errors returned from the following functions.
201 typedef enum {
202 E_PGN_ERR = -1,
203 E_PGN_OK,
204 E_PGN_PARSE,
205 E_PGN_AMBIGUOUS,
206 E_PGN_INVALID
207 } pgn_error_t;
209 typedef struct {
210 FILE *fp;
211 char *filename;
212 char *tmpfile; /* For appending. */
213 int pipe;
214 } PGN_FILE;
217 * The prototype of the PGN_PROGRESS_FUNC function pointer.
219 typedef void (*pgn_progress)(long size, long offset);
222 * Sets config flag 'f' to the next argument. Returns E_PGN_OK on success or
223 * E_PGN_ERR if 'f' is an invalid flag or E_PGN_INVALID if 'val' is an invalid
224 * flag value.
226 pgn_error_t pgn_config_set(pgn_config_flag f, ...);
229 * Gets the value of config flag 'f'. The next argument should be a pointer of
230 * the config type which is set to the value of 'f'. Returns E_PGN_ERR if 'f'
231 * is an invalid flag or E_PGN_OK on success.
233 pgn_error_t pgn_config_get(pgn_config_flag f, ...);
236 * Returns E_PGN_OK if 'filename' is a recognized compressed filetype or
237 * E_PGN_ERR if not.
239 pgn_error_t pgn_is_compressed(const char *filename);
242 * Opens a file 'filename' with the given 'mode'. 'mode' should be "r" for
243 * reading, "w" for writing (will truncate if the file exists) or "a" for
244 * appending to an existing file or creating a new one. Returns E_PGN_OK on
245 * success and sets 'result' to a file handle for use will the other file
246 * functions or E_PGN_ERR if there is an error opening the file in which case
247 * errno will be set to the error or E_PGN_INVALID if 'mode' is an invalid
248 * mode or if 'filename' is not a regular file.
250 pgn_error_t pgn_open(const char *filename, const char *mode, PGN_FILE **result);
253 * Closes and free's a PGN file handle. Returns E_PGN_OK on success,
254 * E_PGN_INVALID if 'pgn' is NULL, or E_PGN_ERR if rename() failed.
256 pgn_error_t pgn_close(PGN_FILE *pgn);
259 * Parses a PGN_FILE which was opened with pgn_open(). If 'pgn' is NULL then a
260 * single empty game will be allocated. If there is a parsing error
261 * E_PGN_PARSE is returned, if there was a memory allocation error E_PGN_ERR
262 * is returned, otherwise E_PGN_OK is returned and the global 'gindex' is set
263 * to the last parsed game in the file and the global 'gtotal' is set to the
264 * total number of games in the file. The file should be closed with
265 * pgn_close() after processing.
267 pgn_error_t pgn_parse(PGN_FILE *pgn);
270 * Allocates a new game and increments 'gtotal'. 'gindex' is then set to the
271 * new game. Returns E_PGN_ERR if there was a memory allocation error or
272 * E_PGN_OK on success.
274 pgn_error_t pgn_new_game();
277 * Writes a PGN formatted game 'g' to a file which was opened with pgn_open().
278 * See 'pgn_config_flag' for output options. Returns E_PGN_ERR if there was a
279 * memory allocation or write error and E_PGN_OK on success.
281 pgn_error_t pgn_write(PGN_FILE *pgn, GAME g);
284 * Frees all games in the global 'game' array. Returns nothing.
286 void pgn_free_all(void);
289 * Frees a single game 'g'. Returns nothing.
291 void pgn_free(GAME g);
294 * Adds a tag 'name' with value 'value' to the pointer to array of TAG
295 * pointers 'dst'. If a duplicate tag 'name' was found then the existing tag
296 * is updated to the new 'value'. If 'value' is NULL, the tag is removed.
297 * Returns E_PGN_ERR if there was a memory allocation error or E_PGN_OK on
298 * success.
300 pgn_error_t pgn_tag_add(TAG ***dst, char *name, char *value);
303 * Returns the total number of tags in 't' or 0 if 't' is NULL.
305 int pgn_tag_total(TAG **t);
308 * Finds a tag 'name' in the structure array 't'. Returns the location in the
309 * array of the found tag or E_PGN_ERR if the tag could not be found.
311 pgn_error_t pgn_tag_find(TAG **t, const char *name);
314 * Sorts a tag array. The first seven tags are in order of the PGN standard so
315 * don't sort'em. Returns nothing.
317 void pgn_tag_sort(TAG **t);
320 * Frees a TAG array. Returns nothing.
322 void pgn_tag_free(TAG **);
325 * It initializes the board (b) to the FEN tag (if found) and sets the
326 * castling and enpassant info for the game 'g'. If 'fen' is set it should be
327 * a fen tag and will be parsed rather than the game 'g'.tag FEN tag. Returns
328 * E_PGN_OK on success or if there was both a FEN and SetUp tag with the SetUp
329 * tag set to 0. Returns E_PGN_PARSE if there was a FEN parse error, E_PGN_ERR
330 * if there was no FEN tag or there was a SetUp tag with a value of 0. Returns
331 * E_PGN_OK on success.
333 pgn_error_t pgn_board_init_fen(GAME g, BOARD b, char *fen);
336 * Creates a FEN tag from the current game 'g', history move (g.hindex) and
337 * board 'b'. Returns a FEN tag.
339 char *pgn_game_to_fen(GAME g, BOARD b);
342 * Resets or initializes a new game board 'b'. Returns nothing.
344 void pgn_board_init(BOARD b);
347 * Valididate move 'm' against the game state 'g' and game board 'b' and
348 * update board 'b'. 'm' is ensured to be in SAN format and 'frfr' will the
349 * file/rank representation of 'm' and should be freed. Returns E_PGN_PARSE if
350 * there was a move text parsing error, E_PGN_INVALID if the move is invalid,
351 * E_PGN_AMBIGUOUS if the move is invalid with ambiguities or E_PGN_OK if
352 * successful.
354 pgn_error_t pgn_parse_move(GAME g, BOARD b, char **m, char **frfr);
357 * Like pgn_parse_move() but don't modify game flags in 'g' or board 'b'.
359 pgn_error_t pgn_validate_move(GAME g, BOARD b, char **m, char **frfr);
362 * Returns the total number of moves in 'h' or 0 if there are none.
364 int pgn_history_total(HISTORY **h);
367 * Returns the history ply 'n' from 'h'. If 'n' is out of range then NULL is
368 * returned.
370 HISTORY *pgn_history_by_n(HISTORY **h, int n);
373 * Appends move 'm' to game 'g' history pointer and creates a FEN tag for the
374 * current game state using board 'b'. The FEN tag makes things faster than
375 * validating the entire move history by validating only the current move to
376 * the previous moves FEN tag. The history pointer may be a in a RAV so
377 * g->rav.hp is also updated to the new (realloc()'ed) pointer. If not in a
378 * RAV then g->history will be updated. Returns E_PGN_ERR if realloc() failed
379 * or E_PGN_OK on success.
381 pgn_error_t pgn_history_add(GAME g, BOARD b, const char *m);
384 * Deallocates all of the history data from position 'start' in the array 'h'.
385 * Returns nothing.
387 void pgn_history_free(HISTORY **h, int start);
390 * Resets the game 'g' using board 'b' up to history move (g.hindex) 'n'.
391 * Returns E_PGN_OK on success or E_PGN_PARSE if there was a FEN tag but the
392 * parsing of it failed. Or returns E_PGN_INVALID if somehow the move failed
393 * validation while resetting.
395 pgn_error_t pgn_board_update(GAME g, BOARD b, int n);
398 * Updates the game 'g' using board 'b' to the next 'n'th history move.
399 * Returns nothing.
401 void pgn_history_prev(GAME g, BOARD b, int n);
404 * Updates the game 'g' using board 'b' to the previous 'n'th history move.
405 * Returns nothing.
407 void pgn_history_next(GAME g, BOARD b, int n);
410 * Converts the character piece 'p' to an integer. Returns the integer
411 * associated with 'p' or E_PGN_ERR if 'p' is invalid.
413 int pgn_piece_to_int(int p);
416 * Converts the integer piece 'n' to a character whose turn is 'turn'. WHITE
417 * piece are uppercase and BLACK pieces are lowercase. Returns the character
418 * associated with 'n' or E_PGN_ERR if 'n' is an invalid piece.
420 pgn_error_t pgn_int_to_piece(char turn, int n);
423 * Toggles g->turn. Returns nothing.
425 void pgn_switch_turn(GAME);
428 * Toggles g->side and switches the White and Black roster tags. Returns
429 * nothing.
431 void pgn_switch_side(GAME g, int t);
434 * Clears the enpassant flag for all positions on board 'b'. Returns nothing.
436 void pgn_reset_enpassant(BOARD b);
439 * Clears the valid move flag for all positions on board 'b'. Returns nothing.
441 void pgn_reset_valid_moves(BOARD b);
444 * Sets valid moves (b.valid) from game 'g' using board 'b'. The valid moves
445 * are for the piece on the board 'b' at 'rank' and 'file'. Returns nothing.
447 void pgn_find_valid_moves(GAME g, BOARD b, int rank, int file);
450 * Returns the version string of the library.
452 char *pgn_version(void);
454 #endif