sen_engine.c

filter: Statementstmt 100% 215 / 215 Branchbran 88.9% 217 / 244 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.1% 432 / 459 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sen_search.c ↑ Summary sen_moves.c »
1 /* sen_engine.c - the board of Senet, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/sen_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 thirty houses, the side to move, the number of pieces
7 * a side has, the grid, the marked houses, the two named rule sets as data, the
8 * key, the position string in and out, and the chance of each roll. What a
9 * roll allows is sen_moves.c, which appends to the table.
10 */
11  
12 #include <stdlib.h>
13 #include <string.h>
14 #include "sen_abi.h"
15  
16 /* ---- the position ---------------------------------------------------------
17 *
18 * No pointers, so a copy is a struct assignment and two boards never share
19 * anything. house[0] is never read or written; house h is house[h].
20 *
21 * THE HOMES ARE DERIVED AND NOT STORED. Nothing enters this board from off it,
22 * so a stored home could only ever disagree with the houses.
23 */
24  
25 struct sen_board {
26 unsigned char house[SEN_HOUSES + 1];
27 int pieces;
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 SEN_LIVE = 0;
35  
36 /* ---- geometry ---------------------------------------------------------------
37 *
38 * The reversed S: the top row left to right, the middle row right to left, the
39 * bottom row left to right. Three formulas, and t/01-houses.t types the grid out
40 * by hand to check them.
41 */
42  
43 36 static int sen_house_of(int row, int col)
44 {
45 36 ▶ if (col < 0 || col >= SEN_COLS) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (col < 0 || col >= SEN_COLS) return -1; 35 1 36
if (col < 0 || col >= SEN_COLS) return -1; 1 34 35
46 34 ▶ if (row == 0) return col + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (row == 0) return col + 1; 10 24 34
47 24 ▶ if (row == 1) return 20 - col;
Branch: 1 branch
BranchTrueFalseTotal
if (row == 1) return 20 - col; 12 12 24
48 12 ▶ if (row == 2) return 21 + col;
Branch: 1 branch
BranchTrueFalseTotal
if (row == 2) return 21 + col; 10 2 12
49 2 return -1;
50 }
51  
52 72 static int sen_row_of(int house)
53 {
54 72 ▶ if (house < 1 || house > SEN_HOUSES) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return -1; 71 1 72
if (house < 1 || house > SEN_HOUSES) return -1; 1 70 71
55 70 return (house - 1) / SEN_COLS;
56 }
57  
58 39 static int sen_col_of(int house)
59 {
60 39 ▶ if (house < 1 || house > SEN_HOUSES) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return -1; 38 1 39
if (house < 1 || house > SEN_HOUSES) return -1; 1 37 38
61 37 ▶ if (sen_row_of(house) == 1) return 20 - house;
Branch: 1 branch
BranchTrueFalseTotal
if (sen_row_of(house) == 1) return 20 - house; 14 23 37
62 23 return (house - 1) % SEN_COLS;
63 }
64  
65 /* The house of rebirth and the last five. The same under every set: which of
66 * them is SAFE is the set's, and lives in the rules struct. */
67 64 static int sen_is_marked(int house)
68 {
69 64 ▶ return house == SEN_REBIRTH || (house >= 26 && house <= SEN_HOUSES);
Branch: 3 branches
BranchTrueFalseTotal
return house == SEN_REBIRTH || (house >= 26 && house <= SEN_HOUSES); 60 4 64
return house == SEN_REBIRTH || (house >= 26 && house <= SEN_HOUSES); 11 49 60
return house == SEN_REBIRTH || (house >= 26 && house <= SEN_HOUSES); 10 1 11
70 }
71  
72 /* ---- the named rule sets ----------------------------------------------------
73 *
74 * THE ONLY PLACE IN THE C WHERE A SET HAS A NAME. Everything else reads fields.
75 * Each is written out from plan_game_senet/00-overview.md: `kendall` from G's
76 * Kendall set, `skyruk` from G's Skyruk set, every divergence between the
77 * sources decided there and not here.
78 */
79  
80 #define SAFE(h) (1UL << (h))
81 #define W SEN_X_WALK
82 #define O SEN_X_OFF
83 #define S SEN_X_STAY
84  
85 static const struct sen_rules RULES_KENDALL = {
86 5, 3, SEN_PROTECT_RUN, 1, SEN_REACH_EXACT, 0,
87 SAFE(15) | SAFE(26) | SAFE(28) | SAFE(29) | SAFE(30),
88 {
89 { {W}, {28}, {29}, {30}, {O} },
90 { {0}, {0}, {0}, {0}, {0} },
91 { {0}, {0}, {O}, {0}, {0} },
92 { {0}, {O}, {0}, {0}, {0} },
93 { {O}, {0}, {0}, {0}, {0} }
94 },
95 { 0, 0, 0, 0 }
96 };
97  
98 static const struct sen_rules RULES_SKYRUK = {
99 5, 2, SEN_PROTECT_ENDS, 1, SEN_REACH_ANY, 1,
100 SAFE(15) | SAFE(26) | SAFE(27) | SAFE(28) | SAFE(29) | SAFE(30),
101 {
102 { {27, W}, {28, 27, W}, {29, 27, W}, {30, 27, W}, {O} },
103 { {W}, {W}, {W}, {O}, {S} },
104 { {0}, {0}, {O}, {0}, {0} },
105 { {0}, {O}, {0}, {0}, {0} },
106 { {O}, {O}, {O}, {O}, {O} }
107 },
108 { 0, 0, 0, 0 }
109 };
110  
111 #undef W
112 #undef O
113 #undef S
114  
115 4812575 static int sen_rules_named(int name, struct sen_rules *r)
116 {
117 4812575 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 4812575 4812575
118 4812575 ▶ if (name == SEN_RULES_KENDALL) { *r = RULES_KENDALL; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (name == SEN_RULES_KENDALL) { *r = RULES_KENDALL; return 1; } 3856821 955754 4812575
119 955754 ▶ if (name == SEN_RULES_SKYRUK) { *r = RULES_SKYRUK; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (name == SEN_RULES_SKYRUK) { *r = RULES_SKYRUK; return 1; } 955752 2 955754
120 2 return 0;
121 }
122  
123 2406277 static int sen_rules_id(const char *name)
124 {
125 2406277 ▶ if (!name) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return -1; 0 2406277 2406277
126 2406277 ▶ if (strcmp(name, "kendall") == 0) return SEN_RULES_KENDALL;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "kendall") == 0) return SEN_RULES_KENDALL; 1450524 955753 2406277
127 955753 ▶ if (strcmp(name, "skyruk") == 0) return SEN_RULES_SKYRUK;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "skyruk") == 0) return SEN_RULES_SKYRUK; 955749 4 955753
128 4 return -1;
129 }
130  
131 8805664 static int sen_rules_ok(const struct sen_rules *r)
132 {
133 int h, roll, i;
134 8805664 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 8805664 8805664
135 8805664 ▶ if (r->pieces < 1 || r->pieces > SEN_PIECES_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->pieces < 1 || r->pieces > SEN_PIECES_MAX) return 0; 8805662 2 8805664
if (r->pieces < 1 || r->pieces > SEN_PIECES_MAX) return 0; 2 8805660 8805662
136 8805660 ▶ if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0; 8605050 200610 8805660
if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0; 4568132 4036918 8605050
if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0; 3 4568129 4568132
137 8805657 ▶ if (r->protect < SEN_PROTECT_NONE || r->protect > SEN_PROTECT_ENDS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->protect < SEN_PROTECT_NONE || r->protect > SEN_PROTECT_ENDS) return 0; 8805656 1 8805657
if (r->protect < SEN_PROTECT_NONE || r->protect > SEN_PROTECT_ENDS) return 0; 1 8805655 8805656
138 8805655 ▶ if (r->backward != 0 && r->backward != 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->backward != 0 && r->backward != 1) return 0; 8805056 599 8805655
if (r->backward != 0 && r->backward != 1) return 0; 2 8805054 8805056
139 8805653 ▶ if (r->reach26 != SEN_REACH_EXACT && r->reach26 != SEN_REACH_ANY) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->reach26 != SEN_REACH_EXACT && r->reach26 != SEN_REACH_ANY) return 0; 6236203 2569450 8805653
if (r->reach26 != SEN_REACH_EXACT && r->reach26 != SEN_REACH_ANY) return 0; 1 6236202 6236203
140 8805652 ▶ if (r->throw_on_26 != 0 && r->throw_on_26 != 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->throw_on_26 != 0 && r->throw_on_26 != 1) return 0; 6236204 2569448 8805652
if (r->throw_on_26 != 0 && r->throw_on_26 != 1) return 0; 2 6236202 6236204
141 8805650 ▶ if (r->safe & 1UL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->safe & 1UL) return 0; 1 8805649 8805650
142 8805649 ▶ if (r->safe >> (SEN_HOUSES + 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->safe >> (SEN_HOUSES + 1)) return 0; 1 8805648 8805649
143 52833867 ▶ for (h = 0; h < SEN_EXIT_HOUSES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < SEN_EXIT_HOUSES; h++) { 44028224 8805643 52833867
144 264169323 ▶ for (roll = 0; roll < SEN_ROLL_MAX; roll++) {
Branch: 1 branch
BranchTrueFalseTotal
for (roll = 0; roll < SEN_ROLL_MAX; roll++) { 220141104 44028219 264169323
145 220141104 int ended = 0;
146 1100705507 ▶ for (i = 0; i < SEN_CHAIN_MAX; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SEN_CHAIN_MAX; i++) { 880564408 220141099 1100705507
147 880564408 int e = r->exit[h][roll][i];
148 880564408 ▶ if (e < SEN_X_END || e > SEN_X_STAY) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (e < SEN_X_END || e > SEN_X_STAY) return 0; 880564407 1 880564408
if (e < SEN_X_END || e > SEN_X_STAY) return 0; 1 880564406 880564407
149 880564406 ▶ if (ended && e != SEN_X_END) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ended && e != SEN_X_END) return 0; 490198932 390365474 880564406
if (ended && e != SEN_X_END) return 0; 3 490198929 490198932
150 880564403 ▶ if (e == SEN_X_END) ended = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e == SEN_X_END) ended = 1; 710340030 170224373 880564403
151 }
152 }
153 }
154 44028211 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 35222569 8805642 44028211
155 35222569 ▶ if (r->reserved[i] != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->reserved[i] != 0) return 0; 1 35222568 35222569
156 8805642 return 1;
157 }
158  
159 /* ---- the board ------------------------------------------------------------- */
160  
161 796478 static int sen_clamp_pieces(int n)
162 {
163 796478 ▶ if (n < 1) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) return 1; 0 796478 796478
164 796478 ▶ if (n > SEN_PIECES_MAX) return SEN_PIECES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SEN_PIECES_MAX) return SEN_PIECES_MAX; 0 796478 796478
165 796478 return n;
166 }
167  
168 796478 static struct sen_board *sen_board_new(int pieces)
169 {
170 796478 struct sen_board *b = (struct sen_board *) malloc(sizeof(struct sen_board));
171 796478 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 796478 796478
172 796478 memset(b, 0, sizeof(*b));
173 796478 b->pieces = sen_clamp_pieces(pieces);
174 796478 b->side = SEN_SIDE_LIGHT;
175 796478 SEN_LIVE++;
176 796478 return b;
177 }
178  
179 637879 static struct sen_board *sen_board_copy(const struct sen_board *b)
180 {
181 struct sen_board *n;
182 637879 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 637879 637879
183 637879 n = (struct sen_board *) malloc(sizeof(struct sen_board));
184 637879 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 637879 637879
185 637879 *n = *b;
186 637879 SEN_LIVE++;
187 637879 return n;
188 }
189  
190 1434357 static void sen_board_drop(struct sen_board *b)
191 {
192 1434357 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 1434357 1434357
193 1434357 SEN_LIVE--;
194 1434357 free(b);
195 }
196  
197 5 static int sen_live(void) { return SEN_LIVE; }
198  
199 627875374 static int sen_at(const struct sen_board *b, int house)
200 {
201 627875374 ▶ if (house < 1 || house > SEN_HOUSES) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return -1; 627875372 2 627875374
if (house < 1 || house > SEN_HOUSES) return -1; 1 627875371 627875372
202 627875371 return b->house[house];
203 }
204  
205 /* A value that is not a piece or SEN_EMPTY is ignored, and so is a house that
206 * is not one. */
207 25487550 static void sen_put(struct sen_board *b, int house, int piece)
208 {
209 25487550 ▶ if (house < 1 || house > SEN_HOUSES) return;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return; 25487549 1 25487550
if (house < 1 || house > SEN_HOUSES) return; 1 25487548 25487549
210 25487548 ▶ if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 15790786 9696762 25487548
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 7889293 7901493 15790786
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 1 7889292 7889293
211 25487547 b->house[house] = (unsigned char) piece;
212 }
213  
214 4614160 static void sen_lift(struct sen_board *b, int house)
215 {
216 4614160 sen_put(b, house, SEN_EMPTY);
217 4614160 }
218  
219 71955855 static int sen_side(const struct sen_board *b) { return b->side; }
220  
221 6751428 static void sen_set_side(struct sen_board *b, int side)
222 {
223 6751428 ▶ if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return; 3232036 3519392 6751428
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return; 1 3232035 3232036
224 6751427 b->side = side;
225 }
226  
227 661 static int sen_pieces(const struct sen_board *b) { return b->pieces; }
228  
229 41876799 static int sen_count(const struct sen_board *b, int side)
230 {
231 41876799 int h, n = 0;
232 41876799 ▶ if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 20610540 21266259 41876799
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 1 20610539 20610540
233 1298180738 ▶ for (h = 1; h <= SEN_HOUSES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= SEN_HOUSES; h++) 1256303940 41876798 1298180738
234 1256303940 ▶ if (b->house[h] == SEN_PIECE_OF(side)) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (b->house[h] == SEN_PIECE_OF(side)) n++; 175204008 1081099932 1256303940
235 41876798 return n;
236 }
237  
238 7154339 static int sen_home(const struct sen_board *b, int side)
239 {
240 7154339 ▶ if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 3580612 3573727 7154339
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 1 3580611 3580612
241 7154338 return b->pieces - sen_count(b, side);
242 }
243  
244 83507 static int sen_consistent(const struct sen_board *b)
245 {
246 83507 ▶ return sen_home(b, SEN_SIDE_LIGHT) >= 0 && sen_home(b, SEN_SIDE_DARK) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
return sen_home(b, SEN_SIDE_LIGHT) >= 0 && sen_home(b, SEN_SIDE_DARK) >= 0; 83503 4 83507
return sen_home(b, SEN_SIDE_LIGHT) >= 0 && sen_home(b, SEN_SIDE_DARK) >= 0; 83500 3 83503
247 }
248  
249 /* The plies made to reach the position. It is carried by the board and is not
250 * part of the position: two boards that differ only in it have one key. */
251 16854923 static int sen_ply(const struct sen_board *b) { return b->ply; }
252  
253 8629604 static void sen_set_ply(struct sen_board *b, int ply)
254 {
255 8629604 ▶ if (ply < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ply < 0) return; 1 8629603 8629604
256 8629603 b->ply = ply;
257 }
258  
259 /* ---- the key ----------------------------------------------------------------
260 *
261 * EXACT, and recomputed every time: thirty houses at two bits and the side to
262 * move. Sixty-one bits, no table and no collision. The homes and `pieces` are
263 * left out on purpose; see sen_abi.h.
264 */
265  
266 119370 static unsigned long long sen_key(const struct sen_board *b)
267 {
268 119370 unsigned long long k = 0;
269 int h;
270 3700470 ▶ for (h = 1; h <= SEN_HOUSES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= SEN_HOUSES; h++) 3581100 119370 3700470
271 3581100 k |= ((unsigned long long) (b->house[h] & 3)) << (2 * (h - 1));
272 119370 k |= ((unsigned long long) (b->side & 1)) << 60;
273 119370 return k;
274 }
275  
276 /* ---- the position string ------------------------------------------------------
277 *
278 * The houses IN TRACK ORDER, three groups of ten with a slash between, a
279 * character a house, then the side to move and `pieces`:
280 *
281 * dldldldldl/........../.......... l 5
282 *
283 * l is a light piece, d a dark one and . an empty house. Character k of the
284 * thirty is house k: the middle group runs 11 to 20 from the left, which the
285 * grid draws from the right. A string is not a picture.
286 *
287 * EVERY FIELD IS REQUIRED. Defaulting one would turn a string cut short into a
288 * position with a player to move.
289 */
290  
291 2506625 static int sen_letter_piece(char c)
292 {
293 2506625 switch (c) {
294 267671 case 'l': return SEN_LIGHT;
295 295642 case 'd': return SEN_DARK;
296 1943278 case '.': return SEN_EMPTY;
297 34 default: return -1;
298 }
299 }
300  
301 83 static struct sen_board *sen_refuse(struct sen_board *b, int *err, int code)
302 {
303 83 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 83 0 83
304 83 sen_board_drop(b);
305 83 return NULL;
306 }
307  
308 83587 static struct sen_board *sen_board_of_string(const char *str, int *err)
309 {
310 struct sen_board *b;
311 83587 int group = 0, width = 0, house = 0;
312 83587 const char *s = str;
313 size_t n;
314  
315 83587 ▶ if (err) *err = SEN_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SEN_OK; 83587 0 83587
316 83587 ▶ if (!str) { if (err) *err = SEN_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str) { if (err) *err = SEN_POS_NULL; return NULL; } 0 83587 83587
if (!str) { if (err) *err = SEN_POS_NULL; return NULL; } 0 0 0
317 83587 n = strlen(str);
318 83587 ▶ if (n == 0) { if (err) *err = SEN_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0) { if (err) *err = SEN_POS_NULL; return NULL; } 3 83584 83587
if (n == 0) { if (err) *err = SEN_POS_NULL; return NULL; } 3 0 3
319 83584 ▶ if (n >= SEN_POS_MAX) { if (err) *err = SEN_POS_LONG; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (n >= SEN_POS_MAX) { if (err) *err = SEN_POS_LONG; return NULL; } 2 83582 83584
if (n >= SEN_POS_MAX) { if (err) *err = SEN_POS_LONG; return NULL; } 2 0 2
320  
321 83582 b = sen_board_new(1);
322 83582 ▶ if (!b) { if (err) *err = SEN_POS_NULL; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!b) { if (err) *err = SEN_POS_NULL; return NULL; } 0 83582 83582
if (!b) { if (err) *err = SEN_POS_NULL; return NULL; } 0 0 0
323  
324 2757270 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 2757268 2 2757270
for (; *s && *s != ' '; s++) { 2673734 83534 2757268
325 int piece;
326 2673734 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 167109 2506625 2673734
327 167109 ▶ if (width != SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (width != SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH); 8 167101 167109
328 167101 ▶ if (group == SEN_ROWS - 1) return sen_refuse(b, err, SEN_POS_GROUPS);
Branch: 1 branch
BranchTrueFalseTotal
if (group == SEN_ROWS - 1) return sen_refuse(b, err, SEN_POS_GROUPS); 2 167099 167101
329 167099 group++; width = 0;
330 167099 continue;
331 }
332 2506625 piece = sen_letter_piece(*s);
333 2506625 ▶ if (piece < 0) return sen_refuse(b, err, SEN_POS_LETTER);
Branch: 1 branch
BranchTrueFalseTotal
if (piece < 0) return sen_refuse(b, err, SEN_POS_LETTER); 34 2506591 2506625
334 2506591 ▶ if (width >= SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (width >= SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH); 2 2506589 2506591
335 2506589 house = group * SEN_COLS + width + 1;
336 2506589 sen_put(b, house, piece);
337 2506589 width++;
338 }
339 83536 ▶ if (group != SEN_ROWS - 1) return sen_refuse(b, err, SEN_POS_GROUPS);
Branch: 1 branch
BranchTrueFalseTotal
if (group != SEN_ROWS - 1) return sen_refuse(b, err, SEN_POS_GROUPS); 6 83530 83536
340 83530 ▶ if (width != SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH);
Branch: 1 branch
BranchTrueFalseTotal
if (width != SEN_COLS) return sen_refuse(b, err, SEN_POS_WIDTH); 2 83528 83530
341  
342 83528 ▶ if (*s != ' ') return sen_refuse(b, err, SEN_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s != ' ') return sen_refuse(b, err, SEN_POS_FIELD); 2 83526 83528
343 83526 s++;
344 83526 ▶ if (*s == 'l') sen_set_side(b, SEN_SIDE_LIGHT);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'l') sen_set_side(b, SEN_SIDE_LIGHT); 42436 41090 83526
345 41090 ▶ else if (*s == 'd') sen_set_side(b, SEN_SIDE_DARK);
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == 'd') sen_set_side(b, SEN_SIDE_DARK); 41084 6 41090
346 6 ▶ else if (*s == '\0') return sen_refuse(b, err, SEN_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
else if (*s == '\0') return sen_refuse(b, err, SEN_POS_FIELD); 0 6 6
347 6 else return sen_refuse(b, err, SEN_POS_SIDE);
348 83520 s++;
349  
350 83520 ▶ if (*s != ' ') return sen_refuse(b, err, *s ? SEN_POS_SIDE : SEN_POS_FIELD);
Branch: 2 branches
BranchTrueFalseTotal
if (*s != ' ') return sen_refuse(b, err, *s ? SEN_POS_SIDE : SEN_POS_FIELD); 4 83516 83520
if (*s != ' ') return sen_refuse(b, err, *s ? SEN_POS_SIDE : SEN_POS_FIELD); 2 2 4
351 83516 s++;
352 83516 ▶ if (*s == '\0') return sen_refuse(b, err, SEN_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\0') return sen_refuse(b, err, SEN_POS_FIELD); 2 83514 83516
353 83514 ▶ if (*s < '1' || *s > '0' + SEN_PIECES_MAX) return sen_refuse(b, err, SEN_POS_PIECES);
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '1' || *s > '0' + SEN_PIECES_MAX) return sen_refuse(b, err, SEN_POS_PIECES); 83512 2 83514
if (*s < '1' || *s > '0' + SEN_PIECES_MAX) return sen_refuse(b, err, SEN_POS_PIECES); 4 83508 83512
354 83508 b->pieces = *s - '0';
355 83508 s++;
356 83508 ▶ if (*s && *s != ' ') return sen_refuse(b, err, SEN_POS_PIECES);
Branch: 2 branches
BranchTrueFalseTotal
if (*s && *s != ' ') return sen_refuse(b, err, SEN_POS_PIECES); 4 83504 83508
if (*s && *s != ' ') return sen_refuse(b, err, SEN_POS_PIECES); 2 2 4
357 83506 ▶ if (*s) return sen_refuse(b, err, SEN_POS_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
if (*s) return sen_refuse(b, err, SEN_POS_FIELD); 2 83504 83506
358  
359 83504 ▶ if (!sen_consistent(b)) return sen_refuse(b, err, SEN_POS_EXCESS);
Branch: 1 branch
BranchTrueFalseTotal
if (!sen_consistent(b)) return sen_refuse(b, err, SEN_POS_EXCESS); 5 83499 83504
360 83499 return b;
361 }
362  
363 116448 static int sen_to_string(const struct sen_board *b, char *buf, int len)
364 {
365 116448 int h, i = 0;
366 116448 ▶ if (!b || !buf || len < SEN_POS_MAX) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !buf || len < SEN_POS_MAX) return -1; 116448 0 116448
if (!b || !buf || len < SEN_POS_MAX) return -1; 116448 0 116448
if (!b || !buf || len < SEN_POS_MAX) return -1; 0 116448 116448
367 3609888 ▶ for (h = 1; h <= SEN_HOUSES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= SEN_HOUSES; h++) { 3493440 116448 3609888
368 3493440 int p = b->house[h];
369 3493440 ▶ buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.';
Branch: 2 branches
BranchTrueFalseTotal
buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.'; 3102397 391043 3493440
buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.'; 421548 2680849 3102397
370 3493440 ▶ if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/';
Branch: 2 branches
BranchTrueFalseTotal
if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/'; 349344 3144096 3493440
if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/'; 232896 116448 349344
371 }
372 116448 buf[i++] = ' ';
373 116448 ▶ buf[i++] = (b->side == SEN_SIDE_DARK) ? 'd' : 'l';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == SEN_SIDE_DARK) ? 'd' : 'l'; 57523 58925 116448
374 116448 buf[i++] = ' ';
375 116448 buf[i++] = (char) ('0' + b->pieces);
376 116448 buf[i] = '\0';
377 116448 return i;
378 }
379  
380 /* ---- the chance of a roll -----------------------------------------------------
381 *
382 * Each stick lands light or dark with equal chance, so the count of light faces
383 * is binomial over sixteen ways: 1, 4, 6, 4, 1 for 0 to 4. A throw of 0 is a
384 * roll of 5, so IN ORDER OF ROLL the weights are 4, 6, 4, 1, 1. The last row is
385 * the throw of nothing.
386 *
387 * NOTHING HERE THROWS A STICK. These are the odds a search weighs.
388 */
389  
390 struct sen_chance { int roll; int weight; };
391  
392 static const struct sen_chance CHANCE[] = { {1,4}, {2,6}, {3,4}, {4,1}, {5,1} };
393  
394 #define CHANCE_COUNT 5
395 #define CHANCE_DENOMINATOR 16
396  
397 1500384 static int sen_chance_count(void) { return CHANCE_COUNT; }
398  
399 1299988 static int sen_chance(int i, int *roll, int *weight, int *denominator)
400 {
401 1299988 ▶ if (i < 0 || i >= CHANCE_COUNT) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i < 0 || i >= CHANCE_COUNT) return 0; 1299987 1 1299988
if (i < 0 || i >= CHANCE_COUNT) return 0; 1 1299986 1299987
402 1299986 ▶ if (roll) *roll = CHANCE[i].roll;
Branch: 1 branch
BranchTrueFalseTotal
if (roll) *roll = CHANCE[i].roll; 1299986 0 1299986
403 1299986 ▶ if (weight) *weight = CHANCE[i].weight;
Branch: 1 branch
BranchTrueFalseTotal
if (weight) *weight = CHANCE[i].weight; 1299986 0 1299986
404 1299986 ▶ if (denominator) *denominator = CHANCE_DENOMINATOR;
Branch: 1 branch
BranchTrueFalseTotal
if (denominator) *denominator = CHANCE_DENOMINATOR; 1299986 0 1299986
405 1299986 return 1;
406 }
407  
408 4600745 static int sen_extra(int roll)
409 {
410 4600745 ▶ if (roll < 1 || roll > SEN_ROLL_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (roll < 1 || roll > SEN_ROLL_MAX) return -1; 4600744 1 4600745
if (roll < 1 || roll > SEN_ROLL_MAX) return -1; 1 4600743 4600744
411 4600743 ▶ return roll == 1 || roll == 4 || roll == 5;
Branch: 3 branches
BranchTrueFalseTotal
return roll == 1 || roll == 4 || roll == 5; 3586591 1014152 4600743
return roll == 1 || roll == 4 || roll == 5; 2850282 736309 3586591
return roll == 1 || roll == 4 || roll == 5; 718380 2131902 2850282
412 }
413  
414 /* ---- the table -------------------------------------------------------------- */
415  
416 static struct sen_abi SEN_TABLE;
417 static int SEN_TABLE_READY = 0;
418  
419 /* sen_moves.c installs itself into the table here. It reaches the board through
420 * the table and through nothing else. */
421 void sen_moves_install(struct sen_abi *t);
422 void sen_search_install(struct sen_abi *t);
423  
424 80 const struct sen_abi *sen_abi_table(void)
425 {
426 80 ▶ if (!SEN_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SEN_TABLE_READY) { 80 0 80
427 80 SEN_TABLE.abi_version = SEN_ABI_VERSION;
428  
429 80 SEN_TABLE.board_new = sen_board_new;
430 80 SEN_TABLE.board_of_string = sen_board_of_string;
431 80 SEN_TABLE.board_copy = sen_board_copy;
432 80 SEN_TABLE.board_drop = sen_board_drop;
433 80 SEN_TABLE.live = sen_live;
434  
435 80 SEN_TABLE.at = sen_at;
436 80 SEN_TABLE.put = sen_put;
437 80 SEN_TABLE.lift = sen_lift;
438 80 SEN_TABLE.side = sen_side;
439 80 SEN_TABLE.set_side = sen_set_side;
440 80 SEN_TABLE.pieces = sen_pieces;
441 80 SEN_TABLE.count = sen_count;
442 80 SEN_TABLE.home = sen_home;
443 80 SEN_TABLE.consistent = sen_consistent;
444 80 SEN_TABLE.ply = sen_ply;
445 80 SEN_TABLE.set_ply = sen_set_ply;
446  
447 80 SEN_TABLE.key = sen_key;
448 80 SEN_TABLE.to_string = sen_to_string;
449  
450 80 SEN_TABLE.house_of = sen_house_of;
451 80 SEN_TABLE.row_of = sen_row_of;
452 80 SEN_TABLE.col_of = sen_col_of;
453 80 SEN_TABLE.is_marked = sen_is_marked;
454  
455 80 SEN_TABLE.rules_named = sen_rules_named;
456 80 SEN_TABLE.rules_id = sen_rules_id;
457 80 SEN_TABLE.rules_ok = sen_rules_ok;
458  
459 80 SEN_TABLE.chance_count = sen_chance_count;
460 80 SEN_TABLE.chance = sen_chance;
461 80 SEN_TABLE.extra = sen_extra;
462  
463 80 SEN_TABLE_READY = 1; /* before the installer, or it recurses */
464 80 sen_moves_install(&SEN_TABLE);
465 80 sen_search_install(&SEN_TABLE);
466 }
467 80 return &SEN_TABLE;
468 }
« sen_search.c ↑ Summary sen_moves.c »