Thu, 28 May 2026 12:15:26 +0200
simplify code structure
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
|
55
54ea19938d57
updated copyright and version info
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
4 | * Copyright 2016 Mike Becker. All rights reserved. |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | */ |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #ifndef RULES_H |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #define RULES_H |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
33 | #include <stdint.h> |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
34 | #include <stdbool.h> |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | |
|
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
36 | #define VALID_MOVE_SYNTAX 0 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
37 | #define VALID_MOVE_SEMANTICS 0 /* use same code for a success */ |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
38 | #define INVALID_MOVE_SYNTAX 1 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
39 | #define INVALID_POSITION 2 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
40 | #define AMBIGUOUS_MOVE 3 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
41 | #define NEED_PROMOTION 4 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
42 | #define PIECE_PINNED 5 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
43 | #define KING_IN_CHECK 6 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
44 | #define KING_MOVES_INTO_CHECK 7 |
|
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
45 | #define RULES_VIOLATED 10 |
|
16
a298c6637c30
introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
46 | |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
47 | #define ENPASSANT_THREAT 0x40 |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
48 | |
| 129 | 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 | ||
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
137 | #define POS_UNSPECIFIED UINT8_MAX |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
138 | #define mdst(b,m) b[(m)->torow][(m)->tofile] |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
139 | #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
140 | |
|
28
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
141 | #define isidx(idx) ((uint8_t)(idx) < 8) |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
142 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
143 | #define isfile(file) (file >= 'a' && file <= 'h') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
144 | #define isrow(row) (row >= '1' && row <= '8') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
145 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
146 | #define rowidx(row) (row-'1') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
147 | #define fileidx(file) (file-'a') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
148 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
149 | #define rowchr(row) (row+'1') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
150 | #define filechr(file) (file+'a') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
151 | |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
152 | #define chkidx_from(move) (isidx((move)->fromfile) && isidx((move)->fromrow)) |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
153 | #define chkidx_to(move) (isidx((move)->tofile) && isidx((move)->torow)) |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
154 | #define chkidx(move) (chkidx_from(move) && chkidx_to(move)) |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
155 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
156 | /* secure versions - use, if index is not checked with isidx() */ |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
157 | #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
158 | #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
159 | |
| 129 | 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); | |
| 172 | ||
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
173 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
174 | * Maps a character to a piece. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
175 | * |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
176 | * Does not work for pawns, since they don't have a character. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
177 | * |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
178 | * @param c one of R,N,B,Q,K |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
179 | * @return numeric value for the specified piece |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
180 | */ |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
181 | uint8_t getpiece(char c); |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
182 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
183 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
184 | * Maps a piece to a character. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
185 | * |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
186 | * Does not work for pawns, scince they don't have a character. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
187 | * |
| 69 | 188 | * @param piece may have color or additional flags |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
189 | * @return character value for the specified piece |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
190 | */ |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
191 | char getpiecechr(uint8_t piece); |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
192 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
193 | /** |
| 69 | 194 | * Maps a piece to a unicode character sequence. |
| 195 | * | |
| 196 | * The returned unicode is for black pieces. | |
| 197 | * You may colorize the output by setting the terminal foreground color. | |
| 198 | * | |
| 199 | * @param piece the piece to dispaly | |
| 200 | * @return unicode character sequence for the specified piece | |
| 201 | */ | |
| 87 | 202 | char* getpieceunicode(uint8_t piece); |
| 69 | 203 | |
| 204 | /** | |
| 25 | 205 | * Checks, if a specified field is covered by a piece of a certain color. |
| 206 | * | |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
207 | * The out-parameters may both be NULL, but if any of them is set, the other |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
208 | * must be set, too. |
|
28
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
209 | * |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
210 | * @param gamestate the current game state |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
211 | * @param row row of the field to check |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
212 | * @param file file of the field to check |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
213 | * @param color the color of the piece that should threaten the field |
|
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
214 | * @param threats the array where to store the threats (should be able to hold |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
215 | * the rare maximum of 16 elements) |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
216 | * @param threatcount a pointer to an uint8_t where the count of threats is |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
217 | * stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
218 | * @return true, if any piece of the specified color threatens the specified |
|
28
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
219 | * field (i.e. could capture an opponent piece) |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
220 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
221 | bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
222 | uint8_t color, Move* threats, uint8_t* threatcount); |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
223 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
224 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
225 | * Checks, if a specified field is covered by a piece of a certain color AND |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
226 | * if this piece is not pinned and therefore able to perform the move. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
227 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
228 | * The out-parameters may both be NULL, but if any of them is set, the other |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
229 | * must be set, too. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
230 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
231 | * @param gamestate the current game state |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
232 | * @param row row of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
233 | * @param file file of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
234 | * @param color the color of the piece that should threaten the field |
|
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
235 | * @param threats the array where to store the threats (should be able to hold |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
236 | * the rare maximum of 16 elements) |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
237 | * @param threatcount a pointer to an uint8_t where the count of threats is |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
238 | * stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
239 | * @return true, if any piece of the specified color threatens the specified |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
240 | * field (i.e. could capture an opponent piece) |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
241 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
242 | bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
243 | uint8_t color, Move* threats, uint8_t* threatcount); |
|
28
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
244 | |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
245 | /** |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
246 | * Checks, if a specified field is covered by a piece of a certain color. |
|
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
247 | * |
| 25 | 248 | * @param gamestate the current game state |
| 249 | * @param row row of the field to check | |
| 250 | * @param file file of the field to check | |
| 251 | * @param color the color of the piece that should cover the field | |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
252 | * @return true, if any piece of the specified color threatens the specified |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
253 | * field |
| 25 | 254 | */ |
|
28
0c1371488d87
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents:
27
diff
changeset
|
255 | #define is_covered(gamestate, row, file, color) \ |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
256 | get_threats(gamestate, row, file, color, NULL, NULL) |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
257 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
258 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
259 | * Checks, if a specified field is attacked by a piece of a certain color. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
260 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
261 | * I.e. the field is covered by a piece AND this piece is not pinned and |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
262 | * therefore able to perform the move. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
263 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
264 | * @param gamestate the current game state |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
265 | * @param row row of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
266 | * @param file file of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
267 | * @param color the color of the piece that should cover the field |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
268 | * @return true, if any piece of the specified color threatens the specified |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
269 | * field and could capture an opponent piece |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
270 | */ |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
271 | #define is_attacked(gamestate, row, file, color) \ |
|
61
e3a1a794351e
fixes wrong macro expansion for is_attacked()
Mike Becker <universe@uap-core.de>
parents:
55
diff
changeset
|
272 | get_real_threats(gamestate, row, file, color, NULL, NULL) |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
273 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
274 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
275 | * Checks, if a specified field is protected by a piece of a certain color. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
276 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
277 | * I.e. the field is covered by a piece that is NOT the king AND this piece is |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
278 | * not pinned and therefore able to perform the move. |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
279 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
280 | * @param gamestate the current game state |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
281 | * @param row row of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
282 | * @param file file of the field to check |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
283 | * @param color the color of the piece that should cover the field |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
284 | * @return true, if any piece (excluding the king) of the specified color |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
285 | * threatens the specified field and could capture an opponent piece |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
286 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
287 | bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
288 | uint8_t color); |
| 25 | 289 | |
| 290 | /** | |
|
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
291 | * Checks, if the specified move cannot be performed, because the piece is |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
292 | * either pinned or cannot remove the check. |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
293 | * |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
294 | * Note: in chess a piece is pinned, when it can't be moved because the move |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
295 | * would result in a check position. But this function <u>also</u> returns true, |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
296 | * if the king is already in check position and the specified move does not |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
297 | * protect the king. |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
298 | * |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
299 | * @param gamestate the current game state |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
300 | * @param move the move to check |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
301 | * @return true, if the move cannot be performed because the king would be in |
|
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
302 | * check after the move |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
303 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
304 | bool is_pinned(GameState *gamestate, Move *move); |
|
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
305 | |
|
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
306 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
307 | * Evaluates a move syntactically and stores the move data in the specified |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
308 | * object. |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
309 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
310 | * When short algebraic notation is used, the source position is determined by |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
311 | * the evaluating the allowed moves according to the current game state. |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
312 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
313 | * For a purely syntactic check, regardless of whether a piece exists that is |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
314 | * allowed to move that way, use check_move(). |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
315 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
316 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
317 | * @param mstr the input string to parse |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
318 | * @param move a pointer to object where the move data shall be stored |
|
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
319 | * @param color the color of the player to evaluate the move for |
|
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
320 | * @return status code (see macros in this file for the list of codes) |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
321 | */ |
|
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
322 | int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
323 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
324 | /** |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
325 | * Syntactically checks a move without verifying that a piece exists that is |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
326 | * allowed to move that way. |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
327 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
328 | * @param mstr the input string to parse |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
329 | * @param color the color of the player to evaluate the move for |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
330 | * @return status code (see macros in this file for the list of codes) |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
331 | */ |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
332 | int check_move(char *mstr, uint8_t color); |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
333 | |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
334 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
335 | * Validates move by applying chess rules. |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
336 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
337 | * @param move the move to validate |
|
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
338 | * @return status code (see macros in this file for the list of codes) |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
339 | */ |
|
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
340 | int validate_move(GameState *gamestate, Move *move); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
341 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
342 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
343 | * Applies a move and deletes captured pieces. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
344 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
345 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
346 | * @param move the move to apply |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
347 | */ |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
348 | void apply_move(GameState *gamestate, Move *move); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
349 | |
|
99
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
350 | /** |
|
122
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
351 | * Copies the state of the game at the specified move number. |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
352 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
353 | * This function is helpful to generate a game state for reviewing past moves. |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
354 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
355 | * @param gamestate the current game state |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
356 | * @param move_number the half-move that would now be played |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
357 | * @param replay the struct to populate with the state at the specified move |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
358 | */ |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
359 | void gamestate_at_move(GameState *gamestate, |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
360 | unsigned move_number, GameState *replay); |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
361 | |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
362 | /** |
|
99
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
363 | * Returns the remaining time on the clock for the specified |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
364 | * half-move number. |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
365 | * |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
366 | * @param gameinfo the information about the game |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
367 | * @param gamestate the current game state |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
368 | * @param move_number the half-move that is now going to be played |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
369 | * @return the remaining time - if time control is disabled, this function |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
370 | * always returns zero |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
371 | */ |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
372 | uint16_t remaining_movetime2(GameInfo *gameinfo, GameState *gamestate, |
|
231a79d93c0c
add API for calculating the clock for a specific move + fixes wrong handling of move time and increment in move 1
Mike Becker <universe@uap-core.de>
parents:
87
diff
changeset
|
373 | unsigned move_number); |
| 33 | 374 | |
| 375 | /** | |
| 376 | * Returns the remaining time on the clock for the specified player. | |
| 377 | * | |
| 378 | * @param gameinfo the information about the game | |
| 379 | * @param gamestate the current game state | |
| 380 | * @param color either BLACK or WHITE | |
| 381 | * @return the remaining time - if time control is disabled, this function | |
| 382 | * always returns zero | |
| 383 | */ | |
| 384 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, | |
| 385 | uint8_t color); | |
| 386 | ||
|
100
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
387 | /** |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
388 | * Converts clock time to string. |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
389 | * |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
390 | * @param time the time to format |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
391 | * @param str the target buffer (should be at least 10 chars large) |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
392 | * @param always_hours if hours should always be printed |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
393 | */ |
|
107
36dd94278142
fix that PGN (with comments) can exceed 80 chars
Mike Becker <universe@uap-core.de>
parents:
100
diff
changeset
|
394 | int print_clk(uint16_t time, char *str, bool always_hours); |
|
100
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
395 | |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
396 | #endif /* RULES_H */ |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
397 |