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 4813115 static int sen_rules_named(int name, struct sen_rules *r)
116 {
117 4813115 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 4813115 4813115
118 4813115 ▶ if (name == SEN_RULES_KENDALL) { *r = RULES_KENDALL; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (name == SEN_RULES_KENDALL) { *r = RULES_KENDALL; return 1; } 3857361 955754 4813115
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 2406547 static int sen_rules_id(const char *name)
124 {
125 2406547 ▶ if (!name) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return -1; 0 2406547 2406547
126 2406547 ▶ if (strcmp(name, "kendall") == 0) return SEN_RULES_KENDALL;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "kendall") == 0) return SEN_RULES_KENDALL; 1450794 955753 2406547
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 8806072 static int sen_rules_ok(const struct sen_rules *r)
132 {
133 int h, roll, i;
134 8806072 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 8806072 8806072
135 8806072 ▶ 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; 8806070 2 8806072
if (r->pieces < 1 || r->pieces > SEN_PIECES_MAX) return 0; 2 8806068 8806070
136 8806068 ▶ 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; 8605458 200610 8806068
if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0; 4568540 4036918 8605458
if (r->blockade != 0 && r->blockade != 2 && r->blockade != 3) return 0; 3 4568537 4568540
137 8806065 ▶ 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; 8806064 1 8806065
if (r->protect < SEN_PROTECT_NONE || r->protect > SEN_PROTECT_ENDS) return 0; 1 8806063 8806064
138 8806063 ▶ if (r->backward != 0 && r->backward != 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->backward != 0 && r->backward != 1) return 0; 8805464 599 8806063
if (r->backward != 0 && r->backward != 1) return 0; 2 8805462 8805464
139 8806061 ▶ 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 2569858 8806061
if (r->reach26 != SEN_REACH_EXACT && r->reach26 != SEN_REACH_ANY) return 0; 1 6236202 6236203
140 8806060 ▶ 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 2569856 8806060
if (r->throw_on_26 != 0 && r->throw_on_26 != 1) return 0; 2 6236202 6236204
141 8806058 ▶ if (r->safe & 1UL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->safe & 1UL) return 0; 1 8806057 8806058
142 8806057 ▶ if (r->safe >> (SEN_HOUSES + 1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->safe >> (SEN_HOUSES + 1)) return 0; 1 8806056 8806057
143 52836315 ▶ for (h = 0; h < SEN_EXIT_HOUSES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < SEN_EXIT_HOUSES; h++) { 44030264 8806051 52836315
144 264181563 ▶ for (roll = 0; roll < SEN_ROLL_MAX; roll++) {
Branch: 1 branch
BranchTrueFalseTotal
for (roll = 0; roll < SEN_ROLL_MAX; roll++) { 220151304 44030259 264181563
145 220151304 int ended = 0;
146 1100756507 ▶ for (i = 0; i < SEN_CHAIN_MAX; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SEN_CHAIN_MAX; i++) { 880605208 220151299 1100756507
147 880605208 int e = r->exit[h][roll][i];
148 880605208 ▶ 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; 880605207 1 880605208
if (e < SEN_X_END || e > SEN_X_STAY) return 0; 1 880605206 880605207
149 880605206 ▶ if (ended && e != SEN_X_END) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ended && e != SEN_X_END) return 0; 490226268 390378938 880605206
if (ended && e != SEN_X_END) return 0; 3 490226265 490226268
150 880605203 ▶ if (e == SEN_X_END) ended = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e == SEN_X_END) ended = 1; 710377566 170227637 880605203
151 }
152 }
153 }
154 44030251 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 35224201 8806050 44030251
155 35224201 ▶ if (r->reserved[i] != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r->reserved[i] != 0) return 0; 1 35224200 35224201
156 8806050 return 1;
157 }
158  
159 /* ---- the board ------------------------------------------------------------- */
160  
161 796529 static int sen_clamp_pieces(int n)
162 {
163 796529 ▶ if (n < 1) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) return 1; 0 796529 796529
164 796529 ▶ if (n > SEN_PIECES_MAX) return SEN_PIECES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SEN_PIECES_MAX) return SEN_PIECES_MAX; 0 796529 796529
165 796529 return n;
166 }
167  
168 796529 static struct sen_board *sen_board_new(int pieces)
169 {
170 796529 struct sen_board *b = (struct sen_board *) malloc(sizeof(struct sen_board));
171 796529 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 796529 796529
172 796529 memset(b, 0, sizeof(*b));
173 796529 b->pieces = sen_clamp_pieces(pieces);
174 796529 b->side = SEN_SIDE_LIGHT;
175 796529 SEN_LIVE++;
176 796529 return b;
177 }
178  
179 643611 static struct sen_board *sen_board_copy(const struct sen_board *b)
180 {
181 struct sen_board *n;
182 643611 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 643611 643611
183 643611 n = (struct sen_board *) malloc(sizeof(struct sen_board));
184 643611 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 643611 643611
185 643611 *n = *b;
186 643611 SEN_LIVE++;
187 643611 return n;
188 }
189  
190 1440140 static void sen_board_drop(struct sen_board *b)
191 {
192 1440140 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 1440140 1440140
193 1440140 SEN_LIVE--;
194 1440140 free(b);
195 }
196  
197 5 static int sen_live(void) { return SEN_LIVE; }
198  
199 627888727 static int sen_at(const struct sen_board *b, int house)
200 {
201 627888727 ▶ if (house < 1 || house > SEN_HOUSES) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return -1; 627888725 2 627888727
if (house < 1 || house > SEN_HOUSES) return -1; 1 627888724 627888725
202 627888724 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 25488330 static void sen_put(struct sen_board *b, int house, int piece)
208 {
209 25488330 ▶ if (house < 1 || house > SEN_HOUSES) return;
Branch: 2 branches
BranchTrueFalseTotal
if (house < 1 || house > SEN_HOUSES) return; 25488329 1 25488330
if (house < 1 || house > SEN_HOUSES) return; 1 25488328 25488329
210 25488328 ▶ if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 15791428 9696900 25488328
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 7889597 7901831 15791428
if (piece != SEN_EMPTY && !SEN_IS_PIECE(piece)) return; 1 7889596 7889597
211 25488327 b->house[house] = (unsigned char) piece;
212 }
213  
214 4614253 static void sen_lift(struct sen_board *b, int house)
215 {
216 4614253 sen_put(b, house, SEN_EMPTY);
217 4614253 }
218  
219 71960108 static int sen_side(const struct sen_board *b) { return b->side; }
220  
221 6751559 static void sen_set_side(struct sen_board *b, int side)
222 {
223 6751559 ▶ if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return;
Branch: 2 branches
BranchTrueFalseTotal
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return; 3232081 3519478 6751559
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return; 1 3232080 3232081
224 6751558 b->side = side;
225 }
226  
227 661 static int sen_pieces(const struct sen_board *b) { return b->pieces; }
228  
229 41880758 static int sen_count(const struct sen_board *b, int side)
230 {
231 41880758 int h, n = 0;
232 41880758 ▶ 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; 20612510 21268248 41880758
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 1 20612509 20612510
233 1298303467 ▶ for (h = 1; h <= SEN_HOUSES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= SEN_HOUSES; h++) 1256422710 41880757 1298303467
234 1256422710 ▶ if (b->house[h] == SEN_PIECE_OF(side)) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (b->house[h] == SEN_PIECE_OF(side)) n++; 175223803 1081198907 1256422710
235 41880757 return n;
236 }
237  
238 7154489 static int sen_home(const struct sen_board *b, int side)
239 {
240 7154489 ▶ 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; 3580687 3573802 7154489
if (side != SEN_SIDE_LIGHT && side != SEN_SIDE_DARK) return -1; 1 3580686 3580687
241 7154488 return b->pieces - sen_count(b, side);
242 }
243  
244 83509 static int sen_consistent(const struct sen_board *b)
245 {
246 83509 ▶ 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; 83505 4 83509
return sen_home(b, SEN_SIDE_LIGHT) >= 0 && sen_home(b, SEN_SIDE_DARK) >= 0; 83502 3 83505
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 16856952 static int sen_ply(const struct sen_board *b) { return b->ply; }
252  
253 8629705 static void sen_set_ply(struct sen_board *b, int ply)
254 {
255 8629705 ▶ if (ply < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ply < 0) return; 1 8629704 8629705
256 8629704 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 2506685 static int sen_letter_piece(char c)
292 {
293 2506685 switch (c) {
294 267681 case 'l': return SEN_LIGHT;
295 295652 case 'd': return SEN_DARK;
296 1943318 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 83589 static struct sen_board *sen_board_of_string(const char *str, int *err)
309 {
310 struct sen_board *b;
311 83589 int group = 0, width = 0, house = 0;
312 83589 const char *s = str;
313 size_t n;
314  
315 83589 ▶ if (err) *err = SEN_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SEN_OK; 83589 0 83589
316 83589 ▶ 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 83589 83589
if (!str) { if (err) *err = SEN_POS_NULL; return NULL; } 0 0 0
317 83589 n = strlen(str);
318 83589 ▶ 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 83586 83589
if (n == 0) { if (err) *err = SEN_POS_NULL; return NULL; } 3 0 3
319 83586 ▶ 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 83584 83586
if (n >= SEN_POS_MAX) { if (err) *err = SEN_POS_LONG; return NULL; } 2 0 2
320  
321 83584 b = sen_board_new(1);
322 83584 ▶ 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 83584 83584
if (!b) { if (err) *err = SEN_POS_NULL; return NULL; } 0 0 0
323  
324 2757336 ▶ for (; *s && *s != ' '; s++) {
Branch: 2 branches
BranchTrueFalseTotal
for (; *s && *s != ' '; s++) { 2757334 2 2757336
for (; *s && *s != ' '; s++) { 2673798 83536 2757334
325 int piece;
326 2673798 ▶ if (*s == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { 167113 2506685 2673798
327 167113 ▶ 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 167105 167113
328 167105 ▶ 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 167103 167105
329 167103 group++; width = 0;
330 167103 continue;
331 }
332 2506685 piece = sen_letter_piece(*s);
333 2506685 ▶ 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 2506651 2506685
334 2506651 ▶ 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 2506649 2506651
335 2506649 house = group * SEN_COLS + width + 1;
336 2506649 sen_put(b, house, piece);
337 2506649 width++;
338 }
339 83538 ▶ 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 83532 83538
340 83532 ▶ 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 83530 83532
341  
342 83530 ▶ 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 83528 83530
343 83528 s++;
344 83528 ▶ if (*s == 'l') sen_set_side(b, SEN_SIDE_LIGHT);
Branch: 1 branch
BranchTrueFalseTotal
if (*s == 'l') sen_set_side(b, SEN_SIDE_LIGHT); 42438 41090 83528
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 83522 s++;
349  
350 83522 ▶ 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 83518 83522
if (*s != ' ') return sen_refuse(b, err, *s ? SEN_POS_SIDE : SEN_POS_FIELD); 2 2 4
351 83518 s++;
352 83518 ▶ 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 83516 83518
353 83516 ▶ 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); 83514 2 83516
if (*s < '1' || *s > '0' + SEN_PIECES_MAX) return sen_refuse(b, err, SEN_POS_PIECES); 4 83510 83514
354 83510 b->pieces = *s - '0';
355 83510 s++;
356 83510 ▶ 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 83506 83510
if (*s && *s != ' ') return sen_refuse(b, err, SEN_POS_PIECES); 2 2 4
357 83508 ▶ 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 83506 83508
358  
359 83506 ▶ 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 83501 83506
360 83501 return b;
361 }
362  
363 116450 static int sen_to_string(const struct sen_board *b, char *buf, int len)
364 {
365 116450 int h, i = 0;
366 116450 ▶ if (!b || !buf || len < SEN_POS_MAX) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !buf || len < SEN_POS_MAX) return -1; 116450 0 116450
if (!b || !buf || len < SEN_POS_MAX) return -1; 116450 0 116450
if (!b || !buf || len < SEN_POS_MAX) return -1; 0 116450 116450
367 3609950 ▶ for (h = 1; h <= SEN_HOUSES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 1; h <= SEN_HOUSES; h++) { 3493500 116450 3609950
368 3493500 int p = b->house[h];
369 3493500 ▶ buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.';
Branch: 2 branches
BranchTrueFalseTotal
buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.'; 3102447 391053 3493500
buf[i++] = p == SEN_LIGHT ? 'l' : p == SEN_DARK ? 'd' : '.'; 421558 2680889 3102447
370 3493500 ▶ if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/';
Branch: 2 branches
BranchTrueFalseTotal
if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/'; 349350 3144150 3493500
if (h % SEN_COLS == 0 && h < SEN_HOUSES) buf[i++] = '/'; 232900 116450 349350
371 }
372 116450 buf[i++] = ' ';
373 116450 ▶ buf[i++] = (b->side == SEN_SIDE_DARK) ? 'd' : 'l';
Branch: 1 branch
BranchTrueFalseTotal
buf[i++] = (b->side == SEN_SIDE_DARK) ? 'd' : 'l'; 57523 58927 116450
374 116450 buf[i++] = ' ';
375 116450 buf[i++] = (char) ('0' + b->pieces);
376 116450 buf[i] = '\0';
377 116450 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 4600838 static int sen_extra(int roll)
409 {
410 4600838 ▶ if (roll < 1 || roll > SEN_ROLL_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (roll < 1 || roll > SEN_ROLL_MAX) return -1; 4600837 1 4600838
if (roll < 1 || roll > SEN_ROLL_MAX) return -1; 1 4600836 4600837
411 4600836 ▶ return roll == 1 || roll == 4 || roll == 5;
Branch: 3 branches
BranchTrueFalseTotal
return roll == 1 || roll == 4 || roll == 5; 3586663 1014173 4600836
return roll == 1 || roll == 4 || roll == 5; 2850354 736309 3586663
return roll == 1 || roll == 4 || roll == 5; 718380 2131974 2850354
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 101 const struct sen_abi *sen_abi_table(void)
425 {
426 101 ▶ if (!SEN_TABLE_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SEN_TABLE_READY) { 101 0 101
427 101 SEN_TABLE.abi_version = SEN_ABI_VERSION;
428  
429 101 SEN_TABLE.board_new = sen_board_new;
430 101 SEN_TABLE.board_of_string = sen_board_of_string;
431 101 SEN_TABLE.board_copy = sen_board_copy;
432 101 SEN_TABLE.board_drop = sen_board_drop;
433 101 SEN_TABLE.live = sen_live;
434  
435 101 SEN_TABLE.at = sen_at;
436 101 SEN_TABLE.put = sen_put;
437 101 SEN_TABLE.lift = sen_lift;
438 101 SEN_TABLE.side = sen_side;
439 101 SEN_TABLE.set_side = sen_set_side;
440 101 SEN_TABLE.pieces = sen_pieces;
441 101 SEN_TABLE.count = sen_count;
442 101 SEN_TABLE.home = sen_home;
443 101 SEN_TABLE.consistent = sen_consistent;
444 101 SEN_TABLE.ply = sen_ply;
445 101 SEN_TABLE.set_ply = sen_set_ply;
446  
447 101 SEN_TABLE.key = sen_key;
448 101 SEN_TABLE.to_string = sen_to_string;
449  
450 101 SEN_TABLE.house_of = sen_house_of;
451 101 SEN_TABLE.row_of = sen_row_of;
452 101 SEN_TABLE.col_of = sen_col_of;
453 101 SEN_TABLE.is_marked = sen_is_marked;
454  
455 101 SEN_TABLE.rules_named = sen_rules_named;
456 101 SEN_TABLE.rules_id = sen_rules_id;
457 101 SEN_TABLE.rules_ok = sen_rules_ok;
458  
459 101 SEN_TABLE.chance_count = sen_chance_count;
460 101 SEN_TABLE.chance = sen_chance;
461 101 SEN_TABLE.extra = sen_extra;
462  
463 101 SEN_TABLE_READY = 1; /* before the installer, or it recurses */
464 101 sen_moves_install(&SEN_TABLE);
465 101 sen_search_install(&SEN_TABLE);
466 }
467 101 return &SEN_TABLE;
468 }
« sen_search.c ↑ Summary sen_moves.c »