src/chess/rules.h

changeset 129
189c7c77aaab
parent 122
e65d9b5e9324
child 130
3fc6b1d6cbe9
equal deleted inserted replaced
128:ce38ee9bc3af 129:189c7c77aaab
28 */ 28 */
29 29
30 #ifndef RULES_H 30 #ifndef RULES_H
31 #define RULES_H 31 #define RULES_H
32 32
33 #include "game-info.h"
34
35 #include <stdint.h> 33 #include <stdint.h>
36 #include <stdbool.h> 34 #include <stdbool.h>
37 35
38 #define VALID_MOVE_SYNTAX 0 36 #define VALID_MOVE_SYNTAX 0
39 #define VALID_MOVE_SEMANTICS 0 /* use same code for a success */ 37 #define VALID_MOVE_SEMANTICS 0 /* use same code for a success */
46 #define KING_MOVES_INTO_CHECK 7 44 #define KING_MOVES_INTO_CHECK 7
47 #define RULES_VIOLATED 10 45 #define RULES_VIOLATED 10
48 46
49 #define ENPASSANT_THREAT 0x40 47 #define ENPASSANT_THREAT 0x40
50 48
49 #define WHITE 0x10
50 #define BLACK 0x20
51 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
52
53 #define PIECE_MASK 0x0F
54 #define COLOR_MASK 0x30
55
56 #define PAWN 0x01
57 #define ROOK 0x02
58 #define KNIGHT 0x03
59 #define BISHOP 0x04
60 #define QUEEN 0x05
61 #define KING 0x06
62
63 #define WPAWN (WHITE|PAWN)
64 #define WROOK (WHITE|ROOK)
65 #define WKNIGHT (WHITE|KNIGHT)
66 #define WBISHOP (WHITE|BISHOP)
67 #define WQUEEN (WHITE|QUEEN)
68 #define WKING (WHITE|KING)
69 #define BPAWN (BLACK|PAWN)
70 #define BROOK (BLACK|ROOK)
71 #define BKNIGHT (BLACK|KNIGHT)
72 #define BBISHOP (BLACK|BISHOP)
73 #define BQUEEN (BLACK|QUEEN)
74 #define BKING (BLACK|KING)
75
76 typedef uint8_t Board[8][8];
77
78 struct movetimeval {
79 uint64_t tv_sec;
80 int32_t tv_usec;
81 };
82
83 typedef struct {
84 uint8_t piece;
85 uint8_t fromfile;
86 uint8_t fromrow;
87 uint8_t tofile;
88 uint8_t torow;
89 uint8_t promotion;
90 uint8_t check;
91 uint8_t capture;
92 struct movetimeval timestamp;
93 struct movetimeval movetime;
94 char string[8];
95 } Move;
96
97 typedef struct {
98 uint8_t servercolor;
99 uint8_t timecontrol;
100 uint16_t time;
101 uint16_t addtime;
102 } GameInfo;
103
104 /** The buffer length for player names in GameInfo structures. */
105 #define PLAYER_NAME_BUFLEN 32
106
107 typedef struct {
108 Board board;
109 Move* moves;
110 /** optional name of the white player */
111 char wname[PLAYER_NAME_BUFLEN];
112 /** optional name of the black player */
113 char bname[PLAYER_NAME_BUFLEN];
114 /** capacity of the move array */
115 unsigned movecapacity;
116 /** number of (half-)moves (counting BOTH colors) */
117 unsigned int movecount;
118 /** a premove that shall be evaluated next time it's our turn */
119 char premove[8];
120 bool checkmate;
121 bool stalemate;
122 bool remis;
123 bool wresign;
124 bool bresign;
125 /** this flag is only supposed to be set when the opponent disconnects */
126 bool ragequit;
127 bool review;
128 } GameState;
129
130 #define is_game_running(gamestate) !((gamestate)->checkmate || \
131 (gamestate)->wresign || (gamestate)->bresign || \
132 (gamestate)->stalemate || (gamestate)->remis || (gamestate)->review)
133
134 #define last_move(gamestate) \
135 ((gamestate)->moves[(gamestate)->movecount-1])
136
51 #define POS_UNSPECIFIED UINT8_MAX 137 #define POS_UNSPECIFIED UINT8_MAX
52 #define mdst(b,m) b[(m)->torow][(m)->tofile] 138 #define mdst(b,m) b[(m)->torow][(m)->tofile]
53 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] 139 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
54 140
55 #define isidx(idx) ((uint8_t)(idx) < 8) 141 #define isidx(idx) ((uint8_t)(idx) < 8)
68 #define chkidx(move) (chkidx_from(move) && chkidx_to(move)) 154 #define chkidx(move) (chkidx_from(move) && chkidx_to(move))
69 155
70 /* secure versions - use, if index is not checked with isidx() */ 156 /* secure versions - use, if index is not checked with isidx() */
71 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) 157 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
72 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) 158 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
159
160
161 /**
162 * Initializes a game state and prepares the chess board.
163 * @param gamestate the game state to initialize
164 */
165 void gamestate_init(GameState *gamestate);
166
167 /**
168 * Cleans up a game state and frees the memory for the movement list.
169 * @param gamestate the game state to clean up
170 */
171 void gamestate_cleanup(GameState *gamestate);
73 172
74 /** 173 /**
75 * Maps a character to a piece. 174 * Maps a character to a piece.
76 * 175 *
77 * Does not work for pawns, since they don't have a character. 176 * Does not work for pawns, since they don't have a character.

mercurial