Fri, 12 Jun 2026 14:33:42 +0200
fixes #893 - pawn could not promote
|
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; | |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
80 | int32_t tv_usec; // important that this is signed b/c potential carry |
| 129 | 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; | |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
99 | /** 1: play with timecontrol, 0: play without time control */ |
| 129 | 100 | uint8_t timecontrol; |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
101 | /** If timecontrol is 1, initial clock time in seconds */ |
| 129 | 102 | uint16_t time; |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
103 | /** If timecontrol is 1, time added per move in seconds */ |
| 129 | 104 | uint16_t addtime; |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
105 | /** If timecontrol is 1, delay before the clock starts ticking down */ |
|
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
106 | uint16_t delay; |
| 129 | 107 | } GameInfo; |
| 108 | ||
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
109 | /** The buffer length for player names in GameState structures. */ |
| 129 | 110 | #define PLAYER_NAME_BUFLEN 32 |
| 111 | ||
| 112 | typedef struct { | |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
113 | GameInfo info; |
| 129 | 114 | Board board; |
| 115 | Move* moves; | |
| 116 | /** optional name of the white player */ | |
| 117 | char wname[PLAYER_NAME_BUFLEN]; | |
| 118 | /** optional name of the black player */ | |
| 119 | char bname[PLAYER_NAME_BUFLEN]; | |
| 120 | /** capacity of the move array */ | |
| 121 | unsigned movecapacity; | |
| 122 | /** number of (half-)moves (counting BOTH colors) */ | |
| 123 | unsigned int movecount; | |
| 124 | /** a premove that shall be evaluated next time it's our turn */ | |
| 125 | char premove[8]; | |
| 126 | bool checkmate; | |
| 127 | bool stalemate; | |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
128 | bool threefold; |
| 129 | 129 | bool remis; |
| 130 | bool wresign; | |
| 131 | bool bresign; | |
| 132 | /** this flag is only supposed to be set when the opponent disconnects */ | |
| 133 | bool ragequit; | |
| 134 | bool review; | |
| 135 | } GameState; | |
| 136 | ||
| 137 | #define is_game_running(gamestate) !((gamestate)->checkmate || \ | |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
138 | (gamestate)->wresign || (gamestate)->bresign || (gamestate)->threefold || \ |
| 129 | 139 | (gamestate)->stalemate || (gamestate)->remis || (gamestate)->review) |
| 140 | ||
| 141 | #define last_move(gamestate) \ | |
| 142 | ((gamestate)->moves[(gamestate)->movecount-1]) | |
| 143 | ||
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
144 | #define POS_UNSPECIFIED UINT8_MAX |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
145 | #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
|
146 | #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
|
147 | |
|
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
|
148 | #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
|
149 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
150 | #define isfile(file) (file >= 'a' && file <= 'h') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
151 | #define isrow(row) (row >= '1' && row <= '8') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
152 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
153 | #define rowidx(row) (row-'1') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
154 | #define fileidx(file) (file-'a') |
|
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 | #define rowchr(row) (row+'1') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
157 | #define filechr(file) (file+'a') |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
158 | |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
159 | #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
|
160 | #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
|
161 | #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
|
162 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
163 | /* 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
|
164 | #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
|
165 | #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
|
166 | |
| 129 | 167 | |
| 168 | /** | |
| 169 | * Initializes a game state and prepares the chess board. | |
| 170 | * @param gamestate the game state to initialize | |
| 171 | */ | |
| 172 | void gamestate_init(GameState *gamestate); | |
| 173 | ||
| 174 | /** | |
| 175 | * Cleans up a game state and frees the memory for the movement list. | |
| 176 | * @param gamestate the game state to clean up | |
| 177 | */ | |
| 178 | void gamestate_cleanup(GameState *gamestate); | |
| 179 | ||
|
19
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 | * Maps a character to a piece. |
|
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 | * 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
|
184 | * |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
185 | * @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
|
186 | * @return numeric value for the specified piece |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
187 | */ |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
188 | uint8_t getpiece(char c); |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
189 | |
|
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 | * Maps a piece to a character. |
|
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 | * 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
|
194 | * |
| 69 | 195 | * @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
|
196 | * @return character value for the specified piece |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
197 | */ |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
198 | char getpiecechr(uint8_t piece); |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
199 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
200 | /** |
| 69 | 201 | * Maps a piece to a unicode character sequence. |
| 202 | * | |
| 203 | * The returned unicode is for black pieces. | |
| 204 | * You may colorize the output by setting the terminal foreground color. | |
| 205 | * | |
| 206 | * @param piece the piece to dispaly | |
| 207 | * @return unicode character sequence for the specified piece | |
| 208 | */ | |
| 87 | 209 | char* getpieceunicode(uint8_t piece); |
| 69 | 210 | |
| 211 | /** | |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
212 | * Returns the color of the player who is next to move. |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
213 | * |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
214 | * @param gamestate the current game state |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
215 | * @return the color of the player who is next to move |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
216 | */ |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
217 | uint8_t current_color(GameState *gamestate); |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
218 | |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
219 | /** |
| 25 | 220 | * Checks, if a specified field is covered by a piece of a certain color. |
| 221 | * | |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
222 | * 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
|
223 | * 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
|
224 | * |
|
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
|
225 | * @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
|
226 | * @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
|
227 | * @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
|
228 | * @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
|
229 | * @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
|
230 | * 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
|
231 | * @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
|
232 | * stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
233 | * @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
|
234 | * 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
|
235 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
236 | 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
|
237 | 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
|
238 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
239 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
240 | * 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
|
241 | * 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
|
242 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
243 | * 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
|
244 | * 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
|
245 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
246 | * @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
|
247 | * @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
|
248 | * @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
|
249 | * @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
|
250 | * @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
|
251 | * 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
|
252 | * @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
|
253 | * stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
254 | * @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
|
255 | * 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
|
256 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
257 | 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
|
258 | 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
|
259 | |
|
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
|
260 | /** |
|
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
|
261 | * 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
|
262 | * |
| 25 | 263 | * @param gamestate the current game state |
| 264 | * @param row row of the field to check | |
| 265 | * @param file file of the field to check | |
| 266 | * @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
|
267 | * @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
|
268 | * field |
| 25 | 269 | */ |
|
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
|
270 | #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
|
271 | 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
|
272 | |
|
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 | * 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
|
275 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
276 | * 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
|
277 | * 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
|
278 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
279 | * @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
|
280 | * @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
|
281 | * @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
|
282 | * @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
|
283 | * @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
|
284 | * 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
|
285 | */ |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
286 | #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
|
287 | 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
|
288 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
289 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
290 | * 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
|
291 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
292 | * 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
|
293 | * 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
|
294 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
295 | * @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
|
296 | * @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
|
297 | * @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
|
298 | * @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
|
299 | * @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
|
300 | * 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
|
301 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
302 | 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
|
303 | uint8_t color); |
| 25 | 304 | |
| 305 | /** | |
|
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
|
306 | * 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
|
307 | * 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
|
308 | * |
|
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
|
309 | * 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
|
310 | * 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
|
311 | * 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
|
312 | * 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
|
313 | * |
|
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
|
314 | * @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
|
315 | * @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
|
316 | * @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
|
317 | * 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
|
318 | */ |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
319 | 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
|
320 | |
|
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
|
321 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
322 | * 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
|
323 | * object. |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
324 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
325 | * 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
|
326 | * 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
|
327 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
328 | * 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
|
329 | * 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
|
330 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
331 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
332 | * @param mstr the input string to parse |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
333 | * @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
|
334 | * @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
|
335 | * @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
|
336 | */ |
|
50
41017d0a72c5
added pgn parser and writer (without comment support yet) + minor refactorings
Mike Becker <universe@uap-core.de>
parents:
49
diff
changeset
|
337 | 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
|
338 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
339 | /** |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
340 | * 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
|
341 | * allowed to move that way. |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
342 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
343 | * @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
|
344 | * @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
|
345 | * @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
|
346 | */ |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
347 | 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
|
348 | |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
349 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
350 | * Validates move by applying chess rules. |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
351 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
352 | * @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
|
353 | * @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
|
354 | */ |
|
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
|
355 | 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
|
356 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
357 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
358 | * Applies a move and deletes captured pieces. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
359 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
360 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
361 | * @param move the move to apply |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
362 | */ |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
363 | 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
|
364 | |
|
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
|
365 | /** |
|
122
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
366 | * 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
|
367 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
368 | * 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
|
369 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
370 | * @param gamestate the current game state |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
371 | * @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
|
372 | * @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
|
373 | */ |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
374 | void gamestate_at_move(GameState *gamestate, |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
375 | unsigned move_number, GameState *replay); |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
376 | |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
377 | /** |
|
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
|
378 | * 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
|
379 | * 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
|
380 | * |
|
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
|
381 | * @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
|
382 | * @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
|
383 | * @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
|
384 | * 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
|
385 | */ |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
386 | uint16_t remaining_movetime2(GameState *gamestate, unsigned move_number); |
| 33 | 387 | |
| 388 | /** | |
| 389 | * Returns the remaining time on the clock for the specified player. | |
| 390 | * | |
| 391 | * @param gamestate the current game state | |
| 392 | * @param color either BLACK or WHITE | |
| 393 | * @return the remaining time - if time control is disabled, this function | |
| 394 | * always returns zero | |
| 395 | */ | |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
396 | uint16_t remaining_movetime(GameState *gamestate, uint8_t color); |
| 33 | 397 | |
|
100
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
398 | /** |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
399 | * Converts clock time to string. |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
400 | * |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
401 | * @param time the time to format |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
402 | * @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
|
403 | * @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
|
404 | */ |
|
107
36dd94278142
fix that PGN (with comments) can exceed 80 chars
Mike Becker <universe@uap-core.de>
parents:
100
diff
changeset
|
405 | 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
|
406 | |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
407 | /** |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
408 | * Checks if the current position already appeared two times before. |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
409 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
410 | * This does not set the threefold flag in the game state as this flag is |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
411 | * intended to be set only when the game ends after actually claiming a draw. |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
412 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
413 | * By standard chess rules this is not automatically a draw. |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
414 | * But implementation may choose to automatically draw the game anyway. |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
415 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
416 | * @param gamestate the current game state |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
417 | * @return true if the game is in a threefold repetition position |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
418 | */ |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
419 | bool check_threefold_repetition(GameState *gamestate); |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
420 | |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | #endif /* RULES_H */ |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 |