bd_moves.c

filter: Statementstmt 99.0% 211 / 213 Branchbran 87.3% 194 / 222 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 93.1% 405 / 435 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bd_search.c ↑ Summary bd_engine.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 48751753662 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 1757706 static void bd_variant_default(struct bd_variant *v)
65 {
66 1757706 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 0 1757706 1757706
67 1757706 memset(v, 0, sizeof(*v));
68 1757706 v->throne_pass = 1;
69 1757706 v->repeat = BD_REPEAT_DEFAULT;
70 1757706 v->ply_cap = BD_PLY_CAP_DEFAULT;
71 }
72  
73 310263286 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 310263286 ▶ if (v) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (v) return v; 310263286 0 310263286
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 5553000499 static int bd_move_make(int from, int to) { return (from & 0x7F) | ((to & 0x7F) << 7); }
85 433667066 static int bd_move_from(int mv) { return mv & 0x7F; }
86 432050967 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 5958585792 static int may_land(int piece, int sq, const struct bd_variant *v)
93 {
94 5958585792 ▶ 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; 104961256 5853624536 5958585792
if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry; 12461285 92499971 104961256
if ((E())->is_throne(sq)) return piece == BD_KING && v->throne_reentry; 1025237 11436048 12461285
95 5853624536 ▶ if ((E())->is_corner(sq)) return piece == BD_KING;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_corner(sq)) return piece == BD_KING; 353645341 5499979195 5853624536
96 5499979195 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 152395603 static int bd_gen_moves(const struct bd_board *b, const struct bd_variant *v, int *out, int max)
107 {
108 152395603 int side = (E())->side(b);
109 152395603 int rank, file, d, n = 0;
110 152395603 v = variant_or_default(v);
111  
112 1219164824 ▶ for (rank = 0; rank < BD_SIZE; rank++) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = 0; rank < BD_SIZE; rank++) { 1066769221 152395603 1219164824
113 8534153768 ▶ for (file = 0; file < BD_SIZE; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < BD_SIZE; file++) { 7467384547 1066769221 8534153768
114 7467384547 int from = (E())->square_of(file, rank);
115 7467384547 int piece = AT(b, from);
116 7467384547 ▶ 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; 2214991164 5252393383 7467384547
if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue; 2214991164 0 2214991164
if (!BD_IS_PIECE(piece) || (E())->side_of(piece) != side) continue; 1015170354 1199820810 2214991164
117 5999104050 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 4799283240 1199820810 5999104050
118 4799283240 int sq = from + ORTHO[d];
119 10749197034 ▶ while (AT(b, sq) == BD_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
while (AT(b, sq) == BD_EMPTY) { 5958392714 4790804320 10749197034
120 5958392714 ▶ if (may_land(piece, sq, v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_land(piece, sq, v)) { 5559707642 398685072 5958392714
121 5559707642 ▶ 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); 5551524382 8183260 5559707642
if (out && n < max) out[n] = bd_move_make(from, sq); 5551524382 0 5551524382
122 5559707642 n++;
123 }
124 5958392714 ▶ if ((E())->is_throne(sq) && !v->throne_pass) break;
Branch: 2 branches
BranchTrueFalseTotal
if ((E())->is_throne(sq) && !v->throne_pass) break; 104960688 5853432026 5958392714
if ((E())->is_throne(sq) && !v->throne_pass) break; 8478920 96481768 104960688
125 5949913794 sq += ORTHO[d];
126 }
127 }
128 }
129 }
130 152395603 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 345099 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 345099 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
140 345099 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 345099 345099
141 7029597 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 6934732 94865 7029597
142 6934732 ▶ if (list[i] == mv) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (list[i] == mv) return 1; 250234 6684498 6934732
143 94865 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 1380137 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 1380137 v = variant_or_default(v);
152  
153 1380137 ▶ 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; 1380135 2 1380137
if (!(E())->on_board(from) || !(E())->on_board(to)) return BD_WHY_OFF_BOARD; 1 1380134 1380135
154 1380134 piece = AT(b, from);
155 1380134 ▶ if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE;
Branch: 2 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE; 831508 548626 1380134
if (!BD_IS_PIECE(piece)) return BD_WHY_NO_PIECE; 0 831508 831508
156 831508 ▶ 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; 328734 502774 831508
157 502774 ▶ if (from == to) return BD_WHY_NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if (from == to) return BD_WHY_NO_MOVE; 6638 496136 502774
158  
159 496136 ff = (E())->file_of(from); fr = (E())->rank_of(from);
160 496136 tf = (E())->file_of(to); tr = (E())->rank_of(to);
161 496136 ▶ 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; 367547 128589 496136
if (ff != tf && fr != tr) return BD_WHY_NOT_A_LINE; 238865 128682 367547
162  
163 257271 ▶ if (fr == tr) step = (tf > ff) ? 1 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fr == tr) step = (tf > ff) ? 1 : -1; 128682 128589 257271
if (fr == tr) step = (tf > ff) ? 1 : -1; 64213 64469 128682
164 128589 ▶ else step = (tr > fr) ? BD_STRIDE : -BD_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
else step = (tr > fr) ? BD_STRIDE : -BD_STRIDE; 64487 64102 128589
165  
166 480158 ▶ for (sq = from + step; sq != to; sq += step) {
Branch: 1 branch
BranchTrueFalseTotal
for (sq = from + step; sq != to; sq += step) { 267601 212557 480158
167 267601 ▶ if (AT(b, sq) != BD_EMPTY) return BD_WHY_BLOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, sq) != BD_EMPTY) return BD_WHY_BLOCKED; 44094 223507 267601
168 223507 ▶ 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; 9880 213627 223507
if ((E())->is_throne(sq) && !v->throne_pass) return BD_WHY_THRONE; 620 9260 9880
169 }
170 212557 ▶ if (AT(b, to) != BD_EMPTY) return BD_WHY_BLOCKED;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, to) != BD_EMPTY) return BD_WHY_BLOCKED; 19479 193078 212557
171 193078 ▶ if (!may_land(piece, to, v))
Branch: 1 branch
BranchTrueFalseTotal
if (!may_land(piece, to, v)) 1299 191779 193078
172 1299 ▶ 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; 349 950 1299
173 191779 return BD_WHY_OK;
174 }
175  
176 /* ---- relocate ------------------------------------------------------------------ */
177  
178 97512990 static void bd_relocate(struct bd_board *b, int mv)
179 {
180 97512990 int from = bd_move_from(mv), to = bd_move_to(mv);
181 int piece;
182 97512990 ▶ if (!(E())->on_board(from) || !(E())->on_board(to)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(E())->on_board(from) || !(E())->on_board(to)) return; 97512989 1 97512990
if (!(E())->on_board(from) || !(E())->on_board(to)) return; 0 97512989 97512989
183 97512989 piece = AT(b, from);
184 97512989 (E())->lift(b, from);
185 97512989 (E())->put(b, to, piece);
186 97512989 (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 97517255 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 97517255 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
202 97517255 unsigned long long total = 0;
203  
204 97517255 ▶ if (depth <= 1) return (unsigned long long) n;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 1) return (unsigned long long) n; 94150262 3366993 97517255
205 3366993 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 3366993 3366993
206 100879112 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 97512119 3366993 100879112
207 97512119 int from = bd_move_from(list[i]), to = bd_move_to(list[i]);
208 97512119 int piece = AT(b, from);
209 97512119 int side = (E())->side(b);
210 97512119 bd_relocate(b, list[i]);
211 97512119 total += perft_go(b, v, depth - 1);
212 97512119 (E())->lift(b, to);
213 97512119 (E())->put(b, from, piece);
214 97512119 (E())->set_side(b, side);
215 }
216 3366993 return total;
217 }
218  
219 5138 static unsigned long long bd_perft_slides(struct bd_board *b, const struct bd_variant *v, int depth)
220 {
221 5138 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 5138 5138
222 5138 ▶ if (depth <= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) return 1; 2 5136 5138
223 5136 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 31219767 static int bd_hostile_to(const struct bd_board *b, int sq, int side)
239 {
240 31219767 int cell = AT(b, sq);
241 31219767 ▶ 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; 12675020 18544747 31219767
if (BD_IS_PIECE(cell)) return (E())->side_of(cell) != side; 6961475 5713545 12675020
242 24258292 ▶ if (cell != BD_EMPTY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cell != BD_EMPTY) return 0; 5713545 18544747 24258292
243 18544747 ▶ if ((E())->is_corner(sq)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_corner(sq)) return 1; 939941 17604806 18544747
244 17604806 ▶ if ((E())->is_throne(sq)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(sq)) return 1; 489186 17115620 17604806
245 17115620 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 5032722 static int king_falls(const struct bd_board *b, const struct bd_variant *v, int king, int step)
256 {
257 int d;
258  
259 5032722 ▶ if (v->king_everywhere_two)
Branch: 1 branch
BranchTrueFalseTotal
if (v->king_everywhere_two) 1022237 4010485 5032722
260 1022237 return bd_hostile_to(b, king + step, BD_DEFENDERS);
261  
262 4010485 ▶ if (v->king_strong) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->king_strong) { 1024486 2985999 4010485
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 1337168 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 1336889 279 1337168
268 1336889 int sq = king + ORTHO[d];
269 1336889 int cell = AT(b, sq);
270 1336889 ▶ if (cell == BD_ATTACKER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cell == BD_ATTACKER) continue; 306081 1030808 1336889
271 1030808 ▶ if (cell == BD_EMPTY && (E())->is_throne(sq)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (cell == BD_EMPTY && (E())->is_throne(sq)) continue; 535445 495363 1030808
if (cell == BD_EMPTY && (E())->is_throne(sq)) continue; 6601 528844 535445
272 1024207 return 0;
273 }
274 279 return 1;
275 }
276  
277 2985999 ▶ if ((E())->is_throne(king)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(king)) { 364943 2621056 2985999
278 502329 ▶ for (d = 0; d < 4; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) 502082 247 502329
279 502082 ▶ if (AT(b, king + ORTHO[d]) != BD_ATTACKER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, king + ORTHO[d]) != BD_ATTACKER) return 0; 364696 137386 502082
280 247 return 1;
281 }
282  
283 2621056 ▶ if ((E())->beside_throne(king)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->beside_throne(king)) { 428286 2192770 2621056
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 807766 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 801268 6498 807766
289 801268 int sq = king + ORTHO[d];
290 801268 int cell = AT(b, sq);
291 801268 ▶ if ((E())->is_throne(sq)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->is_throne(sq)) { 110396 690872 801268
292 110396 ▶ 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; 11522 98874 110396
if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0; 11522 0 11522
if (BD_IS_PIECE(cell) && (E())->side_of(cell) == BD_DEFENDERS) return 0; 5746 5776 11522
293 }
294 690872 ▶ else if (cell != BD_ATTACKER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (cell != BD_ATTACKER) return 0; 416042 274830 690872
295 }
296 6498 return 1;
297 }
298  
299 2192770 return bd_hostile_to(b, king + step, BD_DEFENDERS);
300 }
301  
302 78240150 static int bd_captures_at(const struct bd_board *b, const struct bd_variant *v, int to, int *out)
303 {
304 78240150 int mover = AT(b, to);
305 78240150 int side, d, n = 0;
306 78240150 v = variant_or_default(v);
307 78240150 ▶ if (!BD_IS_PIECE(mover)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(mover)) return 0; 78240149 1 78240150
if (!BD_IS_PIECE(mover)) return 0; 0 78240149 78240149
308 78240149 side = (E())->side_of(mover);
309  
310 391200745 ▶ for (d = 0; d < 4; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < 4; d++) { 312960596 78240149 391200745
311 312960596 int sq = to + ORTHO[d];
312 312960596 int victim = AT(b, sq);
313 int taken;
314 312960596 ▶ 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; 89953467 223007129 312960596
if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue; 55978523 33974944 89953467
if (!BD_IS_PIECE(victim) || (E())->side_of(victim) == side) continue; 22941127 33037396 55978523
315 33037396 ▶ 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]); 5032722 28004674 33037396
316 28004674 else taken = bd_hostile_to(b, sq + ORTHO[d], (E())->side_of(victim));
317 33037396 ▶ if (taken) {
Branch: 1 branch
BranchTrueFalseTotal
if (taken) { 4768487 28268909 33037396
318 4768487 ▶ if (out) out[n] = sq;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out[n] = sq; 4768487 0 4768487
319 4768487 n++;
320 }
321 }
322 78240149 return n;
323 }
324  
325 10807 static int on_edge(int sq)
326 {
327 10807 int f = (E())->file_of(sq), r = (E())->rank_of(sq);
328 10807 ▶ 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; 10126 681 10807
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 9483 643 10126
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 8902 581 9483
return f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; 613 8289 8902
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 78237442 static int bd_do_move(struct bd_board *b, const struct bd_variant *v, int mv, struct bd_undo *u)
336 {
337 78237442 int from = bd_move_from(mv), to = bd_move_to(mv);
338 int taken[BD_CAPTURES_MAX];
339 78237442 int piece, n, i, flags = 0;
340 78237442 v = variant_or_default(v);
341  
342 78237442 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 78237442 0 78237442
343 78237442 memset(u, 0, sizeof(*u));
344 78237442 u->side = (E())->side(b);
345 78237442 u->key = (E())->key(b);
346 }
347 78237442 ▶ 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; 78237441 1 78237442
if (!(E())->on_board(from) || !(E())->on_board(to)) return 0; 0 78237441 78237441
348 78237441 piece = AT(b, from);
349 78237441 ▶ 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; 78237440 1 78237441
if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0; 78237440 0 78237440
if (!BD_IS_PIECE(piece) || AT(b, to) != BD_EMPTY) return 0; 1 78237439 78237440
350  
351 78237439 (E())->lift(b, from);
352 78237439 (E())->put(b, to, piece);
353  
354 78237439 n = bd_captures_at(b, v, to, taken);
355 83003011 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4765572 78237439 83003011
356 4765572 int gone = AT(b, taken[i]);
357 4765572 ▶ if (gone == BD_KING) flags |= BD_KING_TAKEN;
Branch: 1 branch
BranchTrueFalseTotal
if (gone == BD_KING) flags |= BD_KING_TAKEN; 363450 4402122 4765572
358 4765572 ▶ 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; } 4765572 0 4765572
359 }
360 83003011 ▶ 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]); 4765572 78237439 83003011
361 78237439 ▶ if (n) flags |= BD_DID_CAPTURE;
Branch: 1 branch
BranchTrueFalseTotal
if (n) flags |= BD_DID_CAPTURE; 4698113 73539326 78237439
362  
363 78237439 ▶ 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)))) 8909616 69327823 78237439
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 8115703 793913 8909616
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 10807 8104896 8115703
if (piece == BD_KING && ((E())->is_corner(to) || (v->escape_edge && on_edge(to)))) 2518 8289 10807
364 796431 flags |= BD_KING_HOME;
365  
366 78237439 (E())->set_side(b, BD_OTHER((E())->side(b)));
367  
368 78237439 ▶ 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; } 78237439 0 78237439
369 78237439 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 76143668 static void bd_undo_move(struct bd_board *b, const struct bd_undo *u)
375 {
376 int from, to, piece, i;
377 76143668 ▶ if (!u || u->mv == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!u || u->mv == 0) return; 76143668 0 76143668
if (!u || u->mv == 0) return; 1 76143667 76143668
378 76143667 from = bd_move_from(u->mv);
379 76143667 to = bd_move_to(u->mv);
380 76143667 piece = AT(b, to);
381 76143667 (E())->lift(b, to);
382 76143667 (E())->put(b, from, piece);
383 80799677 ▶ 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++) 4656010 76143667 80799677
for (i = 0; i < u->n && i < BD_CAPTURES_MAX; i++) 4656010 0 4656010
384 4656010 (E())->put(b, u->square[i], u->piece[i]);
385 76143667 (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 1867702 static int bd_preview(const struct bd_board *b, const struct bd_variant *v, int mv, int *out, int *flags)
391 {
392 1867702 struct bd_board *c = (E())->board_copy(b);
393 struct bd_undo u;
394 int i, n;
395 1867702 ▶ if (flags) *flags = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (flags) *flags = 0; 1867702 0 1867702
396 1867702 ▶ if (!c) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return 0; 0 1867702 1867702
397 1867702 bd_do_move(c, v, mv, &u);
398 1867702 (E())->board_drop(c);
399 1867702 n = u.n;
400 1966719 ▶ 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]; 99017 1867702 1966719
for (i = 0; i < n && out; i++) out[i] = u.square[i]; 99017 0 99017
401 1867702 ▶ if (flags) *flags = u.flags;
Branch: 1 branch
BranchTrueFalseTotal
if (flags) *flags = u.flags; 1867702 0 1867702
402 1867702 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 51334814 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 51334814 int n = bd_gen_moves(b, v, list, BD_MOVES_MAX), i;
416 51334814 unsigned long long total = 0;
417  
418 51334814 ▶ if (depth <= 1) return (unsigned long long) n;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 1) return (unsigned long long) n; 49562129 1772685 51334814
419 1772685 ▶ if (n > BD_MOVES_MAX) n = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) n = BD_MOVES_MAX; 0 1772685 1772685
420 53102681 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 51329996 1772685 53102681
421 struct bd_undo u;
422 51329996 bd_do_move(b, v, list[i], &u);
423 51329996 total += perft_full(b, v, depth - 1);
424 51329996 bd_undo_move(b, &u);
425 }
426 1772685 return total;
427 }
428  
429 4818 static unsigned long long bd_perft(struct bd_board *b, const struct bd_variant *v, int depth)
430 {
431 4818 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 4818 4818
432 4818 ▶ if (depth <= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (depth <= 0) return 1; 0 4818 4818
433 4818 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 59 void bd_moves_install(struct bd_abi *t)
441 {
442 59 t->variant_default = bd_variant_default;
443 59 t->move_make = bd_move_make;
444 59 t->move_from = bd_move_from;
445 59 t->move_to = bd_move_to;
446 59 t->gen_moves = bd_gen_moves;
447 59 t->is_legal = bd_is_legal;
448 59 t->why_not = bd_why_not;
449 59 t->relocate = bd_relocate;
450 59 t->perft_slides = bd_perft_slides;
451  
452 59 t->hostile_to = bd_hostile_to;
453 59 t->captures_at = bd_captures_at;
454 59 t->do_move = bd_do_move;
455 59 t->undo_move = bd_undo_move;
456 59 t->preview = bd_preview;
457 59 t->perft = bd_perft;
458 59 }
« bd_search.c ↑ Summary bd_engine.c »