ru_engine.c

filter: Statementstmt 0.0% 0 / 264 Branchbran 0.0% 0 / 280 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 544 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« RoyalUr.xs ↑ Summary ru_moves.c »
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 0 static int ru_cell_of(int file, int row)
61 {
62 0 ▶ 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; 0 0 0
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 0 0 0
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 0 0 0
if (file < 0 || file >= RU_FILES || row < 0 || row >= RU_ROWS) return -1; 0 0 0
63 0 return CELL_AT[row][file];
64 }
65  
66 0 static int ru_file_of(int cell)
67 {
68 0 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
69 0 return FILE_AT[cell];
70 }
71  
72 0 static int ru_row_of(int cell)
73 {
74 0 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
75 0 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 0 static int ru_is_rosette(int cell)
81 {
82 0 int f = ru_file_of(cell), r = ru_row_of(cell);
83 0 ▶ if (f < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (f < 0) return 0; 0 0 0
84 0 ▶ if (r == 1) return f == 3;
Branch: 1 branch
BranchTrueFalseTotal
if (r == 1) return f == 3; 0 0 0
85 0 ▶ return f == 0 || f == 6;
Branch: 2 branches
BranchTrueFalseTotal
return f == 0 || f == 6; 0 0 0
return f == 0 || f == 6; 0 0 0
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 0 static int ru_route_len(int route)
112 {
113 0 ▶ if (route == RU_ROUTE_SHORT) return 14;
Branch: 1 branch
BranchTrueFalseTotal
if (route == RU_ROUTE_SHORT) return 14; 0 0 0
114 0 ▶ if (route == RU_ROUTE_LONG) return 16;
Branch: 1 branch
BranchTrueFalseTotal
if (route == RU_ROUTE_LONG) return 16; 0 0 0
115 0 return -1;
116 }
117  
118 0 static void routes_init(void)
119 {
120 int route, side, step, c;
121 0 ▶ if (ROUTES_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ROUTES_READY) return; 0 0 0
122 0 ▶ for (route = 0; route < 2; route++) {
Branch: 1 branch
BranchTrueFalseTotal
for (route = 0; route < 2; route++) { 0 0 0
123 0 ▶ 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; 0 0 0
124 0 int len = ru_route_len(route);
125 0 ▶ for (side = 0; side < 2; side++) {
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) { 0 0 0
126 0 ▶ 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; 0 0 0
127 0 ▶ 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; 0 0 0
128 0 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 0 0 0
129 0 int file = light[step - 1][0];
130 0 int row = light[step - 1][1];
131 int cell;
132 0 ▶ if (side == RU_SIDE_DARK) row = RU_ROWS - 1 - row;
Branch: 1 branch
BranchTrueFalseTotal
if (side == RU_SIDE_DARK) row = RU_ROWS - 1 - row; 0 0 0
133 0 cell = ru_cell_of(file, row);
134 0 ROUTE_CELL[route][side][step] = cell;
135 0 ▶ if (cell >= 0) ROUTE_STEP[route][side][cell] = step;
Branch: 1 branch
BranchTrueFalseTotal
if (cell >= 0) ROUTE_STEP[route][side][cell] = step; 0 0 0
136 }
137 }
138 }
139 0 ROUTES_READY = 1;
140 }
141  
142 0 static int ru_route_cell(int route, int side, int step)
143 {
144 0 routes_init();
145 0 ▶ 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; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return -1; 0 0 0
146 0 ▶ if (step < 1 || step > ru_route_len(route)) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (step < 1 || step > ru_route_len(route)) return -1; 0 0 0
if (step < 1 || step > ru_route_len(route)) return -1; 0 0 0
147 0 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 0 static int ru_route_step(int route, int side, int cell)
153 {
154 0 routes_init();
155 0 ▶ 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; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 0 0 0
if (route < 0 || route > 1 || side < 0 || side > 1) return 0; 0 0 0
156 0 ▶ if (cell < 0 || cell >= RU_CELLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return 0; 0 0 0
if (cell < 0 || cell >= RU_CELLS) return 0; 0 0 0
157 0 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 0 static int ru_route_shared(int route, int cell)
163 {
164 0 return ru_route_step(route, RU_SIDE_LIGHT, cell) > 0
165 0 ▶ && ru_route_step(route, RU_SIDE_DARK, cell) > 0;
Branch: 2 branches
BranchTrueFalseTotal
&& ru_route_step(route, RU_SIDE_DARK, cell) > 0; 0 0 0
&& ru_route_step(route, RU_SIDE_DARK, cell) > 0; 0 0 0
166 }
167  
168 /* ---- the board ------------------------------------------------------------- */
169  
170 0 static int clamp_pieces(int n)
171 {
172 0 ▶ if (n < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return 0; 0 0 0
173 0 ▶ if (n > RU_PIECES_MAX) return RU_PIECES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > RU_PIECES_MAX) return RU_PIECES_MAX; 0 0 0
174 0 return n;
175 }
176  
177 0 static struct ru_board *ru_board_new(int pieces)
178 {
179 struct ru_board *b;
180 0 routes_init();
181 0 b = (struct ru_board *) malloc(sizeof(struct ru_board));
182 0 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 0 0
183 0 memset(b, 0, sizeof(*b));
184 0 b->hand[RU_SIDE_LIGHT] = b->hand[RU_SIDE_DARK] = clamp_pieces(pieces);
185 0 b->side = RU_SIDE_LIGHT;
186 0 RU_LIVE++;
187 0 return b;
188 }
189  
190 0 static struct ru_board *ru_board_copy(const struct ru_board *b)
191 {
192 struct ru_board *n;
193 0 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 0 0
194 0 n = (struct ru_board *) malloc(sizeof(struct ru_board));
195 0 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 0 0
196 0 *n = *b;
197 0 RU_LIVE++;
198 0 return n;
199 }
200  
201 0 static void ru_board_drop(struct ru_board *b)
202 {
203 0 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 0 0
204 0 RU_LIVE--;
205 0 free(b);
206 }
207  
208 0 static int ru_live(void) { return RU_LIVE; }
209  
210 0 static int ru_at(const struct ru_board *b, int cell)
211 {
212 0 ▶ if (cell < 0 || cell >= RU_CELLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
if (cell < 0 || cell >= RU_CELLS) return -1; 0 0 0
213 0 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 0 static void ru_put(struct ru_board *b, int cell, int piece)
219 {
220 0 ▶ if (cell < 0 || cell >= RU_CELLS) return;
Branch: 2 branches
BranchTrueFalseTotal
if (cell < 0 || cell >= RU_CELLS) return; 0 0 0
if (cell < 0 || cell >= RU_CELLS) return; 0 0 0
221 0 ▶ if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 0 0 0
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 0 0 0
if (piece != RU_EMPTY && !RU_IS_PIECE(piece)) return; 0 0 0
222 0 b->cell[cell] = (unsigned char) piece;
223 }
224  
225 0 static void ru_lift(struct ru_board *b, int cell)
226 {
227 0 ru_put(b, cell, RU_EMPTY);
228 0 }
229  
230 0 static int ru_hand(const struct ru_board *b, int side)
231 {
232 0 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 0 0 0
if (side < 0 || side > 1) return -1; 0 0 0
233 0 return b->hand[side];
234 }
235  
236 0 static void ru_set_hand(struct ru_board *b, int side, int n)
237 {
238 0 ▶ if (side < 0 || side > 1) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return; 0 0 0
if (side < 0 || side > 1) return; 0 0 0
239 0 ▶ if (n < 0 || n > RU_PIECES_MAX) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || n > RU_PIECES_MAX) return; 0 0 0
if (n < 0 || n > RU_PIECES_MAX) return; 0 0 0
240 0 b->hand[side] = n;
241 }
242  
243 0 static int ru_home(const struct ru_board *b, int side)
244 {
245 0 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 0 0 0
if (side < 0 || side > 1) return -1; 0 0 0
246 0 return b->home[side];
247 }
248  
249 0 static void ru_set_home(struct ru_board *b, int side, int n)
250 {
251 0 ▶ if (side < 0 || side > 1) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return; 0 0 0
if (side < 0 || side > 1) return; 0 0 0
252 0 ▶ if (n < 0 || n > RU_PIECES_MAX) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || n > RU_PIECES_MAX) return; 0 0 0
if (n < 0 || n > RU_PIECES_MAX) return; 0 0 0
253 0 b->home[side] = n;
254 }
255  
256 0 static int ru_side(const struct ru_board *b) { return b->side; }
257  
258 0 static void ru_set_side(struct ru_board *b, int side)
259 {
260 0 ▶ if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return; 0 0 0
if (side != RU_SIDE_LIGHT && side != RU_SIDE_DARK) return; 0 0 0
261 0 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 0 static int ru_ply(const struct ru_board *b) { return b->ply; }
267  
268 0 static void ru_set_ply(struct ru_board *b, int ply)
269 {
270 0 ▶ if (ply < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ply < 0) return; 0 0 0
271 0 b->ply = ply;
272 }
273  
274 0 static int ru_count(const struct ru_board *b, int side)
275 {
276 0 int c, n = 0;
277 0 ▶ if (side < 0 || side > 1) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side < 0 || side > 1) return -1; 0 0 0
if (side < 0 || side > 1) return -1; 0 0 0
278 0 ▶ for (c = 0; c < RU_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RU_CELLS; c++) 0 0 0
279 0 ▶ if (b->cell[c] == RU_PIECE_OF(side)) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[c] == RU_PIECE_OF(side)) n++; 0 0 0
280 0 return n;
281 }
282  
283 0 static int ru_consistent(const struct ru_board *b, int n)
284 {
285 int side;
286 0 ▶ for (side = 0; side < 2; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) 0 0 0
287 0 ▶ 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; 0 0 0
288 0 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 0 static unsigned long long ru_key(const struct ru_board *b)
299 {
300 0 unsigned long long k = 0;
301 int c;
302 0 ▶ for (c = 0; c < RU_CELLS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RU_CELLS; c++) 0 0 0
303 0 k |= ((unsigned long long) (b->cell[c] & 3)) << (2 * c);
304 0 k |= ((unsigned long long) (b->home[RU_SIDE_LIGHT] & 7)) << 40;
305 0 k |= ((unsigned long long) (b->home[RU_SIDE_DARK] & 7)) << 43;
306 0 k |= ((unsigned long long) (b->side & 1)) << 46;
307 0 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 0 static int letter_piece(char c)
325 {
326 0 switch (c) {
327 0 case 'l': return RU_LIGHT;
328 0 case 'd': return RU_DARK;
329 0 default: return -1;
330 }
331 }
332  
333 0 static struct ru_board *refuse(struct ru_board *b, int *err, int code)
334 {
335 0 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 0 0 0
336 0 ru_board_drop(b);
337 0 return NULL;
338 }
339  
340 0 static struct ru_board *ru_board_of_string(const char *str, int *err)
341 {
342 struct ru_board *b;
343 0 int row = RU_ROWS - 1, file = 0, field;
344 int value[4];
345 0 const char *s = str;
346 size_t n;
347  
348 0 ▶ if (err) *err = RU_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = RU_OK; 0 0 0
349 0 ▶ 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 0 0
if (!str) { if (err) *err = RU_POS_NULL; return NULL; } 0 0 0
350 0 n = strlen(str);
351 0 ▶ 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; } 0 0 0
if (n == 0) { if (err) *err = RU_POS_NULL; return NULL; } 0 0 0
352 0 ▶ 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; } 0 0 0
if (n >= RU_POS_MAX) { if (err) *err = RU_POS_LONG; return NULL; } 0 0 0
353  
354 0 b = ru_board_new(0);
355 0 ▶ 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 0 0
if (!b) { if (err) *err = RU_POS_NULL; return NULL; } 0 0 0
356  
357 0 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 0 0 0
for (; *s && *s != ' '; s++) { 0 0 0
358 0 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 0 0 0
359 0 ▶ 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 0 0
360 0 ▶ if (row == 0) return refuse(b, err, RU_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (row == 0) return refuse(b, err, RU_POS_ROWS); 0 0 0
361 0 row--; file = 0;
362 0 continue;
363 }
364 0 ▶ if (*s >= '1' && *s <= '8') {
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '8') { 0 0 0
if (*s >= '1' && *s <= '8') { 0 0 0
365 0 int run = *s - '0';
366 0 ▶ while (run-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (run-- > 0) { 0 0 0
367 0 ▶ 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 0 0
368 0 ▶ 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); 0 0 0
369 0 file++;
370 }
371 0 continue;
372 }
373 0 ▶ if (*s == 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'x') { 0 0 0
374 0 ▶ 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 0 0
375 0 ▶ 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); 0 0 0
376 0 file++;
377 0 continue;
378 }
379 {
380 0 int piece = letter_piece(*s);
381 0 ▶ if (piece < 0) return refuse(b, err, RU_POS_LETTER);
Branch: 1 branch
BranchTrueFalseTotal
if (piece < 0) return refuse(b, err, RU_POS_LETTER); 0 0 0
382 0 ▶ 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 0 0
383 0 ▶ 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); 0 0 0
384 0 ru_put(b, ru_cell_of(file, row), piece);
385 0 file++;
386 }
387 }
388 0 ▶ if (row != 0) return refuse(b, err, RU_POS_ROWS);
Branch: 1 branch
BranchTrueFalseTotal
if (row != 0) return refuse(b, err, RU_POS_ROWS); 0 0 0
389 0 ▶ 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 0 0
390  
391 0 ▶ if (*s != ' ') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return refuse(b, err, RU_POS_FIELD); 0 0 0
392 0 s++;
393 0 ▶ if (*s == 'l') ru_set_side(b, RU_SIDE_LIGHT);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'l') ru_set_side(b, RU_SIDE_LIGHT); 0 0 0
394 0 ▶ 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); 0 0 0
395 0 else return refuse(b, err, RU_POS_SIDE);
396 0 s++;
397  
398 0 ▶ for (field = 0; field < 4; field++) {
Branch: 1 branch
BranchTrueFalseTotal
for (field = 0; field < 4; field++) { 0 0 0
399 0 ▶ if (*s != ' ') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return refuse(b, err, RU_POS_FIELD); 0 0 0
400 0 s++;
401 0 ▶ if (*s == '\0') return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\0') return refuse(b, err, RU_POS_FIELD); 0 0 0
402 0 ▶ 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); 0 0 0
if (*s < '0' || *s > '0' + RU_PIECES_MAX) return refuse(b, err, RU_POS_COUNT); 0 0 0
403 0 value[field] = *s - '0';
404 0 s++;
405 0 ▶ if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT);
Branch: 2 branches
BranchTrueFalseTotal
if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT); 0 0 0
if (*s && *s != ' ') return refuse(b, err, RU_POS_COUNT); 0 0 0
406 }
407 0 ▶ if (*s) return refuse(b, err, RU_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s) return refuse(b, err, RU_POS_FIELD); 0 0 0
408  
409 0 ru_set_hand(b, RU_SIDE_LIGHT, value[0]);
410 0 ru_set_home(b, RU_SIDE_LIGHT, value[1]);
411 0 ru_set_hand(b, RU_SIDE_DARK, value[2]);
412 0 ru_set_home(b, RU_SIDE_DARK, value[3]);
413  
414 0 return b;
415 }
416  
417 0 static int ru_to_string(const struct ru_board *b, char *buf, int len)
418 {
419 0 int row, file, i = 0, run, side;
420 0 ▶ if (!b || !buf || len < RU_POS_MAX) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !buf || len < RU_POS_MAX) return -1; 0 0 0
if (!b || !buf || len < RU_POS_MAX) return -1; 0 0 0
if (!b || !buf || len < RU_POS_MAX) return -1; 0 0 0
421 0 ▶ for (row = RU_ROWS - 1; row >= 0; row--) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = RU_ROWS - 1; row >= 0; row--) { 0 0 0
422 0 run = 0;
423 0 ▶ for (file = 0; file < RU_FILES; file++) {
Branch: 1 branch
BranchTrueFalseTotal
for (file = 0; file < RU_FILES; file++) { 0 0 0
424 0 int cell = ru_cell_of(file, row);
425 0 ▶ int piece = cell < 0 ? -1 : b->cell[cell];
Branch: 1 branch
BranchTrueFalseTotal
int piece = cell < 0 ? -1 : b->cell[cell]; 0 0 0
426 0 ▶ if (piece == RU_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (piece == RU_EMPTY) { run++; continue; } 0 0 0
427 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
428 0 ▶ buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd';
Branch: 2 branches
BranchTrueFalseTotal
buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd'; 0 0 0
buf[i++] = piece < 0 ? 'x' : piece == RU_LIGHT ? 'l' : 'd'; 0 0 0
429 }
430 0 ▶ if (run) buf[i++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) buf[i++] = (char) ('0' + run); 0 0 0
431 0 ▶ if (row) buf[i++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (row) buf[i++] = '/'; 0 0 0
432 }
433 0 buf[i++] = ' ';
434 0 ▶ buf[i++] = (b->side == RU_SIDE_DARK) ? 'd' : 'l';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == RU_SIDE_DARK) ? 'd' : 'l'; 0 0 0
435 0 ▶ for (side = 0; side < 2; side++) {
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < 2; side++) { 0 0 0
436 0 buf[i++] = ' ';
437 0 buf[i++] = (char) ('0' + b->hand[side]);
438 0 buf[i++] = ' ';
439 0 buf[i++] = (char) ('0' + b->home[side]);
440 }
441 0 buf[i] = '\0';
442 0 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 0 static const struct ru_chance *chance_table(int dice, int zero_rolls, int *count, int *denominator)
464 {
465 0 ▶ 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; } 0 0 0
if (dice == 4 && zero_rolls == 0) { *count = 5; *denominator = 16; return FOUR_ZERO_NOTHING; } 0 0 0
466 0 ▶ 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; } 0 0 0
if (dice == 4 && zero_rolls == 4) { *count = 4; *denominator = 16; return FOUR_ZERO_FOUR; } 0 0 0
467 0 ▶ 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; } 0 0 0
if (dice == 3 && zero_rolls == 0) { *count = 4; *denominator = 8; return THREE_ZERO_NOTHING; } 0 0 0
468 0 ▶ 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; } 0 0 0
if (dice == 3 && zero_rolls == 4) { *count = 4; *denominator = 8; return THREE_ZERO_FOUR; } 0 0 0
469 0 *count = 0; *denominator = 0;
470 0 return NULL;
471 }
472  
473 0 static int ru_chance_count(int dice, int zero_rolls)
474 {
475 int count, denominator;
476 0 chance_table(dice, zero_rolls, &count, &denominator);
477 0 return count;
478 }
479  
480 0 static int ru_chance(int dice, int zero_rolls, int i, int *roll, int *weight, int *denominator)
481 {
482 int count, den;
483 0 const struct ru_chance *table = chance_table(dice, zero_rolls, &count, &den);
484 0 ▶ if (!table || i < 0 || i >= count) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!table || i < 0 || i >= count) return 0; 0 0 0
if (!table || i < 0 || i >= count) return 0; 0 0 0
if (!table || i < 0 || i >= count) return 0; 0 0 0
485 0 ▶ if (roll) *roll = table[i].roll;
Branch: 1 branch
BranchTrueFalseTotal
if (roll) *roll = table[i].roll; 0 0 0
486 0 ▶ if (weight) *weight = table[i].weight;
Branch: 1 branch
BranchTrueFalseTotal
if (weight) *weight = table[i].weight; 0 0 0
487 0 ▶ if (denominator) *denominator = den;
Branch: 1 branch
BranchTrueFalseTotal
if (denominator) *denominator = den; 0 0 0
488 0 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 0 const struct ru_abi *ru_abi_table(void)
502 {
503 0 ▶ if (!RU_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!RU_TABLE_READY) { 0 0 0
504 0 routes_init();
505 0 RU_TABLE.abi_version = RU_ABI_VERSION;
506  
507 0 RU_TABLE.board_new = ru_board_new;
508 0 RU_TABLE.board_of_string = ru_board_of_string;
509 0 RU_TABLE.board_copy = ru_board_copy;
510 0 RU_TABLE.board_drop = ru_board_drop;
511 0 RU_TABLE.live = ru_live;
512  
513 0 RU_TABLE.at = ru_at;
514 0 RU_TABLE.put = ru_put;
515 0 RU_TABLE.lift = ru_lift;
516 0 RU_TABLE.hand = ru_hand;
517 0 RU_TABLE.set_hand = ru_set_hand;
518 0 RU_TABLE.home = ru_home;
519 0 RU_TABLE.set_home = ru_set_home;
520 0 RU_TABLE.side = ru_side;
521 0 RU_TABLE.set_side = ru_set_side;
522 0 RU_TABLE.count = ru_count;
523 0 RU_TABLE.consistent = ru_consistent;
524  
525 0 RU_TABLE.key = ru_key;
526 0 RU_TABLE.to_string = ru_to_string;
527  
528 0 RU_TABLE.cell_of = ru_cell_of;
529 0 RU_TABLE.file_of = ru_file_of;
530 0 RU_TABLE.row_of = ru_row_of;
531 0 RU_TABLE.is_rosette = ru_is_rosette;
532  
533 0 RU_TABLE.route_len = ru_route_len;
534 0 RU_TABLE.route_cell = ru_route_cell;
535 0 RU_TABLE.route_step = ru_route_step;
536 0 RU_TABLE.route_shared = ru_route_shared;
537  
538 0 RU_TABLE.chance_count = ru_chance_count;
539 0 RU_TABLE.chance = ru_chance;
540  
541 0 RU_TABLE.ply = ru_ply;
542 0 RU_TABLE.set_ply = ru_set_ply;
543  
544 0 RU_TABLE_READY = 1; /* before the installer, or it recurses */
545 0 ru_moves_install(&RU_TABLE);
546 0 ru_search_install(&RU_TABLE);
547 }
548 0 return &RU_TABLE;
549 }
« RoyalUr.xs ↑ Summary ru_moves.c »