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 121996 static unsigned long long splitmix64(unsigned long long *state)
53 {
54 unsigned long long z;
55 121996 *state += 0x9E3779B97F4A7C15ULL;
56 121996 z = *state;
57 121996 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
58 121996 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
59 121996 return z ^ (z >> 31);
60 }
61  
62 127490201 static void zob_init(void)
63 {
64 127490201 unsigned long long s = 0x7869616E67716921ULL; /* "xiangqi!" */
65 int p, c;
66 127490201 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 127490173 28 127490201
67 952 ▶ for (p = 0; p <= XQ_BORDER; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p <= XQ_BORDER; p++) 924 28 952
68 122892 ▶ for (c = 0; c < XQ_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < XQ_CELLS; c++) 121968 924 122892
69 121968 ZOB[p][c] = splitmix64(&s);
70 28 ZOB_SIDE = splitmix64(&s);
71 28 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 127489713 static unsigned long long zob_of(int piece, int pt)
79 {
80 127489713 zob_init();
81 127489713 ▶ if (piece <= 0 || piece > XQ_BORDER) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (piece <= 0 || piece > XQ_BORDER) return 0; 127489713 0 127489713
if (piece <= 0 || piece > XQ_BORDER) return 0; 0 127489713 127489713
82 127489713 ▶ if (pt < 0 || pt >= XQ_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return 0; 127489713 0 127489713
if (pt < 0 || pt >= XQ_CELLS) return 0; 0 127489713 127489713
83 127489713 return ZOB[piece][pt];
84 }
85  
86 /* ---- geometry -------------------------------------------------------------- */
87  
88 3 static int xq_stride(void) { return XQ_STRIDE; }
89  
90 1256985623 static int xq_point_of(int file, int rank)
91 {
92 1256985623 ▶ 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; 1256985623 0 1256985623
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 1256985623 0 1256985623
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 1256985623 0 1256985623
if (file < 0 || file >= XQ_FILES || rank < 0 || rank >= XQ_RANKS) return -1; 0 1256985623 1256985623
93 1256985623 return (rank + 1) * XQ_STRIDE + (file + 1);
94 }
95  
96 6858078490 static int xq_on_board(int pt)
97 {
98 int f, r;
99 6858078490 ▶ if (pt < 0 || pt >= XQ_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return 0; 6737148097 120930393 6858078490
if (pt < 0 || pt >= XQ_CELLS) return 0; 150729035 6586419062 6737148097
100 6586419062 f = pt % XQ_STRIDE;
101 6586419062 r = pt / XQ_STRIDE;
102 6586419062 ▶ 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; 6278041127 308377935 6586419062
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5966702316 311338811 6278041127
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5566090542 400611774 5966702316
return f >= 1 && f <= XQ_FILES && r >= 1 && r <= XQ_RANKS; 5118908642 447181900 5566090542
103 }
104  
105 494245814 static int xq_file_of(int pt)
106 {
107 494245814 ▶ if (!xq_on_board(pt)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return -1; 62601868 431643946 494245814
108 431643946 return (pt % XQ_STRIDE) - 1;
109 }
110  
111 378924533 static int xq_rank_of(int pt)
112 {
113 378924533 ▶ if (!xq_on_board(pt)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return -1; 62601868 316322665 378924533
114 316322665 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 163702996 static int xq_in_palace(int pt, int colour)
120 {
121 163702996 int f = xq_file_of(pt), r = xq_rank_of(pt);
122 163702996 ▶ if (f < 3 || f > 5) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (f < 3 || f > 5) return 0; 88747533 74955463 163702996
if (f < 3 || f > 5) return 0; 13231609 75515924 88747533
123 75515924 ▶ if (colour == XQ_RED) return r >= 0 && r <= 2;
Branch: 3 branches
BranchTrueFalseTotal
if (colour == XQ_RED) return r >= 0 && r <= 2; 38344893 37171031 75515924
if (colour == XQ_RED) return r >= 0 && r <= 2; 38344893 0 38344893
if (colour == XQ_RED) return r >= 0 && r <= 2; 38265197 79696 38344893
124 37171031 ▶ if (colour == XQ_BLACK) return r >= 7 && r <= 9;
Branch: 3 branches
BranchTrueFalseTotal
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 37171031 0 37171031
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 36995990 175041 37171031
if (colour == XQ_BLACK) return r >= 7 && r <= 9; 36995990 0 36995990
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 213141986 static int xq_crossed_river(int pt, int colour)
131 {
132 213141986 int r = xq_rank_of(pt);
133 213141986 ▶ if (r < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r < 0) return 0; 0 213141986 213141986
134 213141986 ▶ if (colour == XQ_RED) return r >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if (colour == XQ_RED) return r >= 5; 104013585 109128401 213141986
135 109128401 ▶ if (colour == XQ_BLACK) return r <= 4;
Branch: 1 branch
BranchTrueFalseTotal
if (colour == XQ_BLACK) return r <= 4; 109128401 0 109128401
136 0 return 0;
137 }
138  
139 /* ---- the board ------------------------------------------------------------- */
140  
141 460 static void board_blank(struct xq_board *b)
142 {
143 int f, r;
144 460 memset(b, 0, sizeof(*b));
145 460 memset(b->cell, XQ_BORDER, sizeof(b->cell));
146 5060 ▶ for (r = 0; r < XQ_RANKS; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < XQ_RANKS; r++) 4600 460 5060
147 46000 ▶ for (f = 0; f < XQ_FILES; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) 41400 4600 46000
148 41400 b->cell[xq_point_of(f, r)] = XQ_EMPTY;
149 460 b->side = XQ_RED;
150 460 b->key = 0;
151 460 }
152  
153 460 static struct xq_board *xq_board_empty(void)
154 {
155 struct xq_board *b;
156 460 zob_init();
157 460 b = (struct xq_board *) malloc(sizeof(struct xq_board));
158 460 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 460 460
159 460 board_blank(b);
160 460 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 121977819 static void xq_put(struct xq_board *b, int pt, int piece)
167 {
168 int old;
169 121977819 ▶ if (!xq_on_board(pt)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!xq_on_board(pt)) return; 0 121977819 121977819
170 121977819 old = b->cell[pt];
171 121977819 ▶ if (old != XQ_EMPTY) b->key ^= zob_of(old, pt);
Branch: 1 branch
BranchTrueFalseTotal
if (old != XQ_EMPTY) b->key ^= zob_of(old, pt); 66495204 55482615 121977819
172 121977819 b->cell[pt] = (unsigned char) piece;
173 121977819 ▶ if (piece != XQ_EMPTY) b->key ^= zob_of(piece, pt);
Branch: 1 branch
BranchTrueFalseTotal
if (piece != XQ_EMPTY) b->key ^= zob_of(piece, pt); 60994503 60983316 121977819
174 }
175  
176 60975090 static void xq_lift(struct xq_board *b, int pt)
177 {
178 60975090 xq_put(b, pt, XQ_EMPTY);
179 60975090 }
180  
181 5722839343 static int xq_at(const struct xq_board *b, int pt)
182 {
183 5722839343 ▶ if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER;
Branch: 2 branches
BranchTrueFalseTotal
if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER; 5722839343 0 5722839343
if (pt < 0 || pt >= XQ_CELLS) return XQ_BORDER; 0 5722839343 5722839343
184 5722839343 return b->cell[pt];
185 }
186  
187 34023939 static int xq_side(const struct xq_board *b) { return b->side; }
188  
189 76085152 static void xq_set_side(struct xq_board *b, int colour)
190 {
191 76085152 ▶ if (colour != XQ_RED && colour != XQ_BLACK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (colour != XQ_RED && colour != XQ_BLACK) return; 22833041 53252111 76085152
if (colour != XQ_RED && colour != XQ_BLACK) return; 0 22833041 22833041
192 76085152 ▶ if (b->side == colour) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->side == colour) return; 5023165 71061987 76085152
193 71061987 b->side = colour;
194 71061987 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 272321883 static int xq_find(const struct xq_board *b, int piece)
206 {
207 int pt;
208 18500924809 ▶ for (pt = 0; pt < XQ_CELLS; pt++)
Branch: 1 branch
BranchTrueFalseTotal
for (pt = 0; pt < XQ_CELLS; pt++) 18500888080 36729 18500924809
209 18500888080 ▶ 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; 272285154 18228602926 18500888080
if (b->cell[pt] == piece && xq_on_board(pt)) return pt; 272285154 0 272285154
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 207 static struct xq_board *xq_board_new(void)
221 {
222 207 struct xq_board *b = xq_board_empty();
223 int f;
224 207 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 207 207
225 2070 ▶ for (f = 0; f < XQ_FILES; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f++) { 1863 207 2070
226 1863 xq_put(b, xq_point_of(f, 0), (int) (XQ_RED | BACK[f]));
227 1863 xq_put(b, xq_point_of(f, 9), (int) (XQ_BLACK | BACK[f]));
228 }
229 207 xq_put(b, xq_point_of(1, 2), XQ_RED | XQ_CANNON);
230 207 xq_put(b, xq_point_of(7, 2), XQ_RED | XQ_CANNON);
231 207 xq_put(b, xq_point_of(1, 7), XQ_BLACK | XQ_CANNON);
232 207 xq_put(b, xq_point_of(7, 7), XQ_BLACK | XQ_CANNON);
233 1242 ▶ for (f = 0; f < XQ_FILES; f += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < XQ_FILES; f += 2) { 1035 207 1242
234 1035 xq_put(b, xq_point_of(f, 3), XQ_RED | XQ_SOLDIER);
235 1035 xq_put(b, xq_point_of(f, 6), XQ_BLACK | XQ_SOLDIER);
236 }
237 207 return b;
238 }
239  
240 719 static struct xq_board *xq_board_copy(const struct xq_board *b)
241 {
242 719 struct xq_board *n = (struct xq_board *) malloc(sizeof(struct xq_board));
243 719 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 719 719
244 719 *n = *b;
245 719 return n;
246 }
247  
248 1179 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 514895105 static int xq_move_make(int from, int to) { return (from & 0xFF) | ((to & 0xFF) << 8); }
258 223306391 static int xq_move_from(int mv) { return mv & 0xFF; }
259 276367877 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 60965853 static int xq_do_move(struct xq_board *b, int mv, struct xq_undo *u)
265 {
266 60965853 int from = xq_move_from(mv), to = xq_move_to(mv);
267 int piece, captured;
268  
269 60965853 ▶ if (!xq_on_board(from) || !xq_on_board(to)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!xq_on_board(from) || !xq_on_board(to)) { 60965853 0 60965853
if (!xq_on_board(from) || !xq_on_board(to)) { 0 60965853 60965853
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 60965853 piece = b->cell[from];
274 60965853 captured = b->cell[to];
275  
276 60965853 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 60965853 0 60965853
277 60965853 u->mv = mv;
278 60965853 u->captured = captured;
279 60965853 u->side = b->side;
280 60965853 u->key = b->key;
281 }
282 60965853 xq_lift(b, from);
283 60965853 xq_put(b, to, piece);
284 60965853 ▶ xq_set_side(b, XQ_OTHER(b->side));
Branch: 1 branch
BranchTrueFalseTotal
xq_set_side(b, XQ_OTHER(b->side)); 15553847 45412006 60965853
285 60965853 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 60958402 static void xq_undo_move(struct xq_board *b, const struct xq_undo *u)
292 {
293 int from, to;
294 60958402 ▶ if (!u || u->mv == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!u || u->mv == 0) return; 60958402 0 60958402
if (!u || u->mv == 0) return; 0 60958402 60958402
295 60958402 from = xq_move_from(u->mv);
296 60958402 to = xq_move_to(u->mv);
297 60958402 b->cell[from] = b->cell[to];
298 60958402 b->cell[to] = (unsigned char) u->captured;
299 60958402 b->side = u->side;
300 60958402 b->key = u->key;
301 }
302  
303 2835726 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 1981 static int letter_piece(char c)
318 {
319 1981 ▶ int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED;
Branch: 2 branches
BranchTrueFalseTotal
int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED; 1006 975 1981
int colour = (c >= 'a' && c <= 'z') ? XQ_BLACK : XQ_RED; 1006 0 1006
320 1981 ▶ 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; 1006 975 1981
char u = (c >= 'a' && c <= 'z') ? (char) (c - 'a' + 'A') : c; 1006 0 1006
321 1981 switch (u) {
322 180 case 'K': return colour | XQ_GENERAL;
323 270 case 'A': return colour | XQ_ADVISOR;
324 252 case 'E': case 'B': return colour | XQ_ELEPHANT;
325 251 case 'R': return colour | XQ_CHARIOT;
326 248 case 'H': case 'N': return colour | XQ_HORSE;
327 241 case 'C': return colour | XQ_CANNON;
328 533 case 'P': return colour | XQ_SOLDIER;
329 6 default: return -1;
330 }
331 }
332  
333 4710 static char piece_letter(int piece)
334 {
335 static const char up[8] = { '.', 'K', 'A', 'E', 'R', 'H', 'C', 'P' };
336 4710 char c = up[XQ_KIND(piece)];
337 4710 ▶ 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'); 2347 2363 4710
338 4710 return c;
339 }
340  
341 100 static struct xq_board *xq_board_of_fen(const char *fen, int *err)
342 {
343 struct xq_board *b;
344 100 int rank = XQ_RANKS - 1, file = 0;
345 100 const char *s = fen;
346 size_t n;
347  
348 100 ▶ if (err) *err = XQ_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = XQ_OK; 100 0 100
349 100 ▶ 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 100 100
if (!fen) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 0 0
350 100 n = strlen(fen);
351 100 ▶ 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 99 100
if (n == 0) { if (err) *err = XQ_FEN_NULL; return NULL; } 1 0 1
352 99 ▶ 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 98 99
if (n >= XQ_FEN_MAX) { if (err) *err = XQ_FEN_LONG; return NULL; } 1 0 1
353  
354 98 b = xq_board_empty();
355 98 ▶ 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 98 98
if (!b) { if (err) *err = XQ_FEN_NULL; return NULL; } 0 0 0
356  
357 4437 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 4434 3 4437
for (; *s && *s != ' '; s++) { 4348 86 4434
358 4348 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 804 3544 4348
359 804 ▶ 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 803 804
if (file != XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 0 1
360 803 ▶ 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 802 803
if (rank == 0) { if (err) *err = XQ_FEN_ROWS; xq_board_drop(b); return NULL; } 1 0 1
361 802 rank--; file = 0;
362 802 continue;
363 }
364 3544 ▶ if (*s >= '1' && *s <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '9') { 3544 0 3544
if (*s >= '1' && *s <= '9') { 1563 1981 3544
365 1563 file += *s - '0';
366 1563 ▶ 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 1563 1563
if (file > XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 0 0 0
367 1563 continue;
368 }
369 {
370 1981 int piece = letter_piece(*s);
371 1981 ▶ 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 1975 1981
if (piece < 0) { if (err) *err = XQ_FEN_LETTER; xq_board_drop(b); return NULL; } 6 0 6
372 1975 ▶ 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 1974 1975
if (file >= XQ_FILES) { if (err) *err = XQ_FEN_WIDTH; xq_board_drop(b); return NULL; } 1 0 1
373 1974 xq_put(b, xq_point_of(file, rank), piece);
374 1974 file++;
375 }
376 }
377 89 ▶ if (rank != 0 || file != XQ_FILES) {
Branch: 2 branches
BranchTrueFalseTotal
if (rank != 0 || file != XQ_FILES) { 87 2 89
if (rank != 0 || file != XQ_FILES) { 0 87 87
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 173 ▶ while (*s == ' ') s++;
Branch: 1 branch
BranchTrueFalseTotal
while (*s == ' ') s++; 86 87 173
388 87 ▶ if (*s) {
Branch: 1 branch
BranchTrueFalseTotal
if (*s) { 86 1 87
389 86 ▶ 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 74 86
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 86 return b;
396 }
397  
398 158 static int xq_to_fen(const struct xq_board *b, char *buf, int len)
399 {
400 158 int rank, file, i = 0, run;
401 158 ▶ if (!buf || len < XQ_FEN_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!buf || len < XQ_FEN_MAX) return -1; 158 0 158
if (!buf || len < XQ_FEN_MAX) return -1; 0 158 158
402 1738 ▶ for (rank = XQ_RANKS - 1; rank >= 0; rank--) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = XQ_RANKS - 1; rank >= 0; rank--) { 1580 158 1738
403 1580 run = 0;
404 15800 ▶ for (file = 0; file < XQ_FILES; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < XQ_FILES; file++) { 14220 1580 15800
405 14220 int piece = b->cell[xq_point_of(file, rank)];
406 14220 ▶ if (piece == XQ_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (piece == XQ_EMPTY) { run++; continue; } 9510 4710 14220
407 4710 ▶ if (run) { buf[i++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { buf[i++] = (char) ('0' + run); run = 0; } 1815 2895 4710
408 4710 buf[i++] = piece_letter(piece);
409 }
410 1580 ▶ if (run) buf[i++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) buf[i++] = (char) ('0' + run); 1010 570 1580
411 1580 ▶ if (rank) buf[i++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (rank) buf[i++] = '/'; 1422 158 1580
412 }
413 158 buf[i++] = ' ';
414 158 ▶ buf[i++] = (b->side == XQ_BLACK) ? 'b' : 'w';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == XQ_BLACK) ? 'b' : 'w'; 8 150 158
415 158 buf[i++] = ' '; buf[i++] = '-';
416 158 buf[i++] = ' '; buf[i++] = '-';
417 158 buf[i++] = ' '; buf[i++] = '0';
418 158 buf[i++] = ' '; buf[i++] = '1';
419 158 buf[i] = '\0';
420 158 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 14536447049 const struct xq_abi *xq_abi_table(void)
441 {
442 14536447049 ▶ if (!XQ_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!XQ_TABLE_READY) { 28 14536447021 14536447049
443 28 zob_init();
444 28 XQ_TABLE.abi_version = XQ_ABI_VERSION;
445  
446 28 XQ_TABLE.board_new = xq_board_new;
447 28 XQ_TABLE.board_empty = xq_board_empty;
448 28 XQ_TABLE.board_of_fen = xq_board_of_fen;
449 28 XQ_TABLE.board_copy = xq_board_copy;
450 28 XQ_TABLE.board_drop = xq_board_drop;
451  
452 28 XQ_TABLE.at = xq_at;
453 28 XQ_TABLE.put = xq_put;
454 28 XQ_TABLE.lift = xq_lift;
455 28 XQ_TABLE.side = xq_side;
456 28 XQ_TABLE.set_side = xq_set_side;
457 28 XQ_TABLE.count = xq_count;
458 28 XQ_TABLE.find = xq_find;
459  
460 28 XQ_TABLE.move_make = xq_move_make;
461 28 XQ_TABLE.move_from = xq_move_from;
462 28 XQ_TABLE.move_to = xq_move_to;
463 28 XQ_TABLE.do_move = xq_do_move;
464 28 XQ_TABLE.undo_move = xq_undo_move;
465  
466 28 XQ_TABLE.key = xq_key;
467 28 XQ_TABLE.to_fen = xq_to_fen;
468 28 XQ_TABLE.zobrist = zob_of;
469  
470 28 XQ_TABLE.point_of = xq_point_of;
471 28 XQ_TABLE.file_of = xq_file_of;
472 28 XQ_TABLE.rank_of = xq_rank_of;
473 28 XQ_TABLE.on_board = xq_on_board;
474 28 XQ_TABLE.in_palace = xq_in_palace;
475 28 XQ_TABLE.crossed_river = xq_crossed_river;
476 28 XQ_TABLE.stride = xq_stride;
477  
478 28 XQ_TABLE_READY = 1; /* before the installer, or it recurses */
479 28 xq_moves_install(&XQ_TABLE);
480 28 xq_mate_install(&XQ_TABLE);
481 28 xq_judge_install(&XQ_TABLE);
482 28 xq_judge2_install(&XQ_TABLE);
483 28 xq_search_install(&XQ_TABLE);
484 }
485 14536447049 return &XQ_TABLE;
486 }
« blib/lib/Game/Xiangqi/Error.pm ↑ Summary Xiangqi.xs »