ru_engine.c

filter: Statementstmt 100% 264 / 264 Branchbran 85.3% 239 / 280 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.4% 503 / 544 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ru_search.c ↑ Summary bin/royalur »
1 /* ru_engine.c - the board of the Royal Game of Ur, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/ru_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 twenty cells, the hands and the homes, the side to
7 * move, the two routes, the key, the position string in and out, and the
8 * chance of each roll. What a roll allows is ru_moves.c, which appends to the
9 * table.
10 */
11  
12 #include <stdlib.h>
13 #include <string.h>
14 #include "ru_abi.h"
15  
16 /* ---- the position ---------------------------------------------------------
17 *
18 * No pointers, so a copy is a struct assignment and two boards never share
19 * anything. THE HANDS ARE STORED AND NOT DERIVED: seven less the pieces on the
20 * board and at home would be right for a legal position and would make an
21 * illegal one impossible to build, and a test needs to build them.
22 */
23  
24 struct ru_board {
25 unsigned char cell[RU_CELLS];
26 int hand[2];
27 int home[2];
28 int side;
29 int ply;
30 };
31  
32 /* Boards handed out and not yet dropped. It exists so a test can show that a
33 * destructor ran exactly once; it is a plain int and is not synchronised. */
34 static int RU_LIVE = 0;
35  
36 /* ---- geometry ---------------------------------------------------------------
37 *
38 * Three rows of eight with four squares missing. The index is row by row and
39 * skips the gaps, and it is OPAQUE: nothing outside this file may rely on it.
40 */
41  
42 static const signed char CELL_AT[RU_ROWS][RU_FILES] = {
43 { 0, 1, 2, 3, -1, -1, 4, 5 },
44 { 6, 7, 8, 9, 10, 11, 12, 13 },
45 { 14, 15, 16, 17, -1, -1, 18, 19 }
46 };
47  
48 static const signed char FILE_AT[RU_CELLS] = {
49 0, 1, 2, 3, 6, 7,
50 0, 1, 2, 3, 4, 5, 6, 7,
51 0, 1, 2, 3, 6, 7
52 };
53  
54 static const signed char ROW_AT[RU_CELLS] = {
55 0, 0, 0, 0, 0, 0,
56 1, 1, 1, 1, 1, 1, 1, 1,
57 2, 2, 2, 2, 2, 2
58 };
59  
60 1909300 static int ru_cell_of(int file, int row)
61 {
62 1909300 ▶ if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 1909299 1 1909300
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 1909298 1 1909299
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 1909297 1 1909298
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 1 1909296 1909297
63 1909296 return CELL_AT[row][file];
64 }
65  
66 46538795 static int ru_file_of(int cell)
67 {
68 46538795 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 46538794 1 46538795
if (cell < 0 || cell >= RU_CELLS) return -1; 2 46538792 46538794
69 46538792 return FILE_AT[cell];
70 }
71  
72 46538795 static int ru_row_of(int cell)
73 {
74 46538795 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 46538793 2 46538795
if (cell < 0 || cell >= RU_CELLS) return -1; 1 46538792 46538793
75 46538792 return ROW_AT[cell];
76 }
77  
78 /* Five of them: the first and the seventh file of each outer row, and the
79 * fourth file of the middle row. */
80 43197543 static int ru_is_rosette(int cell)
81 {
82 43197543 int f = ru_file_of(cell), r = ru_row_of(cell);
83 43197543 ▶ if (f < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (f < 0) return 0; 2 43197541 43197543
84 43197541 ▶ if (r == 1) return f == 3;
Branch: 1 branch
BranchTrueFalseTotal
if (r == 1) return f == 3; 25212487 17985054 43197541
85 17985054 ▶ return f == 0 || f == 6;
Branch: 2 branches
BranchTrueFalseTotal
return f == 0 || f == 6; 13855700 4129354 17985054
return f == 0 || f == 6; 1766132 12089568 13855700
86 }
87  
88 /* ---- the routes ---------------------------------------------------------------
89 *
90 * LIGHT'S ROUTE IS WRITTEN OUT, as file and row, and dark's is light's with
91 * rows 1 and 3 exchanged, computed once and not typed twice. Both enter on the
92 * fourth file of their own row and walk toward the corner.
93 */
94  
95 static const signed char SHORT_LIGHT[14][2] = {
96 {3,0}, {2,0}, {1,0}, {0,0},
97 {0,1}, {1,1}, {2,1}, {3,1}, {4,1}, {5,1}, {6,1}, {7,1},
98 {7,0}, {6,0}
99 };
100  
101 static const signed char LONG_LIGHT[16][2] = {
102 {3,0}, {2,0}, {1,0}, {0,0},
103 {0,1}, {1,1}, {2,1}, {3,1}, {4,1}, {5,1}, {6,1},
104 {6,2}, {7,2}, {7,1}, {7,0}, {6,0}
105 };
106  
107 static int ROUTE_CELL[2][2][RU_ROUTE_MAX + 1];
108 static int ROUTE_STEP[2][2][RU_CELLS];
109 static int ROUTES_READY = 0;
110  
111 567044017 static int ru_route_len(int route)
112 {
113 567044017 ▶ if (route == RU_ROUTE_SHORT) return 14;
Branch: 1 branch
BranchTrueFalseTotal
if (route == RU_ROUTE_SHORT) return 14; 347820793 219223224 567044017
114 219223224 ▶ if (route == RU_ROUTE_LONG) return 16;
Branch: 1 branch
BranchTrueFalseTotal
if (route == RU_ROUTE_LONG) return 16; 219223222 2 219223224
115 2 return -1;
116 }
117  
118 1331067324 static void routes_init(void)
119 {
120 int route, side, step, c;
121 1331067324 ▶ if (ROUTES_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ROUTES_READY) return; 1331067228 96 1331067324
122 288 ▶ for (route = 0; route < 2; route++) {
Branch: 1 branch
BranchTrueFalseTotal
for (route = 0; route < 2; route++) { 192 96 288
123 192 ▶ const signed char (*light)[2] = (route == RU_ROUTE_LONG) ? LONG_LIGHT : SHORT_LIGHT;
Branch: 1 branch
BranchTrueFalseTotal
const signed char (*light)[2] = (route == RU_ROUTE_LONG) ? LONG_LIGHT : SHORT_LIGHT; 96 96 192
124 192 int len = ru_route_len(route);
125 576 ▶ for (side = 0; side < 2; side++) {
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) { 384 192 576
126 6912 ▶ for (step = 0; step <= RU_ROUTE_MAX; step++) ROUTE_CELL[route][side][step] = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (step = 0; step <= RU_ROUTE_MAX; step++) ROUTE_CELL[route][side][step] = -1; 6528 384 6912
127 8064 ▶ for (c = 0; c < RU_CELLS; c++) ROUTE_STEP[route][side][c] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RU_CELLS; c++) ROUTE_STEP[route][side][c] = 0; 7680 384 8064
128 6144 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 5760 384 6144
129 5760 int file = light[step - 1][0];
130 5760 int row = light[step - 1][1];
131 int cell;
132 5760 ▶ if (side == RU_SIDE_DARK) row = RU_ROWS - 1 - row;
Branch: 1 branch
BranchTrueFalseTotal
if (side == RU_SIDE_DARK) row = RU_ROWS - 1 - row; 2880 2880 5760
133 5760 cell = ru_cell_of(file, row);
134 5760 ROUTE_CELL[route][side][step] = cell;
135 5760 ▶ if (cell >= 0) ROUTE_STEP[route][side][cell] = step;
Branch: 1 branch
BranchTrueFalseTotal
if (cell >= 0) ROUTE_STEP[route][side][cell] = step; 5760 0 5760
136 }
137 }
138 }
139 96 ROUTES_READY = 1;
140 }
141  
142 524482392 static int ru_route_cell(int route, int side, int step)
143 {
144 524482392 routes_init();
145 524482392 ▶ if (route < 0 || route > 1 || side < 0 || side > 1) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 524482392 0 524482392
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 524482391 1 524482392
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 524482391 0 524482391
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 1 524482390 524482391
146 524482390 ▶ if (step < 1 || step > ru_route_len(route)) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (step < 1 || step > ru_route_len(route)) return -1; 524482382 8 524482390
if (step < 1 || step > ru_route_len(route)) return -1; 9 524482373 524482382
147 524482373 return ROUTE_CELL[route][side][step];
148 }
149  
150 /* The step a cell is FOR THAT SIDE, or 0 when the side's route does not visit
151 * it. On the long route the answer for one cell differs between the sides. */
152 806563238 static int ru_route_step(int route, int side, int cell)
153 {
154 806563238 routes_init();
155 806563238 ▶ if (route < 0 || route > 1 || side < 0 || side > 1) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 806563238 0 806563238
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 806563237 1 806563238
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 806563237 0 806563237
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 0 806563237 806563237
156 806563237 ▶ if (cell < 0 || cell >= RU_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return 0; 806563235 2 806563237
if (cell < 0 || cell >= RU_CELLS) return 0; 1 806563234 806563235
157 806563234 return ROUTE_STEP[route][side][cell];
158 }
159  
160 /* On both sides' routes. NOT "the middle row": that is true of the short route
161 * and false of the long one, whose last five steps run through the far row. */
162 36845488 static int ru_route_shared(int route, int cell)
163 {
164 36845488 return ru_route_step(route, RU_SIDE_LIGHT, cell) > 0
165 36845488 ▶ && ru_route_step(route, RU_SIDE_DARK, cell) > 0;
Branch: 2 branches
BranchTrueFalseTotal
&& ru_route_step(route, RU_SIDE_DARK, cell) > 0; 25766899 11078589 36845488
&& ru_route_step(route, RU_SIDE_DARK, cell) > 0; 15886212 9880687 25766899
166 }
167  
168 /* ---- the board ------------------------------------------------------------- */
169  
170 21598 static int clamp_pieces(int n)
171 {
172 21598 ▶ if (n < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return 0; 0 21598 21598
173 21598 ▶ if (n > RU_PIECES_MAX) return RU_PIECES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > RU_PIECES_MAX) return RU_PIECES_MAX; 0 21598 21598
174 21598 return n;
175 }
176  
177 21598 static struct ru_board *ru_board_new(int pieces)
178 {
179 struct ru_board *b;
180 21598 routes_init();
181 21598 b = (struct ru_board *) malloc(sizeof(struct ru_board));
182 21598 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 21598 21598
183 21598 memset(b, 0, sizeof(*b));
184 21598 b->hand[RU_SIDE_LIGHT] = b->hand[RU_SIDE_DARK] = clamp_pieces(pieces);
185 21598 b->side = RU_SIDE_LIGHT;
186 21598 RU_LIVE++;
187 21598 return b;
188 }
189  
190 72434 static struct ru_board *ru_board_copy(const struct ru_board *b)
191 {
192 struct ru_board *n;
193 72434 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 72434 72434
194 72434 n = (struct ru_board *) malloc(sizeof(struct ru_board));
195 72434 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 72434 72434
196 72434 *n = *b;
197 72434 RU_LIVE++;
198 72434 return n;
199 }
200  
201 94032 static void ru_board_drop(struct ru_board *b)
202 {
203 94032 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 94032 94032
204 94032 RU_LIVE--;
205 94032 free(b);
206 }
207  
208 12 static int ru_live(void) { return RU_LIVE; }
209  
210 528649196 static int ru_at(const struct ru_board *b, int cell)
211 {
212 528649196 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 528649195 1 528649196
if (cell < 0 || cell >= RU_CELLS) return -1; 1 528649194 528649195
213 528649194 return b->cell[cell];
214 }
215  
216 /* A value that is not a piece or RU_EMPTY is ignored, and so is a cell that is
217 * not one. */
218 24268069 static void ru_put(struct ru_board *b, int cell, int piece)
219 {
220 24268069 ▶ if (cell < 0 || cell >= RU_CELLS) return;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return; 24268068 1 24268069
if (cell < 0 || cell >= RU_CELLS) return; 1 24268067 24268068
221 24268067 ▶ if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 13007865 11260202 24268067
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 6576002 6431863 13007865
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 2 6576000 6576002
222 24268065 b->cell[cell] = (unsigned char) piece;
223 }
224  
225 11253499 static void ru_lift(struct ru_board *b, int cell)
226 {
227 11253499 ru_put(b, cell, RU_EMPTY);
228 11253499 }
229  
230 15356344 static int ru_hand(const struct ru_board *b, int side)
231 {
232 15356344 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 15356344 0 15356344
if (side < 0 || side > 1) return -1; 1 15356343 15356344
233 15356343 return b->hand[side];
234 }
235  
236 4718356 static void ru_set_hand(struct ru_board *b, int side, int n)
237 {
238 4718356 ▶ if (side < 0 || side > 1) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return; 4718356 0 4718356
if (side < 0 || side > 1) return; 1 4718355 4718356
239 4718355 ▶ if (n < 0 || n > RU_PIECES_MAX) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || n > RU_PIECES_MAX) return; 4718354 1 4718355
if (n < 0 || n > RU_PIECES_MAX) return; 1 4718353 4718354
240 4718353 b->hand[side] = n;
241 }
242  
243 40700052 static int ru_home(const struct ru_board *b, int side)
244 {
245 40700052 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 40700051 1 40700052
if (side < 0 || side > 1) return -1; 0 40700051 40700051
246 40700051 return b->home[side];
247 }
248  
249 617173 static void ru_set_home(struct ru_board *b, int side, int n)
250 {
251 617173 ▶ if (side < 0 || side > 1) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return; 617173 0 617173
if (side < 0 || side > 1) return; 0 617173 617173
252 617173 ▶ if (n < 0 || n > RU_PIECES_MAX) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || n > RU_PIECES_MAX) return; 617173 0 617173
if (n < 0 || n > RU_PIECES_MAX) return; 1 617172 617173
253 617172 b->home[side] = n;
254 }
255  
256 13900509 static int ru_side(const struct ru_board *b) { return b->side; }
257  
258 13169442 static void ru_set_side(struct ru_board *b, int side)
259 {
260 13169442 ▶ if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return; 6565542 6603900 13169442
if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return; 1 6565541 6565542
261 13169441 b->side = side;
262 }
263  
264 /* The plies made to reach the position. It is carried by the board and is not
265 * part of the position: two boards that differ only in it have one key. */
266 16096893 static int ru_ply(const struct ru_board *b) { return b->ply; }
267  
268 14606106 static void ru_set_ply(struct ru_board *b, int ply)
269 {
270 14606106 ▶ if (ply < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ply < 0) return; 1 14606105 14606106
271 14606105 b->ply = ply;
272 }
273  
274 43145 static int ru_count(const struct ru_board *b, int side)
275 {
276 43145 int c, n = 0;
277 43145 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 43145 0 43145
if (side < 0 || side > 1) return -1; 1 43144 43145
278 906024 ▶ for (c = 0; c < RU_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RU_CELLS; c++) 862880 43144 906024
279 862880 ▶ if (b->cell[c] == RU_PIECE_OF(side)) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[c] == RU_PIECE_OF(side)) n++; 131133 731747 862880
280 43144 return n;
281 }
282  
283 21570 static int ru_consistent(const struct ru_board *b, int n)
284 {
285 int side;
286 64700 ▶ for (side = 0; side < 2; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) 43136 21564 64700
287 43136 ▶ if (b->hand[side] + b->home[side] + ru_count(b, side) != n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b->hand[side] + b->home[side] + ru_count(b, side) != n) return 0; 6 43130 43136
288 21564 return 1;
289 }
290  
291 /* ---- the key ----------------------------------------------------------------
292 *
293 * EXACT, and recomputed every time: twenty cells at two bits, two homes at
294 * three, and the side to move. Forty-seven bits, no table and no collision.
295 * The hands are left out on purpose; see ru_abi.h.
296 */
297  
298 66820 static unsigned long long ru_key(const struct ru_board *b)
299 {
300 66820 unsigned long long k = 0;
301 int c;
302 1403220 ▶ for (c = 0; c < RU_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RU_CELLS; c++) 1336400 66820 1403220
303 1336400 k |= ((unsigned long long) (b->cell[c] & 3)) << (2 * c);
304 66820 k |= ((unsigned long long) (b->home[RU_SIDE_LIGHT] & 7)) << 40;
305 66820 k |= ((unsigned long long) (b->home[RU_SIDE_DARK] & 7)) << 43;
306 66820 k |= ((unsigned long long) (b->side & 1)) << 46;
307 66820 return k;
308 }
309  
310 /* ---- the position string ------------------------------------------------------
311 *
312 * Three rows, row 3 first, a slash between rows, a digit for a run of empty
313 * squares, an x for each square that does not exist, then the side to move,
314 * light's hand and home, and dark's hand and home:
315 *
316 * 4xx2/8/4xx2 l 7 0 7 0
317 *
318 * l is a light piece and d a dark one; the side is l or d.
319 *
320 * EVERY FIELD IS REQUIRED. Defaulting one would turn a string cut short into a
321 * position with a player to move and a full hand.
322 */
323  
324 45739 static int letter_piece(char c)
325 {
326 45739 switch (c) {
327 22581 case 'l': return RU_LIGHT;
328 23147 case 'd': return RU_DARK;
329 11 default: return -1;
330 }
331 }
332  
333 89 static struct ru_board *refuse(struct ru_board *b, int *err, int code)
334 {
335 89 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 89 0 89
336 89 ru_board_drop(b);
337 89 return NULL;
338 }
339  
340 8590 static struct ru_board *ru_board_of_string(const char *str, int *err)
341 {
342 struct ru_board *b;
343 8590 int row = RU_ROWS - 1, file = 0, field;
344 int value[4];
345 8590 const char *s = str;
346 size_t n;
347  
348 8590 ▶ if (err) *err = RU_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = RU_OK; 8590 0 8590
349 8590 ▶ if (!str) { if (err) *err = RU_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str) { if (err) *err = RU_POS_NULL; return NULL; } 0 8590 8590
if (!str) { if (err) *err = RU_POS_NULL; return NULL; } 0 0 0
350 8590 n = strlen(str);
351 8590 ▶ if (n == 0) { if (err) *err = RU_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0) { if (err) *err = RU_POS_NULL; return NULL; } 3 8587 8590
if (n == 0) { if (err) *err = RU_POS_NULL; return NULL; } 3 0 3
352 8587 ▶ if (n >= RU_POS_MAX) { if (err) *err = RU_POS_LONG; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n >= RU_POS_MAX) { if (err) *err = RU_POS_LONG; return NULL; } 2 8585 8587
if (n >= RU_POS_MAX) { if (err) *err = RU_POS_LONG; return NULL; } 2 0 2
353  
354 8585 b = ru_board_new(0);
355 8585 ▶ if (!b) { if (err) *err = RU_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!b) { if (err) *err = RU_POS_NULL; return NULL; } 0 8585 8585
if (!b) { if (err) *err = RU_POS_NULL; return NULL; } 0 0 0
356  
357 159824 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 159820 4 159824
for (; *s && *s != ' '; s++) { 151283 8537 159820
358 151283 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 17116 134167 151283
359 17116 ▶ if (file != RU_FILES) return refuse(b, err, RU_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file != RU_FILES) return refuse(b, err, RU_POS_WIDTH); 7 17109 17116
360 17109 ▶ if (row == 0) return refuse(b, err, RU_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (row == 0) return refuse(b, err, RU_POS_ROWS); 2 17107 17109
361 17107 row--; file = 0;
362 17107 continue;
363 }
364 134167 ▶ if (*s >= '1' && *s <= '8') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '8') { 134166 1 134167
if (*s >= '1' && *s <= '8') { 54212 79954 134166
365 54212 int run = *s - '0';
366 179584 ▶ while (run-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (run-- > 0) { 125383 54201 179584
367 125383 ▶ if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH); 4 125379 125383
368 125379 ▶ if (ru_cell_of(file, row) < 0) return refuse(b, err, RU_POS_GAP);
Branch: 1 branch
BranchTrueFalseTotal
if (ru_cell_of(file, row) < 0) return refuse(b, err, RU_POS_GAP); 7 125372 125379
369 125372 file++;
370 }
371 54201 continue;
372 }
373 79955 ▶ if (*s == 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'x') { 34216 45739 79955
374 34216 ▶ if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH); 0 34216 34216
375 34216 ▶ if (ru_cell_of(file, row) >= 0) return refuse(b, err, RU_POS_X);
Branch: 1 branch
BranchTrueFalseTotal
if (ru_cell_of(file, row) >= 0) return refuse(b, err, RU_POS_X); 7 34209 34216
376 34209 file++;
377 34209 continue;
378 }
379 {
380 45739 int piece = letter_piece(*s);
381 45739 ▶ if (piece < 0) return refuse(b, err, RU_POS_LETTER);
Branch: 1 branch
BranchTrueFalseTotal
if (piece < 0) return refuse(b, err, RU_POS_LETTER); 11 45728 45739
382 45728 ▶ if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file >= RU_FILES) return refuse(b, err, RU_POS_WIDTH); 2 45726 45728
383 45726 ▶ if (ru_cell_of(file, row) < 0) return refuse(b, err, RU_POS_GAP);
Branch: 1 branch
BranchTrueFalseTotal
if (ru_cell_of(file, row) < 0) return refuse(b, err, RU_POS_GAP); 4 45722 45726
384 45722 ru_put(b, ru_cell_of(file, row), piece);
385 45722 file++;
386 }
387 }
388 8541 ▶ if (row != 0) return refuse(b, err, RU_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (row != 0) return refuse(b, err, RU_POS_ROWS); 6 8535 8541
389 8535 ▶ if (file != RU_FILES) return refuse(b, err, RU_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (file != RU_FILES) return refuse(b, err, RU_POS_WIDTH); 1 8534 8535
390  
391 8534 ▶ if (*s != ' ') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return refuse(b, err, RU_POS_FIELD); 2 8532 8534
392 8532 s++;
393 8532 ▶ if (*s == 'l') ru_set_side(b, RU_SIDE_LIGHT);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'l') ru_set_side(b, RU_SIDE_LIGHT); 4251 4281 8532
394 4281 ▶ else if (*s == 'd') ru_set_side(b, RU_SIDE_DARK);
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == 'd') ru_set_side(b, RU_SIDE_DARK); 4274 7 4281
395 7 else return refuse(b, err, RU_POS_SIDE);
396 8525 s++;
397  
398 42555 ▶ for (field = 0; field < 4; field++) {
Branch: 1 branch
BranchTrueFalseTotal
for (field = 0; field < 4; field++) { 34055 8500 42555
399 34055 ▶ if (*s != ' ') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return refuse(b, err, RU_POS_FIELD); 10 34045 34055
400 34045 s++;
401 34045 ▶ if (*s == '\0') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\0') return refuse(b, err, RU_POS_FIELD); 2 34043 34045
402 34043 ▶ if (*s < '0' || *s > '0' + RU_PIECES_MAX) return refuse(b, err, RU_POS_COUNT);
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '0' || *s > '0' + RU_PIECES_MAX) return refuse(b, err, RU_POS_COUNT); 34040 3 34043
if (*s < '0' || *s > '0' + RU_PIECES_MAX) return refuse(b, err, RU_POS_COUNT); 5 34035 34040
403 34035 value[field] = *s - '0';
404 34035 s++;
405 34035 ▶ if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT);
Branch: 2 branches
BranchTrueFalseTotal
if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT); 25534 8501 34035
if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT); 5 25529 25534
406 }
407 8500 ▶ if (*s) return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s) return refuse(b, err, RU_POS_FIELD); 4 8496 8500
408  
409 8496 ru_set_hand(b, RU_SIDE_LIGHT, value[0]);
410 8496 ru_set_home(b, RU_SIDE_LIGHT, value[1]);
411 8496 ru_set_hand(b, RU_SIDE_DARK, value[2]);
412 8496 ru_set_home(b, RU_SIDE_DARK, value[3]);
413  
414 8496 return b;
415 }
416  
417 65530 static int ru_to_string(const struct ru_board *b, char *buf, int len)
418 {
419 65530 int row, file, i = 0, run, side;
420 65530 ▶ if (!b || !buf || len < RU_POS_MAX) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !buf || len < RU_POS_MAX) return -1; 65530 0 65530
if (!b || !buf || len < RU_POS_MAX) return -1; 65530 0 65530
if (!b || !buf || len < RU_POS_MAX) return -1; 0 65530 65530
421 262120 ▶ for (row = RU_ROWS - 1; row >= 0; row--) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = RU_ROWS - 1; row >= 0; row--) { 196590 65530 262120
422 196590 run = 0;
423 1769310 ▶ for (file = 0; file < RU_FILES; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < RU_FILES; file++) { 1572720 196590 1769310
424 1572720 int cell = ru_cell_of(file, row);
425 1572720 ▶ int piece = cell < 0 ? -1 : b->cell[cell];
Branch: 1 branch
BranchTrueFalseTotal
int piece = cell < 0 ? -1 : b->cell[cell]; 1310600 262120 1572720
426 1572720 ▶ if (piece == RU_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (piece == RU_EMPTY) { run++; continue; } 930729 641991 1572720
427 641991 ▶ if (run) { buf[i++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { buf[i++] = (char) ('0' + run); run = 0; } 286702 355289 641991
428 641991 ▶ buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd';
Branch: 2 branches
BranchTrueFalseTotal
buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd'; 379871 262120 641991
buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd'; 189532 190339 379871
429 }
430 196590 ▶ if (run) buf[i++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) buf[i++] = (char) ('0' + run); 143641 52949 196590
431 196590 ▶ if (row) buf[i++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (row) buf[i++] = '/'; 131060 65530 196590
432 }
433 65530 buf[i++] = ' ';
434 65530 ▶ buf[i++] = (b->side == RU_SIDE_DARK) ? 'd' : 'l';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == RU_SIDE_DARK) ? 'd' : 'l'; 32568 32962 65530
435 196590 ▶ for (side = 0; side < 2; side++) {
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) { 131060 65530 196590
436 131060 buf[i++] = ' ';
437 131060 buf[i++] = (char) ('0' + b->hand[side]);
438 131060 buf[i++] = ' ';
439 131060 buf[i++] = (char) ('0' + b->home[side]);
440 }
441 65530 buf[i] = '\0';
442 65530 return i;
443 }
444  
445 /* ---- the chance of a roll -----------------------------------------------------
446 *
447 * Each die is marked or not with equal chance, so the count of marked dice is
448 * binomial. A roll is the count, except that a count of nothing is worth
449 * `zero_rolls`. WHERE THAT IS FOUR AND THERE ARE FOUR DICE, a roll of four comes
450 * up two ways, from nothing marked and from everything marked, and its weight
451 * is 2.
452 *
453 * NOTHING HERE THROWS A DIE. These are the odds a search weighs.
454 */
455  
456 struct ru_chance { int roll; int weight; };
457  
458 static const struct ru_chance FOUR_ZERO_NOTHING[] = { {0,1}, {1,4}, {2,6}, {3,4}, {4,1} };
459 static const struct ru_chance FOUR_ZERO_FOUR[] = { {1,4}, {2,6}, {3,4}, {4,2} };
460 static const struct ru_chance THREE_ZERO_NOTHING[] = { {0,1}, {1,3}, {2,3}, {3,1} };
461 static const struct ru_chance THREE_ZERO_FOUR[] = { {1,3}, {2,3}, {3,1}, {4,1} };
462  
463 60400153 static const struct ru_chance *chance_table(int dice, int zero_rolls, int *count, int *denominator)
464 {
465 60400153 ▶ if (dice == 4 && zero_rolls == 0) { *count = 5; *denominator = 16; return FOUR_ZERO_NOTHING; }
Branch: 2 branches
BranchTrueFalseTotal
if (dice == 4 && zero_rolls == 0) { *count = 5; *denominator = 16; return FOUR_ZERO_NOTHING; } 38673859 21726294 60400153
if (dice == 4 && zero_rolls == 0) { *count = 5; *denominator = 16; return FOUR_ZERO_NOTHING; } 38673853 6 38673859
466 21726300 ▶ if (dice == 4 && zero_rolls == 4) { *count = 4; *denominator = 16; return FOUR_ZERO_FOUR; }
Branch: 2 branches
BranchTrueFalseTotal
if (dice == 4 && zero_rolls == 4) { *count = 4; *denominator = 16; return FOUR_ZERO_FOUR; } 6 21726294 21726300
if (dice == 4 && zero_rolls == 4) { *count = 4; *denominator = 16; return FOUR_ZERO_FOUR; } 5 1 6
467 21726295 ▶ if (dice == 3 && zero_rolls == 0) { *count = 4; *denominator = 8; return THREE_ZERO_NOTHING; }
Branch: 2 branches
BranchTrueFalseTotal
if (dice == 3 && zero_rolls == 0) { *count = 4; *denominator = 8; return THREE_ZERO_NOTHING; } 21726292 3 21726295
if (dice == 3 && zero_rolls == 0) { *count = 4; *denominator = 8; return THREE_ZERO_NOTHING; } 5 21726287 21726292
468 21726290 ▶ if (dice == 3 && zero_rolls == 4) { *count = 4; *denominator = 8; return THREE_ZERO_FOUR; }
Branch: 2 branches
BranchTrueFalseTotal
if (dice == 3 && zero_rolls == 4) { *count = 4; *denominator = 8; return THREE_ZERO_FOUR; } 21726287 3 21726290
if (dice == 3 && zero_rolls == 4) { *count = 4; *denominator = 8; return THREE_ZERO_FOUR; } 21726287 0 21726287
469 3 *count = 0; *denominator = 0;
470 3 return NULL;
471 }
472  
473 14124084 static int ru_chance_count(int dice, int zero_rolls)
474 {
475 int count, denominator;
476 14124084 chance_table(dice, zero_rolls, &count, &denominator);
477 14124084 return count;
478 }
479  
480 46276069 static int ru_chance(int dice, int zero_rolls, int i, int *roll, int *weight, int *denominator)
481 {
482 int count, den;
483 46276069 const struct ru_chance *table = chance_table(dice, zero_rolls, &count, &den);
484 46276069 ▶ if (!table || i < 0 || i >= count) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!table || i < 0 || i >= count) return 0; 46276069 0 46276069
if (!table || i < 0 || i >= count) return 0; 46276069 0 46276069
if (!table || i < 0 || i >= count) return 0; 0 46276069 46276069
485 46276069 ▶ if (roll) *roll = table[i].roll;
Branch: 1 branch
BranchTrueFalseTotal
if (roll) *roll = table[i].roll; 46276069 0 46276069
486 46276069 ▶ if (weight) *weight = table[i].weight;
Branch: 1 branch
BranchTrueFalseTotal
if (weight) *weight = table[i].weight; 46276069 0 46276069
487 46276069 ▶ if (denominator) *denominator = den;
Branch: 1 branch
BranchTrueFalseTotal
if (denominator) *denominator = den; 46276069 0 46276069
488 46276069 return 1;
489 }
490  
491 /* ---- the table -------------------------------------------------------------- */
492  
493 static struct ru_abi RU_TABLE;
494 static int RU_TABLE_READY = 0;
495  
496 /* ru_moves.c installs itself into the table here. It reaches the board through
497 * the table and through nothing else. */
498 void ru_moves_install(struct ru_abi *t);
499 void ru_search_install(struct ru_abi *t);
500  
501 96 const struct ru_abi *ru_abi_table(void)
502 {
503 96 ▶ if (!RU_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RU_TABLE_READY) { 96 0 96
504 96 routes_init();
505 96 RU_TABLE.abi_version = RU_ABI_VERSION;
506  
507 96 RU_TABLE.board_new = ru_board_new;
508 96 RU_TABLE.board_of_string = ru_board_of_string;
509 96 RU_TABLE.board_copy = ru_board_copy;
510 96 RU_TABLE.board_drop = ru_board_drop;
511 96 RU_TABLE.live = ru_live;
512  
513 96 RU_TABLE.at = ru_at;
514 96 RU_TABLE.put = ru_put;
515 96 RU_TABLE.lift = ru_lift;
516 96 RU_TABLE.hand = ru_hand;
517 96 RU_TABLE.set_hand = ru_set_hand;
518 96 RU_TABLE.home = ru_home;
519 96 RU_TABLE.set_home = ru_set_home;
520 96 RU_TABLE.side = ru_side;
521 96 RU_TABLE.set_side = ru_set_side;
522 96 RU_TABLE.count = ru_count;
523 96 RU_TABLE.consistent = ru_consistent;
524  
525 96 RU_TABLE.key = ru_key;
526 96 RU_TABLE.to_string = ru_to_string;
527  
528 96 RU_TABLE.cell_of = ru_cell_of;
529 96 RU_TABLE.file_of = ru_file_of;
530 96 RU_TABLE.row_of = ru_row_of;
531 96 RU_TABLE.is_rosette = ru_is_rosette;
532  
533 96 RU_TABLE.route_len = ru_route_len;
534 96 RU_TABLE.route_cell = ru_route_cell;
535 96 RU_TABLE.route_step = ru_route_step;
536 96 RU_TABLE.route_shared = ru_route_shared;
537  
538 96 RU_TABLE.chance_count = ru_chance_count;
539 96 RU_TABLE.chance = ru_chance;
540  
541 96 RU_TABLE.ply = ru_ply;
542 96 RU_TABLE.set_ply = ru_set_ply;
543  
544 96 RU_TABLE_READY = 1; /* before the installer, or it recurses */
545 96 ru_moves_install(&RU_TABLE);
546 96 ru_search_install(&RU_TABLE);
547 }
548 96 return &RU_TABLE;
549 }
« ru_search.c ↑ Summary bin/royalur »