bd_moves.c

filter: Statementstmt 0.0% 0 / 213 Branchbran 0.0% 0 / 222 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 435 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bd_engine.c ↑ Summary bd_rules.c »
1 /* bd_moves.c - where a piece may go, what it captures, and two perfts.
2 *
3 * PERL-FREE, as bd_engine.c is.
4 *
5 * THE RULES THIS FILE IMPLEMENTS, quoted from the source the distribution
6 * names in its documentation:
7 *
8 * 4. "Pieces move any distance orthogonally, not landing on nor jumping over
9 * other pieces on the board."
10 * 5. "No piece may land on the central square, not even the king once he has
11 * left it. Only the king may land on the corner squares."
12 *
13 * and one decision the source leaves open: a piece may SLIDE ACROSS the empty
14 * throne and may not stop on it. Rule 4 forbids jumping pieces and rule 5
15 * forbids landing; neither says the empty square blocks. bd_variant.throne_pass
16 * turns that off.
17 *
18 * TWO PERFTS. `relocate` moves a piece and passes the turn, and perft_slides
19 * counts that capture-free game; `do_move` captures as well, and perft counts
20 * that one. Two ladders, so that a wrong number says which half is wrong.
21 *
22 * THE CAPTURE RULES, from the same source:
23 *
24 * 6. "A piece other than the king is captured when it is surrounded
25 * orthogonally on two opposite squares by enemies. The king can take part
26 * in captures in partnership with a defender."
27 * 7. "A piece may also be captured between an enemy and the empty central
28 * square or a corner square."
29 * 8. "When in the central square, the king is captured by surrounding him on
30 * four orthogonal sides with attackers."
31 * 9. "When standing beside the central square, the king may be captured by
32 * surrounding him on the remaining three sides with attackers."
33 * 10. "Elsewhere on the board, the king is captured as other pieces. This
34 * includes beside the corners, where he can be captured between an
35 * attacker and the corner as in rule 7."
36 *
37 * and three decisions where the source is silent or brief:
38 *
39 * - ONLY THE PIECE THAT MOVED CAPTURES. A piece may stand between two
40 * enemies unharmed if neither of them just moved to make it so. The search
41 * therefore goes outward from the square moved to and nowhere else.
42 * - EVERY ENEMY THE MOVE CLOSES ON IS TAKEN, up to three, each judged on the
43 * board as it stands after the move and before anything is removed.
44 * - THE KING CAPTURES like any piece of his side, as the piece that moves or
45 * as the piece that waits.
46 */
47  
48  
49 #include <string.h>
50 #include "bd_abi.h"
51  
52 /* bd_engine.c owns the struct; this file reaches it through the same table
53 * every other consumer uses, which keeps the two files honest about the
54 * interface. */
55 0 static const struct bd_abi *E(void) { return bd_abi_table(); }
56  
57 #define AT(b, sq) ((E())->at((b), (sq)))
58  
59 /* left, right, down, up: the order the move list is written in */
60 static const int ORTHO[4] = { -1, 1, -BD_STRIDE, BD_STRIDE };
61  
62 /* ---- the rule set ------------------------------------------------------------ */
63  
64 0 static void bd_variant_default(struct bd_variant *v)
65 {
66 0 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 0 0 0
67 0 memset(v, 0, sizeof(*v));
68 0 v->throne_pass = 1;
69 0 v->repeat = BD_REPEAT_DEFAULT;
70 0 v->ply_cap = BD_PLY_CAP_DEFAULT;
71 }
72  
73 0 static const struct bd_variant *variant_or_default(const struct bd_variant *v)
74 {
75 static struct bd_variant DEFAULT;
76 static int ready = 0;
77 0 ▶ if (v) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (v) return v; 0 0 0
78 0 ▶ if (!ready) { bd_variant_default(&DEFAULT); ready = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ready) { bd_variant_default(&DEFAULT); ready = 1; } 0 0 0
79 0 return &DEFAULT;
80 }
81  
82 /* ---- a move ------------------------------------------------------------------ */
83  
84 0 static int bd_move_make(int from, int to) { return (from & 0x7F) | ((to & 0x7F) << 7); }
85 0 static int bd_move_from(int mv) { return mv & 0x7F; }
86 0 static int bd_move_to(int mv) { return (mv >> 7) & 0x7F; }
87  
88 /* ---- rule 5: who may STOP where ------------------------------------------------
89 *
90 * The one place that knows. The square is empty when this is asked.
91 */
92 0 static int may_land(int piece, int sq, const struct bd_variant *v)
93 {
94 0 ▶ if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry;
Branch: 3 branches
BranchTrueFalseTotal
if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry; 0 0 0
if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry; 0 0 0
if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry; 0 0 0
95 0 ▶ if ((E())->is_corner(sq)) return piece == BD_KING;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_corner(sq)) return piece == BD_KING; 0 0 0
96 0 return 1;
97 }
98  
99 /* ---- generation ----------------------------------------------------------------
100 *
101 * A slide walks while the cell is BD_EMPTY. The ring reads BD_BORDER and a
102 * piece reads as itself, so both end the walk with no bounds test. An empty
103 * throne does not end it unless the rule set says so; it is only refused as a
104 * place to stop.
105 */
106 0 static int bd_gen_moves(const struct bd_board *b, const struct bd_variant *v, int *out, int max)
107 {
108 0 int side = (E())->side(b);
109 0 int rank, file, d, n = 0;
110 0 v = variant_or_default(v);
111  
112 0 ▶ for (rank = 0; rank < BD_SIZE; rank++) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < BD_SIZE; rank++) { 0 0 0
113 0 ▶ for (file = 0; file < BD_SIZE; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < BD_SIZE; file++) { 0 0 0
114 0 int from = (E())->square_of(file, rank);
115 0 int piece = AT(b, from);
116 0 ▶ if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue; 0 0 0
if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue; 0 0 0
if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue; 0 0 0
117 0 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 0 0 0
118 0 int sq = from + ORTHO[d];
119 0 ▶ while (AT(b, sq) == BD_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
while (AT(b, sq) == BD_EMPTY) { 0 0 0
120 0 ▶ if (may_land(piece, sq, v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_land(piece, sq, v)) { 0 0 0
121 0 ▶ if (out && n < max) out[n] = bd_move_make(from, sq);
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) out[n] = bd_move_make(from, sq); 0 0 0
if (out && n < max) out[n] = bd_move_make(from, sq); 0 0 0
122 0 n++;
123 }
124 0 ▶ if ((E())->is_throne(sq) && !v->throne_pass) break;
Branch: 2 branches
BranchTrueFalseTotal
if ((E())->is_throne(sq) && !v->throne_pass) break; 0 0 0
if ((E())->is_throne(sq) && !v->throne_pass) break; 0 0 0
125 0 sq += ORTHO[d];
126 }
127 }
128 }
129 }
130 0 return n;
131 }
132  
133 /* Generation and a search of the list. NOT a second implementation of the
134 * rules: why_not below is that, by necessity, and a test holds the two
135 * together. */
136 0 static int bd_is_legal(const struct bd_board *b, const struct bd_variant *v, int mv)
137 {
138 int list[BD_MOVES_MAX];
139 0 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
140 0 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 0 0
141 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
142 0 ▶ if (list[i] == mv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (list[i] == mv) return 1; 0 0 0
143 0 return 0;
144 }
145  
146 /* The reason a person is shown. It walks the line itself, so that it can say
147 * WHAT was wrong and not only that something was. */
148 0 static int bd_why_not(const struct bd_board *b, const struct bd_variant *v, int from, int to)
149 {
150 int piece, step, sq, ff, fr, tf, tr;
151 0 v = variant_or_default(v);
152  
153 0 ▶ if (!(E())->on_board(from) || !(E())->on_board(to)) return BD_WHY_OFF_BOARD;
Branch: 2 branches
BranchTrueFalseTotal
if (!(E())->on_board(from) || !(E())->on_board(to)) return BD_WHY_OFF_BOARD; 0 0 0
if (!(E())->on_board(from) || !(E())->on_board(to)) return BD_WHY_OFF_BOARD; 0 0 0
154 0 piece = AT(b, from);
155 0 ▶ if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE;
Branch: 2 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE; 0 0 0
if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE; 0 0 0
156 0 ▶ if ((E())->side_of(piece) != (E())->side(b)) return BD_WHY_NOT_YOURS;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->side_of(piece) != (E())->side(b)) return BD_WHY_NOT_YOURS; 0 0 0
157 0 ▶ if (from == to) return BD_WHY_NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if (from == to) return BD_WHY_NO_MOVE; 0 0 0
158  
159 0 ff = (E())->file_of(from); fr = (E())->rank_of(from);
160 0 tf = (E())->file_of(to); tr = (E())->rank_of(to);
161 0 ▶ if (ff != tf && fr != tr) return BD_WHY_NOT_A_LINE;
Branch: 2 branches
BranchTrueFalseTotal
if (ff != tf && fr != tr) return BD_WHY_NOT_A_LINE; 0 0 0
if (ff != tf && fr != tr) return BD_WHY_NOT_A_LINE; 0 0 0
162  
163 0 ▶ if (fr == tr) step = (tf > ff) ? 1 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fr == tr) step = (tf > ff) ? 1 : -1; 0 0 0
if (fr == tr) step = (tf > ff) ? 1 : -1; 0 0 0
164 0 ▶ else step = (tr > fr) ? BD_STRIDE : -BD_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
else step = (tr > fr) ? BD_STRIDE : -BD_STRIDE; 0 0 0
165  
166 0 ▶ for (sq = from + step; sq != to; sq += step) {
Branch: 1 branch
BranchTrueFalseTotal
for (sq = from + step; sq != to; sq += step) { 0 0 0
167 0 ▶ if (AT(b, sq) != BD_EMPTY) return BD_WHY_BLOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, sq) != BD_EMPTY) return BD_WHY_BLOCKED; 0 0 0
168 0 ▶ if ((E())->is_throne(sq) && !v->throne_pass) return BD_WHY_THRONE;
Branch: 2 branches
BranchTrueFalseTotal
if ((E())->is_throne(sq) && !v->throne_pass) return BD_WHY_THRONE; 0 0 0
if ((E())->is_throne(sq) && !v->throne_pass) return BD_WHY_THRONE; 0 0 0
169 }
170 0 ▶ if (AT(b, to) != BD_EMPTY) return BD_WHY_BLOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, to) != BD_EMPTY) return BD_WHY_BLOCKED; 0 0 0
171 0 ▶ if (!may_land(piece, to, v))
Branch: 1 branch
BranchTrueFalseTotal
if (!may_land(piece, to, v)) 0 0 0
172 0 ▶ return (E())->is_throne(to) ? BD_WHY_THRONE : BD_WHY_CORNER;
Branch: 1 branch
BranchTrueFalseTotal
return (E())->is_throne(to) ? BD_WHY_THRONE : BD_WHY_CORNER; 0 0 0
173 0 return BD_WHY_OK;
174 }
175  
176 /* ---- relocate ------------------------------------------------------------------ */
177  
178 0 static void bd_relocate(struct bd_board *b, int mv)
179 {
180 0 int from = bd_move_from(mv), to = bd_move_to(mv);
181 int piece;
182 0 ▶ if (!(E())->on_board(from) || !(E())->on_board(to)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(E())->on_board(from) || !(E())->on_board(to)) return; 0 0 0
if (!(E())->on_board(from) || !(E())->on_board(to)) return; 0 0 0
183 0 piece = AT(b, from);
184 0 (E())->lift(b, from);
185 0 (E())->put(b, to, piece);
186 0 (E())->set_side(b, BD_OTHER((E())->side(b)));
187 }
188  
189 /* ---- perft, slides only --------------------------------------------------------
190 *
191 * The last ply is counted and not played: the number of leaves under a node
192 * one ply from the bottom is the length of its move list.
193 *
194 * A move is taken back by hand here because nothing is ever captured: put the
195 * piece where it was and give the turn back. The key comes back with it, being
196 * an xor each way.
197 */
198 0 static unsigned long long perft_go(struct bd_board *b, const struct bd_variant *v, int depth)
199 {
200 int list[BD_MOVES_MAX];
201 0 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
202 0 unsigned long long total = 0;
203  
204 0 ▶ if (depth <= 1) return (unsigned long long) n;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 1) return (unsigned long long) n; 0 0 0
205 0 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 0 0
206 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
207 0 int from = bd_move_from(list[i]), to = bd_move_to(list[i]);
208 0 int piece = AT(b, from);
209 0 int side = (E())->side(b);
210 0 bd_relocate(b, list[i]);
211 0 total += perft_go(b, v, depth - 1);
212 0 (E())->lift(b, to);
213 0 (E())->put(b, from, piece);
214 0 (E())->set_side(b, side);
215 }
216 0 return total;
217 }
218  
219 0 static unsigned long long bd_perft_slides(struct bd_board *b, const struct bd_variant *v, int depth)
220 {
221 0 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 0 0
222 0 ▶ if (depth <= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) return 1; 0 0 0
223 0 return perft_go(b, variant_or_default(v), depth);
224 }
225  
226 /* ---- capture -------------------------------------------------------------------
227 *
228 * THE ONE PREDICATE. A piece is judged as a piece wherever it stands, the
229 * throne and the corners included: the king on his throne is an enemy of the
230 * attackers because he is the king, and a friend of the defenders for the same
231 * reason. Only an EMPTY marked square is hostile in its own right, and it is
232 * hostile to both sides ("A piece may also be captured...").
233 *
234 * The ring is not hostile. Nothing in the rules captures against the edge, and
235 * that is what lets a walk two cells out from the moved piece read the border
236 * and carry on.
237 */
238 0 static int bd_hostile_to(const struct bd_board *b, int sq, int side)
239 {
240 0 int cell = AT(b, sq);
241 0 ▶ if (BD_IS_PIECE(cell)) return (E())->side_of(cell) != side;
Branch: 2 branches
BranchTrueFalseTotal
if (BD_IS_PIECE(cell)) return (E())->side_of(cell) != side; 0 0 0
if (BD_IS_PIECE(cell)) return (E())->side_of(cell) != side; 0 0 0
242 0 ▶ if (cell != BD_EMPTY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cell != BD_EMPTY) return 0; 0 0 0
243 0 ▶ if ((E())->is_corner(sq)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_corner(sq)) return 1; 0 0 0
244 0 ▶ if ((E())->is_throne(sq)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(sq)) return 1; 0 0 0
245 0 return 0;
246 }
247  
248 /* The king stands on `king` and an attacker has just arrived at king - step.
249 * Three places, and three rules.
250 *
251 * On the throne and beside it the king is judged on his whole neighbourhood
252 * and not on the line through the piece that moved. That piece is one of the
253 * neighbours by construction, so the mover is still what closes the capture.
254 */
255 0 static int king_falls(const struct bd_board *b, const struct bd_variant *v, int king, int step)
256 {
257 int d;
258  
259 0 ▶ if (v->king_everywhere_two)
Branch: 1 branch
BranchTrueFalseTotal
if (v->king_everywhere_two) 0 0 0
260 0 return bd_hostile_to(b, king + step, BD_DEFENDERS);
261  
262 0 ▶ if (v->king_strong) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->king_strong) { 0 0 0
263 /* every side closed: by an attacker or by the empty throne. The edge
264 * closes nothing, so a king on it cannot be taken; and since every
265 * square next to a corner is on the edge, a corner never gets to
266 * count and is not asked about. */
267 0 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 0 0 0
268 0 int sq = king + ORTHO[d];
269 0 int cell = AT(b, sq);
270 0 ▶ if (cell == BD_ATTACKER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cell == BD_ATTACKER) continue; 0 0 0
271 0 ▶ if (cell == BD_EMPTY && (E())->is_throne(sq)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (cell == BD_EMPTY && (E())->is_throne(sq)) continue; 0 0 0
if (cell == BD_EMPTY && (E())->is_throne(sq)) continue; 0 0 0
272 0 return 0;
273 }
274 0 return 1;
275 }
276  
277 0 ▶ if ((E())->is_throne(king)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(king)) { 0 0 0
278 0 ▶ for (d = 0; d < 4; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) 0 0 0
279 0 ▶ if (AT(b, king + ORTHO[d]) != BD_ATTACKER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, king + ORTHO[d]) != BD_ATTACKER) return 0; 0 0 0
280 0 return 1;
281 }
282  
283 0 ▶ if ((E())->beside_throne(king)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->beside_throne(king)) { 0 0 0
284 /* "the remaining three sides": the fourth is the throne. In play it is
285 * empty, the king being the only piece that ever stands there. A
286 * DEFENDER on it, which only a hand-built board has, is a friend at
287 * his side and he is not surrounded. */
288 0 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 0 0 0
289 0 int sq = king + ORTHO[d];
290 0 int cell = AT(b, sq);
291 0 ▶ if ((E())->is_throne(sq)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(sq)) { 0 0 0
292 0 ▶ if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0; 0 0 0
if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0; 0 0 0
if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0; 0 0 0
293 }
294 0 ▶ else if (cell != BD_ATTACKER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (cell != BD_ATTACKER) return 0; 0 0 0
295 }
296 0 return 1;
297 }
298  
299 0 return bd_hostile_to(b, king + step, BD_DEFENDERS);
300 }
301  
302 0 static int bd_captures_at(const struct bd_board *b, const struct bd_variant *v, int to, int *out)
303 {
304 0 int mover = AT(b, to);
305 0 int side, d, n = 0;
306 0 v = variant_or_default(v);
307 0 ▶ if (!BD_IS_PIECE(mover)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(mover)) return 0; 0 0 0
if (!BD_IS_PIECE(mover)) return 0; 0 0 0
308 0 side = (E())->side_of(mover);
309  
310 0 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 0 0 0
311 0 int sq = to + ORTHO[d];
312 0 int victim = AT(b, sq);
313 int taken;
314 0 ▶ if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue; 0 0 0
if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue; 0 0 0
if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue; 0 0 0
315 0 ▶ if (victim == BD_KING) taken = king_falls(b, v, sq, ORTHO[d]);
Branch: 1 branch
BranchTrueFalseTotal
if (victim == BD_KING) taken = king_falls(b, v, sq, ORTHO[d]); 0 0 0
316 0 else taken = bd_hostile_to(b, sq + ORTHO[d], (E())->side_of(victim));
317 0 ▶ if (taken) {
Branch: 1 branch
BranchTrueFalseTotal
if (taken) { 0 0 0
318 0 ▶ if (out) out[n] = sq;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = sq; 0 0 0
319 0 n++;
320 }
321 }
322 0 return n;
323 }
324  
325 0 static int on_edge(int sq)
326 {
327 0 int f = (E())->file_of(sq), r = (E())->rank_of(sq);
328 0 ▶ return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1;
Branch: 4 branches
BranchTrueFalseTotal
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 0 0 0
329 }
330  
331 /* COLLECT, THEN REMOVE. Every capture is decided on the board as it stands
332 * after the move and before any piece leaves it, so the order they leave in
333 * cannot matter. A test removes them in every order to show that it does not.
334 */
335 0 static int bd_do_move(struct bd_board *b, const struct bd_variant *v, int mv, struct bd_undo *u)
336 {
337 0 int from = bd_move_from(mv), to = bd_move_to(mv);
338 int taken[BD_CAPTURES_MAX];
339 0 int piece, n, i, flags = 0;
340 0 v = variant_or_default(v);
341  
342 0 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 0 0 0
343 0 memset(u, 0, sizeof(*u));
344 0 u->side = (E())->side(b);
345 0 u->key = (E())->key(b);
346 }
347 0 ▶ if (!(E())->on_board(from) || !(E())->on_board(to)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(E())->on_board(from) || !(E())->on_board(to)) return 0; 0 0 0
if (!(E())->on_board(from) || !(E())->on_board(to)) return 0; 0 0 0
348 0 piece = AT(b, from);
349 0 ▶ if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0; 0 0 0
if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0; 0 0 0
if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0; 0 0 0
350  
351 0 (E())->lift(b, from);
352 0 (E())->put(b, to, piece);
353  
354 0 n = bd_captures_at(b, v, to, taken);
355 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
356 0 int gone = AT(b, taken[i]);
357 0 ▶ if (gone == BD_KING) flags |= BD_KING_TAKEN;
Branch: 1 branch
BranchTrueFalseTotal
if (gone == BD_KING) flags |= BD_KING_TAKEN; 0 0 0
358 0 ▶ if (u) { u->square[i] = taken[i]; u->piece[i] = gone; }
Branch: 1 branch
BranchTrueFalseTotal
if (u) { u->square[i] = taken[i]; u->piece[i] = gone; } 0 0 0
359 }
360 0 ▶ for (i = 0; i < n; i++) (E())->lift(b, taken[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) (E())->lift(b, taken[i]); 0 0 0
361 0 ▶ if (n) flags |= BD_DID_CAPTURE;
Branch: 1 branch
BranchTrueFalseTotal
if (n) flags |= BD_DID_CAPTURE; 0 0 0
362  
363 0 ▶ if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to))))
Branch: 4 branches
BranchTrueFalseTotal
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 0 0 0
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 0 0 0
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 0 0 0
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 0 0 0
364 0 flags |= BD_KING_HOME;
365  
366 0 (E())->set_side(b, BD_OTHER((E())->side(b)));
367  
368 0 ▶ if (u) { u->mv = mv; u->flags = flags; u->n = n; }
Branch: 1 branch
BranchTrueFalseTotal
if (u) { u->mv = mv; u->flags = flags; u->n = n; } 0 0 0
369 0 return flags;
370 }
371  
372 /* Puts every piece back and the turn with it. The key returns by the same
373 * xors that moved it, and a test holds it to the key that was saved. */
374 0 static void bd_undo_move(struct bd_board *b, const struct bd_undo *u)
375 {
376 int from, to, piece, i;
377 0 ▶ if (!u || u->mv == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!u || u->mv == 0) return; 0 0 0
if (!u || u->mv == 0) return; 0 0 0
378 0 from = bd_move_from(u->mv);
379 0 to = bd_move_to(u->mv);
380 0 piece = AT(b, to);
381 0 (E())->lift(b, to);
382 0 (E())->put(b, from, piece);
383 0 ▶ for (i = 0; i < u->n && i < BD_CAPTURES_MAX; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < u->n && i < BD_CAPTURES_MAX; i++) 0 0 0
for (i = 0; i < u->n && i < BD_CAPTURES_MAX; i++) 0 0 0
384 0 (E())->put(b, u->square[i], u->piece[i]);
385 0 (E())->set_side(b, u->side);
386 }
387  
388 /* do_move on a copy, so that the board asked about is never touched, not even
389 * for the length of the call. */
390 0 static int bd_preview(const struct bd_board *b, const struct bd_variant *v, int mv, int *out, int *flags)
391 {
392 0 struct bd_board *c = (E())->board_copy(b);
393 struct bd_undo u;
394 int i, n;
395 0 ▶ if (flags) *flags = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (flags) *flags = 0; 0 0 0
396 0 ▶ if (!c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return 0; 0 0 0
397 0 bd_do_move(c, v, mv, &u);
398 0 (E())->board_drop(c);
399 0 n = u.n;
400 0 ▶ for (i = 0; i < n && out; i++) out[i] = u.square[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && out; i++) out[i] = u.square[i]; 0 0 0
for (i = 0; i < n && out; i++) out[i] = u.square[i]; 0 0 0
401 0 ▶ if (flags) *flags = u.flags;
Branch: 1 branch
BranchTrueFalseTotal
if (flags) *flags = u.flags; 0 0 0
402 0 return n;
403 }
404  
405 /* ---- perft, with captures -------------------------------------------------------
406 *
407 * The same shape as perft_slides: the last ply is counted and not played.
408 * NOTHING ENDS THE WALK. A king may be taken and the count goes on through
409 * what his side has left, which is what the reference implementation it is
410 * compared with does too.
411 */
412 0 static unsigned long long perft_full(struct bd_board *b, const struct bd_variant *v, int depth)
413 {
414 int list[BD_MOVES_MAX];
415 0 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
416 0 unsigned long long total = 0;
417  
418 0 ▶ if (depth <= 1) return (unsigned long long) n;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 1) return (unsigned long long) n; 0 0 0
419 0 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 0 0
420 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
421 struct bd_undo u;
422 0 bd_do_move(b, v, list[i], &u);
423 0 total += perft_full(b, v, depth - 1);
424 0 bd_undo_move(b, &u);
425 }
426 0 return total;
427 }
428  
429 0 static unsigned long long bd_perft(struct bd_board *b, const struct bd_variant *v, int depth)
430 {
431 0 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 0 0
432 0 ▶ if (depth <= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) return 1; 0 0 0
433 0 return perft_full(b, variant_or_default(v), depth);
434 }
435  
436 /* ---- installed into the table by bd_engine.c ---------------------------------- */
437  
438 void bd_moves_install(struct bd_abi *t);
439  
440 0 void bd_moves_install(struct bd_abi *t)
441 {
442 0 t->variant_default = bd_variant_default;
443 0 t->move_make = bd_move_make;
444 0 t->move_from = bd_move_from;
445 0 t->move_to = bd_move_to;
446 0 t->gen_moves = bd_gen_moves;
447 0 t->is_legal = bd_is_legal;
448 0 t->why_not = bd_why_not;
449 0 t->relocate = bd_relocate;
450 0 t->perft_slides = bd_perft_slides;
451  
452 0 t->hostile_to = bd_hostile_to;
453 0 t->captures_at = bd_captures_at;
454 0 t->do_move = bd_do_move;
455 0 t->undo_move = bd_undo_move;
456 0 t->preview = bd_preview;
457 0 t->perft = bd_perft;
458 0 }
« bd_engine.c ↑ Summary bd_rules.c »