xq_judge.c

filter: Statementstmt 94.9% 298 / 314 Branchbran 85.4% 277 / 324 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.1% 575 / 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 7113784 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 139712689 static int xq_value_of(int piece)
38 {
39 139712689 switch (XQ_KIND(piece)) {
40 20233795 case XQ_CHARIOT: return 900;
41 18279044 case XQ_CANNON: return 450;
42 15589080 case XQ_HORSE: return 400;
43 19670998 case XQ_ADVISOR: return 200;
44 19524915 case XQ_ELEPHANT: return 200;
45 36289047 case XQ_SOLDIER: return 100;
46 10125810 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 12046 static int xq_is_check(struct xq_board *b, int mv)
55 {
56 struct xq_undo u;
57 12046 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 6362 5684 12046
58 int r;
59 12046 (E())->do_move(b, mv, &u);
60 12046 r = (E())->in_check(b, them);
61 12046 (E())->undo_move(b, &u);
62 12046 return r;
63 }
64  
65 /* "Check in such a way that the opponent's King cannot resolve the check." */
66 98 static int xq_is_mate(struct xq_board *b, int mv)
67 {
68 struct xq_undo u;
69 98 int reason = XQ_ONGOING;
70 98 (E())->do_move(b, mv, &u);
71 98 (E())->outcome(b, &reason);
72 98 (E())->undo_move(b, &u);
73 98 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 11497 static int xq_is_ttc(struct xq_board *b, int mv)
98 {
99 struct xq_undo u;
100 11497 int me = (E())->side(b);
101 int r;
102  
103 11497 (E())->do_move(b, mv, &u);
104 11497 (E())->set_side(b, me); /* the null move */
105 11497 r = (E())->mate_in(b, 1);
106 11497 (E())->undo_move(b, &u); /* restores the side and the key exactly */
107 11497 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 11391 static int xq_is_chase(struct xq_board *b, int mv, int *victim)
126 {
127 struct xq_undo u;
128 11391 int me = (E())->side(b);
129 11391 ▶ int them = XQ_OTHER(me);
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER(me); 6057 5334 11391
130 unsigned char before[XQ_CELLS];
131 11391 int pt, found = 0;
132  
133 11391 ▶ if (victim) *victim = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (victim) *victim = 0; 5975 5416 11391
134  
135 1515003 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 1503612 11391 1515003
136 1503612 int p = AT(b, pt);
137 1503612 before[pt] = (unsigned char)
138 2528802 ▶ ((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 158297 866893 1025190
((E())->on_board(pt) && XQ_COLOUR(p) == them && XQ_KIND(p) != XQ_GENERAL 146906 11391 158297
139 2528802 ▶ && (E())->attacked(b, pt, me));
Branch: 2 branches
BranchTrueFalseTotal
&& (E())->attacked(b, pt, me)); 1025190 478422 1503612
&& (E())->attacked(b, pt, me)); 19386 127520 146906
140 }
141  
142 11391 (E())->do_move(b, mv, &u);
143 1284186 ▶ for (pt = 0; pt < XQ_CELLS && !found; pt++) {
Branch: 2 branches
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS && !found; pt++) { 1276957 7229 1284186
for (pt = 0; pt < XQ_CELLS && !found; pt++) { 1272795 4162 1276957
144 1272795 int p = AT(b, pt);
145 1272795 ▶ if (!(E())->on_board(pt)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->on_board(pt)) continue; 398586 874209 1272795
146 874209 ▶ 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; 130887 743322 874209
if (XQ_COLOUR(p) != them || XQ_KIND(p) == XQ_GENERAL) continue; 9140 121747 130887
147 121747 ▶ if (before[pt]) continue; /* already attacked */
Branch: 1 branch
BranchTrueFalseTotal
if (before[pt]) continue; /* already attacked */ 14769 106978 121747
148 106978 ▶ if (!(E())->attacked(b, pt, me)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(E())->attacked(b, pt, me)) continue; 102816 4162 106978
149 4162 found = 1;
150 4162 ▶ if (victim) *victim = pt;
Branch: 1 branch
BranchTrueFalseTotal
if (victim) *victim = pt; 2105 2057 4162
151 }
152 11391 (E())->undo_move(b, &u);
153 11391 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 9417 static int xq_is_exchange(struct xq_board *b, int mv)
169 {
170 struct xq_undo u;
171 9417 int to = (E())->move_to(mv);
172 9417 int from = (E())->move_from(mv);
173 9417 int mover = AT(b, from);
174 9417 int taken = AT(b, to);
175 9417 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 4728 4689 9417
176 int recapturable;
177  
178 9417 ▶ 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 */ 8315 1102 9417
179 1102 ▶ 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; 530 572 1102
180 572 ▶ 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 462 572
181  
182 462 (E())->do_move(b, mv, &u);
183 462 recapturable = (E())->attacked(b, to, them);
184 462 (E())->undo_move(b, &u);
185 462 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 18470 static void dests_by_origin(struct xq_board *b, int side, int *out)
208 {
209 int buf[XQ_MAX_MOVES];
210 18470 int keep = (E())->side(b);
211 int n, i;
212 18470 (E())->set_side(b, side);
213 18470 n = (E())->gen_moves(b, buf);
214 18470 (E())->set_side(b, keep);
215 18470 memset(out, 0, sizeof(int) * XQ_CELLS);
216 793915 ▶ 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])]++; 775445 18470 793915
217 18470 }
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 9235 static int xq_is_block(struct xq_board *b, int mv)
236 {
237 9235 int from = (E())->move_from(mv);
238 9235 int to = (E())->move_to(mv);
239 9235 int mover = AT(b, from);
240 9235 int taken = AT(b, to);
241 9235 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 4546 4689 9235
242 int was[XQ_CELLS], now[XQ_CELLS];
243 9235 int pt, lost = 0;
244  
245 9235 (E())->lift(b, from);
246 9235 dests_by_origin(b, them, was);
247 9235 (E())->put(b, to, mover);
248 9235 dests_by_origin(b, them, now);
249 9235 (E())->put(b, to, taken); /* XQ_EMPTY when it was not a capture */
250 9235 (E())->put(b, from, mover); /* and the key comes back with it */
251  
252 1008594 ▶ for (pt = 0; pt < XQ_CELLS; pt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) { 1002660 5934 1008594
253 1002660 ▶ 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 */ 8010 994650 1002660
254 994650 ▶ if (now[pt] < was[pt]) { lost = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (now[pt] < was[pt]) { lost = 1; break; } 3301 991349 994650
255 }
256 9235 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 8125 static int xq_is_sacrifice(struct xq_board *b, int mv)
263 {
264 struct xq_undo u;
265 8125 int to = (E())->move_to(mv);
266 8125 ▶ int them = XQ_OTHER((E())->side(b));
Branch: 1 branch
BranchTrueFalseTotal
int them = XQ_OTHER((E())->side(b)); 3991 4134 8125
267 int r;
268 8125 (E())->do_move(b, mv, &u);
269 8125 r = (E())->attacked(b, to, them);
270 8125 (E())->undo_move(b, &u);
271 8125 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 6022 static int xq_is_idle(struct xq_board *b, int mv)
283 {
284 6022 ▶ if (xq_is_check(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_check(b, mv)) return 0; 550 5472 6022
285 5472 ▶ if (xq_is_ttc(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_ttc(b, mv)) return 0; 56 5416 5472
286 5416 ▶ if (xq_is_chase(b, mv, 0)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_chase(b, mv, 0)) return 0; 2057 3359 5416
287 3359 ▶ if (xq_is_exchange(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_exchange(b, mv)) return 0; 88 3271 3359
288 3271 ▶ if (xq_is_block(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_block(b, mv)) return 0; 1155 2116 3271
289 2116 ▶ if (xq_is_sacrifice(b, mv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (xq_is_sacrifice(b, mv)) return 0; 166 1950 2116
290 1950 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 2176 static int xq_protected_at(struct xq_board *b, int pt, int *real)
316 {
317 2176 int piece = AT(b, pt);
318 int mine, theirs;
319 int buf[XQ_MAX_MOVES];
320 2176 int keep = (E())->side(b);
321 2176 int n, i, defended, all_recaptured = 1, tested = 0;
322  
323 2176 ▶ if (real) *real = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (real) *real = 0; 2176 0 2176
324 2176 ▶ 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; 2176 0 2176
if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0; 2118 58 2176
if (!(E())->on_board(pt) || !XQ_IS_PIECE(piece)) return 0; 0 2118 2118
325 2118 mine = XQ_COLOUR(piece);
326 2118 ▶ theirs = XQ_OTHER(mine);
Branch: 1 branch
BranchTrueFalseTotal
theirs = XQ_OTHER(mine); 710 1408 2118
327  
328 2118 defended = (E())->attacked(b, pt, mine);
329 2118 ▶ if (!defended) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!defended) return 0; 1313 805 2118
330  
331 /* every legal capture of this piece, answered */
332 805 (E())->set_side(b, theirs);
333 805 n = (E())->gen_legal(b, buf);
334 30574 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 29899 675 30574
335 struct xq_undo u;
336 int back[XQ_MAX_MOVES];
337 29899 int m, j, answered = 0;
338 29899 ▶ if ((E())->move_to(buf[i]) != pt) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->move_to(buf[i]) != pt) continue; 29104 795 29899
339 795 tested++;
340 795 (E())->do_move(b, buf[i], &u);
341 795 m = (E())->gen_legal(b, back);
342 13873 ▶ for (j = 0; j < m; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) 13743 130 13873
343 13743 ▶ if ((E())->move_to(back[j]) == pt) { answered = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((E())->move_to(back[j]) == pt) { answered = 1; break; } 665 13078 13743
344 795 (E())->undo_move(b, &u);
345 795 ▶ if (!answered) { all_recaptured = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!answered) { all_recaptured = 0; break; } 130 665 795
346 }
347 805 (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 805 ▶ if (real) *real = (tested == 0) ? 1 : all_recaptured;
Branch: 2 branches
BranchTrueFalseTotal
if (real) *real = (tested == 0) ? 1 : all_recaptured; 805 0 805
if (real) *real = (tested == 0) ? 1 : all_recaptured; 795 10 805
352 805 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 28 void xq_judge_install(struct xq_abi *t)
360 {
361 28 t->is_check = xq_is_check;
362 28 t->is_mate = xq_is_mate;
363 28 t->is_ttc = xq_is_ttc;
364 28 t->is_chase = xq_is_chase;
365 28 t->is_exchange = xq_is_exchange;
366 28 t->is_block = xq_is_block;
367 28 t->is_sacrifice = xq_is_sacrifice;
368 28 t->is_idle = xq_is_idle;
369 28 t->protected_at = xq_protected_at;
370 28 t->value_of = xq_value_of;
371 28 }
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 5879 static int soldier_advance(struct xq_board *b, int mv)
489 {
490 5879 int from = (E())->move_from(mv);
491 5879 int p = AT(b, from);
492 5879 ▶ if (XQ_KIND(p) != XQ_SOLDIER) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_KIND(p) != XQ_SOLDIER) return 0; 5211 668 5879
493 668 return (E())->crossed_river(from, XQ_COLOUR(p));
494 }
495  
496 /* ---- the judge ------------------------------------------------------------------- */
497  
498 1389 static int beh_of(const struct ply *p)
499 {
500 1389 ▶ if (p->check) return XQ_BEH_CHECK;
Branch: 1 branch
BranchTrueFalseTotal
if (p->check) return XQ_BEH_CHECK; 307 1082 1389
501 1082 ▶ if (p->chase) return XQ_BEH_CHASE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->chase) return XQ_BEH_CHASE; 455 627 1082
502 627 ▶ if (p->ttc) return XQ_BEH_TTC;
Branch: 1 branch
BranchTrueFalseTotal
if (p->ttc) return XQ_BEH_TTC; 7 620 627
503 620 ▶ if (p->block) return XQ_BEH_BLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if (p->block) return XQ_BEH_BLOCK; 162 458 620
504 458 ▶ if (p->exchange) return XQ_BEH_EXCHANGE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->exchange) return XQ_BEH_EXCHANGE; 8 450 458
505 450 ▶ if (p->sacrifice) return XQ_BEH_SACRIFICE;
Branch: 1 branch
BranchTrueFalseTotal
if (p->sacrifice) return XQ_BEH_SACRIFICE; 15 435 450
506 435 return XQ_BEH_IDLE;
507 }
508  
509 741 static int threshold(int kinds)
510 {
511 741 ▶ if (kinds <= 1) return XQ_PERPETUAL_1;
Branch: 1 branch
BranchTrueFalseTotal
if (kinds <= 1) return XQ_PERPETUAL_1; 695 46 741
512 46 ▶ if (kinds == 2) return XQ_PERPETUAL_2;
Branch: 1 branch
BranchTrueFalseTotal
if (kinds == 2) return XQ_PERPETUAL_2; 38 8 46
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 527 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 527 int effective = 0, progress = 0;
548 527 int tolerating = 0; /* a check or chase run that has not reached CXQ's threshold */
549  
550 527 ▶ if (out) {
Branch: 1 branch
BranchTrueFalseTotal
if (out) { 527 0 527
551 527 out->winner = 0; out->reason = XQ_JUDGE_ONGOING; out->rule = 0;
552 527 out->loop_from = -1; out->loop_len = 0;
553 527 out->behaviour[0] = out->behaviour[1] = XQ_BEH_NONE;
554 527 out->run[0] = out->run[1] = 0;
555 527 out->kinds[0] = out->kinds[1] = 0;
556 527 out->effective = out->progress = out->plies = 0;
557 }
558 527 ▶ if (n <= 0 || n > XQ_MAX_PLIES) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || n > XQ_MAX_PLIES) return 0; 398 129 527
if (n <= 0 || n > XQ_MAX_PLIES) return 0; 0 398 398
559  
560 398 b = (E())->board_copy(start);
561 398 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 398 398
562  
563 /* ---- 1 and 2: replay, describing every ply as it goes ------------------- */
564 398 seen[0] = (E())->key(b);
565 6277 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5879 398 6277
566 struct xq_undo u;
567 5879 struct ply *p = &ply[i];
568 5879 int victim = 0;
569  
570 5879 p->side = (E())->side(b);
571 5879 p->mv = moves[i];
572 5879 p->kind = XQ_KIND(AT(b, (E())->move_from(moves[i])));
573 5879 p->check = (E())->is_check(b, moves[i]);
574 5879 p->ttc = (E())->is_ttc(b, moves[i]);
575 5879 p->chase = (E())->is_chase(b, moves[i], &victim);
576 5879 p->exchange = (E())->is_exchange(b, moves[i]);
577 5879 p->block = (E())->is_block(b, moves[i]);
578 5879 p->sacrifice = (E())->is_sacrifice(b, moves[i]);
579 5879 p->idle = (E())->is_idle(b, moves[i]);
580 5879 p->progress = soldier_advance(b, moves[i]);
581  
582 5879 p->victim_kind = 0;
583 5879 p->victim_prot = 0;
584 5879 p->victim_def = 0;
585 5879 p->captured = (E())->do_move(b, moves[i], &u);
586 5879 ▶ if (p->captured != XQ_EMPTY) p->progress = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (p->captured != XQ_EMPTY) p->progress = 1; 768 5111 5879
587 5879 ▶ if (victim) {
Branch: 1 branch
BranchTrueFalseTotal
if (victim) { 2079 3800 5879
588 2079 int real = 0;
589 2079 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 2079 p->victim_def = (E())->protected_at(b, victim, &real);
595 2079 ▶ p->victim_prot = p->victim_def && real;
Branch: 2 branches
BranchTrueFalseTotal
p->victim_prot = p->victim_def && real; 788 1291 2079
p->victim_prot = p->victim_def && real; 659 129 788
596 }
597 5879 p->key = (E())->key(b);
598 5879 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 5879 ▶ if (!p->check && !p->chase &&
Branch: 3 branches
BranchTrueFalseTotal
if (!p->check && !p->chase && 5330 549 5879
if (!p->check && !p->chase && 3300 2030 5330
if (!p->check && !p->chase && 3078 222 3300
605 3078 ▶ !(i > 0 && (ply[i - 1].check || ply[i - 1].chase)))
Branch: 2 branches
BranchTrueFalseTotal
!(i > 0 && (ply[i - 1].check || ply[i - 1].chase))) 2572 506 3078
!(i > 0 && (ply[i - 1].check || ply[i - 1].chase))) 1547 1025 2572
606 1769 effective++;
607 5879 ▶ if (p->progress) progress = 0; else progress++;
Branch: 1 branch
BranchTrueFalseTotal
if (p->progress) progress = 0; else progress++; 808 5071 5879
608 }
609  
610 398 ▶ if (out) {
Branch: 1 branch
BranchTrueFalseTotal
if (out) { 398 0 398
611 398 out->effective = effective / 2;
612 398 out->progress = progress / 2;
613 398 out->plies = n;
614 }
615 398 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 398 raw[0] = raw[1] = XQ_BEH_NONE;
622 1194 ▶ for (s = 0; s < 2; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) { 796 398 1194
623 796 ▶ int colour = s == 0 ? XQ_RED : XQ_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
int colour = s == 0 ? XQ_RED : XQ_BLACK; 398 398 796
624 796 int run = 0, beh = XQ_BEH_NONE, kindmask = 0, kinds = 0, k;
625 2686 ▶ for (i = n - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) { 2464 222 2686
626 2464 ▶ if (ply[i].side != colour) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ply[i].side != colour) continue; 1075 1389 2464
627 1389 ▶ if (beh == XQ_BEH_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (beh == XQ_BEH_NONE) { 741 648 1389
628 741 beh = beh_of(&ply[i]);
629 741 ▶ if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE &&
Branch: 3 branches
BranchTrueFalseTotal
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 655 86 741
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 430 225 655
if (beh != XQ_BEH_CHECK && beh != XQ_BEH_CHASE && 423 7 430
630 423 ▶ beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK &&
Branch: 2 branches
BranchTrueFalseTotal
beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK && 311 112 423
beh != XQ_BEH_TTC && beh != XQ_BEH_BLOCK && 307 4 311
631 307 ▶ beh != XQ_BEH_EXCHANGE && beh != XQ_BEH_SACRIFICE) break;
Branch: 1 branch
BranchTrueFalseTotal
beh != XQ_BEH_EXCHANGE && beh != XQ_BEH_SACRIFICE) break; 298 9 307
632 }
633 648 ▶ else if (beh_of(&ply[i]) != beh) break;
Branch: 1 branch
BranchTrueFalseTotal
else if (beh_of(&ply[i]) != beh) break; 276 372 648
634 815 run++;
635 815 kindmask |= 1 << ply[i].kind;
636 }
637 7164 ▶ 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++; 497 5871 6368
for (k = 0; k < 8; k++) if (kindmask & (1 << k)) kinds++; 6368 796 7164
638 796 ▶ if (out) { out->run[s] = run; out->kinds[s] = kinds; }
Branch: 1 branch
BranchTrueFalseTotal
if (out) { out->run[s] = run; out->kinds[s] = kinds; } 796 0 796
639 796 raw[s] = beh;
640 796 ▶ if (beh != XQ_BEH_NONE && run >= threshold(kinds)) {
Branch: 2 branches
BranchTrueFalseTotal
if (beh != XQ_BEH_NONE && run >= threshold(kinds)) { 741 55 796
if (beh != XQ_BEH_NONE && run >= threshold(kinds)) { 16 725 741
641 16 ▶ if (out) out->behaviour[s] = beh;
Branch: 1 branch
BranchTrueFalseTotal
if (out) out->behaviour[s] = beh; 16 0 16
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 1194 ▶ for (s = 0; s < 2; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) 796 398 1194
658 796 ▶ 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) && 710 86 796
if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) && 225 485 710
if ((raw[s] == XQ_BEH_CHECK || raw[s] == XQ_BEH_CHASE) && 311 0 311
659 311 ▶ out && out->behaviour[s] == XQ_BEH_NONE)
Branch: 1 branch
BranchTrueFalseTotal
out && out->behaviour[s] == XQ_BEH_NONE) 295 16 311
660 295 tolerating = 1;
661 }
662  
663 /* ---- the loop, which principle 1 needs --------------------------------- */
664 {
665 398 int from = -1;
666 5013 ▶ for (i = n - 1; i >= 0 && from < 0; i--)
Branch: 2 branches
BranchTrueFalseTotal
for (i = n - 1; i >= 0 && from < 0; i--) 4678 335 5013
for (i = n - 1; i >= 0 && from < 0; i--) 4615 63 4678
667 4615 ▶ if (seen[i] == seen[n]) from = i;
Branch: 1 branch
BranchTrueFalseTotal
if (seen[i] == seen[n]) from = i; 67 4548 4615
668 398 ▶ 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; } 398 0 398
if (out && from >= 0) { out->loop_from = from; out->loop_len = n - from; } 67 331 398
669 }
670  
671 398 (E())->board_drop(b);
672 398 ▶ if (!out) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return 0; 0 398 398
673  
674 /* ---- 4: rule ------------------------------------------------------------ */
675 {
676 398 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 398 ▶ if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) { 8 390 398
if (r == XQ_BEH_CHECK && k == XQ_BEH_CHASE) { 0 8 8
683 0 out->winner = XQ_BLACK; out->reason = XQ_JUDGE_PERPETUAL_CHECK; out->rule = 12;
684 0 return out->winner;
685 }
686 398 ▶ if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) {
Branch: 2 branches
BranchTrueFalseTotal
if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) { 5 393 398
if (k == XQ_BEH_CHECK && r == XQ_BEH_CHASE) { 0 5 5
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 398 ▶ if (r != XQ_BEH_NONE && r == k) {
Branch: 2 branches
BranchTrueFalseTotal
if (r != XQ_BEH_NONE && r == k) { 11 387 398
if (r != XQ_BEH_NONE && r == k) { 0 11 11
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 398 ▶ 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; } 8 390 398
702 390 ▶ 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; } 5 385 390
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 385 ▶ if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) { 385 0 385
if (r == XQ_BEH_TTC || k == XQ_BEH_TTC) { 0 385 385
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 385 ▶ if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) { 385 0 385
if (r == XQ_BEH_BLOCK || k == XQ_BEH_BLOCK) { 0 385 385
713 0 out->winner = 0; out->reason = XQ_JUDGE_MUTUAL; out->rule = 39; return 0;
714 }
715 385 ▶ if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE ||
Branch: 3 branches
BranchTrueFalseTotal
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 385 0 385
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 385 0 385
if (r == XQ_BEH_EXCHANGE || k == XQ_BEH_EXCHANGE || 385 0 385
716 385 ▶ r == XQ_BEH_SACRIFICE || k == XQ_BEH_SACRIFICE) {
Branch: 1 branch
BranchTrueFalseTotal
r == XQ_BEH_SACRIFICE || k == XQ_BEH_SACRIFICE) { 0 385 385
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 1149 ▶ for (s = 0; s < 2; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 2; s++) { 767 382 1149
722 767 ▶ int colour = s == 0 ? XQ_RED : XQ_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
int colour = s == 0 ? XQ_RED : XQ_BLACK; 385 382 767
723 767 int kindmask = 0, vmask = 0, chasers = 0, victims = 0, kk;
724 const struct xq_chase_rule *row;
725 767 int vkind = 0, prot = 1, def = 1, ckind = 0;
726 767 ▶ if (out->behaviour[s] != XQ_BEH_CHASE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (out->behaviour[s] != XQ_BEH_CHASE) continue; 764 3 767
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 382 ▶ if (out->progress >= XQ_PROGRESS_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->progress >= XQ_PROGRESS_CAP) { 1 381 382
762 1 out->winner = 0; out->reason = XQ_JUDGE_PROGRESS; out->rule = 0; return 0;
763 }
764 381 ▶ if (out->effective >= XQ_EFFECTIVE_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->effective >= XQ_EFFECTIVE_CAP) { 0 381 381
765 0 out->winner = 0; out->reason = XQ_JUDGE_EFFECTIVE; out->rule = 0; return 0;
766 }
767 381 ▶ if (n / 2 >= XQ_MOVES_CAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (n / 2 >= XQ_MOVES_CAP) { 0 381 381
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 381 ▶ 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 && 50 331 381
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 50 0 50
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 50 0 50
if (out->loop_from >= 0 && r == XQ_BEH_NONE && k == XQ_BEH_NONE && 1 49 50
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 380 return 0;
781 }
782  
783 void xq_judge2_install(struct xq_abi *t);
784  
785 28 void xq_judge2_install(struct xq_abi *t)
786 {
787 28 t->judge = xq_judge;
788 28 t->rule_count = xq_rule_count;
789 28 t->rule_at = xq_rule_at;
790 28 }
« Xiangqi.xs ↑ Summary xq_moves.c »