xq_judge.c

filter: Statementstmt 94.9% 298 / 314 Branchbran 83.6% 271 / 324 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.1% 569 / 638 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Xiangqi.xs ↑ Summary xq_moves.c »
1 /* xq_judge.c - the vocabulary of the Asian Rules. Phase 05 of plan_xiangqi.
2 *
3 * PERL-FREE, as the rest of the engine is.
4 *
5 * THIS FILE RULES ON NOTHING. Not one function here returns "loses" or "draw".
6 * It writes the nine words that Chapter 4 Section 3's FORTY rules are written
7 * in, taken from the Asian Rules' own Section 1, "Terms Used in Defining Asian
8 * Rules" (clubxiangqi.com/rules/asiarule.htm, curl'd 25 Sep 2026), so that phase
9 * 06 can be a table of sentences over predicates rather than forty hand-rolled
10 * position tests.
11 *
12 * Seven of the nine are one-line compositions of phases 03 and 04. The value of
13 * writing them separately is that phase 06's forty rules then READ AS THE SOURCE
14 * READS, which is the only way forty rules get reviewed by a person.
15 *
16 * Every predicate takes the position BEFORE the move and the move, so the judge
17 * can walk a stored log forwards and ask about either side's moves without
18 * building a second code path.
19 */
20  
21 #include <string.h>
22 #include "xq_abi.h"
23  
24 3226025 static const struct xq_abi *E(void) { return xq_abi_table(); }
25  
26 #define AT(b, pt) ((E())->at((b), (pt)))
27  
28 /* ---- what a piece is worth ----------------------------------------------------
29 *
30 * In centi-soldiers. Here and not in the search because `is_exchange` needs it
31 * first, and two tables would drift. Phase 09's evaluation uses this one.
32 *
33 * The general is 0 rather than huge: it is never exchanged, it cannot be
34 * captured in a legal game, and a huge number here would make every comparison
35 * involving it meaningless rather than absurd.
36 */
37 137758645 static int xq_value_of(int piece)
38 {
39 137758645 switch (XQ_KIND(piece)) {
40 19938278 case XQ_CHARIOT: return 900;
41 18049034 case XQ_CANNON: return 450;
42 15364454 case XQ_HORSE: return 400;
43 19404383 case XQ_ADVISOR: return 200;
44 19288848 case XQ_ELEPHANT: return 200;
45 35722517 case XQ_SOLDIER: return 100;
46 9991131 default: return 0;
47 }
48 }
49  
50 /* ---- the three that are phases 03 and 04 wearing the source's words ----------- */
51  
52 /* "A move of any piece that causes the opponent's King to be threatened with
53 * capture in the next move." */
54 5523 static int xq_is_check(struct xq_board *b, int mv)
55 {
56 struct xq_undo u;
57 5523 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 2985 2538 5523
58 int r;
59 5523 (E())->do_move(b, mv, &u);
60 5523 r = (E())->in_check(b, them);
61 5523 (E())->undo_move(b, &u);
62 5523 return r;
63 }
64  
65 /* "Check in such a way that the opponent's King cannot resolve the check." */
66 57 static int xq_is_mate(struct xq_board *b, int mv)
67 {
68 struct xq_undo u;
69 57 int reason = XQ_ONGOING;
70 57 (E())->do_move(b, mv, &u);
71 57 (E())->outcome(b, &reason);
72 57 (E())->undo_move(b, &u);
73 57 return reason == XQ_BY_CHECKMATE;
74 }
75  
76 /* "Threatening to Checkmate (TTC) - A piece moves into a position where it can
77 * launch a sequence of attack that leads to checkmate."
78 *
79 * THE INTERPRETATION, AND IT IS OURS. The source bounds nothing: "a sequence"
80 * read literally makes almost every move a TTC and the forty rules unusable.
81 * What a referee means at a board is that the opponent must answer NOW or be
82 * mated, so this is implemented as
83 *
84 * play the move, hand the turn straight back, and ask for a mate in one
85 *
86 * which is "after this move I threaten mate next move". The null move is what
87 * makes it a THREAT rather than a forced mate: a forced mate is what `mate_in`
88 * answers and it is a different question.
89 *
90 * THE PLAN SAID 3 PLIES AND THAT WAS WRONG, not by a little. After the move it
91 * is the OPPONENT's turn, so any depth has to be measured after a null move,
92 * and `mate_in(3)` after a null move means "I could mate in two if you never
93 * moved again", which is true of a large fraction of middlegame positions. The
94 * deviation is recorded in the phase file. If the judge later disagrees with a
95 * cited diagram because of this, the number moves and the file records it.
96 */
97 5378 static int xq_is_ttc(struct xq_board *b, int mv)
98 {
99 struct xq_undo u;
100 5378 int me = (E())->side(b);
101 int r;
102  
103 5378 (E())->do_move(b, mv, &u);
104 5378 (E())->set_side(b, me); /* the null move */
105 5378 r = (E())->mate_in(b, 1);
106 5378 (E())->undo_move(b, &u); /* restores the side and the key exactly */
107 5378 return r;
108 }
109  
110 /* ---- chase ---------------------------------------------------------------------
111 *
112 * "Chase - A piece moves to a position where it can capture an opponent's piece,
113 * which is not the King, in the next move."
114 *
115 * IMPLEMENTED AS A NEW ATTACK BY THE SIDE, not by the moved piece alone, and
116 * that is deliberate. The source's sentence describes the ordinary case, and
117 * Section 3 rules 25 and 26 are explicit that a chase happens when only the
118 * cannon's SCREEN moves and neither the cannon nor its target does. A
119 * moved-piece reading gets both of those rules wrong, and they are two of the
120 * forty.
121 *
122 * So: an enemy piece that is not a general and that the mover's side did NOT
123 * attack before and DOES attack after.
124 */
125 5313 static int xq_is_chase(struct xq_board *b, int mv, int *victim)
126 {
127 struct xq_undo u;
128 5313 int me = (E())->side(b);
129 5313 ▶ int them = XQ_OTHER(me);
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER(me); 2807 2506 5313
130 unsigned char before[XQ_CELLS];
131 5313 int pt, found = 0;
132  
133 5313 ▶ if (victim) *victim = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (victim) *victim = 0; 2733 2580 5313
134  
135 706629 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 701316 5313 706629
136 701316 int p = AT(b, pt);
137 701316 before[pt] = (unsigned char)
138 1179486 ▶ ((E())->on_board(pt) && XQ_COLOUR(p) == them && XQ_KIND(p) != XQ_GENERAL
Branch: 2 branches
BranchTrueFalseTotal
((E())->on_board(pt) && XQ_COLOUR(p) == them && XQ_KIND(p) != XQ_GENERAL 70429 407741 478170
((E())->on_board(pt) && XQ_COLOUR(p) == them && XQ_KIND(p) != XQ_GENERAL 65116 5313 70429
139 1179486 ▶ && (E())->attacked(b, pt, me));
Branch: 2 branches
BranchTrueFalseTotal
&& (E())->attacked(b, pt, me)); 478170 223146 701316
&& (E())->attacked(b, pt, me)); 7739 57377 65116
140 }
141  
142 5313 (E())->do_move(b, mv, &u);
143 584229 ▶ for (pt = 0; pt < XQ_CELLS && !found; pt++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS && !found; pt++) { 581009 3220 584229
for (pt = 0; pt < XQ_CELLS && !found; pt++) { 578916 2093 581009
144 578916 int p = AT(b, pt);
145 578916 ▶ if (!(E())->on_board(pt)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->on_board(pt)) continue; 181768 397148 578916
146 397148 ▶ if (XQ_COLOUR(p) != them || XQ_KIND(p) == XQ_GENERAL) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (XQ_COLOUR(p) != them || XQ_KIND(p) == XQ_GENERAL) continue; 54582 342566 397148
if (XQ_COLOUR(p) != them || XQ_KIND(p) == XQ_GENERAL) continue; 4127 50455 54582
147 50455 ▶ if (before[pt]) continue; /* already attacked */
Branch: 1 branch
BranchTrueFalseTotal
if (before[pt]) continue; /* already attacked */ 5703 44752 50455
148 44752 ▶ if (!(E())->attacked(b, pt, me)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->attacked(b, pt, me)) continue; 42659 2093 44752
149 2093 found = 1;
150 2093 ▶ if (victim) *victim = pt;
Branch: 1 branch
BranchTrueFalseTotal
if (victim) *victim = pt; 1046 1047 2093
151 }
152 5313 (E())->undo_move(b, &u);
153 5313 return found;
154 }
155  
156 /* ---- exchange -------------------------------------------------------------------
157 *
158 * "Exchange - Using piece A to capture the opponent's piece B and let the
159 * opponent take piece A. ... Usually it is an exchange only when the value of A
160 * and B are similar."
161 *
162 * THAT "USUALLY" IS THE ONE SOFT EDGE IN THE WHOLE VOCABULARY and this pins it:
163 * SIMILAR MEANS WITHIN ONE SOLDIER on `value_of`'s table, which is 100
164 * centi-soldiers. That number is ours, not the source's, and it is written here
165 * rather than in a comment somewhere else so the next reader finds it where the
166 * decision is made.
167 */
168 4308 static int xq_is_exchange(struct xq_board *b, int mv)
169 {
170 struct xq_undo u;
171 4308 int to = (E())->move_to(mv);
172 4308 int from = (E())->move_from(mv);
173 4308 int mover = AT(b, from);
174 4308 int taken = AT(b, to);
175 4308 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 2123 2185 4308
176 int recapturable;
177  
178 4308 ▶ if (taken == XQ_EMPTY) return 0; /* not a capture at all */
Branch: 1 branch
BranchTrueFalseTotal
if (taken == XQ_EMPTY) return 0; /* not a capture at all */ 3872 436 4308
179 436 ▶ if (xq_value_of(mover) - xq_value_of(taken) > 100) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_value_of(mover) - xq_value_of(taken) > 100) return 0; 244 192 436
180 192 ▶ if (xq_value_of(taken) - xq_value_of(mover) > 100) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_value_of(taken) - xq_value_of(mover) > 100) return 0; 110 82 192
181  
182 82 (E())->do_move(b, mv, &u);
183 82 recapturable = (E())->attacked(b, to, them);
184 82 (E())->undo_move(b, &u);
185 82 return recapturable;
186 }
187  
188 /* ---- block ----------------------------------------------------------------------
189 *
190 * "Block - A piece moves to a position where it prevents the opponent from
191 * moving one of its pieces in certain direction."
192 *
193 * Cheap here and nowhere else, because phase 03 already named the blocking
194 * points: the horse's leg, the elephant's eye, and a ray for the chariot and the
195 * cannon. This counts the opponent's PSEUDO-LEGAL destinations before and after
196 * and asks whether the square the mover landed on is what removed one.
197 *
198 * Pseudo-legal and not legal, deliberately: a move that puts the opponent in
199 * check removes most of their legal moves without blocking anything, and that is
200 * a check, which is a different word in this vocabulary.
201 */
202 /* Counted PER ORIGIN and not per destination, which the first version got
203 * wrong. A capture removes the taken piece, so every destination that piece had
204 * disappears, and a destination-total comparison reports a plain capture as a
205 * block: the cannon taking a horse in the opening read as one. Per origin, and
206 * skipping the square the capture emptied, a block is what it says it is. */
207 8510 static void dests_by_origin(struct xq_board *b, int side, int *out)
208 {
209 int buf[XQ_MAX_MOVES];
210 8510 int keep = (E())->side(b);
211 int n, i;
212 8510 (E())->set_side(b, side);
213 8510 n = (E())->gen_moves(b, buf);
214 8510 (E())->set_side(b, keep);
215 8510 memset(out, 0, sizeof(int) * XQ_CELLS);
216 323388 ▶ for (i = 0; i < n; i++) out[(E())->move_from(buf[i])]++;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) out[(E())->move_from(buf[i])]++; 314878 8510 323388
217 8510 }
218  
219 /* A BLOCK IS CAUSED BY OCCUPYING THE DESTINATION, and that has to be isolated
220 * from everything else the move does, which took two goes to get right.
221 *
222 * Comparing before against after catches two things that are not blocks:
223 * - a CAPTURE removes the taken piece, so all of its moves vanish
224 * - VACATING the origin removes a capture target, or removes a cannon's
225 * screen, so the enemy loses destinations because the mover RAN AWAY
226 * The second is the subtle one: a red horse stepping off b0 was reported as
227 * blocking, because the black cannon on b7 could no longer jump the cannon on
228 * b2 to take it.
229 *
230 * So the baseline is the position with the origin ALREADY EMPTY and the
231 * destination untouched, and the comparison is against the same position with
232 * the mover placed. The only difference between those two boards is the
233 * occupancy of one square, which is what the word means.
234 */
235 4255 static int xq_is_block(struct xq_board *b, int mv)
236 {
237 4255 int from = (E())->move_from(mv);
238 4255 int to = (E())->move_to(mv);
239 4255 int mover = AT(b, from);
240 4255 int taken = AT(b, to);
241 4255 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 2070 2185 4255
242 int was[XQ_CELLS], now[XQ_CELLS];
243 4255 int pt, lost = 0;
244  
245 4255 (E())->lift(b, from);
246 4255 dests_by_origin(b, them, was);
247 4255 (E())->put(b, to, mover);
248 4255 dests_by_origin(b, them, now);
249 4255 (E())->put(b, to, taken); /* XQ_EMPTY when it was not a capture */
250 4255 (E())->put(b, from, mover); /* and the key comes back with it */
251  
252 479478 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 476645 2833 479478
253 476645 ▶ if (pt == to) continue; /* the piece that stood here was taken */
Branch: 1 branch
BranchTrueFalseTotal
if (pt == to) continue; /* the piece that stood here was taken */ 3806 472839 476645
254 472839 ▶ if (now[pt] < was[pt]) { lost = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (now[pt] < was[pt]) { lost = 1; break; } 1422 471417 472839
255 }
256 4255 return lost;
257 }
258  
259 /* "Sacrifice - A piece moves to a position where it can be taken by the
260 * opponent." Literally that, and the overlap with `is_exchange` is the source's
261 * and not ours: the two are separate words and a move can be both. */
262 3719 static int xq_is_sacrifice(struct xq_board *b, int mv)
263 {
264 struct xq_undo u;
265 3719 int to = (E())->move_to(mv);
266 3719 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 1809 1910 3719
267 int r;
268 3719 (E())->do_move(b, mv, &u);
269 3719 r = (E())->attacked(b, to, them);
270 3719 (E())->undo_move(b, &u);
271 3719 return r;
272 }
273  
274 /* "Idle - A move that does not Check, TTC, Chase, Exchange, Block, or
275 * Sacrifice."
276 *
277 * WRITTEN AS EXACTLY THAT NEGATION and never as a list of its own. Two
278 * definitions of the same word drift, and the drift is invisible: a seventh
279 * predicate added to the vocabulary is automatically excluded from idle here,
280 * and would have to be remembered in a hand-rolled version.
281 */
282 2780 static int xq_is_idle(struct xq_board *b, int mv)
283 {
284 2780 ▶ if (xq_is_check(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_check(b, mv)) return 0; 144 2636 2780
285 2636 ▶ if (xq_is_ttc(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_ttc(b, mv)) return 0; 56 2580 2636
286 2580 ▶ if (xq_is_chase(b, mv, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_chase(b, mv, 0)) return 0; 1047 1533 2580
287 1533 ▶ if (xq_is_exchange(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_exchange(b, mv)) return 0; 0 1533 1533
288 1533 ▶ if (xq_is_block(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_block(b, mv)) return 0; 540 993 1533
289 993 ▶ if (xq_is_sacrifice(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_sacrifice(b, mv)) return 0; 99 894 993
290 894 return 1;
291 }
292  
293 /* ---- protected, and the real protector -------------------------------------------
294 *
295 * "A piece is protected if there is a piece that can capture any piece that
296 * takes the protected piece. When A takes B, if C can take A, B is protected."
297 *
298 * "Real protector - When a protected piece is taken, the protector can
299 * actually remove the taker.
300 * False protector - When a protected piece is taken, the protector cannot
301 * actually remove the taker."
302 *
303 * `real` IS COMPUTED BY PLAYING THE CAPTURE AND THE RECAPTURE, never from a
304 * static attack map, because in this game a protector is false for reasons that
305 * do not exist in chess:
306 *
307 * the recapture would leave its own general facing the enemy general
308 * the protector is an advisor or an elephant that cannot legally reach
309 * the protector is a horse whose leg the TAKER has just occupied
310 *
311 * Rule 34 of Section 3 ("a protected piece cannot be perpetually chased if its
312 * protector has lost its effectness") is unimplementable without this, and it is
313 * one of the forty.
314 */
315 1117 static int xq_protected_at(struct xq_board *b, int pt, int *real)
316 {
317 1117 int piece = AT(b, pt);
318 int mine, theirs;
319 int buf[XQ_MAX_MOVES];
320 1117 int keep = (E())->side(b);
321 1117 int n, i, defended, all_recaptured = 1, tested = 0;
322  
323 1117 ▶ if (real) *real = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (real) *real = 0; 1117 0 1117
324 1117 ▶ if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0; 1117 0 1117
if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0; 1059 58 1117
if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0; 0 1059 1059
325 1059 mine = XQ_COLOUR(piece);
326 1059 ▶ theirs = XQ_OTHER(mine);
Branch: 1 branch
BranchTrueFalseTotal
theirs = XQ_OTHER(mine); 337 722 1059
327  
328 1059 defended = (E())->attacked(b, pt, mine);
329 1059 ▶ if (!defended) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!defended) return 0; 739 320 1059
330  
331 /* every legal capture of this piece, answered */
332 320 (E())->set_side(b, theirs);
333 320 n = (E())->gen_legal(b, buf);
334 12732 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12423 309 12732
335 struct xq_undo u;
336 int back[XQ_MAX_MOVES];
337 12423 int m, j, answered = 0;
338 12423 ▶ if ((E())->move_to(buf[i]) != pt) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->move_to(buf[i]) != pt) continue; 12113 310 12423
339 310 tested++;
340 310 (E())->do_move(b, buf[i], &u);
341 310 m = (E())->gen_legal(b, back);
342 4543 ▶ for (j = 0; j < m; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) 4532 11 4543
343 4532 ▶ if ((E())->move_to(back[j]) == pt) { answered = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->move_to(back[j]) == pt) { answered = 1; break; } 299 4233 4532
344 310 (E())->undo_move(b, &u);
345 310 ▶ if (!answered) { all_recaptured = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!answered) { all_recaptured = 0; break; } 11 299 310
346 }
347 320 (E())->set_side(b, keep);
348  
349 /* Nothing can take it: the protection is untested, not false. A piece
350 * nobody attacks is not one whose protector has failed. */
351 320 ▶ if (real) *real = (tested == 0) ? 1 : all_recaptured;
Branch: 2 branches
BranchTrueFalseTotal
if (real) *real = (tested == 0) ? 1 : all_recaptured; 320 0 320
if (real) *real = (tested == 0) ? 1 : all_recaptured; 310 10 320
352 320 return 1;
353 }
354  
355 /* ---- installed into the table by xq_engine.c ------------------------------------- */
356  
357 void xq_judge_install(struct xq_abi *t);
358  
359 24 void xq_judge_install(struct xq_abi *t)
360 {
361 24 t->is_check = xq_is_check;
362 24 t->is_mate = xq_is_mate;
363 24 t->is_ttc = xq_is_ttc;
364 24 t->is_chase = xq_is_chase;
365 24 t->is_exchange = xq_is_exchange;
366 24 t->is_block = xq_is_block;
367 24 t->is_sacrifice = xq_is_sacrifice;
368 24 t->is_idle = xq_is_idle;
369 24 t->protected_at = xq_protected_at;
370 24 t->value_of = xq_value_of;
371 24 }
372  
373 /* ================================================================================
374 * THE JUDGE. Phase 06 of plan_xiangqi, and the largest phase in it.
375 *
376 * A POSITION CAN NEVER BE A DRAW IN THIS GAME. `outcome` says so by having no
377 * draw value at all. Every draw xiangqi has is a property of a SEQUENCE, and
378 * this is where they live.
379 *
380 * FOUR STEPS, each testable on its own:
381 *
382 * 1. find the loop walk the keys back for a repeat, same side to move
383 * 2. describe every move the nine predicates of phase 05, both sides
384 * 3. classify each side perpetual check, chase, TTC, block, exchange, none
385 * 4. rule Section 2's four principles over Section 3's forty
386 *
387 * AND THE PART THAT IS NOT THE ASIAN RULES AT ALL. Rule 3 of Section 3 reads
388 * "The side who violates a rule, asked by the referee to alter, and repeats the
389 * violation for three times will be ruled to lose", and principle 4 reads "the
390 * side violating the rules has to change. Otherwise, it loses". Both presuppose
391 * somebody at the table saying so. A bot cannot be asked to alter and a player
392 * at a one-minute limit cannot be warned. CXQ is a website that had the same
393 * problem and published its answer, so its thresholds supply the WHEN that the
394 * Asian Rules leave to a person, and the rules page says they are ours.
395 * ============================================================================= */
396  
397 /* ---- the chase table, rules 15 to 40 --------------------------------------------
398 *
399 * ROWS CARRY THEIR RULE NUMBER so a ruling can cite it: a player who loses a won
400 * position to rule 19 is owed the number, and the site's log prints it.
401 *
402 * FIRST MATCH WINS, so the specific rows come before the general ones.
403 *
404 * SIX RULES ARE NOT HERE AND ARE NOT SILENTLY DROPPED: 19, 20, 22, 23, 27 and 37
405 * turn on positional facts this table cannot express (a chariot immobilised by a
406 * horse, a chariot confined to a line by a cannon, two pieces taking turns under
407 * a named control pattern). `rule_count` and `rule_at` enumerate what IS here so
408 * a test computes the gap instead of a comment claiming it.
409 */
410 static const struct xq_chase_rule CHASE[] = {
411 /* num chaser victim prot chasers victims verdict text */
412 /* RULE 34 IS DEFENDED-BUT-FALSELY, NOT UNDEFENDED, and the two have to be
413 * told apart or this row shadows every specific rule below it. It matched the
414 * first live chase fixture and cited 34 where 18 was the right number, which
415 * matters because the site's log prints the number to the player. */
416 { 34, 0, 0, 2, 0, 1, XQ_CHASE_FORBIDDEN,
417 "a protected piece cannot be perpetually chased if its protector has lost its effectness" },
418 { 32, 0, 0, -1, 1, 2, XQ_CHASE_DRAW,
419 "one piece perpetually chases two pieces or more should be ruled as a draw" },
420 { 35, 0, 0, 1, 2, 1, XQ_CHASE_DRAW,
421 "two or more pieces trying to capture a protected piece does not constitute a perpetual chase" },
422 { 33, 0, 0, 0, 2, 1, XQ_CHASE_FORBIDDEN,
423 "two or more pieces cannot take turns to perpetually chase a piece" },
424 { 30, XQ_GENERAL, 0, -1, 2, 0, XQ_CHASE_FORBIDDEN,
425 "a King or a Pawn cannot work together with another piece to perpetually chase" },
426 { 30, XQ_SOLDIER, 0, -1, 2, 0, XQ_CHASE_FORBIDDEN,
427 "a King or a Pawn cannot work together with another piece to perpetually chase" },
428 { 28, XQ_GENERAL, 0, -1, 1, 0, XQ_CHASE_ALLOWED,
429 "the King can perpetually chase an enemy piece" },
430 { 29, XQ_SOLDIER, 0, -1, 0, 0, XQ_CHASE_ALLOWED,
431 "a Pawn can perpetually chase; two or more Pawns can work together" },
432 { 16, XQ_CANNON, XQ_CHARIOT, -1, 0, 2, XQ_CHASE_DRAW,
433 "one Cannon perpetually chasing two Rooks can be ruled a draw; two Cannons likewise" },
434 { 15, XQ_CANNON, XQ_CHARIOT, -1, 0, 0, XQ_CHASE_FORBIDDEN,
435 "one or two Cannons cannot perpetually chase a Rook even if the Rook is protected" },
436 { 21, XQ_HORSE, XQ_CHARIOT, -1, 0, 0, XQ_CHASE_FORBIDDEN,
437 "a Knight cannot perpetually chase a Rook, regardless of whether the Rook is protected" },
438 { 31, XQ_CHARIOT, XQ_SOLDIER, -1, 0, 0, XQ_CHASE_FORBIDDEN,
439 "a Rook cannot perpetually chase a Pawn that has crossed the river" },
440 { 17, XQ_CHARIOT, XQ_CANNON, 1, 0, 0, XQ_CHASE_ALLOWED,
441 "Rook can perpetually chase a protected Cannon" },
442 { 18, XQ_CHARIOT, XQ_CANNON, 0, 0, 0, XQ_CHASE_FORBIDDEN,
443 "a Rook cannot perpetually chase an unprotected Cannon" },
444 { 25, XQ_CANNON, XQ_HORSE, 0, 0, 0, XQ_CHASE_FORBIDDEN,
445 "a Cannon attacking an unprotected Knight, Guard, Minister or crossed Pawn is a chase" },
446 { 25, XQ_CANNON, XQ_ADVISOR, 0, 0, 0, XQ_CHASE_FORBIDDEN,
447 "a Cannon attacking an unprotected Knight, Guard, Minister or crossed Pawn is a chase" },
448 { 25, XQ_CANNON, XQ_ELEPHANT, 0, 0, 0, XQ_CHASE_FORBIDDEN,
449 "a Cannon attacking an unprotected Knight, Guard, Minister or crossed Pawn is a chase" },
450 { 24, XQ_CHARIOT, XQ_CHARIOT, -1, 0, 0, XQ_CHASE_DRAW,
451 "Rook chases Rook: if both can capture the other it is perpetual sacrifice, a draw" },
452 { 24, XQ_CANNON, XQ_CANNON, -1, 0, 0, XQ_CHASE_DRAW,
453 "Cannon chases Cannon: if both can capture the other it is perpetual sacrifice, a draw" },
454 { 24, XQ_HORSE, XQ_HORSE, -1, 0, 0, XQ_CHASE_DRAW,
455 "Knight chases Knight: if both can capture the other it is perpetual sacrifice, a draw" },
456 /* the general case, which is the encyclopaedia's one-line summary and the
457 * LAST row rather than the first: an unprotected piece may not be chased */
458 { 36, 0, 0, 0, 0, 0, XQ_CHASE_FORBIDDEN,
459 "perpetual chase of an unprotected piece is forbidden; an exchange offer is still a chase" },
460 { 36, 0, 0, 1, 0, 0, XQ_CHASE_ALLOWED,
461 "a protected piece may be perpetually chased unless a more specific rule says otherwise" }
462 };
463  
464 #define CHASE_N ((int)(sizeof(CHASE) / sizeof(CHASE[0])))
465  
466 8 static int xq_rule_count(void) { return CHASE_N; }
467 176 static const struct xq_chase_rule *xq_rule_at(int i)
468 {
469 176 ▶ if (i < 0 || i >= CHASE_N) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i < 0 || i >= CHASE_N) return 0; 176 0 176
if (i < 0 || i >= CHASE_N) return 0; 0 176 176
470 176 return &CHASE[i];
471 }
472  
473 /* ---- describing one ply ---------------------------------------------------------- */
474  
475 struct ply {
476 int side;
477 int mv;
478 int kind; /* the kind of piece that moved */
479 int check, ttc, chase, exchange, block, sacrifice, idle;
480 int victim_kind; /* the chased piece's kind, 0 if not a chase */
481 int victim_prot; /* 1 protected by a REAL protector, 0 otherwise */
482 int victim_def; /* 1 defended AT ALL, real protector or not */
483 unsigned long long key; /* the key AFTER the move */
484 int captured;
485 int progress; /* a capture, or a crossed soldier advancing */
486 };
487  
488 2637 static int soldier_advance(struct xq_board *b, int mv)
489 {
490 2637 int from = (E())->move_from(mv);
491 2637 int p = AT(b, from);
492 2637 ▶ if (XQ_KIND(p) != XQ_SOLDIER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_KIND(p) != XQ_SOLDIER) return 0; 2458 179 2637
493 179 return (E())->crossed_river(from, XQ_COLOUR(p));
494 }
495  
496 /* ---- the judge ------------------------------------------------------------------- */
497  
498 668 static int beh_of(const struct ply *p)
499 {
500 668 ▶ if (p->check) return XQ_BEH_CHECK;
Branch: 1 branch
BranchTrueFalseTotal
if (p->check) return XQ_BEH_CHECK; 94 574 668
501 574 ▶ if (p->chase) return XQ_BEH_CHASE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->chase) return XQ_BEH_CHASE; 268 306 574
502 306 ▶ if (p->ttc) return XQ_BEH_TTC;
Branch: 1 branch
BranchTrueFalseTotal
if (p->ttc) return XQ_BEH_TTC; 7 299 306
503 299 ▶ if (p->block) return XQ_BEH_BLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if (p->block) return XQ_BEH_BLOCK; 59 240 299
504 240 ▶ if (p->exchange) return XQ_BEH_EXCHANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->exchange) return XQ_BEH_EXCHANGE; 0 240 240
505 240 ▶ if (p->sacrifice) return XQ_BEH_SACRIFICE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->sacrifice) return XQ_BEH_SACRIFICE; 10 230 240
506 230 return XQ_BEH_IDLE;
507 }
508  
509 377 static int threshold(int kinds)
510 {
511 377 ▶ if (kinds <= 1) return XQ_PERPETUAL_1;
Branch: 1 branch
BranchTrueFalseTotal
if (kinds <= 1) return XQ_PERPETUAL_1; 353 24 377
512 24 ▶ if (kinds == 2) return XQ_PERPETUAL_2;
Branch: 1 branch
BranchTrueFalseTotal
if (kinds == 2) return XQ_PERPETUAL_2; 16 8 24
513 8 return XQ_PERPETUAL_3;
514 }
515  
516 /* Section 3's table, applied to one side's perpetual chase. Returns the row or
517 * NULL when nothing matches, which cannot happen because the last two rows are
518 * general and between them cover every protectedness. */
519 3 static const struct xq_chase_rule *chase_rule(int chaser_kind, int victim_kind,
520 int prot, int def,
521 int chasers, int victims)
522 {
523 int i;
524 29 ▶ for (i = 0; i < CHASE_N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < CHASE_N; i++) { 29 0 29
525 29 const struct xq_chase_rule *r = &CHASE[i];
526 29 ▶ if (r->chaser && r->chaser != chaser_kind) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->chaser && r->chaser != chaser_kind) continue; 20 9 29
if (r->chaser && r->chaser != chaser_kind) continue; 14 6 20
527 15 ▶ if (r->victim && r->victim != victim_kind) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->victim && r->victim != victim_kind) continue; 6 9 15
if (r->victim && r->victim != victim_kind) continue; 2 4 6
528 /* protectedness 2 is rule 34's case: DEFENDED, but falsely */
529 13 ▶ if (r->protectedness == 2) { if (!(def && !prot)) continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (r->protectedness == 2) { if (!(def && !prot)) continue; } 3 10 13
if (r->protectedness == 2) { if (!(def && !prot)) continue; } 1 2 3
if (r->protectedness == 2) { if (!(def && !prot)) continue; } 0 1 1
530 10 ▶ else if (r->protectedness >= 0 && r->protectedness != prot) continue;
Branch: 2 branches
BranchTrueFalseTotal
else if (r->protectedness >= 0 && r->protectedness != prot) continue; 8 2 10
else if (r->protectedness >= 0 && r->protectedness != prot) continue; 4 4 8
531 7 ▶ if (r->chasers == 1 && chasers != 1) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->chasers == 1 && chasers != 1) continue; 2 5 7
if (r->chasers == 1 && chasers != 1) continue; 0 2 2
532 7 ▶ if (r->chasers == 2 && chasers < 2) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->chasers == 2 && chasers < 2) continue; 2 5 7
if (r->chasers == 2 && chasers < 2) continue; 2 0 2
533 5 ▶ if (r->victims == 1 && victims != 1) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->victims == 1 && victims != 1) continue; 1 4 5
if (r->victims == 1 && victims != 1) continue; 0 1 1
534 5 ▶ if (r->victims == 2 && victims < 2) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (r->victims == 2 && victims < 2) continue; 2 3 5
if (r->victims == 2 && victims < 2) continue; 2 0 2
535 3 return r;
536 }
537 0 return 0;
538 }
539  
540 283 static int xq_judge(struct xq_board *start, const int *moves, int n,
541 struct xq_verdict *out)
542 {
543 static struct ply ply[XQ_MAX_PLIES];
544 struct xq_board *b;
545 unsigned long long seen[XQ_MAX_PLIES + 1];
546 int i, s, red_first;
547 283 int effective = 0, progress = 0;
548 283 int tolerating = 0; /* a check or chase run that has not reached CXQ's threshold */
549  
550 283 ▶ if (out) {
Branch: 1 branch
BranchTrueFalseTotal
if (out) { 283 0 283
551 283 out->winner = 0; out->reason = XQ_JUDGE_ONGOING; out->rule = 0;
552 283 out->loop_from = -1; out->loop_len = 0;
553 283 out->behaviour[0] = out->behaviour[1] = XQ_BEH_NONE;
554 283 out->run[0] = out->run[1] = 0;
555 283 out->kinds[0] = out->kinds[1] = 0;
556 283 out->effective = out->progress = out->plies = 0;
557 }
558 283 ▶ if (n <= 0 || n > XQ_MAX_PLIES) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || n > XQ_MAX_PLIES) return 0; 205 78 283
if (n <= 0 || n > XQ_MAX_PLIES) return 0; 0 205 205
559  
560 205 b = (E())->board_copy(start);
561 205 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 205 205
562  
563 /* ---- 1 and 2: replay, describing every ply as it goes ------------------- */
564 205 seen[0] = (E())->key(b);
565 2842 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2637 205 2842
566 struct xq_undo u;
567 2637 struct ply *p = &ply[i];
568 2637 int victim = 0;
569  
570 2637 p->side = (E())->side(b);
571 2637 p->mv = moves[i];
572 2637 p->kind = XQ_KIND(AT(b, (E())->move_from(moves[i])));
573 2637 p->check = (E())->is_check(b, moves[i]);
574 2637 p->ttc = (E())->is_ttc(b, moves[i]);
575 2637 p->chase = (E())->is_chase(b, moves[i], &victim);
576 2637 p->exchange = (E())->is_exchange(b, moves[i]);
577 2637 p->block = (E())->is_block(b, moves[i]);
578 2637 p->sacrifice = (E())->is_sacrifice(b, moves[i]);
579 2637 p->idle = (E())->is_idle(b, moves[i]);
580 2637 p->progress = soldier_advance(b, moves[i]);
581  
582 2637 p->victim_kind = 0;
583 2637 p->victim_prot = 0;
584 2637 p->victim_def = 0;
585 2637 p->captured = (E())->do_move(b, moves[i], &u);
586 2637 ▶ if (p->captured != XQ_EMPTY) p->progress = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p->captured != XQ_EMPTY) p->progress = 1; 308 2329 2637
587 2637 ▶ if (victim) {
Branch: 1 branch
BranchTrueFalseTotal
if (victim) { 1020 1617 2637
588 1020 int real = 0;
589 1020 p->victim_kind = XQ_KIND(AT(b, victim));
590 /* RULE 34 IS APPLIED HERE AND NOT IN THE TABLE: a protected piece
591 * whose protector cannot actually recapture is chased as an
592 * unprotected one. Folding it in at the point the protection is
593 * measured is what makes the table readable. */
594 1020 p->victim_def = (E())->protected_at(b, victim, &real);
595 1020 ▶ p->victim_prot = p->victim_def && real;
Branch: 2 branches
BranchTrueFalseTotal
p->victim_prot = p->victim_def && real; 303 717 1020
p->victim_prot = p->victim_def && real; 293 10 303
596 }
597 2637 p->key = (E())->key(b);
598 2637 seen[i + 1] = p->key;
599  
600 /* CXQ's Effective Rule: "moves made by each side excluding
601 * checking/chasing moves or moves to respond to checking/chasing". The
602 * count cannot be taken before the classification, which is why phase
603 * 08's `play` judges and only then counts. */
604 2637 ▶ if (!p->check && !p->chase &&
Branch: 3 branches
BranchTrueFalseTotal
if (!p->check && !p->chase && 2494 143 2637
if (!p->check && !p->chase && 1474 1020 2494
if (!p->check && !p->chase && 1389 85 1474
605 1389 ▶ !(i > 0 && (ply[i - 1].check || ply[i - 1].chase)))
Branch: 2 branches
BranchTrueFalseTotal
!(i > 0 && (ply[i - 1].check || ply[i - 1].chase))) 1254 135 1389
!(i > 0 && (ply[i - 1].check || ply[i - 1].chase))) 665 589 1254
606 750 effective++;
607 2637 ▶ if (p->progress) progress = 0; else progress++;
Branch: 1 branch
BranchTrueFalseTotal
if (p->progress) progress = 0; else progress++; 308 2329 2637
608 }
609  
610 205 ▶ if (out) {
Branch: 1 branch
BranchTrueFalseTotal
if (out) { 205 0 205
611 205 out->effective = effective / 2;
612 205 out->progress = progress / 2;
613 205 out->plies = n;
614 }
615 205 red_first = ply[0].side == XQ_RED ? 0 : 1;
616 (void) red_first;
617  
618 /* ---- 3: the run at the end, and how many kinds made it ------------------ */
619 {
620 int raw[2];
621 205 raw[0] = raw[1] = XQ_BEH_NONE;
622 615 ▶ for (s = 0; s < 2; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) { 410 205 615
623 410 ▶ int colour = s == 0 ? XQ_RED : XQ_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
int colour = s == 0 ? XQ_RED : XQ_BLACK; 205 205 410
624 410 int run = 0, beh = XQ_BEH_NONE, kindmask = 0, kinds = 0, k;
625 1288 ▶ for (i = n - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) { 1171 117 1288
626 1171 ▶ if (ply[i].side != colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ply[i].side != colour) continue; 503 668 1171
627 668 ▶ if (beh == XQ_BEH_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (beh == XQ_BEH_NONE) { 377 291 668
628 377 beh = beh_of(&ply[i]);
629 377 ▶ if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE &&
Branch: 3 branches
BranchTrueFalseTotal
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 357 20 377
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 226 131 357
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 219 7 226
630 219 ▶ beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK &&
Branch: 2 branches
BranchTrueFalseTotal
beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK && 180 39 219
beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK && 180 0 180
631 180 ▶ beh != XQ_BEH_EXCHANGE && beh != XQ_BEH_SACRIFICE) break;
Branch: 1 branch
BranchTrueFalseTotal
beh != XQ_BEH_EXCHANGE && beh != XQ_BEH_SACRIFICE) break; 176 4 180
632 }
633 291 ▶ else if (beh_of(&ply[i]) != beh) break;
Branch: 1 branch
BranchTrueFalseTotal
else if (beh_of(&ply[i]) != beh) break; 117 174 291
634 375 run++;
635 375 kindmask |= 1 << ply[i].kind;
636 }
637 3690 ▶ for (k = 0; k < 8; k++) if (kindmask & (1 << k)) kinds++;
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < 8; k++) if (kindmask & (1 << k)) kinds++; 233 3047 3280
for (k = 0; k < 8; k++) if (kindmask & (1 << k)) kinds++; 3280 410 3690
638 410 ▶ if (out) { out->run[s] = run; out->kinds[s] = kinds; }
Branch: 1 branch
BranchTrueFalseTotal
if (out) { out->run[s] = run; out->kinds[s] = kinds; } 410 0 410
639 410 raw[s] = beh;
640 410 ▶ if (beh != XQ_BEH_NONE && run >= threshold(kinds)) {
Branch: 2 branches
BranchTrueFalseTotal
if (beh != XQ_BEH_NONE && run >= threshold(kinds)) { 377 33 410
if (beh != XQ_BEH_NONE && run >= threshold(kinds)) { 8 369 377
641 8 ▶ if (out) out->behaviour[s] = beh;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out->behaviour[s] = beh; 8 0 8
642 }
643 }
644  
645 /* PRINCIPLE 1 MUST NOT FIRE WHILE CXQ'S TOLERANCE IS STILL RUNNING, and the
646 * first version of this let it.
647 *
648 * "When neither side violates the rules and both persist on not altering
649 * their moves. The game can be ruled as a draw." A side four checks into a
650 * shuttle is not a side that violates nothing: it is a side whose violation
651 * has not been reached yet. Ruling that a draw hands the checking side an
652 * escape it has not earned, and it fired on the perpetual-check fixture at
653 * every length below twelve plies.
654 *
655 * So: if either side's run IS a check or a chase but has not reached the
656 * threshold, the answer is ONGOING and nothing else. */
657 615 ▶ for (s = 0; s < 2; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) 410 205 615
658 410 ▶ if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) &&
Branch: 3 branches
BranchTrueFalseTotal
if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) && 390 20 410
if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) && 131 259 390
if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) && 151 0 151
659 151 ▶ out && out->behaviour[s] == XQ_BEH_NONE)
Branch: 1 branch
BranchTrueFalseTotal
out && out->behaviour[s] == XQ_BEH_NONE) 143 8 151
660 143 tolerating = 1;
661 }
662  
663 /* ---- the loop, which principle 1 needs --------------------------------- */
664 {
665 205 int from = -1;
666 2646 ▶ for (i = n - 1; i >= 0 && from < 0; i--)
Branch: 2 branches
BranchTrueFalseTotal
for (i = n - 1; i >= 0 && from < 0; i--) 2464 182 2646
for (i = n - 1; i >= 0 && from < 0; i--) 2441 23 2464
667 2441 ▶ if (seen[i] == seen[n]) from = i;
Branch: 1 branch
BranchTrueFalseTotal
if (seen[i] == seen[n]) from = i; 25 2416 2441
668 205 ▶ if (out && from >= 0) { out->loop_from = from; out->loop_len = n - from; }
Branch: 2 branches
BranchTrueFalseTotal
if (out && from >= 0) { out->loop_from = from; out->loop_len = n - from; } 205 0 205
if (out && from >= 0) { out->loop_from = from; out->loop_len = n - from; } 25 180 205
669 }
670  
671 205 (E())->board_drop(b);
672 205 ▶ if (!out) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return 0; 0 205 205
673  
674 /* ---- 4: rule ------------------------------------------------------------ */
675 {
676 205 int r = out->behaviour[0], k = out->behaviour[1];
677  
678 /* Section 2, principle 3: "If one side perpetually check and the other
679 * side perpetually chase, the perpetually checking side has to stop or
680 * be ruled to lose." It comes FIRST because it decides a case both of
681 * the others would also match. */
682 205 ▶ if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) { 5 200 205
if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) { 0 5 5
683 0 out->winner = XQ_BLACK; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 12;
684 0 return out->winner;
685 }
686 205 ▶ if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) {
Branch: 2 branches
BranchTrueFalseTotal
if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) { 0 205 205
if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) { 0 0 0
687 0 out->winner = XQ_RED; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 12;
688 0 return out->winner;
689 }
690  
691 /* principle 2 and rule 7: "It is a draw when both sides keep checking."
692 * Both violating the same rule at the same time is a draw. */
693 205 ▶ if (r != XQ_BEH_NONE && r == k) {
Branch: 2 branches
BranchTrueFalseTotal
if (r != XQ_BEH_NONE && r == k) { 8 197 205
if (r != XQ_BEH_NONE && r == k) { 0 8 8
694 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL;
695 0 ▶ out->rule = (r == XQ_BEH_CHECK) ? 7 : 0;
Branch: 1 branch
BranchTrueFalseTotal
out->rule = (r == XQ_BEH_CHECK) ? 7 : 0; 0 0 0
696 0 return 0;
697 }
698  
699 /* Asian Rules 6: "Under any circumstance, the side that perpetually
700 * checks with one piece or several pieces will be ruled to lose." */
701 205 ▶ if (r == XQ_BEH_CHECK) { out->winner = XQ_BLACK; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 6; return out->winner; }
Branch: 1 branch
BranchTrueFalseTotal
if (r == XQ_BEH_CHECK) { out->winner = XQ_BLACK; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 6; return out->winner; } 5 200 205
702 200 ▶ if (k == XQ_BEH_CHECK) { out->winner = XQ_RED; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 6; return out->winner; }
Branch: 1 branch
BranchTrueFalseTotal
if (k == XQ_BEH_CHECK) { out->winner = XQ_RED; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 6; return out->winner; } 0 200 200
703  
704 /* rules 9 and 10: perpetual TTC, and resolve-TTC-and-TTC-back, are
705 * draws rather than losses, which is the half of the source a summary
706 * always drops */
707 200 ▶ if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) { 200 0 200
if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) { 0 200 200
708 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL; out->rule = 9; return 0;
709 }
710 /* rule 39, perpetual block, and rule 40, perpetual exchange or
711 * sacrifice: both draws */
712 200 ▶ if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) { 200 0 200
if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) { 0 200 200
713 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL; out->rule = 39; return 0;
714 }
715 200 ▶ if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE ||
Branch: 3 branches
BranchTrueFalseTotal
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 200 0 200
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 200 0 200
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 200 0 200
716 200 ▶ r == XQ_BEH_SACRIFICE || k == XQ_BEH_SACRIFICE) {
Branch: 1 branch
BranchTrueFalseTotal
r == XQ_BEH_SACRIFICE || k == XQ_BEH_SACRIFICE) { 0 200 200
717 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL; out->rule = 40; return 0;
718 }
719  
720 /* Section 3's chase table, for whichever side is chasing */
721 594 ▶ for (s = 0; s < 2; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) { 397 197 594
722 397 ▶ int colour = s == 0 ? XQ_RED : XQ_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
int colour = s == 0 ? XQ_RED : XQ_BLACK; 200 197 397
723 397 int kindmask = 0, vmask = 0, chasers = 0, victims = 0, kk;
724 const struct xq_chase_rule *row;
725 397 int vkind = 0, prot = 1, def = 1, ckind = 0;
726 397 ▶ if (out->behaviour[s] != XQ_BEH_CHASE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (out->behaviour[s] != XQ_BEH_CHASE) continue; 394 3 397
727 54 ▶ for (i = n - 1; i >= 0 && n - i <= out->run[s] * 2 + 1; i--) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = n - 1; i >= 0 && n - i <= out->run[s] * 2 + 1; i--) { 51 3 54
for (i = n - 1; i >= 0 && n - i <= out->run[s] * 2 + 1; i--) { 51 0 51
728 51 ▶ if (ply[i].side != colour || !ply[i].chase) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (ply[i].side != colour || !ply[i].chase) continue; 26 25 51
if (ply[i].side != colour || !ply[i].chase) continue; 0 26 26
729 26 kindmask |= 1 << ply[i].kind;
730 26 vmask |= 1 << ply[i].victim_kind;
731 26 vkind = ply[i].victim_kind;
732 /* THE CHASER'S KIND COMES FROM THE CHASING SIDE'S OWN PLY.
733 * Taking it from the last ply of the log takes it from the side
734 * RUNNING AWAY, which cited rule 24 (chariot chases chariot) for
735 * a chariot chasing a cannon. */
736 26 ▶ if (!ckind) ckind = ply[i].kind;
Branch: 1 branch
BranchTrueFalseTotal
if (!ckind) ckind = ply[i].kind; 3 23 26
737 26 ▶ if (!ply[i].victim_prot) prot = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ply[i].victim_prot) prot = 0; 26 0 26
738 26 ▶ if (!ply[i].victim_def) def = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ply[i].victim_def) def = 0; 16 10 26
739 }
740 27 ▶ for (kk = 0; kk < 8; kk++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kk = 0; kk < 8; kk++) { 24 3 27
741 24 ▶ if (kindmask & (1 << kk)) chasers++;
Branch: 1 branch
BranchTrueFalseTotal
if (kindmask & (1 << kk)) chasers++; 3 21 24
742 24 ▶ if (kk && (vmask & (1 << kk))) victims++;
Branch: 2 branches
BranchTrueFalseTotal
if (kk && (vmask & (1 << kk))) victims++; 21 3 24
if (kk && (vmask & (1 << kk))) victims++; 3 18 21
743 }
744 3 row = chase_rule(ckind, vkind, prot, def, chasers, victims);
745 3 ▶ if (!row) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) continue; 0 3 3
746 3 out->rule = row->number;
747 3 ▶ if (row->verdict == XQ_CHASE_FORBIDDEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (row->verdict == XQ_CHASE_FORBIDDEN) { 3 0 3
748 3 ▶ out->winner = XQ_OTHER(colour);
Branch: 1 branch
BranchTrueFalseTotal
out->winner = XQ_OTHER(colour); 3 0 3
749 3 out->reason = XQ_JUDGE_PERPETUAL_CHASE;
750 3 return out->winner;
751 }
752 0 ▶ if (row->verdict == XQ_CHASE_DRAW) {
Branch: 1 branch
BranchTrueFalseTotal
if (row->verdict == XQ_CHASE_DRAW) { 0 0 0
753 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL; return 0;
754 }
755 /* ALLOWED: the chased side must live with it, and the game goes on
756 * unless a counter fires below */
757 0 out->rule = row->number;
758 }
759  
760 /* ---- CXQ's three automatic draws, and they are OURS by adoption ----- */
761 197 ▶ if (out->progress >= XQ_PROGRESS_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->progress >= XQ_PROGRESS_CAP) { 1 196 197
762 1 out->winner = 0; out->reason = XQ_JUDGE_PROGRESS; out->rule = 0; return 0;
763 }
764 196 ▶ if (out->effective >= XQ_EFFECTIVE_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->effective >= XQ_EFFECTIVE_CAP) { 0 196 196
765 0 out->winner = 0; out->reason = XQ_JUDGE_EFFECTIVE; out->rule = 0; return 0;
766 }
767 196 ▶ if (n / 2 >= XQ_MOVES_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (n / 2 >= XQ_MOVES_CAP) { 0 196 196
768 0 out->winner = 0; out->reason = XQ_JUDGE_MOVES; out->rule = 0; return 0;
769 }
770  
771 /* Section 2, principle 1: "When neither side violates the rules and both
772 * persist on not altering their moves. The game can be ruled as a draw."
773 * A loop with nobody in breach is that, and nothing else is. */
774 196 ▶ if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE &&
Branch: 4 branches
BranchTrueFalseTotal
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 16 180 196
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 16 0 16
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 16 0 16
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 1 15 16
775 1 ▶ !tolerating && out->loop_len >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
!tolerating && out->loop_len >= 4) { 1 0 1
776 1 out->winner = 0; out->reason = XQ_JUDGE_NO_VIOLATION; out->rule = 5;
777 1 return 0;
778 }
779 }
780 195 return 0;
781 }
782  
783 void xq_judge2_install(struct xq_abi *t);
784  
785 24 void xq_judge2_install(struct xq_abi *t)
786 {
787 24 t->judge = xq_judge;
788 24 t->rule_count = xq_rule_count;
789 24 t->rule_at = xq_rule_at;
790 24 }
« Xiangqi.xs ↑ Summary xq_moves.c »