42 #define PIECE_PINNED 5 |
43 #define PIECE_PINNED 5 |
43 #define KING_IN_CHECK 6 |
44 #define KING_IN_CHECK 6 |
44 #define KING_MOVES_INTO_CHECK 7 |
45 #define KING_MOVES_INTO_CHECK 7 |
45 #define RULES_VIOLATED 10 |
46 #define RULES_VIOLATED 10 |
46 |
47 |
47 |
|
48 #define PIECE_MASK 0x0F |
|
49 #define COLOR_MASK 0x30 |
|
50 #define ENPASSANT_THREAT 0x40 |
48 #define ENPASSANT_THREAT 0x40 |
51 |
|
52 #define WHITE 0x10 |
|
53 #define BLACK 0x20 |
|
54 |
|
55 #define PAWN 0x01 |
|
56 #define ROOK 0x02 |
|
57 #define KNIGHT 0x03 |
|
58 #define BISHOP 0x04 |
|
59 #define QUEEN 0x05 |
|
60 #define KING 0x06 |
|
61 |
|
62 #define WPAWN (WHITE|PAWN) |
|
63 #define WROOK (WHITE|ROOK) |
|
64 #define WKNIGHT (WHITE|KNIGHT) |
|
65 #define WBISHOP (WHITE|BISHOP) |
|
66 #define WQUEEN (WHITE|QUEEN) |
|
67 #define WKING (WHITE|KING) |
|
68 #define BPAWN (BLACK|PAWN) |
|
69 #define BROOK (BLACK|ROOK) |
|
70 #define BKNIGHT (BLACK|KNIGHT) |
|
71 #define BBISHOP (BLACK|BISHOP) |
|
72 #define BQUEEN (BLACK|QUEEN) |
|
73 #define BKING (BLACK|KING) |
|
74 |
|
75 typedef uint8_t Board[8][8]; |
|
76 |
|
77 struct movetimeval { |
|
78 uint64_t tv_sec; |
|
79 int32_t tv_usec; |
|
80 }; |
|
81 |
|
82 typedef struct { |
|
83 uint8_t piece; |
|
84 uint8_t fromfile; |
|
85 uint8_t fromrow; |
|
86 uint8_t tofile; |
|
87 uint8_t torow; |
|
88 uint8_t promotion; |
|
89 uint8_t check; |
|
90 uint8_t capture; |
|
91 struct movetimeval timestamp; |
|
92 struct movetimeval movetime; |
|
93 char string[8]; |
|
94 } Move; |
|
95 |
|
96 typedef struct MoveList MoveList; |
|
97 |
|
98 struct MoveList { |
|
99 Move move; |
|
100 MoveList* next; |
|
101 }; |
|
102 |
|
103 |
|
104 typedef struct { |
|
105 uint8_t servercolor; |
|
106 uint8_t timecontrol; |
|
107 uint16_t time; |
|
108 uint16_t addtime; |
|
109 } GameInfo; |
|
110 |
|
111 typedef struct { |
|
112 Board board; |
|
113 MoveList* movelist; |
|
114 MoveList* lastmove; |
|
115 unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */ |
|
116 _Bool checkmate; |
|
117 _Bool stalemate; |
|
118 _Bool remis; |
|
119 _Bool resign; |
|
120 } GameState; |
|
121 |
|
122 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE) |
|
123 |
49 |
124 #define POS_UNSPECIFIED UINT8_MAX |
50 #define POS_UNSPECIFIED UINT8_MAX |
125 #define mdst(b,m) b[(m)->torow][(m)->tofile] |
51 #define mdst(b,m) b[(m)->torow][(m)->tofile] |
126 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] |
52 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] |
127 |
53 |
140 isidx((move)->tofile) && isidx((move)->torow)) |
66 isidx((move)->tofile) && isidx((move)->torow)) |
141 |
67 |
142 /* secure versions - use, if index is not checked with isidx() */ |
68 /* secure versions - use, if index is not checked with isidx() */ |
143 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) |
69 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) |
144 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) |
70 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) |
145 |
|
146 #define is_game_running(gamestate) !((gamestate)->checkmate || \ |
|
147 (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) |
|
148 |
|
149 |
|
150 /** |
|
151 * Initializes a game state and prepares the chess board. |
|
152 * @param gamestate the game state to initialize |
|
153 */ |
|
154 void gamestate_init(GameState *gamestate); |
|
155 |
|
156 /** |
|
157 * Cleans up a game state and frees the memory for the movement list. |
|
158 * @param gamestate the game state to clean up |
|
159 */ |
|
160 void gamestate_cleanup(GameState *gamestate); |
|
161 |
71 |
162 /** |
72 /** |
163 * Maps a character to a piece. |
73 * Maps a character to a piece. |
164 * |
74 * |
165 * Does not work for pawns, since they don't have a character. |
75 * Does not work for pawns, since they don't have a character. |