]> git.sesse.net Git - pgn-extract/blob - defs.h
Push through a computer/human flag to the binary output.
[pgn-extract] / defs.h
1 /*
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  *  David Barnes may be contacted as D.J.Barnes@kent.ac.uk
19  *  http://www.cs.kent.ac.uk/people/staff/djb/
20  *
21  */
22
23
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).
28  */
29 typedef enum { BLACK, WHITE } Colour;
30 typedef enum {
31     OFF, EMPTY,
32     /* The order of these is important and used in several places.
33      * In particular, several for-loops iterate from PAWN to KING.
34      */
35     PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
36     /* Must be last. */
37     NUM_PIECE_VALUES
38 } Piece;
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,
42                NULL_MOVE,
43                UNKNOWN_MOVE
44              } MoveClass;
45
46 /* Types for algebraic rank and column. */
47 typedef char Rank;
48 typedef char Col;
49
50 /* Define the base characters for ranks and columns. */
51 #define RANKBASE '1'
52 #define COLBASE 'a'
53 #define FIRSTRANK (RANKBASE)
54 #define LASTRANK (RANKBASE+BOARDSIZE-1)
55 #define FIRSTCOL (COLBASE)
56 #define LASTCOL (COLBASE+BOARDSIZE-1)
57
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)
64
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)
69
70 #define BOARDSIZE 8
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.
73  */
74 #define HEDGE 2
75
76 /* Define a type for position hashing. */
77 typedef unsigned long HashCode;
78
79 typedef struct {
80     Piece board[HEDGE+BOARDSIZE+HEDGE][HEDGE+BOARDSIZE+HEDGE];
81     /* Who has the next move. */
82     Colour to_move;
83     /* The current move number. */
84     unsigned 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
89      * simple.
90      */
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.
95      */
96     Boolean EnPassant;
97     Rank ep_rank;
98     Col ep_col;
99     HashCode hash_value;
100     /* The half-move clock since the last pawn move or capture. */
101     unsigned halfmove_clock;
102 } Board;
103
104 /* Define a type that can be used to create a list of possible source
105  * squares for a move.
106  */
107 typedef struct move_pair {
108     Col from_col;
109     Rank from_rank;
110     Col to_col;
111     Rank to_rank;
112     struct move_pair *next;
113 } MovePair;
114     
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)
124
125 /* The string for internally representing the non-standard PGN
126  * notation for null moves.
127  */
128 #define NULL_MOVE_STRING ("--")
129