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 9098167752 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 74704185 static int xq_horse_leg(const struct xq_board *b, int from, int to)
50 {
51 74704185 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 311559968 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 311559967 1 311559968
63 311559967 ▶ if (d == MOVE[i]) return from + LEG[i];
Branch: 1 branch
BranchTrueFalseTotal
if (d == MOVE[i]) return from + LEG[i]; 74704184 236855783 311559967
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 99994016 static int xq_generals_face(const struct xq_board *b)
127 {
128 99994016 int rg = (E())->find(b, XQ_RED | XQ_GENERAL);
129 99994016 int bg = (E())->find(b, XQ_BLACK | XQ_GENERAL);
130 int pt;
131  
132 99994016 ▶ if (!rg || !bg) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!rg || !bg) return 0; 99978195 15821 99994016
if (!rg || !bg) return 0; 3177 99975018 99978195
133 99975018 ▶ if (FILE_OF(rg) != FILE_OF(bg)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (FILE_OF(rg) != FILE_OF(bg)) return 0; 12604683 87370335 99975018
134 231044216 ▶ for (pt = rg + XQ_STRIDE; pt < bg; pt += XQ_STRIDE)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = rg + XQ_STRIDE; pt < bg; pt += XQ_STRIDE) 230954133 90083 231044216
135 230954133 ▶ if (AT(b, pt) != XQ_EMPTY) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (AT(b, pt) != XQ_EMPTY) return 0; 87280252 143673881 230954133
136 90083 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 514894160 static int add(int *out, int n, int from, int to)
146 {
147 514894160 ▶ if (n >= XQ_MAX_MOVES) return n;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= XQ_MAX_MOVES) return n; 0 514894160 514894160
148 514894160 out[n] = (E())->move_make(from, to);
149 514894160 return n + 1;
150 }
151  
152 13964967 static int xq_gen_moves(const struct xq_board *b, int *out)
153 {
154 13964967 int side = (E())->side(b);
155 13964967 int n = 0, f, r, i;
156  
157 153614637 ▶ for (r = 0; r < XQ_RANKS; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < XQ_RANKS; r++) { 139649670 13964967 153614637
158 1396496700 ▶ for (f = 0; f < XQ_FILES; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) { 1256847030 139649670 1396496700
159 1256847030 int from = PT(f, r);
160 1256847030 int piece = AT(b, from);
161 int kind;
162 1256847030 ▶ if (XQ_COLOUR(piece) != side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(piece) != side) continue; 1075020861 181826169 1256847030
163 181826169 kind = XQ_KIND(piece);
164  
165 181826169 switch (kind) {
166 13964418 case XQ_GENERAL:
167 69822090 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 55857672 13964418 69822090
168 55857672 int to = from + ORTHO[i];
169 55857672 ▶ if (!(E())->in_palace(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->in_palace(to, side)) continue; 13711479 42146193 55857672
170 42146193 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 25287674 16858519 42146193
171 16858519 n = add(out, n, from, to);
172 }
173 13964418 break;
174  
175 26961133 case XQ_ADVISOR:
176 134805665 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 107844532 26961133 134805665
177 107844532 int to = from + DIAG[i];
178 107844532 ▶ if (!(E())->in_palace(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->in_palace(to, side)) continue; 74729766 33114766 107844532
179 33114766 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 5513970 27600796 33114766
180 27600796 n = add(out, n, from, to);
181 }
182 26961133 break;
183  
184 26308145 case XQ_ELEPHANT:
185 131540725 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 105232580 26308145 131540725
186 105232580 int to = from + 2 * DIAG[i];
187 105232580 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 48160614 57071966 105232580
188 /* never crosses the river: the elephant stays on its own half */
189 57071966 ▶ if ((E())->crossed_river(to, side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->crossed_river(to, side)) continue; 263428 56808538 57071966
190 56808538 ▶ 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 */ 1539239 55269299 56808538
191 55269299 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 7926725 47342574 55269299
192 47342574 n = add(out, n, from, to);
193 }
194 26308145 break;
195  
196 20297754 case XQ_HORSE:
197 182679786 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 162382032 20297754 182679786
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 162382032 int to = from + MOVE[i];
205 int leg;
206 162382032 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 87897366 74484666 162382032
207 74484666 leg = xq_horse_leg(b, from, to);
208 74484666 ▶ if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue; 74484666 0 74484666
if (leg < 0 || AT(b, leg) != XQ_EMPTY) continue; 18133142 56351524 74484666
209 56351524 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 7901136 48450388 56351524
210 48450388 n = add(out, n, from, to);
211 }
212 20297754 break;
213  
214 24777367 case XQ_CHARIOT:
215 123886835 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 99109468 24777367 123886835
216 int to;
217 198310241 ▶ for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) { 148874939 49435302 198310241
218 148874939 int t = AT(b, to);
219 148874939 ▶ 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; } 99200773 49674166 148874939
220 49674166 ▶ 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); 8803787 40870379 49674166
221 49674166 break;
222 }
223 }
224 24777367 break;
225  
226 20842653 case XQ_CANNON:
227 104213265 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 83370612 20842653 104213265
228 83370612 int to, seen = 0;
229 395378037 ▶ for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (to = from + ORTHO[i]; ONB(to); to += ORTHO[i]) { 332703138 62674899 395378037
230 332703138 int t = AT(b, to);
231 332703138 ▶ if (!seen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) { 258299889 74403249 332703138
232 258299889 ▶ 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; } 200657466 57642423 258299889
233 57642423 seen = 1; /* the screen; not a capture */
234 57642423 continue;
235 }
236 74403249 ▶ if (t == XQ_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) continue; 53707536 20695713 74403249
237 20695713 ▶ 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); 12908893 7786820 20695713
238 20695713 break; /* one screen only, ever */
239 }
240 }
241 20842653 break;
242  
243 48674699 case XQ_SOLDIER:
244 {
245 48674699 ▶ int fwd = (side == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
int fwd = (side == XQ_RED) ? XQ_STRIDE : -XQ_STRIDE; 22371917 26302782 48674699
246 int cand[3];
247 48674699 int c = 0, k;
248 48674699 cand[c++] = from + fwd;
249 48674699 ▶ if ((E())->crossed_river(from, side)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->crossed_river(from, side)) { 2726897 45947802 48674699
250 2726897 cand[c++] = from - 1;
251 2726897 cand[c++] = from + 1;
252 }
253 102803192 ▶ for (k = 0; k < c; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < c; k++) { 54128493 48674699 102803192
254 54128493 int to = cand[k];
255 54128493 ▶ if (!ONB(to)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(to)) continue; 100955 54027538 54128493
256 54027538 ▶ if (XQ_COLOUR(AT(b, to)) == side) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(AT(b, to)) == side) continue; 956574 53070964 54027538
257 53070964 n = add(out, n, from, to);
258 }
259 }
260 48674699 break;
261  
262 0 default:
263 0 break;
264 }
265 }
266 }
267 13964967 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 62444520 static int xq_attacked(const struct xq_board *b, int pt, int by)
281 {
282 int i;
283  
284 62444520 ▶ if (!ONB(pt)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(pt)) return 0; 0 62444520 62444520
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 299957370 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 242316465 57640905 299957370
289 242316465 int s, first = 0, t;
290 742855174 ▶ for (s = pt + ORTHO[i]; ONB(s); s += ORTHO[i]) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = pt + ORTHO[i]; ONB(s); s += ORTHO[i]) { 647292037 95563137 742855174
291 647292037 t = AT(b, s);
292 647292037 ▶ if (t == XQ_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (t == XQ_EMPTY) continue; 337245855 310046182 647292037
293 310046182 ▶ if (!first) {
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { 164454849 145591333 310046182
294 164454849 ▶ 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; 12457118 151997731 164454849
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CHARIOT) return 1; 1161797 11295321 12457118
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 163293052 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL 11295321 151997731 163293052
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_GENERAL 2158 11293163 11295321
307 2158 ▶ && 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; 506 1652 2158
&& s == pt + ORTHO[i] && (E())->in_palace(pt, by)) return 1; 198 308 506
308 163292854 first = 1;
309 163292854 continue;
310 }
311 145591333 ▶ 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; 40843061 104748272 145591333
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_CANNON) return 1; 3641620 37201441 40843061
312 141949713 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 518131149 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 460631796 57499353 518131149
325 460631796 int from = pt - MOVE[i];
326 int t, leg;
327 460631796 ▶ if (!ONB(from)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(from)) continue; 212809599 247822197 460631796
328 247822197 t = AT(b, from);
329 247822197 ▶ 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; 3017794 244804403 247822197
if (XQ_COLOUR(t) != by || XQ_KIND(t) != XQ_HORSE) continue; 2798280 219514 3017794
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 287495570 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 229996605 57498965 287495570
338 229996605 int from = pt - 2 * DIAG[i];
339 int t;
340 229996605 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 115314210 114682395 229996605
341 115314210 t = AT(b, from);
342 115314210 ▶ 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 111808027 115314210
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 229996218 from = pt - DIAG[i];
347 229996218 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 132169939 97826279 229996218
348 132169939 t = AT(b, from);
349 132169939 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR 1932391 130237548 132169939
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_ADVISOR 2 1932389 1932391
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 57498965 ▶ int back = (by == XQ_RED) ? -XQ_STRIDE : XQ_STRIDE;
Branch: 1 branch
BranchTrueFalseTotal
int back = (by == XQ_RED) ? -XQ_STRIDE : XQ_STRIDE; 32201620 25297345 57498965
357 57498965 int from = pt + back, t;
358 57498965 ▶ if (ONB(from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ONB(from)) { 57497579 1386 57498965
359 57497579 t = AT(b, from);
360 57497579 ▶ 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; 482518 57015061 57497579
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER) return 1; 26779 455739 482518
361 }
362 172352725 ▶ for (i = -1; i <= 1; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = -1; i <= 1; i += 2) { 114912924 57439801 172352725
363 114912924 from = pt + i;
364 114912924 ▶ if (!ONB(from)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!ONB(from)) continue; 67476 114845448 114912924
365 114845448 t = AT(b, from);
366 114845448 ▶ if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER 1006878 113838570 114845448
if (XQ_COLOUR(t) == by && XQ_KIND(t) == XQ_SOLDIER 34873 972005 1006878
367 34873 ▶ && (E())->crossed_river(from, by)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
&& (E())->crossed_river(from, by)) return 1; 32385 2488 34873
368 }
369 }
370  
371 57439801 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 62287328 static int xq_in_check(const struct xq_board *b, int colour)
388 {
389 62287328 int g = (E())->find(b, colour | XQ_GENERAL);
390 62287328 ▶ if (!g) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return 0; 17731 62269597 62287328
391 62269597 ▶ if (xq_generals_face(b)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_generals_face(b)) return 1; 90079 62179518 62269597
392 62179518 ▶ return xq_attacked(b, g, XQ_OTHER(colour));
Branch: 1 branch
BranchTrueFalseTotal
return xq_attacked(b, g, XQ_OTHER(colour)); 27453151 34726367 62179518
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 1082357 static int xq_gen_legal(struct xq_board *b, int *out)
399 {
400 int buf[XQ_MAX_MOVES];
401 1082357 int n = xq_gen_moves(b, buf);
402 1082357 int side = (E())->side(b);
403 1082357 int i, k = 0;
404  
405 43176424 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 42094067 1082357 43176424
406 struct xq_undo u;
407 42094067 (E())->do_move(b, buf[i], &u);
408 42094067 ▶ 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]; 37724409 4369658 42094067
if (!xq_in_check(b, side) && !xq_generals_face(b)) out[k++] = buf[i]; 37724409 0 37724409
409 42094067 (E())->undo_move(b, &u);
410 }
411 1082357 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 28 void xq_moves_install(struct xq_abi *t)
491 {
492 28 t->horse_leg = xq_horse_leg;
493 28 t->elephant_eye = xq_elephant_eye;
494 28 t->cannon_screens = xq_cannon_screens;
495 28 t->soldier_may = xq_soldier_may;
496 28 t->generals_face = xq_generals_face;
497 28 t->gen_moves = xq_gen_moves;
498 28 t->gen_legal = xq_gen_legal;
499 28 t->attacked = xq_attacked;
500 28 t->in_check = xq_in_check;
501 28 t->perft = xq_perft;
502 28 }
« xq_judge.c ↑ Summary xq_search.c »