bd_engine.c

filter: Statementstmt 0.0% 0 / 225 Branchbran 0.0% 0 / 194 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 419 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Brandubh.xs ↑ Summary bd_moves.c »
1 /* bd_engine.c - the brandubh board, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/bd_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 * THIS FILE'S SCOPE. The board, the padded index, put and lift, the side to
7 * move, the zobrist key, and the position string in and out. Where a piece
8 * may go is bd_moves.c, which appends to the table.
9 */
10  
11 #include <stdlib.h>
12 #include <string.h>
13 #include "bd_abi.h"
14  
15 /* ---- the position ---------------------------------------------------------
16 *
17 * A flat 81-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 bd_board {
23 unsigned char cell[BD_CELLS];
24 int side;
25 unsigned long long key;
26 };
27  
28 /* Boards handed out and not yet dropped. It exists so a test can show that a
29 * destructor ran exactly once; it is a plain int and is not synchronised. */
30 static int BD_LIVE = 0;
31  
32 /* ---- the zobrist table -----------------------------------------------------
33 *
34 * DERIVED, NOT COMMITTED. Two workers in a pool must agree about whether a
35 * position has been seen, so the table must not come from rand() or from
36 * anything that varies per process. A fixed function of a fixed seed satisfies
37 * that exactly as a committed array does, and it cannot suffer the
38 * transcription error a hand-pasted array can. splitmix64 is published, has no
39 * weak seeds, and is what the sibling engine uses.
40 *
41 * CHANGING THIS GENERATOR OR ITS SEED CHANGES EVERY KEY. t/03-key.t pins
42 * values so that is caught by a red test.
43 */
44  
45 static unsigned long long ZOB[BD_KING + 1][BD_CELLS];
46 static unsigned long long ZOB_SIDE;
47 static int ZOB_READY = 0;
48  
49 0 static unsigned long long splitmix64(unsigned long long *state)
50 {
51 unsigned long long z;
52 0 *state += 0x9E3779B97F4A7C15ULL;
53 0 z = *state;
54 0 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
55 0 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
56 0 return z ^ (z >> 31);
57 }
58  
59 0 static void zob_init(void)
60 {
61 0 unsigned long long s = 0x6272616E64756268ULL; /* "brandubh" */
62 int p, c;
63 0 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 0 0 0
64 0 ▶ for (p = 0; p <= BD_KING; p++)
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p <= BD_KING; p++) 0 0 0
65 0 ▶ for (c = 0; c < BD_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < BD_CELLS; c++) 0 0 0
66 0 ZOB[p][c] = splitmix64(&s);
67 0 ZOB_SIDE = splitmix64(&s);
68 0 ZOB_READY = 1;
69 }
70  
71 /* ---- geometry -------------------------------------------------------------- */
72  
73 0 static int bd_stride(void) { return BD_STRIDE; }
74  
75 0 static int bd_square_of(int file, int rank)
76 {
77 0 ▶ if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; 0 0 0
if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; 0 0 0
if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; 0 0 0
if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; 0 0 0
78 0 return (rank + 1) * BD_STRIDE + (file + 1);
79 }
80  
81 0 static int bd_on_board(int sq)
82 {
83 int f, r;
84 0 ▶ if (sq < 0 || sq >= BD_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (sq < 0 || sq >= BD_CELLS) return 0; 0 0 0
if (sq < 0 || sq >= BD_CELLS) return 0; 0 0 0
85 0 f = sq % BD_STRIDE;
86 0 r = sq / BD_STRIDE;
87 0 ▶ return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE;
Branch: 4 branches
BranchTrueFalseTotal
return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; 0 0 0
return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; 0 0 0
return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; 0 0 0
return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; 0 0 0
88 }
89  
90 0 static int bd_file_of(int sq)
91 {
92 0 ▶ if (!bd_on_board(sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!bd_on_board(sq)) return -1; 0 0 0
93 0 return (sq % BD_STRIDE) - 1;
94 }
95  
96 0 static int bd_rank_of(int sq)
97 {
98 0 ▶ if (!bd_on_board(sq)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!bd_on_board(sq)) return -1; 0 0 0
99 0 return (sq / BD_STRIDE) - 1;
100 }
101  
102 #define BD_THRONE ((BD_SIZE / 2 + 1) * BD_STRIDE + (BD_SIZE / 2 + 1))
103  
104 0 static int bd_is_throne(int sq) { return sq == BD_THRONE; }
105  
106 0 static int bd_is_corner(int sq)
107 {
108 0 int f = bd_file_of(sq), r = bd_rank_of(sq);
109 0 ▶ if (f < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (f < 0) return 0; 0 0 0
110 0 ▶ return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1);
Branch: 4 branches
BranchTrueFalseTotal
return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); 0 0 0
return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); 0 0 0
return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); 0 0 0
return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); 0 0 0
111 }
112  
113 /* The four squares that share an edge with the throne. Not the diagonals: the
114 * king's capture there counts orthogonal neighbours and nothing else. */
115 0 static int bd_beside_throne(int sq)
116 {
117 0 ▶ if (!bd_on_board(sq)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!bd_on_board(sq)) return 0; 0 0 0
118 0 ▶ return sq == BD_THRONE - 1 || sq == BD_THRONE + 1
Branch: 1 branch
BranchTrueFalseTotal
return sq == BD_THRONE - 1 || sq == BD_THRONE + 1 0 0 0
119 0 ▶ || sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE;
Branch: 3 branches
BranchTrueFalseTotal
|| sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; 0 0 0
|| sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; 0 0 0
|| sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; 0 0 0
120 }
121  
122 /* THE KING IS ON THE DEFENDERS' SIDE, and this is the one place that says so. */
123 0 static int bd_side_of(int piece)
124 {
125 0 ▶ if (piece == BD_ATTACKER) return BD_ATTACKERS;
Branch: 1 branch
BranchTrueFalseTotal
if (piece == BD_ATTACKER) return BD_ATTACKERS; 0 0 0
126 0 ▶ if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS;
Branch: 2 branches
BranchTrueFalseTotal
if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS; 0 0 0
if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS; 0 0 0
127 0 return -1;
128 }
129  
130 /* ---- the key ----------------------------------------------------------------
131 *
132 * The key covers the pieces, their squares, and the side to move, and NOTHING
133 * else, so a position repeating is a position repeating.
134 */
135  
136 0 static unsigned long long bd_zobrist(int piece, int sq)
137 {
138 0 zob_init();
139 0 ▶ if (!BD_IS_PIECE(piece)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!BD_IS_PIECE(piece)) return 0; 0 0 0
if (!BD_IS_PIECE(piece)) return 0; 0 0 0
140 0 ▶ if (!bd_on_board(sq)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!bd_on_board(sq)) return 0; 0 0 0
141 0 return ZOB[piece][sq];
142 }
143  
144 0 static unsigned long long bd_zobrist_side(void)
145 {
146 0 zob_init();
147 0 return ZOB_SIDE;
148 }
149  
150 /* ---- the board ------------------------------------------------------------- */
151  
152 0 static void board_blank(struct bd_board *b)
153 {
154 int f, r;
155 0 memset(b, 0, sizeof(*b));
156 0 memset(b->cell, BD_BORDER, sizeof(b->cell));
157 0 ▶ for (r = 0; r < BD_SIZE; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < BD_SIZE; r++) 0 0 0
158 0 ▶ for (f = 0; f < BD_SIZE; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < BD_SIZE; f++) 0 0 0
159 0 b->cell[bd_square_of(f, r)] = BD_EMPTY;
160 0 b->side = BD_ATTACKERS;
161 0 b->key = 0;
162 0 }
163  
164 0 static struct bd_board *bd_board_empty(void)
165 {
166 struct bd_board *b;
167 0 zob_init();
168 0 b = (struct bd_board *) malloc(sizeof(struct bd_board));
169 0 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 0 0
170 0 board_blank(b);
171 0 BD_LIVE++;
172 0 return b;
173 }
174  
175 /* put and lift MAINTAIN THE KEY and judge nothing else. A piece value that is
176 * not one of the three, or BD_EMPTY, is ignored: the ring is not writable and
177 * neither is a square with a border on it. */
178 0 static void bd_put(struct bd_board *b, int sq, int piece)
179 {
180 int old;
181 0 ▶ if (!bd_on_board(sq)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!bd_on_board(sq)) return; 0 0 0
182 0 ▶ if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; 0 0 0
if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; 0 0 0
if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; 0 0 0
183 0 old = b->cell[sq];
184 0 ▶ if (old != BD_EMPTY) b->key ^= bd_zobrist(old, sq);
Branch: 1 branch
BranchTrueFalseTotal
if (old != BD_EMPTY) b->key ^= bd_zobrist(old, sq); 0 0 0
185 0 b->cell[sq] = (unsigned char) piece;
186 0 ▶ if (piece != BD_EMPTY) b->key ^= bd_zobrist(piece, sq);
Branch: 1 branch
BranchTrueFalseTotal
if (piece != BD_EMPTY) b->key ^= bd_zobrist(piece, sq); 0 0 0
187 }
188  
189 0 static void bd_lift(struct bd_board *b, int sq)
190 {
191 0 bd_put(b, sq, BD_EMPTY);
192 0 }
193  
194 0 static int bd_at(const struct bd_board *b, int sq)
195 {
196 0 ▶ if (sq < 0 || sq >= BD_CELLS) return BD_BORDER;
Branch: 2 branches
BranchTrueFalseTotal
if (sq < 0 || sq >= BD_CELLS) return BD_BORDER; 0 0 0
if (sq < 0 || sq >= BD_CELLS) return BD_BORDER; 0 0 0
197 0 return b->cell[sq];
198 }
199  
200 0 static int bd_side(const struct bd_board *b) { return b->side; }
201  
202 0 static void bd_set_side(struct bd_board *b, int side)
203 {
204 0 ▶ if (side != BD_ATTACKERS && side != BD_DEFENDERS) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side != BD_ATTACKERS && side != BD_DEFENDERS) return; 0 0 0
if (side != BD_ATTACKERS && side != BD_DEFENDERS) return; 0 0 0
205 0 ▶ if (b->side == side) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->side == side) return; 0 0 0
206 0 b->side = side;
207 0 b->key ^= ZOB_SIDE;
208 }
209  
210 0 static int bd_count(const struct bd_board *b, int piece)
211 {
212 0 int sq, n = 0;
213 0 ▶ for (sq = 0; sq < BD_CELLS; sq++)
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < BD_CELLS; sq++) 0 0 0
214 0 ▶ if (b->cell[sq] == piece && bd_on_board(sq)) n++;
Branch: 2 branches
BranchTrueFalseTotal
if (b->cell[sq] == piece && bd_on_board(sq)) n++; 0 0 0
if (b->cell[sq] == piece && bd_on_board(sq)) n++; 0 0 0
215 0 return n;
216 }
217  
218 /* -1 and not 0 for "no king": 0 is a cell of the ring, and a caller that walked
219 * from it would be walking the border. */
220 0 static int bd_king_square(const struct bd_board *b)
221 {
222 int sq;
223 0 ▶ for (sq = 0; sq < BD_CELLS; sq++)
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < BD_CELLS; sq++) 0 0 0
224 0 ▶ if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq;
Branch: 2 branches
BranchTrueFalseTotal
if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq; 0 0 0
if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq; 0 0 0
225 0 return -1;
226 }
227  
228 0 static unsigned long long bd_key(const struct bd_board *b) { return b->key; }
229  
230 /* The key from nothing, by walking the board. It must equal the maintained key
231 * after any sequence of put, lift and set_side, and a test holds it to that. */
232 0 static unsigned long long bd_key_full(const struct bd_board *b)
233 {
234 0 unsigned long long k = 0;
235 int sq;
236 0 ▶ for (sq = 0; sq < BD_CELLS; sq++)
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < BD_CELLS; sq++) 0 0 0
237 0 ▶ if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY)
Branch: 2 branches
BranchTrueFalseTotal
if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY) 0 0 0
if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY) 0 0 0
238 0 k ^= bd_zobrist(b->cell[sq], sq);
239 0 ▶ if (b->side == BD_DEFENDERS) k ^= bd_zobrist_side();
Branch: 1 branch
BranchTrueFalseTotal
if (b->side == BD_DEFENDERS) k ^= bd_zobrist_side(); 0 0 0
240 0 return k;
241 }
242  
243 /* The set-up: the king on the throne, a defender on each side of him, and the
244 * attackers two deep at the end of each arm of the cross. Attackers move first.
245 *
246 * 7 . . . A . . .
247 * 6 . . . A . . .
248 * 5 . . . D . . .
249 * 4 A A D K D A A
250 * 3 . . . D . . .
251 * 2 . . . A . . .
252 * 1 . . . A . . .
253 * a b c d e f g
254 */
255 0 static struct bd_board *bd_board_new(void)
256 {
257 static const int ARM[BD_SIZE] = {
258 BD_ATTACKER, BD_ATTACKER, BD_DEFENDER, BD_KING,
259 BD_DEFENDER, BD_ATTACKER, BD_ATTACKER
260 };
261 0 struct bd_board *b = bd_board_empty();
262 0 int mid = BD_SIZE / 2, i;
263 0 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 0 0
264 0 ▶ for (i = 0; i < BD_SIZE; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < BD_SIZE; i++) { 0 0 0
265 0 bd_put(b, bd_square_of(i, mid), ARM[i]);
266 0 bd_put(b, bd_square_of(mid, i), ARM[i]);
267 }
268 0 return b;
269 }
270  
271 0 static struct bd_board *bd_board_copy(const struct bd_board *b)
272 {
273 struct bd_board *n;
274 0 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 0 0
275 0 n = (struct bd_board *) malloc(sizeof(struct bd_board));
276 0 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 0 0
277 0 *n = *b;
278 0 BD_LIVE++;
279 0 return n;
280 }
281  
282 0 static void bd_board_drop(struct bd_board *b)
283 {
284 0 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 0 0
285 0 BD_LIVE--;
286 0 free(b);
287 }
288  
289 0 static int bd_live(void) { return BD_LIVE; }
290  
291 /* ---- the position string ------------------------------------------------------
292 *
293 * Seven rows, rank 7 first, a slash between rows, a digit for a run of empty
294 * squares, then one space and the side to move:
295 *
296 * 3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a
297 *
298 * a is an attacker, d a defender, k the king; the side is a or d.
299 *
300 * THE SIDE IS REQUIRED. Defaulting it would turn a string with the field cut
301 * off into a position with a player to move, which is a different position
302 * from the one somebody meant.
303 */
304  
305 0 static int letter_piece(char c)
306 {
307 0 switch (c) {
308 0 case 'a': return BD_ATTACKER;
309 0 case 'd': return BD_DEFENDER;
310 0 case 'k': return BD_KING;
311 0 default: return -1;
312 }
313 }
314  
315 0 static char piece_letter(int piece)
316 {
317 0 switch (piece) {
318 0 case BD_ATTACKER: return 'a';
319 0 case BD_DEFENDER: return 'd';
320 0 case BD_KING: return 'k';
321 0 default: return '?';
322 }
323 }
324  
325 0 static struct bd_board *refuse(struct bd_board *b, int *err, int code)
326 {
327 0 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 0 0 0
328 0 bd_board_drop(b);
329 0 return NULL;
330 }
331  
332 0 static struct bd_board *bd_board_of_string(const char *str, int *err)
333 {
334 struct bd_board *b;
335 0 int rank = BD_SIZE - 1, file = 0;
336 0 const char *s = str;
337 size_t n;
338  
339 0 ▶ if (err) *err = BD_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = BD_OK; 0 0 0
340 0 ▶ if (!str) { if (err) *err = BD_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
if (!str) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
341 0 n = strlen(str);
342 0 ▶ if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
343 0 ▶ if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; } 0 0 0
if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; } 0 0 0
344  
345 0 b = bd_board_empty();
346 0 ▶ if (!b) { if (err) *err = BD_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!b) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
if (!b) { if (err) *err = BD_POS_NULL; return NULL; } 0 0 0
347  
348 0 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 0 0 0
for (; *s && *s != ' '; s++) { 0 0 0
349 0 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 0 0 0
350 0 ▶ if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); 0 0 0
351 0 ▶ if (rank == 0) return refuse(b, err, BD_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (rank == 0) return refuse(b, err, BD_POS_ROWS); 0 0 0
352 0 rank--; file = 0;
353 0 continue;
354 }
355 0 ▶ if (*s >= '1' && *s <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '7') { 0 0 0
if (*s >= '1' && *s <= '7') { 0 0 0
356 0 file += *s - '0';
357 0 ▶ if (file > BD_SIZE) return refuse(b, err, BD_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file > BD_SIZE) return refuse(b, err, BD_POS_WIDTH); 0 0 0
358 0 continue;
359 }
360 {
361 0 int piece = letter_piece(*s);
362 0 ▶ if (piece < 0) return refuse(b, err, BD_POS_LETTER);
Branch: 1 branch
BranchTrueFalseTotal
if (piece < 0) return refuse(b, err, BD_POS_LETTER); 0 0 0
363 0 ▶ if (file >= BD_SIZE) return refuse(b, err, BD_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file >= BD_SIZE) return refuse(b, err, BD_POS_WIDTH); 0 0 0
364 0 bd_put(b, bd_square_of(file, rank), piece);
365 0 file++;
366 }
367 }
368 0 ▶ if (rank != 0) return refuse(b, err, BD_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (rank != 0) return refuse(b, err, BD_POS_ROWS); 0 0 0
369 0 ▶ if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); 0 0 0
370  
371 0 ▶ if (*s != ' ') return refuse(b, err, BD_POS_SIDE);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return refuse(b, err, BD_POS_SIDE); 0 0 0
372 0 s++;
373 0 ▶ if (*s == 'a') bd_set_side(b, BD_ATTACKERS);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'a') bd_set_side(b, BD_ATTACKERS); 0 0 0
374 0 ▶ else if (*s == 'd') bd_set_side(b, BD_DEFENDERS);
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == 'd') bd_set_side(b, BD_DEFENDERS); 0 0 0
375 0 else return refuse(b, err, BD_POS_SIDE);
376 0 s++;
377 0 ▶ if (*s) return refuse(b, err, BD_POS_SIDE);
Branch: 1 branch
BranchTrueFalseTotal
if (*s) return refuse(b, err, BD_POS_SIDE); 0 0 0
378  
379 0 return b;
380 }
381  
382 0 static int bd_to_string(const struct bd_board *b, char *buf, int len)
383 {
384 0 int rank, file, i = 0, run;
385 0 ▶ if (!b || !buf || len < BD_POS_MAX) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !buf || len < BD_POS_MAX) return -1; 0 0 0
if (!b || !buf || len < BD_POS_MAX) return -1; 0 0 0
if (!b || !buf || len < BD_POS_MAX) return -1; 0 0 0
386 0 ▶ for (rank = BD_SIZE - 1; rank >= 0; rank--) {
Branch: 1 branch
BranchTrueFalseTotal
for (rank = BD_SIZE - 1; rank >= 0; rank--) { 0 0 0
387 0 run = 0;
388 0 ▶ for (file = 0; file < BD_SIZE; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < BD_SIZE; file++) { 0 0 0
389 0 int piece = b->cell[bd_square_of(file, rank)];
390 0 ▶ if (piece == BD_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (piece == BD_EMPTY) { run++; continue; } 0 0 0
391 0 ▶ if (run) { buf[i++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { buf[i++] = (char) ('0' + run); run = 0; } 0 0 0
392 0 buf[i++] = piece_letter(piece);
393 }
394 0 ▶ if (run) buf[i++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) buf[i++] = (char) ('0' + run); 0 0 0
395 0 ▶ if (rank) buf[i++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (rank) buf[i++] = '/'; 0 0 0
396 }
397 0 buf[i++] = ' ';
398 0 ▶ buf[i++] = (b->side == BD_DEFENDERS) ? 'd' : 'a';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == BD_DEFENDERS) ? 'd' : 'a'; 0 0 0
399 0 buf[i] = '\0';
400 0 return i;
401 }
402  
403 /* ---- the table -------------------------------------------------------------- */
404  
405 static struct bd_abi BD_TABLE;
406 static int BD_TABLE_READY = 0;
407  
408 /* bd_moves.c installs itself into the table here. The two files call into each
409 * other and it terminates: BD_TABLE_READY is set before the installer runs, and
410 * bd_moves.c only reaches bd_abi_table from inside a function the installer
411 * has already handed over. */
412 void bd_moves_install(struct bd_abi *t);
413 void bd_rules_install(struct bd_abi *t);
414 void bd_search_install(struct bd_abi *t);
415  
416 0 const struct bd_abi *bd_abi_table(void)
417 {
418 0 ▶ if (!BD_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!BD_TABLE_READY) { 0 0 0
419 0 zob_init();
420 0 BD_TABLE.abi_version = BD_ABI_VERSION;
421  
422 0 BD_TABLE.board_new = bd_board_new;
423 0 BD_TABLE.board_empty = bd_board_empty;
424 0 BD_TABLE.board_of_string = bd_board_of_string;
425 0 BD_TABLE.board_copy = bd_board_copy;
426 0 BD_TABLE.board_drop = bd_board_drop;
427 0 BD_TABLE.live = bd_live;
428  
429 0 BD_TABLE.at = bd_at;
430 0 BD_TABLE.put = bd_put;
431 0 BD_TABLE.lift = bd_lift;
432 0 BD_TABLE.side = bd_side;
433 0 BD_TABLE.set_side = bd_set_side;
434 0 BD_TABLE.count = bd_count;
435 0 BD_TABLE.king_square = bd_king_square;
436  
437 0 BD_TABLE.key = bd_key;
438 0 BD_TABLE.key_full = bd_key_full;
439 0 BD_TABLE.zobrist = bd_zobrist;
440 0 BD_TABLE.zobrist_side = bd_zobrist_side;
441 0 BD_TABLE.to_string = bd_to_string;
442  
443 0 BD_TABLE.stride = bd_stride;
444 0 BD_TABLE.square_of = bd_square_of;
445 0 BD_TABLE.file_of = bd_file_of;
446 0 BD_TABLE.rank_of = bd_rank_of;
447 0 BD_TABLE.on_board = bd_on_board;
448 0 BD_TABLE.is_throne = bd_is_throne;
449 0 BD_TABLE.is_corner = bd_is_corner;
450 0 BD_TABLE.beside_throne = bd_beside_throne;
451 0 BD_TABLE.side_of = bd_side_of;
452  
453 0 BD_TABLE_READY = 1; /* before the installer, or it recurses */
454 0 bd_moves_install(&BD_TABLE);
455 0 bd_rules_install(&BD_TABLE);
456 0 bd_search_install(&BD_TABLE);
457 }
458 0 return &BD_TABLE;
459 }
« Brandubh.xs ↑ Summary bd_moves.c »