2 * Program: pgn-extract: a Portable Game Notation (PGN) extractor.
3 * Copyright (C) 1994-2014 David Barnes
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19 * http://www.cs.kent.ac.uk/people/staff/djb/
24 /* These colour values are used as modifiers of the Piece values to
25 * produce pieces of the appropriate colours.
26 * A coloured piece is formed by shifting the piece value and setting the
27 * bottom bit to either 0 (BLACK) or 1 (WHITE).
29 typedef enum { BLACK, WHITE } Colour;
32 /* The order of these is important and used in several places.
33 * In particular, several for-loops iterate from PAWN to KING.
35 PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
39 /* Different classes of move determined by the lexical analyser. */
40 typedef enum { PAWN_MOVE, PAWN_MOVE_WITH_PROMOTION, ENPASSANT_PAWN_MOVE,
41 PIECE_MOVE, KINGSIDE_CASTLE, QUEENSIDE_CASTLE,
46 /* Types for algebraic rank and column. */
50 /* Define the base characters for ranks and columns. */
53 #define FIRSTRANK (RANKBASE)
54 #define LASTRANK (RANKBASE+BOARDSIZE-1)
55 #define FIRSTCOL (COLBASE)
56 #define LASTCOL (COLBASE+BOARDSIZE-1)
58 /* Convert the given rank to the correct index into a board. */
59 #define RankConvert(rank) ((FIRSTRANK <= (rank)) && ((rank) <= LASTRANK)?\
60 ((rank)-RANKBASE+HEDGE):0)
61 /* Convert the given column to the correct index into a board. */
62 #define ColConvert(col) ((FIRSTCOL <= (col)) && ((col) <= LASTCOL)?\
63 ((col)-COLBASE+HEDGE):0)
65 /* Convert a board index back to Rank or Col form. */
66 #define ToRank(r) ((r)+RANKBASE-HEDGE)
67 #define ToCol(c) ((c)+COLBASE-HEDGE)
68 #define COLOUR_OFFSET(colour) (((colour) == WHITE)? 1 : -1)
71 /* Define the size of a hedge around the board.
72 * This should have a size of 2 to make calculation of Knight moves easier.
76 /* Define a type for position hashing. */
77 typedef unsigned long HashCode;
80 Piece board[HEDGE+BOARDSIZE+HEDGE][HEDGE+BOARDSIZE+HEDGE];
81 /* Who has the next move. */
83 /* The current move number. */
85 /* Are the following castling options available? */
86 Boolean WKingCastle, WQueenCastle;
87 Boolean BKingCastle, BQueenCastle;
88 /* Keep track of where the two kings are, to make check-detection
91 Col WKingCol; Rank WKingRank;
92 Col BKingCol; Rank BKingRank;
93 /* Is EnPassant capture possible? If so then ep_rank and ep_col have
94 * the square on which this can be made.
100 /* The half-move clock since the last pawn move or capture. */
101 unsigned halfmove_clock;
104 /* Define a type that can be used to create a list of possible source
105 * squares for a move.
107 typedef struct move_pair {
112 struct move_pair *next;
115 /* Conversion macros. */
116 #define PIECE_SHIFT 3
117 #define MAKE_COLOURED_PIECE(colour,piece) (((piece) << PIECE_SHIFT) | (colour))
118 #define W(piece) MAKE_COLOURED_PIECE(WHITE,piece)
119 #define B(piece) MAKE_COLOURED_PIECE(BLACK,piece)
120 /* Conversion macro, from one colour to another. */
121 #define OPPOSITE_COLOUR(colour) (!(colour))
122 #define EXTRACT_COLOUR(coloured_piece) ((coloured_piece) & 0x01)
123 #define EXTRACT_PIECE(coloured_piece) ((coloured_piece) >> PIECE_SHIFT)
125 /* The string for internally representing the non-standard PGN
126 * notation for null moves.
128 #define NULL_MOVE_STRING ("--")