xq_engine.c

filter: Statementstmt 98.3% 241 / 245 Branchbran 74.0% 163 / 220 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.8% 404 / 465 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/Xiangqi/Error.pm ↑ Summary Xiangqi.xs »
1 /* xq_engine.c - the xiangqi board, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/xq_abi.h. This file compiles into the XS,
4 * into a plain program, or to wasm, and knows nothing about any of them.
5 *
6 * PHASE 02 SCOPE. The board, the padded index, put and lift, do and undo, the
7 * zobrist key, and FEN in and out. No move generation, no legality, no check.
8 * Those are phases 03 and 04 and they append to the table.
9 */
10  
11 #include <stdlib.h>
12 #include <string.h>
13 #include "xq_abi.h"
14  
15 /* ---- the position ---------------------------------------------------------
16 *
17 * A flat 132-cell array with a sentinel ring, the side to move, and a key
18 * maintained incrementally. No pointers, so a copy is a struct assignment and
19 * two boards never share anything.
20 */
21  
22 struct xq_board {
23 unsigned char cell[XQ_CELLS];
24 int side;
25 unsigned long long key;
26 };
27  
28 /* ---- the zobrist table -----------------------------------------------------
29 *
30 * DERIVED, NOT COMMITTED, and the difference is worth a paragraph because the
31 * plan asked for a committed table of constants.
32 *
33 * What the requirement actually is: two workers in a pool must agree about
34 * whether a position has been seen, so the table must not come from rand() or
35 * from anything that varies per process. A fixed function of a fixed seed
36 * satisfies that exactly as a committed array does, in four lines instead of
37 * four thousand, and it cannot suffer the transcription error a hand-pasted
38 * array can. splitmix64 is used rather than a hand-rolled mixer because it is
39 * published, has no weak seeds, and passes the usual test batteries.
40 *
41 * The consequence to keep in mind: CHANGING THIS GENERATOR CHANGES EVERY KEY.
42 * Nothing persists a key across versions today, and if anything ever does, this
43 * is the line that breaks it. t/03-key.t pins four values so the change is
44 * caught by a red test rather than by a silent disagreement between a stored
45 * key and a live one.
46 */
47  
48 static unsigned long long ZOB[XQ_BORDER + 1][XQ_CELLS];
49 static unsigned long long ZOB_SIDE;
50 static int ZOB_READY = 0;
51  
52 230921 static unsigned long long splitmix64(unsigned long long *state)
53 {
54 unsigned long long z;
55 230921 *state += 0x9E3779B97F4A7C15ULL;
56 230921 z = *state;
57 230921 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
58 230921 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
59 230921 return z ^ (z >> 31);
60 }
61  
62 128074745 static void zob_init(void)
63 {
64 128074745 unsigned long long s = 0x7869616E67716921ULL; /* "xiangqi!" */
65 int p, c;
66 128074745 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 128074692 53 128074745
67 1802 ▶ for (p = 0; p <= XQ_BORDER; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p <= XQ_BORDER; p++) 1749 53 1802
68 232617 ▶ for (c = 0; c < XQ_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < XQ_CELLS; c++) 230868 1749 232617
69 230868 ZOB[p][c] = splitmix64(&s);
70 53 ZOB_SIDE = splitmix64(&s);
71 53 ZOB_READY = 1;
72 }
73  
74 /* The key covers the pieces, their points, and the side to move, and NOTHING
75 * else. There is no castling, no en passant and no halfmove clock in this game,
76 * so a position repeating is a position repeating, which is what phase 06's
77 * loop detector needs it to mean. */
78 128074212 static unsigned long long zob_of(int piece, int pt)
79 {
80 128074212 zob_init();
81 128074212 ▶ if (piece <= 0 || piece > XQ_BORDER) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (piece <= 0 || piece > XQ_BORDER) return 0; 128074212 0 128074212
if (piece <= 0 || piece > XQ_BORDER) return 0; 0 128074212 128074212
82 128074212 ▶ if (pt < 0 || pt >= XQ_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return 0; 128074212 0 128074212
if (pt < 0 || pt >= XQ_CELLS) return 0; 0 128074212 128074212
83 128074212 return ZOB[piece][pt];
84 }
85  
86 /* ---- geometry -------------------------------------------------------------- */
87  
88 3 static int xq_stride(void) { return XQ_STRIDE; }
89  
90 1259571889 static int xq_point_of(int file, int rank)
91 {
92 1259571889 ▶ if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 1259571889 0 1259571889
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 1259571889 0 1259571889
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 1259571889 0 1259571889
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 0 1259571889 1259571889
93 1259571889 return (rank + 1) * XQ_STRIDE + (file + 1);
94 }
95  
96 6877611016 static int xq_on_board(int pt)
97 {
98 int f, r;
99 6877611016 ▶ if (pt < 0 || pt >= XQ_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return 0; 6756148727 121462289 6877611016
if (pt < 0 || pt >= XQ_CELLS) return 0; 151463763 6604684964 6756148727
100 6604684964 f = pt % XQ_STRIDE;
101 6604684964 r = pt / XQ_STRIDE;
102 6604684964 ▶ return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS;
Branch: 4 branches
BranchTrueFalseTotal
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 6295803213 308881751 6604684964
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5983946516 311856697 6295803213
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5582268564 401677952 5983946516
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5133769766 448498798 5582268564
103 }
104  
105 495891522 static int xq_file_of(int pt)
106 {
107 495891522 ▶ if (!xq_on_board(pt)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return -1; 62739481 433152041 495891522
108 433152041 return (pt % XQ_STRIDE) - 1;
109 }
110  
111 379712055 static int xq_rank_of(int pt)
112 {
113 379712055 ▶ if (!xq_on_board(pt)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return -1; 62739481 316972574 379712055
114 316972574 return (pt / XQ_STRIDE) - 1;
115 }
116  
117 /* The palace is files d to f and the three ranks at that side's own end. Nine
118 * points each, and the general and the advisor never leave them. */
119 164038283 static int xq_in_palace(int pt, int colour)
120 {
121 164038283 int f = xq_file_of(pt), r = xq_rank_of(pt);
122 164038283 ▶ if (f < 3 || f > 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (f < 3 || f > 5) return 0; 88917939 75120344 164038283
if (f < 3 || f > 5) return 0; 13259702 75658237 88917939
123 75658237 ▶ if (colour == XQ_RED) return r >= 0 && r <= 2;
Branch: 3 branches
BranchTrueFalseTotal
if (colour == XQ_RED) return r >= 0 && r <= 2; 38414252 37243985 75658237
if (colour == XQ_RED) return r >= 0 && r <= 2; 38414252 0 38414252
if (colour == XQ_RED) return r >= 0 && r <= 2; 38334556 79696 38414252
124 37243985 ▶ if (colour == XQ_BLACK) return r >= 7 && r <= 9;
Branch: 3 branches
BranchTrueFalseTotal
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 37243985 0 37243985
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 37068944 175041 37243985
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 37068944 0 37068944
125 0 return 0;
126 }
127  
128 /* Red advances up the ranks and Black down them, so "across the river" is rank
129 * 5 or more for Red and rank 4 or less for Black. Forty-five points each. */
130 213593835 static int xq_crossed_river(int pt, int colour)
131 {
132 213593835 int r = xq_rank_of(pt);
133 213593835 ▶ if (r < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) return 0; 0 213593835 213593835
134 213593835 ▶ if (colour == XQ_RED) return r >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if (colour == XQ_RED) return r >= 5; 104241653 109352182 213593835
135 109352182 ▶ if (colour == XQ_BLACK) return r <= 4;
Branch: 1 branch
BranchTrueFalseTotal
if (colour == XQ_BLACK) return r <= 4; 109352182 0 109352182
136 0 return 0;
137 }
138  
139 /* ---- the board ------------------------------------------------------------- */
140  
141 480 static void board_blank(struct xq_board *b)
142 {
143 int f, r;
144 480 memset(b, 0, sizeof(*b));
145 480 memset(b->cell, XQ_BORDER, sizeof(b->cell));
146 5280 ▶ for (r = 0; r < XQ_RANKS; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < XQ_RANKS; r++) 4800 480 5280
147 48000 ▶ for (f = 0; f < XQ_FILES; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) 43200 4800 48000
148 43200 b->cell[xq_point_of(f, r)] = XQ_EMPTY;
149 480 b->side = XQ_RED;
150 480 b->key = 0;
151 480 }
152  
153 480 static struct xq_board *xq_board_empty(void)
154 {
155 struct xq_board *b;
156 480 zob_init();
157 480 b = (struct xq_board *) malloc(sizeof(struct xq_board));
158 480 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 480 480
159 480 board_blank(b);
160 480 return b;
161 }
162  
163 /* put and lift MAINTAIN THE KEY and judge nothing else. A caller may stack
164 * three generals on three points or leave a side without one; that is what the
165 * structure primitives are for and it is how a test builds a position. */
166 122546077 static void xq_put(struct xq_board *b, int pt, int piece)
167 {
168 int old;
169 122546077 ▶ if (!xq_on_board(pt)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return; 0 122546077 122546077
170 122546077 old = b->cell[pt];
171 122546077 ▶ if (old != XQ_EMPTY) b->key ^= zob_of(old, pt);
Branch: 1 branch
BranchTrueFalseTotal
if (old != XQ_EMPTY) b->key ^= zob_of(old, pt); 66795242 55750835 122546077
172 122546077 b->cell[pt] = (unsigned char) piece;
173 122546077 ▶ if (piece != XQ_EMPTY) b->key ^= zob_of(piece, pt);
Branch: 1 branch
BranchTrueFalseTotal
if (piece != XQ_EMPTY) b->key ^= zob_of(piece, pt); 61278964 61267113 122546077
174 }
175  
176 61258801 static void xq_lift(struct xq_board *b, int pt)
177 {
178 61258801 xq_put(b, pt, XQ_EMPTY);
179 61258801 }
180  
181 5737429595 static int xq_at(const struct xq_board *b, int pt)
182 {
183 5737429595 ▶ if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER; 5737429595 0 5737429595
if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER; 0 5737429595 5737429595
184 5737429595 return b->cell[pt];
185 }
186  
187 34093804 static int xq_side(const struct xq_board *b) { return b->side; }
188  
189 76392584 static void xq_set_side(struct xq_board *b, int colour)
190 {
191 76392584 ▶ if (colour != XQ_RED && colour != XQ_BLACK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != XQ_RED && colour != XQ_BLACK) return; 22963482 53429102 76392584
if (colour != XQ_RED && colour != XQ_BLACK) return; 0 22963482 22963482
192 76392584 ▶ if (b->side == colour) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->side == colour) return; 5030918 71361666 76392584
193 71361666 b->side = colour;
194 71361666 b->key ^= ZOB_SIDE;
195 }
196  
197 6 static int xq_count(const struct xq_board *b, int piece)
198 {
199 6 int pt, n = 0;
200 798 ▶ for (pt = 0; pt < XQ_CELLS; pt++)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) 792 6 798
201 792 ▶ if (b->cell[pt] == piece && xq_on_board(pt)) n++;
Branch: 2 branches
BranchTrueFalseTotal
if (b->cell[pt] == piece && xq_on_board(pt)) n++; 13 779 792
if (b->cell[pt] == piece && xq_on_board(pt)) n++; 13 0 13
202 6 return n;
203 }
204  
205 273694235 static int xq_find(const struct xq_board *b, int piece)
206 {
207 int pt;
208 18594518119 ▶ for (pt = 0; pt < XQ_CELLS; pt++)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) 18594481390 36729 18594518119
209 18594481390 ▶ if (b->cell[pt] == piece && xq_on_board(pt)) return pt;
Branch: 2 branches
BranchTrueFalseTotal
if (b->cell[pt] == piece && xq_on_board(pt)) return pt; 273657506 18320823884 18594481390
if (b->cell[pt] == piece && xq_on_board(pt)) return pt; 273657506 0 273657506
210 36729 return 0;
211 }
212  
213 /* The opening position, from the Asian Rules chapter 1 section 1. Rank 0 is
214 * Red's back rank. */
215 static const int BACK[XQ_FILES] = {
216 XQ_CHARIOT, XQ_HORSE, XQ_ELEPHANT, XQ_ADVISOR, XQ_GENERAL,
217 XQ_ADVISOR, XQ_ELEPHANT, XQ_HORSE, XQ_CHARIOT
218 };
219  
220 216 static struct xq_board *xq_board_new(void)
221 {
222 216 struct xq_board *b = xq_board_empty();
223 int f;
224 216 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 216 216
225 2160 ▶ for (f = 0; f < XQ_FILES; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) { 1944 216 2160
226 1944 xq_put(b, xq_point_of(f, 0), (int) (XQ_RED | BACK[f]));
227 1944 xq_put(b, xq_point_of(f, 9), (int) (XQ_BLACK | BACK[f]));
228 }
229 216 xq_put(b, xq_point_of(1, 2), XQ_RED | XQ_CANNON);
230 216 xq_put(b, xq_point_of(7, 2), XQ_RED | XQ_CANNON);
231 216 xq_put(b, xq_point_of(1, 7), XQ_BLACK | XQ_CANNON);
232 216 xq_put(b, xq_point_of(7, 7), XQ_BLACK | XQ_CANNON);
233 1296 ▶ for (f = 0; f < XQ_FILES; f += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f += 2) { 1080 216 1296
234 1080 xq_put(b, xq_point_of(f, 3), XQ_RED | XQ_SOLDIER);
235 1080 xq_put(b, xq_point_of(f, 6), XQ_BLACK | XQ_SOLDIER);
236 }
237 216 return b;
238 }
239  
240 781 static struct xq_board *xq_board_copy(const struct xq_board *b)
241 {
242 781 struct xq_board *n = (struct xq_board *) malloc(sizeof(struct xq_board));
243 781 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 781 781
244 781 *n = *b;
245 781 return n;
246 }
247  
248 1261 static void xq_board_drop(struct xq_board *b) { free(b); }
249  
250 /* ---- moves ------------------------------------------------------------------
251 *
252 * from and to each fit in eight bits (a point is 0 to 131), so a move is packed
253 * into sixteen and the upper bits are left for the flags phases 03 and beyond
254 * will want. Nothing outside the header unpacks one by hand.
255 */
256  
257 516013457 static int xq_move_make(int from, int to) { return (from & 0xFF) | ((to & 0xFF) << 8); }
258 224116667 static int xq_move_from(int mv) { return mv & 0xFF; }
259 277309014 static int xq_move_to(int mv) { return (mv >> 8) & 0xFF; }
260  
261 /* NOT A RULES CALL. It moves whatever is on `from` to `to`, captures whatever
262 * was there, flips the side and maintains the key. Whether the move was legal
263 * is phase 03's question and this function never asks it. */
264 61249466 static int xq_do_move(struct xq_board *b, int mv, struct xq_undo *u)
265 {
266 61249466 int from = xq_move_from(mv), to = xq_move_to(mv);
267 int piece, captured;
268  
269 61249466 ▶ if (!xq_on_board(from) || !xq_on_board(to)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!xq_on_board(from) || !xq_on_board(to)) { 61249466 0 61249466
if (!xq_on_board(from) || !xq_on_board(to)) { 0 61249466 61249466
270 0 ▶ if (u) { u->mv = 0; u->captured = XQ_EMPTY; u->side = b->side; u->key = b->key; }
Branch: 1 branch
BranchTrueFalseTotal
if (u) { u->mv = 0; u->captured = XQ_EMPTY; u->side = b->side; u->key = b->key; } 0 0 0
271 0 return XQ_EMPTY;
272 }
273 61249466 piece = b->cell[from];
274 61249466 captured = b->cell[to];
275  
276 61249466 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 61249466 0 61249466
277 61249466 u->mv = mv;
278 61249466 u->captured = captured;
279 61249466 u->side = b->side;
280 61249466 u->key = b->key;
281 }
282 61249466 xq_lift(b, from);
283 61249466 xq_put(b, to, piece);
284 61249466 ▶ xq_set_side(b, XQ_OTHER(b->side));
Branch: 1 branch
BranchTrueFalseTotal
xq_set_side(b, XQ_OTHER(b->side)); 15672168 45577298 61249466
285 61249466 return captured;
286 }
287  
288 /* Restores the position EXACTLY, key included, by putting the key back rather
289 * than by xor-ing the same values again. The two are equal when nothing else
290 * touched the board, and only one of them stays true if anything did. */
291 61241879 static void xq_undo_move(struct xq_board *b, const struct xq_undo *u)
292 {
293 int from, to;
294 61241879 ▶ if (!u || u->mv == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!u || u->mv == 0) return; 61241879 0 61241879
if (!u || u->mv == 0) return; 0 61241879 61241879
295 61241879 from = xq_move_from(u->mv);
296 61241879 to = xq_move_to(u->mv);
297 61241879 b->cell[from] = b->cell[to];
298 61241879 b->cell[to] = (unsigned char) u->captured;
299 61241879 b->side = u->side;
300 61241879 b->key = u->key;
301 }
302  
303 2839366 static unsigned long long xq_key(const struct xq_board *b) { return b->key; }
304  
305 /* ---- FEN --------------------------------------------------------------------
306 *
307 * THE READER TAKES BOTH LETTER CONVENTIONS AND THE WRITER EMITS h/e.
308 *
309 * The same opening position is published both ways by the same author: the
310 * Chess Programming Wiki's Chinese Chess perft page spells the horse h and the
311 * elephant e, and Maksim Korzh's TalkChess post of the same table spells them n
312 * and b after chess's knight and bishop. A reader that takes one convention
313 * cannot load half the oracles there are, and the failure is the bad kind: the
314 * position does not parse, the test skips, and the suite is green.
315 */
316  
317 2333 static int letter_piece(char c)
318 {
319 2333 ▶ int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED;
Branch: 2 branches
BranchTrueFalseTotal
int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED; 1182 1151 2333
int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED; 1182 0 1182
320 2333 ▶ char u = (c >= 'a' && c <= 'z') ? (char) (c - 'a' + 'A') : c;
Branch: 2 branches
BranchTrueFalseTotal
char u = (c >= 'a' && c <= 'z') ? (char) (c - 'a' + 'A') : c; 1182 1151 2333
char u = (c >= 'a' && c <= 'z') ? (char) (c - 'a' + 'A') : c; 1182 0 1182
321 2333 switch (u) {
322 202 case 'K': return colour | XQ_GENERAL;
323 314 case 'A': return colour | XQ_ADVISOR;
324 296 case 'E': case 'B': return colour | XQ_ELEPHANT;
325 295 case 'R': return colour | XQ_CHARIOT;
326 292 case 'H': case 'N': return colour | XQ_HORSE;
327 285 case 'C': return colour | XQ_CANNON;
328 643 case 'P': return colour | XQ_SOLDIER;
329 6 default: return -1;
330 }
331 }
332  
333 6822 static char piece_letter(int piece)
334 {
335 static const char up[8] = { '.', 'K', 'A', 'E', 'R', 'H', 'C', 'P' };
336 6822 char c = up[XQ_KIND(piece)];
337 6822 ▶ if (XQ_COLOUR(piece) == XQ_BLACK) c = (char) (c - 'A' + 'a');
Branch: 1 branch
BranchTrueFalseTotal
if (XQ_COLOUR(piece) == XQ_BLACK) c = (char) (c - 'A' + 'a'); 3403 3419 6822
338 6822 return c;
339 }
340  
341 111 static struct xq_board *xq_board_of_fen(const char *fen, int *err)
342 {
343 struct xq_board *b;
344 111 int rank = XQ_RANKS - 1, file = 0;
345 111 const char *s = fen;
346 size_t n;
347  
348 111 ▶ if (err) *err = XQ_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = XQ_OK; 111 0 111
349 111 ▶ if (!fen) { if (err) *err = XQ_FEN_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!fen) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 111 111
if (!fen) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 0 0
350 111 n = strlen(fen);
351 111 ▶ if (n == 0) { if (err) *err = XQ_FEN_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0) { if (err) *err = XQ_FEN_NULL; return NULL; } 1 110 111
if (n == 0) { if (err) *err = XQ_FEN_NULL; return NULL; } 1 0 1
352 110 ▶ if (n >= XQ_FEN_MAX) { if (err) *err = XQ_FEN_LONG; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n >= XQ_FEN_MAX) { if (err) *err = XQ_FEN_LONG; return NULL; } 1 109 110
if (n >= XQ_FEN_MAX) { if (err) *err = XQ_FEN_LONG; return NULL; } 1 0 1
353  
354 109 b = xq_board_empty();
355 109 ▶ if (!b) { if (err) *err = XQ_FEN_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!b) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 109 109
if (!b) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 0 0
356  
357 5097 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 5094 3 5097
for (; *s && *s != ' '; s++) { 4997 97 5094
358 4997 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 903 4094 4997
359 903 ▶ if (file != XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (file != XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 902 903
if (file != XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 0 1
360 902 ▶ if (rank == 0) { if (err) *err = XQ_FEN_ROWS; xq_board_drop(b); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (rank == 0) { if (err) *err = XQ_FEN_ROWS; xq_board_drop(b); return NULL; } 1 901 902
if (rank == 0) { if (err) *err = XQ_FEN_ROWS; xq_board_drop(b); return NULL; } 1 0 1
361 901 rank--; file = 0;
362 901 continue;
363 }
364 4094 ▶ if (*s >= '1' && *s <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '9') { 4094 0 4094
if (*s >= '1' && *s <= '9') { 1761 2333 4094
365 1761 file += *s - '0';
366 1761 ▶ if (file > XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (file > XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 0 1761 1761
if (file > XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 0 0 0
367 1761 continue;
368 }
369 {
370 2333 int piece = letter_piece(*s);
371 2333 ▶ if (piece < 0) { if (err) *err = XQ_FEN_LETTER; xq_board_drop(b); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (piece < 0) { if (err) *err = XQ_FEN_LETTER; xq_board_drop(b); return NULL; } 6 2327 2333
if (piece < 0) { if (err) *err = XQ_FEN_LETTER; xq_board_drop(b); return NULL; } 6 0 6
372 2327 ▶ if (file >= XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (file >= XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 2326 2327
if (file >= XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 0 1
373 2326 xq_put(b, xq_point_of(file, rank), piece);
374 2326 file++;
375 }
376 }
377 100 ▶ if (rank != 0 || file != XQ_FILES) {
Branch: 2 branches
BranchTrueFalseTotal
if (rank != 0 || file != XQ_FILES) { 98 2 100
if (rank != 0 || file != XQ_FILES) { 0 98 98
378 2 ▶ if (err) *err = (rank != 0) ? XQ_FEN_ROWS : XQ_FEN_WIDTH;
Branch: 2 branches
BranchTrueFalseTotal
if (err) *err = (rank != 0) ? XQ_FEN_ROWS : XQ_FEN_WIDTH; 2 0 2
if (err) *err = (rank != 0) ? XQ_FEN_ROWS : XQ_FEN_WIDTH; 2 0 2
379 2 xq_board_drop(b);
380 2 return NULL;
381 }
382  
383 /* the side field. w and r both mean Red: the perft sources write w, and
384 * much of the xiangqi world writes r. Anything else is a refusal rather
385 * than a silent default, because defaulting here would make a typo into a
386 * position with the wrong player to move, which is a different position. */
387 195 ▶ while (*s == ' ') s++;
Branch: 1 branch
BranchTrueFalseTotal
while (*s == ' ') s++; 97 98 195
388 98 ▶ if (*s) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s) { 97 1 98
389 97 ▶ if (*s == 'w' || *s == 'W' || *s == 'r' || *s == 'R') xq_set_side(b, XQ_RED);
Branch: 4 branches
BranchTrueFalseTotal
if (*s == 'w' || *s == 'W' || *s == 'r' || *s == 'R') xq_set_side(b, XQ_RED); 12 85 97
if (*s == 'w' || *s == 'W' || *s == 'r' || *s == 'R') xq_set_side(b, XQ_RED); 12 0 12
if (*s == 'w' || *s == 'W' || *s == 'r' || *s == 'R') xq_set_side(b, XQ_RED); 11 1 12
if (*s == 'w' || *s == 'W' || *s == 'r' || *s == 'R') xq_set_side(b, XQ_RED); 0 11 11
390 11 ▶ else if (*s == 'b' || *s == 'B') xq_set_side(b, XQ_BLACK);
Branch: 2 branches
BranchTrueFalseTotal
else if (*s == 'b' || *s == 'B') xq_set_side(b, XQ_BLACK); 1 10 11
else if (*s == 'b' || *s == 'B') xq_set_side(b, XQ_BLACK); 0 1 1
391 1 ▶ else { if (err) *err = XQ_FEN_SIDE; xq_board_drop(b); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
else { if (err) *err = XQ_FEN_SIDE; xq_board_drop(b); return NULL; } 1 0 1
392 }
393 /* every field after the side is ignored: this game has no castling, no en
394 * passant, and the two clocks are the caller's business */
395 97 return b;
396 }
397  
398 224 static int xq_to_fen(const struct xq_board *b, char *buf, int len)
399 {
400 224 int rank, file, i = 0, run;
401 224 ▶ if (!buf || len < XQ_FEN_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || len < XQ_FEN_MAX) return -1; 224 0 224
if (!buf || len < XQ_FEN_MAX) return -1; 0 224 224
402 2464 ▶ for (rank = XQ_RANKS - 1; rank >= 0; rank--) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = XQ_RANKS - 1; rank >= 0; rank--) { 2240 224 2464
403 2240 run = 0;
404 22400 ▶ for (file = 0; file < XQ_FILES; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < XQ_FILES; file++) { 20160 2240 22400
405 20160 int piece = b->cell[xq_point_of(file, rank)];
406 20160 ▶ if (piece == XQ_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (piece == XQ_EMPTY) { run++; continue; } 13338 6822 20160
407 6822 ▶ if (run) { buf[i++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { buf[i++] = (char) ('0' + run); run = 0; } 2607 4215 6822
408 6822 buf[i++] = piece_letter(piece);
409 }
410 2240 ▶ if (run) buf[i++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) buf[i++] = (char) ('0' + run); 1406 834 2240
411 2240 ▶ if (rank) buf[i++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (rank) buf[i++] = '/'; 2016 224 2240
412 }
413 224 buf[i++] = ' ';
414 224 ▶ buf[i++] = (b->side == XQ_BLACK) ? 'b' : 'w';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == XQ_BLACK) ? 'b' : 'w'; 8 216 224
415 224 buf[i++] = ' '; buf[i++] = '-';
416 224 buf[i++] = ' '; buf[i++] = '-';
417 224 buf[i++] = ' '; buf[i++] = '0';
418 224 buf[i++] = ' '; buf[i++] = '1';
419 224 buf[i] = '\0';
420 224 return i;
421 }
422  
423 /* ---- the table -------------------------------------------------------------- */
424  
425 static struct xq_abi XQ_TABLE;
426 static int XQ_TABLE_READY = 0;
427  
428 /* Phase 03 lives in xq_moves.c and installs itself into the table here.
429 *
430 * The two files call into each other and it terminates: xq_abi_table sets
431 * XQ_TABLE_READY before calling the installer, and xq_moves.c only ever reaches
432 * xq_abi_table from inside a function the installer has already been handed.
433 * Game::Go's search does the same dance and its comment says the same thing. */
434 void xq_moves_install(struct xq_abi *t);
435 void xq_mate_install(struct xq_abi *t);
436 void xq_judge_install(struct xq_abi *t);
437 void xq_judge2_install(struct xq_abi *t);
438 void xq_search_install(struct xq_abi *t);
439  
440 14574355235 const struct xq_abi *xq_abi_table(void)
441 {
442 14574355235 ▶ if (!XQ_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!XQ_TABLE_READY) { 53 14574355182 14574355235
443 53 zob_init();
444 53 XQ_TABLE.abi_version = XQ_ABI_VERSION;
445  
446 53 XQ_TABLE.board_new = xq_board_new;
447 53 XQ_TABLE.board_empty = xq_board_empty;
448 53 XQ_TABLE.board_of_fen = xq_board_of_fen;
449 53 XQ_TABLE.board_copy = xq_board_copy;
450 53 XQ_TABLE.board_drop = xq_board_drop;
451  
452 53 XQ_TABLE.at = xq_at;
453 53 XQ_TABLE.put = xq_put;
454 53 XQ_TABLE.lift = xq_lift;
455 53 XQ_TABLE.side = xq_side;
456 53 XQ_TABLE.set_side = xq_set_side;
457 53 XQ_TABLE.count = xq_count;
458 53 XQ_TABLE.find = xq_find;
459  
460 53 XQ_TABLE.move_make = xq_move_make;
461 53 XQ_TABLE.move_from = xq_move_from;
462 53 XQ_TABLE.move_to = xq_move_to;
463 53 XQ_TABLE.do_move = xq_do_move;
464 53 XQ_TABLE.undo_move = xq_undo_move;
465  
466 53 XQ_TABLE.key = xq_key;
467 53 XQ_TABLE.to_fen = xq_to_fen;
468 53 XQ_TABLE.zobrist = zob_of;
469  
470 53 XQ_TABLE.point_of = xq_point_of;
471 53 XQ_TABLE.file_of = xq_file_of;
472 53 XQ_TABLE.rank_of = xq_rank_of;
473 53 XQ_TABLE.on_board = xq_on_board;
474 53 XQ_TABLE.in_palace = xq_in_palace;
475 53 XQ_TABLE.crossed_river = xq_crossed_river;
476 53 XQ_TABLE.stride = xq_stride;
477  
478 53 XQ_TABLE_READY = 1; /* before the installer, or it recurses */
479 53 xq_moves_install(&XQ_TABLE);
480 53 xq_mate_install(&XQ_TABLE);
481 53 xq_judge_install(&XQ_TABLE);
482 53 xq_judge2_install(&XQ_TABLE);
483 53 xq_search_install(&XQ_TABLE);
484 }
485 14574355235 return &XQ_TABLE;
486 }
« blib/lib/Game/Xiangqi/Error.pm ↑ Summary Xiangqi.xs »