Tue, 25 Aug 2026 19:42:15 +0200
flip File and Rank parameters into correct order
resolves #956
|
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 | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
33 | #include <stdlib.h> |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
34 | #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
|
35 | #include <stdbool.h> |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | |
|
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
|
37 | #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
|
38 | #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
|
39 | #define INVALID_MOVE_SYNTAX 1 |
|
154
5336e985bd69
rename INVALID_POSITION to PIECE_NOT_FOUND
Mike Becker <universe@uap-core.de>
parents:
153
diff
changeset
|
40 | #define PIECE_NOT_FOUND 2 |
|
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
|
41 | #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
|
42 | #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
|
43 | #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
|
44 | #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
|
45 | #define KING_MOVES_INTO_CHECK 7 |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
46 | #define MISSING_CHECK 8 |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
47 | #define MISSING_CHECKMATE 9 |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
48 | #define INVALID_CHECK 10 |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
49 | #define INVALID_CHECKMATE 11 |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
50 | #define RULES_VIOLATED 32 |
|
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
|
51 | |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
52 | #if __STDC_VERSION__ < 202310L |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
53 | /* since #warning is also a C23 feature, the only hope is this: */ |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
54 | #pragma GCC warning "Type safety for enums is only available since C23" |
| 194 | 55 | #define enum_byte(name) enum e##name |
| 56 | #define typedef_enum_byte(name) typedef uint8_t name | |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
57 | #else |
| 194 | 58 | #define enum_byte(name) enum e##name : uint8_t |
| 59 | #define typedef_enum_byte(name) typedef enum e##name name | |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
60 | #endif |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
61 | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
62 | #define ENPASSANT_THREAT 0x40u |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
63 | |
| 194 | 64 | enum_byte(Color) { |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
65 | WHITE = 0x10u, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
66 | BLACK = 0x20u, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
67 | }; |
| 194 | 68 | typedef_enum_byte(Color); |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
69 | |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
70 | static inline Color opponent_color(Color color) { |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
71 | return color == WHITE ? BLACK : WHITE; |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
72 | } |
| 129 | 73 | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
74 | #define PIECE_MASK 0x0Fu |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
75 | #define COLOR_MASK 0x30u |
| 129 | 76 | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
77 | #define PAWN 0x01u |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
78 | #define ROOK 0x02u |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
79 | #define KNIGHT 0x03u |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
80 | #define BISHOP 0x04u |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
81 | #define QUEEN 0x05u |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
82 | #define KING 0x06u |
| 129 | 83 | |
| 194 | 84 | enum_byte(Piece) { |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
85 | WPAWN = WHITE|PAWN, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
86 | WROOK = WHITE|ROOK, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
87 | WKNIGHT = WHITE|KNIGHT, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
88 | WBISHOP = WHITE|BISHOP, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
89 | WQUEEN = WHITE|QUEEN, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
90 | WKING = WHITE|KING, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
91 | BPAWN = BLACK|PAWN, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
92 | BROOK = BLACK|ROOK, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
93 | BKNIGHT = BLACK|KNIGHT, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
94 | BBISHOP = BLACK|BISHOP, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
95 | BQUEEN = BLACK|QUEEN, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
96 | BKING = BLACK|KING, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
97 | }; |
| 194 | 98 | typedef_enum_byte(Piece); |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
99 | |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
100 | #define POS_UNSPECIFIED 255u |
| 194 | 101 | enum_byte(Rank) { |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
102 | RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
103 | RANK_UNSPECIFIED = POS_UNSPECIFIED |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
104 | }; |
| 194 | 105 | typedef_enum_byte(Rank); |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
106 | |
| 194 | 107 | enum_byte(File) { |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
108 | FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
109 | FILE_UNSPECIFIED = POS_UNSPECIFIED |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
110 | }; |
| 194 | 111 | typedef_enum_byte(File); |
| 129 | 112 | |
| 113 | typedef uint8_t Board[8][8]; | |
| 114 | ||
| 115 | struct movetimeval { | |
|
181
8bda076d0a16
simplify how the elapsed move time is recorded
Mike Becker <universe@uap-core.de>
parents:
179
diff
changeset
|
116 | uint64_t sec; |
|
8bda076d0a16
simplify how the elapsed move time is recorded
Mike Becker <universe@uap-core.de>
parents:
179
diff
changeset
|
117 | int32_t usec; /* important that this is signed b/c potential carry */ |
| 129 | 118 | }; |
| 119 | ||
| 120 | typedef struct { | |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
121 | char string[8]; |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
122 | struct movetimeval timestamp; /* TODO: remove this from the struct */ |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
123 | uint64_t movetime; /* the time for this move in microseconds */ |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
124 | Piece piece; |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
125 | File fromfile; |
| 194 | 126 | Rank fromrank; |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
127 | File tofile; |
| 194 | 128 | Rank torank; |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
129 | Piece promotion; |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
130 | bool check; /* must always be set if checkmate is set */ |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
131 | bool checkmate; |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
132 | bool capture; |
| 129 | 133 | } Move; |
| 134 | ||
| 135 | typedef struct { | |
|
184
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
136 | Color servercolor; |
|
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
137 | /** play with timecontrol? */ |
|
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
138 | bool timecontrol; |
|
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
139 | /** If timecontrol is true, initial clock time in seconds */ |
| 129 | 140 | uint16_t time; |
|
184
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
141 | /** If timecontrol is true, time added per move in seconds */ |
| 129 | 142 | uint16_t addtime; |
|
184
93c81539b702
fix byte-order problem in GameInfo and change decline reason to one byte
Mike Becker <universe@uap-core.de>
parents:
182
diff
changeset
|
143 | /** If timecontrol is true, delay before the clock starts ticking down */ |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
144 | uint16_t delay; |
| 129 | 145 | } GameInfo; |
| 146 | ||
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
147 | /** The buffer length for player names in GameState structures. */ |
| 129 | 148 | #define PLAYER_NAME_BUFLEN 32 |
| 149 | ||
| 150 | typedef struct { | |
|
157
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
151 | /** optional name of the white player - only used for PGN exports */ |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
152 | char wname[PLAYER_NAME_BUFLEN]; |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
153 | /** optional name of the black player - only used for PGN exports */ |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
154 | char bname[PLAYER_NAME_BUFLEN]; |
|
130
3fc6b1d6cbe9
implement optional delay - resolves #820
Mike Becker <universe@uap-core.de>
parents:
129
diff
changeset
|
155 | GameInfo info; |
| 129 | 156 | Board board; |
| 157 | Move* moves; | |
|
157
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
158 | /** starting position in FEN notation */ |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
159 | char *fen_start; |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
160 | /** array of subsequent positions in FEN notation. |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
161 | * The capacity and element count are identical to the moves array. |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
162 | */ |
|
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
163 | char **fen; |
| 129 | 164 | /** capacity of the move array */ |
|
157
07cbfc477b22
add FEN history to the game state
Mike Becker <universe@uap-core.de>
parents:
154
diff
changeset
|
165 | unsigned int movecapacity; |
| 129 | 166 | /** number of (half-)moves (counting BOTH colors) */ |
| 167 | unsigned int movecount; | |
| 168 | /** a premove that shall be evaluated next time it's our turn */ | |
| 169 | char premove[8]; | |
| 170 | bool checkmate; | |
| 171 | bool stalemate; | |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
172 | bool threefold; |
|
179
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
173 | /** drawn due to insufficient material on both sides */ |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
174 | bool nomaterial; |
|
171
09f18c3316f9
add expected game result to test-real-pgn suite
Mike Becker <universe@uap-core.de>
parents:
170
diff
changeset
|
175 | /** flag is only set when players agreed on remis */ |
| 129 | 176 | bool remis; |
| 177 | bool wresign; | |
| 178 | bool bresign; | |
| 179 | /** this flag is only supposed to be set when the opponent disconnects */ | |
| 180 | bool ragequit; | |
| 181 | bool review; | |
| 182 | } GameState; | |
| 183 | ||
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
184 | #define piece_type(piece) ((uint8_t)(piece)&PIECE_MASK) |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
185 | #define piece_color(piece) ((uint8_t)(piece)&COLOR_MASK) |
|
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
186 | #define mkpiece(type,color) (Piece)((type)|(color)) |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
187 | |
|
153
bad2d6d4b861
fix wrong error message when move indices are invalid
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
188 | /** Checks if the index is specified and valid. */ |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
189 | static inline bool isidx(uint8_t idx) {return idx < 8;} |
|
153
bad2d6d4b861
fix wrong error message when move indices are invalid
Mike Becker <universe@uap-core.de>
parents:
147
diff
changeset
|
190 | /** Checks if the index is unspecified or valid. */ |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
191 | static inline bool isidxr(uint8_t idx) {return idx==POS_UNSPECIFIED || idx<8;} |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
192 | |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
193 | static inline bool isfile(char file) {return file >= 'a' && file <= 'h';} |
| 194 | 194 | static inline bool isrank(char rank) {return rank >= '1' && rank <= '8';} |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
195 | |
| 194 | 196 | static inline Rank rankidx(char rank) {return rank-'1';} |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
197 | static inline File fileidx(char file) {return file-'a';} |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
198 | |
| 194 | 199 | static inline char rankchr(Rank rank) {return (char)rank+'1';} |
|
193
d1420f5c5704
increase type safety + add backwards compatibility including C99
Mike Becker <universe@uap-core.de>
parents:
186
diff
changeset
|
200 | static inline char filechr(File file) {return (char)file+'a';} |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
201 | |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
202 | |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
203 | static inline void enpassant_threat_add(GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
204 | File file, Rank rank) { |
| 194 | 205 | gamestate->board[rank][file] |= ENPASSANT_THREAT; |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
206 | } |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
207 | |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
208 | static inline void enpassant_threat_remove(GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
209 | File file, Rank rank) { |
| 194 | 210 | gamestate->board[rank][file] &= ~ENPASSANT_THREAT; |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
211 | } |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
212 | |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
213 | static inline bool enpassant_threat_exists(const GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
214 | File file, Rank rank) { |
| 194 | 215 | return gamestate->board[rank][file] & ENPASSANT_THREAT; |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
216 | } |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
217 | |
|
173
e541b6002933
implement first naive draw by threefold repetition
Mike Becker <universe@uap-core.de>
parents:
171
diff
changeset
|
218 | static inline bool is_game_drawn(const GameState *gamestate) { |
|
179
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
219 | return gamestate->threefold || gamestate->stalemate |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
220 | || gamestate->nomaterial || gamestate->remis; |
|
173
e541b6002933
implement first naive draw by threefold repetition
Mike Becker <universe@uap-core.de>
parents:
171
diff
changeset
|
221 | } |
|
e541b6002933
implement first naive draw by threefold repetition
Mike Becker <universe@uap-core.de>
parents:
171
diff
changeset
|
222 | |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
223 | static inline bool is_game_running(const GameState *gamestate) { |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
224 | return !(gamestate->checkmate || gamestate->wresign || gamestate->bresign |
|
173
e541b6002933
implement first naive draw by threefold repetition
Mike Becker <universe@uap-core.de>
parents:
171
diff
changeset
|
225 | || is_game_drawn(gamestate) || gamestate->review); |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
226 | } |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
227 | |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
228 | static inline bool is_check_position(const GameState *gamestate) { |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
229 | return gamestate->moves[gamestate->movecount - 1].check; |
|
161
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
230 | } |
|
3ff96fec144a
convert some macros to static inline functions
Mike Becker <universe@uap-core.de>
parents:
160
diff
changeset
|
231 | |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
232 | static inline Color field_color(File f, Rank r) { |
|
179
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
233 | return (r + f) % 2 == 0 ? BLACK : WHITE; |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
234 | } |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
235 | |
| 129 | 236 | /** |
| 237 | * Initializes a game state and prepares the chess board. | |
| 238 | * @param gamestate the game state to initialize | |
| 239 | */ | |
| 240 | void gamestate_init(GameState *gamestate); | |
| 241 | ||
| 242 | /** | |
| 243 | * Cleans up a game state and frees the memory for the movement list. | |
| 244 | * @param gamestate the game state to clean up | |
| 245 | */ | |
| 246 | void gamestate_cleanup(GameState *gamestate); | |
| 247 | ||
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
248 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
249 | * Maps a character to a piece. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
250 | * |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
251 | * Does not work for pawns, since they don't have a character. |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
252 | * |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
253 | * @param c one of R,N,B,Q,K |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
254 | * @param color the piece color |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
255 | * @return the specified piece or zero when the character is invalid |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
256 | */ |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
257 | Piece getpiece(char c, Color color); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
258 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
259 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
260 | * Maps a piece to a character. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
261 | * |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
262 | * Does not work for pawns, since they don't have a character. |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
263 | * |
| 69 | 264 | * @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
|
265 | * @return character value for the specified piece |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
266 | */ |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
267 | char getpiecechr(Piece piece); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
268 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
269 | /** |
| 69 | 270 | * Maps a piece to a unicode character sequence. |
| 271 | * | |
| 272 | * The returned unicode is for black pieces. | |
| 273 | * You may colorize the output by setting the terminal foreground color. | |
| 274 | * | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
275 | * @param piece the piece to display |
| 69 | 276 | * @return unicode character sequence for the specified piece |
| 277 | */ | |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
278 | char* getpieceunicode(Piece piece); |
| 69 | 279 | |
| 280 | /** | |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
281 | * 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
|
282 | * |
|
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
283 | * @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
|
284 | * @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
|
285 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
286 | Color current_color(const GameState *gamestate); |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
287 | |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
288 | /** |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
289 | * Returns the piece at the specified position. |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
290 | * |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
291 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
292 | * @param file the file |
| 194 | 293 | * @param rank the rank |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
294 | * @return the piece at the specified position |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
295 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
296 | Piece piece_at(const GameState *gamestate, File file, Rank rank); |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
297 | |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
298 | /** |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
299 | * Places a piece at the specified position in the current game state. |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
300 | * |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
301 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
302 | * @param file the file |
| 194 | 303 | * @param rank the rank |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
304 | * @param piece the piece to place at the specified position |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
305 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
306 | void piece_set(GameState *gamestate, File file, Rank rank, Piece piece); |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
307 | |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
308 | /** |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
309 | * Removes the piece at the specified position in the current game state. |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
310 | * |
|
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
311 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
312 | * @param file the file |
| 194 | 313 | * @param rank the rank |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
314 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
315 | static inline void piece_remove(GameState *gamestate, File file, Rank rank) { |
|
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
316 | piece_set(gamestate, file, rank, 0); |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
317 | } |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
318 | |
|
170
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
319 | typedef size_t(*moves_generator_func)(const GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
320 | Color c, File f, Rank r, Move *moves); |
|
169
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
321 | |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
322 | /** |
|
170
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
323 | * Calculates all allowed moves for a specific piece. |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
324 | * |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
325 | * Use the macros for the specific pieces instead. |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
326 | * |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
327 | * @param gamestate the current gamestate |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
328 | * @param f the file of the piece |
| 194 | 329 | * @param r the rank of the piece |
|
170
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
330 | * @param moves target array for the list of moves |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
331 | * @return the number of moves stored in the @p moves array |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
332 | */ |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
333 | size_t piece_moves_allowed(const GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
334 | File f, Rank r, Move *moves); |
|
170
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
335 | |
|
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
336 | /** |
|
169
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
337 | * Internal function used to filter out illegal moves. |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
338 | * |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
339 | * Use the macros for the specific pieces instead. |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
340 | * |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
341 | * @param gamestate the current gamestate |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
342 | * @param c color of the piece |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
343 | * @param f the file of the piece |
| 194 | 344 | * @param r the rank of the piece |
|
169
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
345 | * @param moves target array for the list of moves |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
346 | * @param func a function that unconditionally generates the moves |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
347 | * @return the number of moves stored in the @p moves array |
|
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
348 | */ |
|
170
bde99d803caf
implements stalemate detection + fixes missing const qualifier in new *_moves[_allowed] API
Mike Becker <universe@uap-core.de>
parents:
169
diff
changeset
|
349 | size_t filter_moves_allowed(const GameState *gamestate, |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
350 | Color c, File f, Rank r, Move *moves, moves_generator_func func); |
|
169
9962f5d98764
add functions to list the possible moves for each piece
Mike Becker <universe@uap-core.de>
parents:
168
diff
changeset
|
351 | |
|
133
c58ae152733e
simplify FEN generation and add current_color() function to rules
Mike Becker <universe@uap-core.de>
parents:
131
diff
changeset
|
352 | /** |
|
167
fd1d3f0a7a73
fix regression: pieces could not threaten fields when pinned
Mike Becker <universe@uap-core.de>
parents:
166
diff
changeset
|
353 | * Determines a list of theoretically possible moves to the specified field. |
|
fd1d3f0a7a73
fix regression: pieces could not threaten fields when pinned
Mike Becker <universe@uap-core.de>
parents:
166
diff
changeset
|
354 | * |
|
fd1d3f0a7a73
fix regression: pieces could not threaten fields when pinned
Mike Becker <universe@uap-core.de>
parents:
166
diff
changeset
|
355 | * This will also list moves for pieces that are actually pinned. |
|
168
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
356 | * Use get_real_candidates() to get only moves for pieces that are not pinned. |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
357 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
358 | * The out-parameters may both be NULL, but if any of them is set, the other |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
359 | * must be set, too. |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
360 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
361 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
362 | * @param file file of the field to check |
| 194 | 363 | * @param rank rank of the field to check |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
364 | * @param color the color of the piece that should move to the field |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
365 | * @param moves the array where to store the moves |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
366 | * (must be large enough, 16 is always enough) |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
367 | * @param movecount a pointer where the number of moves is stored |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
368 | * @return true, if any piece of the specified color can move to the specified |
|
167
fd1d3f0a7a73
fix regression: pieces could not threaten fields when pinned
Mike Becker <universe@uap-core.de>
parents:
166
diff
changeset
|
369 | * field regardless of being pinned |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
370 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
371 | bool get_candidates(const GameState *gamestate, File file, Rank rank, |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
372 | Color color, Move* moves, size_t* movecount); |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
373 | |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
374 | /** |
|
168
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
375 | * Determines a list of possible moves to the specified field. |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
376 | * |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
377 | * This cannot be used to check if a piece covers / threatens a field because |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
378 | * this is also possible when the piece is pinned. Use get_candidates() for a |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
379 | * list of possible moves regardless of pins. |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
380 | * |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
381 | * The out-parameters may both be NULL, but if any of them is set, the other |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
382 | * must be set, too. |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
383 | * |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
384 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
385 | * @param file file of the field to check |
| 194 | 386 | * @param rank rank of the field to check |
|
168
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
387 | * @param color the color of the piece that should move to the field |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
388 | * @param moves the array where to store the moves |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
389 | * (must be large enough, 16 is always enough) |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
390 | * @param movecount a pointer where the number of moves is stored |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
391 | * @return true, if any piece of the specified color can move to the specified |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
392 | * field and is not pinned |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
393 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
394 | bool get_real_candidates(const GameState *gamestate, File file, Rank rank, |
|
168
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
395 | Color color, Move* moves, size_t* movecount); |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
396 | |
|
663676cfef6e
fix unnecessary disambiguation + simplify get_real_threats() code
Mike Becker <universe@uap-core.de>
parents:
167
diff
changeset
|
397 | /** |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
398 | * Checks, if a specified field is threatened by a piece of a certain color. |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
399 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
400 | * A field is threatened, if there is a piece of the specified color that could |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
401 | * capture an opponent piece on this field, regardless of being pinned. |
| 25 | 402 | * |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
403 | * 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
|
404 | * 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
|
405 | * |
|
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
|
406 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
407 | * @param file file of the field to check |
| 194 | 408 | * @param rank rank of the field to check |
|
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
|
409 | * @param color the color of the piece that should threaten the field |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
410 | * @param threats the array where to store the threats |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
411 | * (must be large enough, 16 is always enough) |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
412 | * @param threatcount a pointer where the count of threats is stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
413 | * @return true, if any piece of the specified color threatens the specified |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
414 | * field |
|
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
|
415 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
416 | bool get_threats(const GameState *gamestate, File file, Rank rank, |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
417 | Color color, Move* threats, size_t* threatcount); |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
418 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
419 | /** |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
420 | * Checks, if a specified field is threatened by a piece of a certain color AND |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
421 | * 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
|
422 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
423 | * 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
|
424 | * 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
|
425 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
426 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
427 | * @param file file of the field to check |
| 194 | 428 | * @param rank rank of the field to check |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
429 | * @param color the color of the piece that should threaten the field |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
430 | * @param threats the array where to store the threats |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
431 | * (must be large enough, 16 is always enough) |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
432 | * @param threatcount a pointer where the count of threats is stored |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
433 | * @return true, if any piece of the specified color threatens the specified |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
434 | * field and is not pinned |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
435 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
436 | bool get_real_threats(const GameState *gamestate, File file, Rank rank, |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
437 | Color color, Move* threats, size_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
|
438 | |
|
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
|
439 | /** |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
440 | * Checks, if a specified field is threatened by a piece of a certain color. |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
441 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
442 | * A field is threatened, if there is a piece of the specified color that could |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
443 | * capture an opponent piece on this field, regardless of being pinned. |
|
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
|
444 | * |
| 25 | 445 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
446 | * @param file file of the field to check |
| 194 | 447 | * @param rank rank of the field to check |
| 25 | 448 | * @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
|
449 | * @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
|
450 | * field |
| 25 | 451 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
452 | #define is_covered(gamestate, file, rank, color) \ |
|
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
453 | get_threats(gamestate, file, rank, 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
|
454 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
455 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
456 | * Checks, if a specified field is attacked by a piece of a certain color. |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
457 | * |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
458 | * I.e. the field is threatened by a piece AND this piece is not pinned and |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
459 | * therefore able to perform the move. |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
460 | * |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
461 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
462 | * @param file file of the field to check |
| 194 | 463 | * @param rank rank of the field to check |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
464 | * @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
|
465 | * @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
|
466 | * 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
|
467 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
468 | #define is_attacked(gamestate, file, rank, color) \ |
|
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
469 | get_real_threats(gamestate, file, rank, 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
|
470 | |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
471 | /** |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
472 | * 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
|
473 | * |
|
166
1a9b662bc201
fix regression: pawns cannot block check threats
Mike Becker <universe@uap-core.de>
parents:
163
diff
changeset
|
474 | * A field is protected, if any piece except the king can either capture on |
|
1a9b662bc201
fix regression: pawns cannot block check threats
Mike Becker <universe@uap-core.de>
parents:
163
diff
changeset
|
475 | * that field or move to that field (and is not pinned). |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
476 | * |
|
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
477 | * @param gamestate the current game state |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
478 | * @param file file of the field to check |
| 194 | 479 | * @param rank rank of the field to check |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
480 | * @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
|
481 | * @return true, if any piece (excluding the king) of the specified color |
|
166
1a9b662bc201
fix regression: pawns cannot block check threats
Mike Becker <universe@uap-core.de>
parents:
163
diff
changeset
|
482 | * can move to the specified field (including capturing moves) |
|
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
483 | */ |
|
195
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
484 | bool is_protected(const GameState *gamestate, |
|
27d02ccb0cef
flip File and Rank parameters into correct order
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
485 | File file, Rank rank, Color color); |
| 25 | 486 | |
| 487 | /** | |
|
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
|
488 | * 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
|
489 | * 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
|
490 | * |
|
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
|
491 | * 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
|
492 | * 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
|
493 | * 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
|
494 | * 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
|
495 | * |
|
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
|
496 | * @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
|
497 | * @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
|
498 | * @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
|
499 | * 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
|
500 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
501 | bool is_pinned(const GameState *gamestate, const 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
|
502 | |
|
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
|
503 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
504 | * 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
|
505 | * object. |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
506 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
507 | * When short algebraic notation is used, the source position is determined by |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
508 | * evaluating the allowed moves according to the current game state. |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
509 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
510 | * This function expects correct notation of check and checkmate indicators. |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
511 | * For a more lazy evaluation, use eval_move(). |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
512 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
513 | * 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
|
514 | * 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
|
515 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
516 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
517 | * @param mstr the input string to parse |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
518 | * @param color the color of the player to evaluate the move for |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
519 | * @param move a pointer to object where the move data shall be stored |
|
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
|
520 | * @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
|
521 | */ |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
522 | int eval_move_strict(const GameState *gamestate, |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
523 | const char *mstr, Color color, Move *move); |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
524 | |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
525 | /** |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
526 | * Evaluates a move syntactically and stores the move data in the specified |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
527 | * object. |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
528 | * |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
529 | * When short algebraic notation is used, the source position is determined by |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
530 | * evaluating the allowed moves according to the current game state. |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
531 | * |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
532 | * This function automatically corrects missing or incorrect check/checkmate |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
533 | * indicators. Use eval_move_strict() if you want to keep the original notation. |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
534 | * |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
535 | * For a purely syntactic check, regardless of whether a piece exists that is |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
536 | * allowed to move that way, use check_move(). |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
537 | * |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
538 | * @param gamestate the current game state |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
539 | * @param mstr the input string to parse |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
540 | * @param color the color of the player to evaluate the move for |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
541 | * @param move a pointer to object where the move data shall be stored |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
542 | * @return status code (see macros in this file for the list of codes) |
|
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
543 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
544 | int eval_move(const GameState *gamestate, |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
545 | const char *mstr, Color color, Move *move); |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
546 | |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
547 | /** |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
548 | * Calculates the move string within the specified move. |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
549 | * |
|
186
8230904458a7
fix regression in network play: we have to transmit capture/check(mate) flags
Mike Becker <universe@uap-core.de>
parents:
184
diff
changeset
|
550 | * The string calculated in short algebraic notation. |
|
8230904458a7
fix regression in network play: we have to transmit capture/check(mate) flags
Mike Becker <universe@uap-core.de>
parents:
184
diff
changeset
|
551 | * The @p gamestate is needed to determine how to disambiguate the move, |
|
8230904458a7
fix regression in network play: we have to transmit capture/check(mate) flags
Mike Becker <universe@uap-core.de>
parents:
184
diff
changeset
|
552 | * if necessary. |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
553 | * |
|
186
8230904458a7
fix regression in network play: we have to transmit capture/check(mate) flags
Mike Becker <universe@uap-core.de>
parents:
184
diff
changeset
|
554 | * Recommended to be called before apply_move() so that a clean |
|
8230904458a7
fix regression in network play: we have to transmit capture/check(mate) flags
Mike Becker <universe@uap-core.de>
parents:
184
diff
changeset
|
555 | * move string is added to the list of recorded moves. |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
556 | * |
|
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
557 | * @param gamestate the current game state |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
558 | * @param move the move data |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
559 | */ |
|
182
04c65336777f
start fixing the network protocol
Mike Becker <universe@uap-core.de>
parents:
181
diff
changeset
|
560 | void format_move(const GameState *gamestate, Move *move); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
561 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
562 | /** |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
563 | * 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
|
564 | * allowed to move that way. |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
565 | * |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
566 | * @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
|
567 | * @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
|
568 | * @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
|
569 | */ |
|
160
f87832cba8b8
refactor rules.h / rules.c
Mike Becker <universe@uap-core.de>
parents:
157
diff
changeset
|
570 | int check_move(const char *mstr, Color color); |
|
115
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
571 | |
|
206201d544be
relax validation of premoves to allow retaking pieces
Mike Becker <universe@uap-core.de>
parents:
107
diff
changeset
|
572 | /** |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
573 | * Validates move by applying chess rules. |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
574 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
575 | * @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
|
576 | * @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
|
577 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
578 | int validate_move(const GameState *gamestate, const Move *move); |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
579 | |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
580 | /** |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
581 | * Applies a move and deletes captured pieces. |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
582 | * |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
583 | * @param gamestate the current game state |
|
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
584 | * @param move the move to apply |
|
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
585 | */ |
|
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
586 | 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
|
587 | |
|
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
|
588 | /** |
|
122
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
589 | * 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
|
590 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
591 | * 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
|
592 | * |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
593 | * @param gamestate the current game state |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
594 | * @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
|
595 | * @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
|
596 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
597 | void gamestate_at_move(const GameState *gamestate, |
|
122
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
598 | unsigned move_number, GameState *replay); |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
599 | |
|
e65d9b5e9324
implement interactive game review
Mike Becker <universe@uap-core.de>
parents:
115
diff
changeset
|
600 | /** |
|
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
|
601 | * 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
|
602 | * 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
|
603 | * |
|
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
|
604 | * @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
|
605 | * @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
|
606 | * @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
|
607 | * 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
|
608 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
609 | uint16_t remaining_movetime2(const GameState *gamestate, unsigned move_number); |
| 33 | 610 | |
| 611 | /** | |
| 612 | * Returns the remaining time on the clock for the specified player. | |
| 613 | * | |
| 614 | * @param gamestate the current game state | |
| 615 | * @param color either BLACK or WHITE | |
| 616 | * @return the remaining time - if time control is disabled, this function | |
| 617 | * always returns zero | |
| 618 | */ | |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
619 | uint16_t remaining_movetime(const GameState *gamestate, Color color); |
| 33 | 620 | |
|
100
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
621 | /** |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
622 | * Converts clock time to string. |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
623 | * |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
624 | * @param time the time to format |
|
685af47592b5
add API for formatting clocks
Mike Becker <universe@uap-core.de>
parents:
99
diff
changeset
|
625 | * @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
|
626 | * @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
|
627 | */ |
|
107
36dd94278142
fix that PGN (with comments) can exceed 80 chars
Mike Becker <universe@uap-core.de>
parents:
100
diff
changeset
|
628 | 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
|
629 | |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
630 | /** |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
631 | * 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
|
632 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
633 | * 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
|
634 | * 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
|
635 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
636 | * 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
|
637 | * 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
|
638 | * |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
639 | * @param gamestate the current game state |
|
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
640 | * @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
|
641 | */ |
|
163
2a6d83f4677e
major refactoring of rules API
Mike Becker <universe@uap-core.de>
parents:
161
diff
changeset
|
642 | bool check_threefold_repetition(const GameState *gamestate); |
|
131
c33567d61ba7
prepare netcode for claiming threefold repetition
Mike Becker <universe@uap-core.de>
parents:
130
diff
changeset
|
643 | |
|
179
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
644 | /** |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
645 | * Checks if neither side has enough material left to win the game. |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
646 | * |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
647 | * Returns true if for both players one of the following three cases are true: |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
648 | * - they have only the king left |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
649 | * - they have a king + knight and the opponent has king + queens |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
650 | * - they have a king + bishop and the opponent doesn't have |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
651 | * opposite color bishops or knights or pawns |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
652 | * |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
653 | * @param gamestate the current game state |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
654 | * @return true if neither side can win due to insufficient material |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
655 | */ |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
656 | bool check_no_material(const GameState *gamestate); |
|
5ef724e21702
implement draw due to insufficient material
Mike Becker <universe@uap-core.de>
parents:
173
diff
changeset
|
657 | |
|
10
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
658 | #endif /* RULES_H */ |
|
1347e4dabac0
prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
659 |