xq_moves.c

filter: Statementstmt 98.6% 215 / 218 Branchbran 90.9% 211 / 232 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.6% 426 / 450 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xq_judge.c ↑ Summary xq_search.c »
1 /* xq_moves.c - move generation, the attack walk, and perft.
2 *
3 * PERL-FREE, as xq_engine.c is. Phase 03 of plan_xiangqi.
4 *
5 * THE FIVE PREDICATES EACH HAVE A NAME so a test can address one. A generator
6 * written as a single loop with the blocking folded in cannot be tested in
7 * pieces, and four of the five are the rules a reader gets wrong:
8 *
9 * horse_leg the orthogonal step must be empty, either colour blocks
10 * elephant_eye the intervening diagonal point must be empty
11 * cannon_screens exactly one, and only for a capture
12 * soldier_may forward always, sideways only across the river, never back
13 * generals_face the flying general, implemented as an ATTACK
14 *
15 * ONE DEVIATION FROM THE PLAN, and it is a phase boundary rather than a
16 * decision: `attacked` was scoped to phase 04, and it cannot be, because
17 * `gen_legal` filters on "does this leave my own general attacked" and perft
18 * counts LEGAL moves. Generation cannot be finished without it. Phase 04 keeps
19 * `outcome`, `mate_in` and the stalemate loss, which genuinely are its own.
20 */
21  
22 #include <string.h>
23 #include "xq_abi.h"
24  
25 /* xq_engine.c owns the struct; this file reaches it through the same table
26 * every other consumer uses, which keeps the two files honest about the
27 * interface and costs one indirection per call in code that is not the search. */
28 9127072473 static const struct xq_abi *E(void) { return xq_abi_table(); }
29  
30 #define AT(b, pt) ((E())->at((b), (pt)))
31 #define PT(f, r) ((E())->point_of((f), (r)))
32 #define FILE_OF(pt) ((E())->file_of(pt))
33 #define RANK_OF(pt) ((E())->rank_of(pt))
34 #define ONB(pt) ((E())->on_board(pt))
35  
36 static const int ORTHO[4] = { -1, 1, -XQ_STRIDE, XQ_STRIDE };
37 static const int DIAG[4] = { -XQ_STRIDE - 1, -XQ_STRIDE + 1, XQ_STRIDE - 1, XQ_STRIDE + 1 };
38  
39 /* ---- the five predicates ----------------------------------------------------- */
40  
41 /* The horse's eight destinations are eight PAIRS: one orthogonal step, which
42 * must be empty, then one diagonal away from it. The blocker is of either
43 * colour and there are four blocking points for eight destinations.
44 *
45 * THE MISTAKE TO AVOID is testing the blocker against the destination's own
46 * neighbour. Both readings agree for six of the eight and differ for two, which
47 * is the shape of a bug that wins most of its games.
48 */
49 74855799 static int xq_horse_leg(const struct xq_board *b, int from, int to)
50 {
51 74855799 int d = to - from, i;
52 static const int MOVE[8] = {
53 -2 * XQ_STRIDE - 1, -2 * XQ_STRIDE + 1, /* two up, one across */
54 2 * XQ_STRIDE - 1, 2 * XQ_STRIDE + 1, /* two down, one across */
55 -XQ_STRIDE - 2, -XQ_STRIDE + 2, /* one up, two across */
56 XQ_STRIDE - 2, XQ_STRIDE + 2
57 };
58 static const int LEG[8] = {
59 -XQ_STRIDE, -XQ_STRIDE, XQ_STRIDE, XQ_STRIDE, -1, 1, -1, 1
60 };
61 (void) b;
62 312134326 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 312134325 1 312134326
63 312134325 ▶ if (d == MOVE[i]) return from + LEG[i];
Branch: 1 branch
BranchTrueFalseTotal
if (d == MOVE[i]) return from + LEG[i]; 74855798 237278527 312134325
64 1 return -1;
65 }
66  
67 /* "Exactly two points diagonally and may not jump over intervening pieces", and
68 * the intervening point is the eye. Four destinations, four eyes, one each. */
69 2 static int xq_elephant_eye(const struct xq_board *b, int from, int to)
70 {
71 2 int d = to - from, i;
72 (void) b;
73 8 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 8 0 8
74 8 ▶ if (d == 2 * DIAG[i]) return from + DIAG[i];
Branch: 1 branch
BranchTrueFalseTotal
if (d == 2 * DIAG[i]) return from + DIAG[i]; 2 6 8
75 0 return -1;
76 }
77  
78 /* One ray walk answers both halves of the cannon.
79 *
80 * 0 screens, destination empty a move
81 * 0 screens, destination occupied NOT a move: a cannon does not capture
82 * adjacently, and this is the row that
83 * gets written wrong
84 * 1 screen, destination enemy a capture
85 * 1 screen, destination own or empty nothing
86 * 2 or more nothing
87 */
88 7 static int xq_cannon_screens(const struct xq_board *b, int from, int to)
89 {
90 7 int df = FILE_OF(to) - FILE_OF(from);
91 7 int dr = RANK_OF(to) - RANK_OF(from);
92 7 int step, pt, n = 0;
93  
94 7 ▶ if (from == to) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (from == to) return -1; 0 7 7
95 7 ▶ if (!ONB(from) || !ONB(to)) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!ONB(from) || !ONB(to)) return -1; 7 0 7
if (!ONB(from) || !ONB(to)) return -1; 0 7 7
96 7 ▶ if (df != 0 && dr != 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (df != 0 && dr != 0) return -1; 1 6 7
if (df != 0 && dr != 0) return -1; 1 0 1
97 6 ▶ step = (df != 0) ? (df > 0 ? 1 : -1) : (dr > 0 ? XQ_STRIDE : -XQ_STRIDE);
Branch: 3 branches
BranchTrueFalseTotal
step = (df != 0) ? (df > 0 ? 1 : -1) : (dr > 0 ? XQ_STRIDE : -XQ_STRIDE); 0 6 6
step = (df != 0) ? (df > 0 ? 1 : -1) : (dr > 0 ? XQ_STRIDE : -XQ_STRIDE); 0 0 0
step = (df != 0) ? (df > 0 ? 1 : -1) : (dr > 0 ? XQ_STRIDE : -XQ_STRIDE); 6 0 6
98 23 ▶ for (pt = from + step; pt != to; pt += step)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = from + step; pt != to; pt += step) 17 6 23
99 17 ▶ if (AT(b, pt) != XQ_EMPTY) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, pt) != XQ_EMPTY) n++; 5 12 17
100 6 return n;
101 }
102  
103 /* One point forward; sideways as well once across the river; NEVER backward and
104 * never promoted. A soldier on the enemy's last rank has only its sideways
105 * moves, which is a rule and not a dead end. */
106 3 static int xq_soldier_may(const struct xq_board *b, int from, int to)
107 {
108 3 int piece = AT(b, from);
109 3 int colour = XQ_COLOUR(piece);
110 3 int d = to - from;
111 3 ▶ int fwd = (colour == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
int fwd = (colour == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE; 3 0 3
112  
113 3 ▶ if (XQ_KIND(piece) != XQ_SOLDIER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_KIND(piece) != XQ_SOLDIER) return 0; 0 3 3
114 3 ▶ if (!ONB(to)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) return 0; 0 3 3
115 3 ▶ if (d == fwd) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (d == fwd) return 1; 0 3 3
116 3 ▶ if ((d == 1 || d == -1) && (E())->crossed_river(from, colour)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if ((d == 1 || d == -1) && (E())->crossed_river(from, colour)) return 1; 1 2 3
if ((d == 1 || d == -1) && (E())->crossed_river(from, colour)) return 1; 0 1 1
if ((d == 1 || d == -1) && (E())->crossed_river(from, colour)) return 1; 1 1 2
117 2 return 0;
118 }
119  
120 /* The flying general. The generals may not end a move facing each other down an
121 * open file, and the encyclopaedia's sentence is the one that matters: "creating
122 * this situation in the first place means moving into check, and is therefore
123 * not allowed". So this is not a special case in the generator: gen_legal
124 * refuses any move that leaves it true, in both directions, with no extra
125 * branch, and the flying capture itself is therefore never generated. */
126 100533145 static int xq_generals_face(const struct xq_board *b)
127 {
128 100533145 int rg = (E())->find(b, XQ_RED | XQ_GENERAL);
129 100533145 int bg = (E())->find(b, XQ_BLACK | XQ_GENERAL);
130 int pt;
131  
132 100533145 ▶ if (!rg || !bg) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!rg || !bg) return 0; 100517324 15821 100533145
if (!rg || !bg) return 0; 3177 100514147 100517324
133 100514147 ▶ if (FILE_OF(rg) != FILE_OF(bg)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (FILE_OF(rg) != FILE_OF(bg)) return 0; 12605434 87908713 100514147
134 232172532 ▶ for (pt = rg + XQ_STRIDE; pt < bg; pt += XQ_STRIDE)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = rg + XQ_STRIDE; pt < bg; pt += XQ_STRIDE) 232082389 90143 232172532
135 232082389 ▶ if (AT(b, pt) != XQ_EMPTY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, pt) != XQ_EMPTY) return 0; 87818570 144263819 232082389
136 90143 return 1;
137 }
138  
139 /* ---- generation --------------------------------------------------------------
140 *
141 * PSEUDO-LEGAL: every move the piece may make, without asking whether it leaves
142 * its own general attacked. gen_legal filters these.
143 */
144  
145 516012459 static int add(int *out, int n, int from, int to)
146 {
147 516012459 ▶ if (n >= XQ_MAX_MOVES) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= XQ_MAX_MOVES) return n; 0 516012459 516012459
148 516012459 out[n] = (E())->move_make(from, to);
149 516012459 return n + 1;
150 }
151  
152 13993523 static int xq_gen_moves(const struct xq_board *b, int *out)
153 {
154 13993523 int side = (E())->side(b);
155 13993523 int n = 0, f, r, i;
156  
157 153928753 ▶ for (r = 0; r < XQ_RANKS; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < XQ_RANKS; r++) { 139935230 13993523 153928753
158 1399352300 ▶ for (f = 0; f < XQ_FILES; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) { 1259417070 139935230 1399352300
159 1259417070 int from = PT(f, r);
160 1259417070 int piece = AT(b, from);
161 int kind;
162 1259417070 ▶ if (XQ_COLOUR(piece) != side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(piece) != side) continue; 1077158854 182258216 1259417070
163 182258216 kind = XQ_KIND(piece);
164  
165 182258216 switch (kind) {
166 13992974 case XQ_GENERAL:
167 69964870 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 55971896 13992974 69964870
168 55971896 int to = from + ORTHO[i];
169 55971896 ▶ if (!(E())->in_palace(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->in_palace(to, side)) continue; 13740305 42231591 55971896
170 42231591 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 25342066 16889525 42231591
171 16889525 n = add(out, n, from, to);
172 }
173 13992974 break;
174  
175 27016396 case XQ_ADVISOR:
176 135081980 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 108065584 27016396 135081980
177 108065584 int to = from + DIAG[i];
178 108065584 ▶ if (!(E())->in_palace(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->in_palace(to, side)) continue; 74893914 33171670 108065584
179 33171670 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 5516170 27655500 33171670
180 27655500 n = add(out, n, from, to);
181 }
182 27016396 break;
183  
184 26365257 case XQ_ELEPHANT:
185 131826285 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 105461028 26365257 131826285
186 105461028 int to = from + 2 * DIAG[i];
187 105461028 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 48274602 57186426 105461028
188 /* never crosses the river: the elephant stays on its own half */
189 57186426 ▶ if ((E())->crossed_river(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->crossed_river(to, side)) continue; 263428 56922998 57186426
190 56922998 ▶ if (AT(b, from + DIAG[i]) != XQ_EMPTY) continue; /* the eye */
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, from + DIAG[i]) != XQ_EMPTY) continue; /* the eye */ 1540439 55382559 56922998
191 55382559 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 7946502 47436057 55382559
192 47436057 n = add(out, n, from, to);
193 }
194 26365257 break;
195  
196 20345518 case XQ_HORSE:
197 183109662 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 162764144 20345518 183109662
198 static const int MOVE[8] = {
199 -2 * XQ_STRIDE - 1, -2 * XQ_STRIDE + 1,
200 2 * XQ_STRIDE - 1, 2 * XQ_STRIDE + 1,
201 -XQ_STRIDE - 2, -XQ_STRIDE + 2,
202 XQ_STRIDE - 2, XQ_STRIDE + 2
203 };
204 162764144 int to = from + MOVE[i];
205 int leg;
206 162764144 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 88127864 74636280 162764144
207 74636280 leg = xq_horse_leg(b, from, to);
208 74636280 ▶ if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue; 74636280 0 74636280
if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue; 18184083 56452197 74636280
209 56452197 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 7906611 48545586 56452197
210 48545586 n = add(out, n, from, to);
211 }
212 20345518 break;
213  
214 24834239 case XQ_CHARIOT:
215 124171195 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 99336956 24834239 124171195
216 int to;
217 198710289 ▶ for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) { 149157133 49553156 198710289
218 149157133 int t = AT(b, to);
219 149157133 ▶ if (t == XQ_EMPTY) { n = add(out, n, from, to); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) { n = add(out, n, from, to); continue; } 99373333 49783800 149157133
220 49783800 ▶ if (XQ_COLOUR(t) != side) n = add(out, n, from, to);
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(t) != side) n = add(out, n, from, to); 8807899 40975901 49783800
221 49783800 break;
222 }
223 }
224 24834239 break;
225  
226 20895334 case XQ_CANNON:
227 104476670 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 83581336 20895334 104476670
228 83581336 int to, seen = 0;
229 396368499 ▶ for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) { 333529851 62838648 396368499
230 333529851 int t = AT(b, to);
231 333529851 ▶ if (!seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) { 258943461 74586390 333529851
232 258943461 ▶ if (t == XQ_EMPTY) { n = add(out, n, from, to); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) { n = add(out, n, from, to); continue; } 201155501 57787960 258943461
233 57787960 seen = 1; /* the screen; not a capture */
234 57787960 continue;
235 }
236 74586390 ▶ if (t == XQ_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) continue; 53843702 20742688 74586390
237 20742688 ▶ if (XQ_COLOUR(t) != side) n = add(out, n, from, to);
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(t) != side) n = add(out, n, from, to); 12944273 7798415 20742688
238 20742688 break; /* one screen only, ever */
239 }
240 }
241 20895334 break;
242  
243 48808498 case XQ_SOLDIER:
244 {
245 48808498 ▶ int fwd = (side == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
int fwd = (side == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE; 22439651 26368847 48808498
246 int cand[3];
247 48808498 int c = 0, k;
248 48808498 cand[c++] = from + fwd;
249 48808498 ▶ if ((E())->crossed_river(from, side)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->crossed_river(from, side)) { 2726910 46081588 48808498
250 2726910 cand[c++] = from - 1;
251 2726910 cand[c++] = from + 1;
252 }
253 103070816 ▶ for (k = 0; k < c; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < c; k++) { 54262318 48808498 103070816
254 54262318 int to = cand[k];
255 54262318 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 100959 54161359 54262318
256 54161359 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 956574 53204785 54161359
257 53204785 n = add(out, n, from, to);
258 }
259 }
260 48808498 break;
261  
262 0 default:
263 0 break;
264 }
265 }
266 }
267 13993523 return n;
268 }
269  
270 /* ---- the attack walk ---------------------------------------------------------
271 *
272 * Generated BACKWARDS from the point rather than forwards from every piece,
273 * which is what makes the legality filter affordable. Three things here are not
274 * a chess engine's:
275 *
276 * the cannon needs exactly one screen, so the ray walk counts
277 * the horse is blocked, and the leg is checked FROM THE HORSE'S SIDE
278 * the general attacks its orthogonal neighbours inside its own palace
279 */
280 62726963 static int xq_attacked(const struct xq_board *b, int pt, int by)
281 {
282 int i;
283  
284 62726963 ▶ if (!ONB(pt)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(pt)) return 0; 0 62726963 62726963
285  
286 /* chariots and cannons share the ray: the first piece along it can be a
287 * chariot, and the first piece BEYOND that first piece can be a cannon */
288 301348899 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 243433639 57915260 301348899
289 243433639 int s, first = 0, t;
290 745694489 ▶ for (s = pt + ORTHO[i]; ONB(s); s += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = pt + ORTHO[i]; ONB(s); s += ORTHO[i]) { 649835271 95859218 745694489
291 649835271 t = AT(b, s);
292 649835271 ▶ if (t == XQ_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) continue; 338141304 311693967 649835271
293 311693967 ▶ if (!first) {
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { 165281553 146412414 311693967
294 165281553 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CHARIOT) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CHARIOT) return 1; 12471124 152810429 165281553
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CHARIOT) return 1; 1161798 11309326 12471124
295 /* A general one step away, and THE TARGET must be in that
296 * general's palace, not merely the general itself. A general
297 * cannot leave its palace, so it cannot capture outside one;
298 * testing the general's own square instead said that a general
299 * on d2 attacked c2, which made an enemy piece there look
300 * defended when it was not.
301 *
302 * PERFT CANNOT SEE THIS, which is why it survived phase 03: the
303 * only point `attacked` is asked about during a legality filter
304 * is a general, and a general is always inside its own palace.
305 * Phase 05's `protected_at` would have inherited it. */
306 164119755 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL 11309326 152810429 164119755
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL 2201 11307125 11309326
307 2201 ▶ && s == pt + ORTHO[i] && (E())->in_palace(pt, by)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& s == pt + ORTHO[i] && (E())->in_palace(pt, by)) return 1; 517 1684 2201
&& s == pt + ORTHO[i] && (E())->in_palace(pt, by)) return 1; 209 308 517
308 164119546 first = 1;
309 164119546 continue;
310 }
311 146412414 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CANNON) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CANNON) return 1; 41013812 105398602 146412414
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CANNON) return 1; 3649696 37364116 41013812
312 142762718 break;
313 }
314 }
315  
316 /* horses: eight origins, each with its leg tested from the horse's side */
317 {
318 static const int MOVE[8] = {
319 -2 * XQ_STRIDE - 1, -2 * XQ_STRIDE + 1,
320 2 * XQ_STRIDE - 1, 2 * XQ_STRIDE + 1,
321 -XQ_STRIDE - 2, -XQ_STRIDE + 2,
322 XQ_STRIDE - 2, XQ_STRIDE + 2
323 };
324 520600344 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 462826636 57773708 520600344
325 462826636 int from = pt - MOVE[i];
326 int t, leg;
327 462826636 ▶ if (!ONB(from)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(from)) continue; 213890279 248936357 462826636
328 248936357 t = AT(b, from);
329 248936357 ▶ if (XQ_COLOUR(t) != by || XQ_KIND(t) != XQ_HORSE) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) != by || XQ_KIND(t) != XQ_HORSE) continue; 3017892 245918465 248936357
if (XQ_COLOUR(t) != by || XQ_KIND(t) != XQ_HORSE) continue; 2798378 219514 3017892
330 219514 leg = xq_horse_leg(b, from, pt);
331 219514 ▶ if (leg >= 0 && AT(b, leg) == XQ_EMPTY) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (leg >= 0 && AT(b, leg) == XQ_EMPTY) return 1; 219514 0 219514
if (leg >= 0 && AT(b, leg) == XQ_EMPTY) return 1; 141552 77962 219514
332 }
333 }
334  
335 /* elephants and advisors capture too, and a legality filter that forgets
336 * them lets a general walk onto a defended point */
337 288867345 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 231094025 57773320 288867345
338 231094025 int from = pt - 2 * DIAG[i];
339 int t;
340 231094025 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 115863838 115230187 231094025
341 115863838 t = AT(b, from);
342 115863838 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ELEPHANT
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ELEPHANT 3506183 112357655 115863838
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ELEPHANT 2410 3503773 3506183
343 2410 ▶ && AT(b, from + DIAG[i]) == XQ_EMPTY
Branch: 1 branch
BranchTrueFalseTotal
&& AT(b, from + DIAG[i]) == XQ_EMPTY 2163 247 2410
344 2163 ▶ && !(E())->crossed_river(pt, by)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& !(E())->crossed_river(pt, by)) return 1; 387 1776 2163
345 }
346 231093638 from = pt - DIAG[i];
347 231093638 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 132734471 98359167 231093638
348 132734471 t = AT(b, from);
349 132734471 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR 1932460 130802011 132734471
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR 2 1932458 1932460
350 2 ▶ && (E())->in_palace(from, by) && (E())->in_palace(pt, by)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& (E())->in_palace(from, by) && (E())->in_palace(pt, by)) return 1; 2 0 2
&& (E())->in_palace(from, by) && (E())->in_palace(pt, by)) return 1; 1 1 2
351 }
352 }
353  
354 /* soldiers: one behind, and one to each side once it is across the river */
355 {
356 57773320 ▶ int back = (by == XQ_RED) ? -XQ_STRIDE : XQ_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
int back = (by == XQ_RED) ? -XQ_STRIDE : XQ_STRIDE; 32363171 25410149 57773320
357 57773320 int from = pt + back, t;
358 57773320 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 57771908 1412 57773320
359 57771908 t = AT(b, from);
360 57771908 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER) return 1; 482770 57289138 57771908
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER) return 1; 26779 455991 482770
361 }
362 173175790 ▶ for (i = -1; i <= 1; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = -1; i <= 1; i += 2) { 115461634 57714156 173175790
363 115461634 from = pt + i;
364 115461634 ▶ if (!ONB(from)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(from)) continue; 68504 115393130 115461634
365 115393130 t = AT(b, from);
366 115393130 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER 1008757 114384373 115393130
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER 35043 973714 1008757
367 35043 ▶ && (E())->crossed_river(from, by)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& (E())->crossed_river(from, by)) return 1; 32385 2658 35043
368 }
369 }
370  
371 57714156 return 0;
372 }
373  
374 /* IN CHECK INCLUDES THE FLYING GENERAL, and `attacked` deliberately does not.
375 *
376 * `attacked` answers "could a piece of that colour capture this point", which is
377 * a question about pieces and is what the perft ladder validates. Facing
378 * generals are a question about the two generals only, so folding it into the
379 * general walk would make `attacked` answer differently for one point than for
380 * every other, which is a worse interface.
381 *
382 * It matters here and nowhere else: after a LEGAL move the generals never face,
383 * so perft never asks. A position built by hand or read from a FEN can have them
384 * facing, and without this a mated general reads as STALEMATED, which phase 04
385 * will turn into the right result by the wrong route. Found by building a mate
386 * fixture for t/04 and being told it was not check. */
387 62565938 static int xq_in_check(const struct xq_board *b, int colour)
388 {
389 62565938 int g = (E())->find(b, colour | XQ_GENERAL);
390 62565938 ▶ if (!g) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return 0; 17731 62548207 62565938
391 62548207 ▶ if (xq_generals_face(b)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_generals_face(b)) return 1; 90139 62458068 62548207
392 62458068 ▶ return xq_attacked(b, g, XQ_OTHER(colour));
Branch: 1 branch
BranchTrueFalseTotal
return xq_attacked(b, g, XQ_OTHER(colour)); 27569199 34888869 62458068
393 }
394  
395 /* A move is legal when it leaves your own general unattacked AND does not leave
396 * the two generals facing each other. The second clause is the flying general
397 * and it is why that rule costs nothing anywhere else. */
398 1089163 static int xq_gen_legal(struct xq_board *b, int *out)
399 {
400 int buf[XQ_MAX_MOVES];
401 1089163 int n = xq_gen_moves(b, buf);
402 1089163 int side = (E())->side(b);
403 1089163 int i, k = 0;
404  
405 43451171 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 42362008 1089163 43451171
406 struct xq_undo u;
407 42362008 (E())->do_move(b, buf[i], &u);
408 42362008 ▶ if (!xq_in_check(b, side) && !xq_generals_face(b)) out[k++] = buf[i];
Branch: 2 branches
BranchTrueFalseTotal
if (!xq_in_check(b, side) && !xq_generals_face(b)) out[k++] = buf[i]; 37984928 4377080 42362008
if (!xq_in_check(b, side) && !xq_generals_face(b)) out[k++] = buf[i]; 37984928 0 37984928
409 42362008 (E())->undo_move(b, &u);
410 }
411 1089163 return k;
412 }
413  
414 /* ---- perft --------------------------------------------------------------------
415 *
416 * In C because depth 4 is 3.29 million nodes and depth 5 is 133 million: a Perl
417 * loop over do_move and undo_move would take the afternoon. The oracle is
418 * external either way, which is the point: this counts, the wiki says what the
419 * count should be.
420 *
421 * NODES, CHECKS AND CAPTURES ARE ABOUT THE LEAF PLY. Of the 44 moves at depth 1,
422 * two are captures and none gives check, which is what the published table says.
423 *
424 * MATES ARE NOT, AND THAT COST AN HOUR. They are counted at the NODE whose side
425 * to move is mated, which is one ply EARLIER than the move that delivered the
426 * mate. So a mate delivered by the third move is counted by perft(4) and not by
427 * perft(3).
428 *
429 * The first version here counted a mate at the move that delivered it, which is
430 * the reading the column name invites, and it produced a table that agreed with
431 * the source on nodes, checks and captures at EVERY depth of every position and
432 * disagreed on mates at every depth by exactly one ply: 23, 1537 and 41673 all
433 * appeared one row too early. Three columns matching exactly is what said the
434 * generator was right and the accounting was ours; a single-column ladder would
435 * have read as a bug in the rules and sent somebody into the cannon code.
436 */
437 360547 static void perft_go(struct xq_board *b, int depth, struct xq_perft *out)
438 {
439 int moves[XQ_MAX_MOVES];
440 360547 int n = xq_gen_legal(b, moves);
441 360547 int side = (E())->side(b);
442 360547 ▶ int them = XQ_OTHER(side);
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER(side); 18634 341913 360547
443 int i;
444  
445 /* No legal move: the side to move is mated if it is in check, and
446 * stalemated if it is not, which phase 04 will make a loss as well. Either
447 * way this node has no descendants and contributes no nodes.
448 *
449 * THE MATE IS COUNTED ONLY AT THE LEAF PLY, which is `depth == 1`. Counting
450 * it at every depth accumulates the shallower plies' mates into the deeper
451 * rows: position 2 at depth 5 then reads 1560, which is the true 1537 plus
452 * the 23 that belong to depth 4. That version PASSED THE WHOLE t/ SUITE,
453 * because the positions run there have no mates above the leaf ply, and only
454 * xt/perft-deep.t found it. */
455 360547 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 340 360207 360547
456 340 ▶ if (depth == 1 && xq_in_check(b, side)) out->mates++;
Branch: 2 branches
BranchTrueFalseTotal
if (depth == 1 && xq_in_check(b, side)) out->mates++; 338 2 340
if (depth == 1 && xq_in_check(b, side)) out->mates++; 337 1 338
457 349959 return;
458 }
459  
460 360207 ▶ if (depth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 1) { 349619 10588 360207
461 12292124 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11942505 349619 12292124
462 struct xq_undo u;
463 11942505 int captured = (E())->do_move(b, moves[i], &u);
464 11942505 out->nodes++;
465 11942505 ▶ if (captured != XQ_EMPTY) out->captures++;
Branch: 1 branch
BranchTrueFalseTotal
if (captured != XQ_EMPTY) out->captures++; 436791 11505714 11942505
466 11942505 ▶ if (xq_in_check(b, them)) out->checks++;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_in_check(b, them)) out->checks++; 192334 11750171 11942505
467 11942505 (E())->undo_move(b, &u);
468 }
469 349619 return;
470 }
471 371081 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 360493 10588 371081
472 struct xq_undo u;
473 360493 (E())->do_move(b, moves[i], &u);
474 360493 perft_go(b, depth - 1, out);
475 360493 (E())->undo_move(b, &u);
476 }
477 }
478  
479 54 static void xq_perft(struct xq_board *b, int depth, struct xq_perft *out)
480 {
481 54 out->nodes = out->checks = out->captures = out->mates = 0;
482 54 ▶ if (depth < 1) { out->nodes = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (depth < 1) { out->nodes = 1; return; } 0 54 54
483 54 perft_go(b, depth, out);
484 }
485  
486 /* ---- installed into the table by xq_engine.c --------------------------------- */
487  
488 void xq_moves_install(struct xq_abi *t);
489  
490 53 void xq_moves_install(struct xq_abi *t)
491 {
492 53 t->horse_leg = xq_horse_leg;
493 53 t->elephant_eye = xq_elephant_eye;
494 53 t->cannon_screens = xq_cannon_screens;
495 53 t->soldier_may = xq_soldier_may;
496 53 t->generals_face = xq_generals_face;
497 53 t->gen_moves = xq_gen_moves;
498 53 t->gen_legal = xq_gen_legal;
499 53 t->attacked = xq_attacked;
500 53 t->in_check = xq_in_check;
501 53 t->perft = xq_perft;
502 53 }
« xq_judge.c ↑ Summary xq_search.c »