sth_rules.c

filter: Statementstmt 99.6% 273 / 274 Branchbran 80.7% 247 / 306 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.6% 520 / 580 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sth_moves.c ↑ Summary sth_search.c »
1 /* sth_rules.c - a game: whose turn, who is home, in what places, and the end.
2 *
3 * PERL-FREE by design: see include/sth_abi.h.
4 *
5 * THIS FILE'S SCOPE, version 4. Only "every piece home" is the rule of the
6 * game (W). Everything else here is this dist's house rule, because W and D
7 * say nothing: the parked piece (a home full of pieces, one of them the seat's
8 * own, is home), the pass of a seat with no move, the end when nobody can
9 * move, the ply cap, and where a resigning seat is placed.
10 */
11  
12 #include <string.h>
13 #include "sth_internal.h"
14  
15 76203 void sth_game_init(struct sth_board *b)
16 {
17 int i;
18 76203 b->ply = 0;
19 76203 b->cap = STH_PLY_CAP_PER_SEAT * b->seats;
20 76203 b->pass_run = 0;
21 76203 b->finished_n = 0;
22 76203 b->resigned_n = 0;
23 533421 ▶ for (i = 0; i < STH_SEATS_MAX; i++) { b->finished[i] = -1; b->resigned[i] = -1; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_SEATS_MAX; i++) { b->finished[i] = -1; b->resigned[i] = -1; } 457218 76203 533421
24 76203 b->over = 0;
25 76203 b->how = STH_ONGOING;
26 76203 }
27  
28 87639919 static int seat_ok(const struct sth_board *b, int seat)
29 {
30 87639919 ▶ return b && seat >= 0 && seat < b->seats;
Branch: 3 branches
BranchTrueFalseTotal
return b && seat >= 0 && seat < b->seats; 87639919 0 87639919
return b && seat >= 0 && seat < b->seats; 87639919 0 87639919
return b && seat >= 0 && seat < b->seats; 87639919 0 87639919
31 }
32  
33 86822544 static int home_point(const struct sth_board *b, int seat)
34 {
35 86822544 return (STH_LAYOUT[b->seats][seat] + 3) % STH_POINTS;
36 }
37  
38 566032169 static int in_list(const int *list, int n, int seat)
39 {
40 int i;
41 571149282 ▶ for (i = 0; i < n; i++) if (list[i] == seat) return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (list[i] == seat) return 1; 1429151 5117113 6546264
for (i = 0; i < n; i++) if (list[i] == seat) return 1; 6546264 564603018 571149282
42 564603018 return 0;
43 }
44  
45 283882025 static int is_finished(const struct sth_board *b, int seat)
46 {
47 283882025 return in_list(b->finished, b->finished_n, seat);
48 }
49  
50 282124797 static int is_resigned(const struct sth_board *b, int seat)
51 {
52 282124797 return in_list(b->resigned, b->resigned_n, seat);
53 }
54  
55 /* Still racing: not home and not resigned. */
56 283391606 static int active(const struct sth_board *b, int seat)
57 {
58 283391606 ▶ return !is_finished(b, seat) && !is_resigned(b, seat);
Branch: 2 branches
BranchTrueFalseTotal
return !is_finished(b, seat) && !is_resigned(b, seat); 282124797 1266809 283391606
return !is_finished(b, seat) && !is_resigned(b, seat); 281963074 161723 282124797
59 }
60  
61 46818803 static int active_count(const struct sth_board *b)
62 {
63 46818803 int s, n = 0;
64 220069201 ▶ for (s = 0; s < b->seats; s++) n += active(b, s);
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < b->seats; s++) n += active(b, s); 173250398 46818803 220069201
65 46818803 return n;
66 }
67  
68 /* THE PARKED PIECE (G2). Every hole of the home holds a piece, and at least
69 * one of them is the seat's own. A rival who never moves a piece out of its
70 * start, which is this seat's home, cannot keep this seat from finishing. */
71 86122750 int sth_home_complete(const struct sth_board *b, int seat)
72 {
73 86122750 int p, i, c, own = 0;
74 86122750 ▶ if (!seat_ok(b, seat)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seat_ok(b, seat)) return 0; 0 86122750 86122750
75 86122750 p = home_point(b, seat);
76 205379004 ▶ for (i = 0; i < STH_POINT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) { 204846383 532621 205379004
77 204846383 c = b->cell[STH_SQ_OF[STH_POINT_LIST[p][i]]];
78 204846383 ▶ if (!STH_IS_PIECE(c)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!STH_IS_PIECE(c)) return 0; 85590129 119256254 204846383
79 119256254 own += c == seat;
80 }
81 532621 ▶ if (b->pieces == STH_PIECES_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->pieces == STH_PIECES_MAX) { 480 532141 532621
82 1275 ▶ for (i = 0; i < STH_FRONT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_FRONT_HOLES; i++) { 1224 51 1275
83 1224 c = b->cell[STH_SQ_OF[STH_FRONT[p][i]]];
84 1224 ▶ if (!STH_IS_PIECE(c)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!STH_IS_PIECE(c)) return 0; 429 795 1224
85 795 own += c == seat;
86 }
87 }
88 532192 return own > 0;
89 }
90  
91 490419 int sth_finished(const struct sth_board *b, int seat)
92 {
93 490419 ▶ return seat_ok(b, seat) && is_finished(b, seat);
Branch: 2 branches
BranchTrueFalseTotal
return seat_ok(b, seat) && is_finished(b, seat); 490419 0 490419
return seat_ok(b, seat) && is_finished(b, seat); 617 489802 490419
94 }
95  
96 14001352 static int hex_distance(int a, int b)
97 {
98 14001352 int dx = STH_HX[a] - STH_HX[b], dy = STH_HY[a] - STH_HY[b], dz = -dx - dy;
99 14001352 ▶ if (dx < 0) dx = -dx;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) dx = -dx; 3108307 10893045 14001352
100 14001352 ▶ if (dy < 0) dy = -dy;
Branch: 1 branch
BranchTrueFalseTotal
if (dy < 0) dy = -dy; 8658367 5342985 14001352
101 14001352 ▶ if (dz < 0) dz = -dz;
Branch: 1 branch
BranchTrueFalseTotal
if (dz < 0) dz = -dz; 3122905 10878447 14001352
102 14001352 return (dx + dy + dz) / 2;
103 }
104  
105 /* DISTANCE LEFT: for each of the seat's pieces, how far it is from the tip of
106 * its home, added up, less what a full home of its own pieces adds up to. A
107 * home full of the seat's own pieces is 0, and every piece out of place adds
108 * to it. It is what a capped or stuck game is placed by. */
109 699794 int sth_distance_left(const struct sth_board *b, int seat)
110 {
111 699794 int p, tip, h, i, sum = 0, floor = 0;
112 699794 ▶ if (!seat_ok(b, seat)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!seat_ok(b, seat)) return -1; 0 699794 699794
113 699794 p = home_point(b, seat);
114 699794 tip = STH_POINT_LIST[p][0];
115 7697734 ▶ for (i = 0; i < STH_POINT_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) { 6997940 699794 7697734
116 6997940 int k, n = 0;
117 48985580 ▶ for (k = 0; k < STH_DIRS; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < STH_DIRS; k++) 41987640 6997940 48985580
118 41987640 n += STH_HOLE_AT[STH_SQ_OF[STH_POINT_LIST[p][i]] + STH_STEP[k]] >= 0;
119 6997940 ▶ if (n == 2) tip = STH_POINT_LIST[p][i];
Branch: 1 branch
BranchTrueFalseTotal
if (n == 2) tip = STH_POINT_LIST[p][i]; 699794 6298146 6997940
120 }
121 85374868 ▶ for (h = 0; h < STH_HOLES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) 84675074 699794 85374868
122 84675074 ▶ if (b->cell[STH_SQ_OF[h]] == seat) sum += hex_distance(h, tip);
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[STH_SQ_OF[h]] == seat) sum += hex_distance(h, tip); 7001012 77674062 84675074
123 7697734 ▶ for (i = 0; i < STH_POINT_HOLES; i++) floor += hex_distance(STH_POINT_LIST[p][i], tip);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) floor += hex_distance(STH_POINT_LIST[p][i], tip); 6997940 699794 7697734
124 699794 ▶ if (b->pieces == STH_PIECES_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (b->pieces == STH_PIECES_MAX) 480 699314 699794
125 2880 ▶ for (i = 0; i < STH_FRONT_HOLES; i++) floor += hex_distance(STH_FRONT[p][i], tip);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_FRONT_HOLES; i++) floor += hex_distance(STH_FRONT[p][i], tip); 2400 480 2880
126 699794 return sum - floor;
127 }
128  
129 /* ---- places ------------------------------------------------------------------
130 *
131 * Seats home, in the order they got there; then the seats still racing when
132 * the game ended, the one left alone or, at the cap or a deadlock, by distance
133 * left, fewest first, equal distances sharing a place; then the seats that
134 * resigned, the last to resign first. While the game runs, only a seat home
135 * has a place.
136 */
137  
138 326252 int sth_place_of(const struct sth_board *b, int seat)
139 {
140 int i, s, place, mine, ahead;
141 326252 ▶ if (!seat_ok(b, seat)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!seat_ok(b, seat)) return 0; 0 326252 326252
142 331733 ▶ for (i = 0; i < b->finished_n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->finished_n; i++) 6918 324815 331733
143 6918 ▶ if (b->finished[i] == seat) return i + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (b->finished[i] == seat) return i + 1; 1437 5481 6918
144 324815 ▶ if (!b->over) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b->over) return 0; 308574 16241 324815
145 16241 place = b->finished_n + 1;
146 16241 ▶ if (active(b, seat)) {
Branch: 1 branch
BranchTrueFalseTotal
if (active(b, seat)) { 15513 728 16241
147 15513 mine = sth_distance_left(b, seat);
148 15513 ahead = 0;
149 58212 ▶ for (s = 0; s < b->seats; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < b->seats; s++) 42699 15513 58212
150 42699 ▶ if (s != seat && active(b, s) && sth_distance_left(b, s) < mine) ahead++;
Branch: 3 branches
BranchTrueFalseTotal
if (s != seat && active(b, s) && sth_distance_left(b, s) < mine) ahead++; 27186 15513 42699
if (s != seat && active(b, s) && sth_distance_left(b, s) < mine) ahead++; 22398 4788 27186
if (s != seat && active(b, s) && sth_distance_left(b, s) < mine) ahead++; 8536 13862 22398
151 15513 return place + ahead;
152 }
153 728 place += active_count(b);
154 983 ▶ for (i = b->resigned_n - 1; i >= 0; i--, place++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = b->resigned_n - 1; i >= 0; i--, place++) 983 0 983
155 983 ▶ if (b->resigned[i] == seat) return place;
Branch: 1 branch
BranchTrueFalseTotal
if (b->resigned[i] == seat) return place; 728 255 983
156 0 return 0;
157 }
158  
159 /* ---- the turn ---------------------------------------------------------------- */
160  
161 23371846 static void next_turn(struct sth_board *b)
162 {
163 int s, k;
164 23543155 ▶ for (k = 1; k <= b->seats; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k <= b->seats; k++) { 23543155 0 23543155
165 23543155 s = (b->to_move + k) % b->seats;
166 23543155 ▶ if (active(b, s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (active(b, s)) { 23371846 171309 23543155
167 23371846 ▶ if (s != b->to_move) {
Branch: 1 branch
BranchTrueFalseTotal
if (s != b->to_move) { 23051482 320364 23371846
168 23051482 b->key ^= STH_ZOB_MOVE[b->to_move] ^ STH_ZOB_MOVE[s];
169 23051482 b->to_move = s;
170 }
171 23371846 return;
172 }
173 }
174 }
175  
176 12739 static void end_game(struct sth_board *b, int how)
177 {
178 12739 b->over = 1;
179 12739 b->how = how;
180 12739 }
181  
182 /* After a move: the mover first, then every other seat still racing,
183 * clockwise from it. A rival can complete this seat's home by moving into the
184 * last hole of it, and then it is home on the rival's move. */
185 23384172 static void check_home(struct sth_board *b)
186 {
187 int k, s;
188 109913163 ▶ for (k = 0; k < b->seats; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < b->seats; k++) { 86528991 23384172 109913163
189 86528991 s = (b->to_move + k) % b->seats;
190 86528991 ▶ if (active(b, s) && sth_home_complete(b, s))
Branch: 2 branches
BranchTrueFalseTotal
if (active(b, s) && sth_home_complete(b, s)) 86122740 406251 86528991
if (active(b, s) && sth_home_complete(b, s)) 4401 86118339 86122740
191 4401 b->finished[b->finished_n++] = s;
192 }
193 23384172 }
194  
195 /* THE RACE IS OVER when nobody is left to race against: one seat still racing
196 * among several, or none. A seat crossing alone races until it is home. */
197 23410102 static int race_over(const struct sth_board *b)
198 {
199 23410102 int n = active_count(b);
200 23410102 ▶ return n == 0 || (b->seats > 1 && n == 1);
Branch: 3 branches
BranchTrueFalseTotal
return n == 0 || (b->seats > 1 && n == 1); 23410101 1 23410102
return n == 0 || (b->seats > 1 && n == 1); 23089737 320364 23410101
return n == 0 || (b->seats > 1 && n == 1); 1410 23088327 23089737
201 }
202  
203 23384348 static void after(struct sth_board *b, int how_if_over)
204 {
205 23384348 ▶ if (race_over(b)) { end_game(b, how_if_over); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (race_over(b)) { end_game(b, how_if_over); return; } 1310 23383038 23384348
206 23383038 ▶ if (b->pass_run >= active_count(b)) { end_game(b, STH_BY_DEADLOCK); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->pass_run >= active_count(b)) { end_game(b, STH_BY_DEADLOCK); return; } 2 23383036 23383038
207 23383036 ▶ if (b->ply >= b->cap) { end_game(b, STH_BY_CAP); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->ply >= b->cap) { end_game(b, STH_BY_CAP); return; } 11351 23371685 23383036
208 23371685 next_turn(b);
209 }
210  
211 23385000 static void remember(const struct sth_board *b, struct sth_undo *u, int kind, int from, int to, int seat)
212 {
213 int i;
214 23385000 ▶ if (!u) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) return; 0 23385000 23385000
215 23385000 u->kind = kind;
216 23385000 u->from = from;
217 23385000 u->to = to;
218 23385000 u->seat = seat;
219 23385000 u->to_move = b->to_move;
220 23385000 u->ply = b->ply;
221 23385000 u->pass_run = b->pass_run;
222 23385000 u->finished_n = b->finished_n;
223 23385000 u->resigned_n = b->resigned_n;
224 23385000 u->over = b->over;
225 23385000 u->how = b->how;
226 210465000 ▶ for (i = 0; i < 8; i++) u->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) u->reserved[i] = 0; 187080000 23385000 210465000
227 }
228  
229 1053664 int sth_play(struct sth_board *b, int from, int to, struct sth_undo *undo)
230 {
231 1053664 int reach[STH_REACH_MAX], n, i, ok = 0, c;
232 1053664 ▶ if (!b) return STH_WHY_NOT_A_HOLE;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return STH_WHY_NOT_A_HOLE; 0 1053664 1053664
233 1053664 ▶ if (b->over) return STH_WHY_OVER;
Branch: 1 branch
BranchTrueFalseTotal
if (b->over) return STH_WHY_OVER; 2 1053662 1053664
234 1053662 ▶ if (!STH_IS_HOLE(from) || !STH_IS_HOLE(to)) return STH_WHY_NOT_A_HOLE;
Branch: 4 branches
BranchTrueFalseTotal
if (!STH_IS_HOLE(from) || !STH_IS_HOLE(to)) return STH_WHY_NOT_A_HOLE; 1053662 0 1053662
if (!STH_IS_HOLE(from) || !STH_IS_HOLE(to)) return STH_WHY_NOT_A_HOLE; 1053662 0 1053662
if (!STH_IS_HOLE(from) || !STH_IS_HOLE(to)) return STH_WHY_NOT_A_HOLE; 1053662 0 1053662
if (!STH_IS_HOLE(from) || !STH_IS_HOLE(to)) return STH_WHY_NOT_A_HOLE; 0 1053662 1053662
235 1053662 c = b->cell[STH_SQ_OF[from]];
236 1053662 ▶ if (!STH_IS_PIECE(c)) return STH_WHY_NO_PIECE;
Branch: 1 branch
BranchTrueFalseTotal
if (!STH_IS_PIECE(c)) return STH_WHY_NO_PIECE; 0 1053662 1053662
237 1053662 ▶ if (c != b->to_move) return STH_WHY_NOT_YOURS;
Branch: 1 branch
BranchTrueFalseTotal
if (c != b->to_move) return STH_WHY_NOT_YOURS; 0 1053662 1053662
238 1053662 n = sth_reach(b, from, reach, STH_REACH_MAX);
239 7707399 ▶ for (i = 0; i < n; i++) if (reach[i] == to) ok = 1;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n; i++) if (reach[i] == to) ok = 1; 1053658 5600079 6653737
for (i = 0; i < n; i++) if (reach[i] == to) ok = 1; 6653737 1053662 7707399
240 1053662 ▶ if (!ok) return STH_WHY_UNREACHABLE;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) return STH_WHY_UNREACHABLE; 4 1053658 1053662
241 1053658 remember(b, undo, STH_DID_MOVE, from, to, c);
242 1053658 sth_do_move(b, from, to);
243 1053658 b->ply++;
244 1053658 b->pass_run = 0;
245 1053658 check_home(b);
246 1053658 after(b, STH_BY_HOME);
247 1053658 return STH_PLAY_OK;
248 }
249  
250 /* For the search, which plays only moves legal gave it: play without asking
251 * the reach again. Nothing outside the engine may call it. */
252 22330514 int sth_play_trusted(struct sth_board *b, int from, int to, struct sth_undo *undo)
253 {
254 22330514 int c = b->cell[STH_SQ_OF[from]];
255 22330514 remember(b, undo, STH_DID_MOVE, from, to, c);
256 22330514 sth_do_move(b, from, to);
257 22330514 b->ply++;
258 22330514 b->pass_run = 0;
259 22330514 check_home(b);
260 22330514 after(b, STH_BY_HOME);
261 22330514 return STH_PLAY_OK;
262 }
263  
264 97074 int sth_can_pass(const struct sth_board *b)
265 {
266 int from[1], to[1];
267 97074 ▶ return b && !b->over && sth_legal(b, from, to, 0) == 0;
Branch: 3 branches
BranchTrueFalseTotal
return b && !b->over && sth_legal(b, from, to, 0) == 0; 97074 0 97074
return b && !b->over && sth_legal(b, from, to, 0) == 0; 97074 0 97074
return b && !b->over && sth_legal(b, from, to, 0) == 0; 180 96894 97074
268 }
269  
270 178 int sth_pass(struct sth_board *b, struct sth_undo *undo)
271 {
272 178 ▶ if (!b) return STH_WHY_NOT_A_HOLE;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return STH_WHY_NOT_A_HOLE; 0 178 178
273 178 ▶ if (b->over) return STH_WHY_OVER;
Branch: 1 branch
BranchTrueFalseTotal
if (b->over) return STH_WHY_OVER; 0 178 178
274 178 ▶ if (!sth_can_pass(b)) return STH_WHY_HAS_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
if (!sth_can_pass(b)) return STH_WHY_HAS_MOVE; 2 176 178
275 176 remember(b, undo, STH_DID_PASS, -1, -1, b->to_move);
276 176 b->ply++;
277 176 b->pass_run++;
278 176 after(b, STH_BY_HOME);
279 176 return STH_PLAY_OK;
280 }
281  
282 /* Any seat still racing may resign, on its turn or not. Its pieces stay where
283 * they are: taking them away would change everyone else's ladders mid-game. */
284 704 int sth_resign(struct sth_board *b, int seat, struct sth_undo *undo)
285 {
286 704 ▶ if (!b) return STH_WHY_NOT_A_SEAT;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return STH_WHY_NOT_A_SEAT; 0 704 704
287 704 ▶ if (b->over) return STH_WHY_OVER;
Branch: 1 branch
BranchTrueFalseTotal
if (b->over) return STH_WHY_OVER; 0 704 704
288 704 ▶ if (!seat_ok(b, seat)) return STH_WHY_NOT_A_SEAT;
Branch: 1 branch
BranchTrueFalseTotal
if (!seat_ok(b, seat)) return STH_WHY_NOT_A_SEAT; 0 704 704
289 704 ▶ if (!active(b, seat)) return STH_WHY_SEAT_DONE;
Branch: 1 branch
BranchTrueFalseTotal
if (!active(b, seat)) return STH_WHY_SEAT_DONE; 52 652 704
290 652 remember(b, undo, STH_DID_RESIGN, -1, -1, seat);
291 652 b->resigned[b->resigned_n++] = seat;
292 652 b->pass_run = 0;
293 652 ▶ if (race_over(b)) { end_game(b, STH_BY_RESIGN); return STH_PLAY_OK; }
Branch: 1 branch
BranchTrueFalseTotal
if (race_over(b)) { end_game(b, STH_BY_RESIGN); return STH_PLAY_OK; } 76 576 652
294 576 ▶ if (seat == b->to_move) next_turn(b);
Branch: 1 branch
BranchTrueFalseTotal
if (seat == b->to_move) next_turn(b); 161 415 576
295 576 return STH_PLAY_OK;
296 }
297  
298 22524269 int sth_unplay(struct sth_board *b, const struct sth_undo *u)
299 {
300 22524269 ▶ if (!b || !u) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !u) return 0; 22524269 0 22524269
if (!b || !u) return 0; 0 22524269 22524269
301 22524269 ▶ if (u->kind == STH_DID_MOVE && !sth_undo_move(b, u->from, u->to)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (u->kind == STH_DID_MOVE && !sth_undo_move(b, u->from, u->to)) return 0; 22524011 258 22524269
if (u->kind == STH_DID_MOVE && !sth_undo_move(b, u->from, u->to)) return 0; 0 22524011 22524011
302 22524269 ▶ if (u->kind != STH_DID_MOVE && u->kind != STH_DID_PASS && u->kind != STH_DID_RESIGN) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (u->kind != STH_DID_MOVE && u->kind != STH_DID_PASS && u->kind != STH_DID_RESIGN) return 0; 258 22524011 22524269
if (u->kind != STH_DID_MOVE && u->kind != STH_DID_PASS && u->kind != STH_DID_RESIGN) return 0; 89 169 258
if (u->kind != STH_DID_MOVE && u->kind != STH_DID_PASS && u->kind != STH_DID_RESIGN) return 0; 0 89 89
303 22524269 ▶ if (u->to_move != b->to_move) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->to_move != b->to_move) { 22512827 11442 22524269
304 22512827 b->key ^= STH_ZOB_MOVE[b->to_move] ^ STH_ZOB_MOVE[u->to_move];
305 22512827 b->to_move = u->to_move;
306 }
307 22524269 b->ply = u->ply;
308 22524269 b->pass_run = u->pass_run;
309 22524269 b->finished_n = u->finished_n;
310 22524269 b->resigned_n = u->resigned_n;
311 22524269 b->over = u->over;
312 22524269 b->how = u->how;
313 22524269 return 1;
314 }
315  
316 702569 ▶int sth_game_over(const struct sth_board *b) { return b ? b->over : 0; }
Branch: 1 branch
BranchTrueFalseTotal
int sth_game_over(const struct sth_board *b) { return b ? b->over : 0; } 702569 0 702569
317 107992 ▶int sth_game_how(const struct sth_board *b) { return b ? b->how : STH_ONGOING; }
Branch: 1 branch
BranchTrueFalseTotal
int sth_game_how(const struct sth_board *b) { return b ? b->how : STH_ONGOING; } 107992 0 107992
318 86058 ▶int sth_ply(const struct sth_board *b) { return b ? b->ply : 0; }
Branch: 1 branch
BranchTrueFalseTotal
int sth_ply(const struct sth_board *b) { return b ? b->ply : 0; } 86058 0 86058
319 741 ▶int sth_cap(const struct sth_board *b) { return b ? b->cap : 0; }
Branch: 1 branch
BranchTrueFalseTotal
int sth_cap(const struct sth_board *b) { return b ? b->cap : 0; } 741 0 741
320  
321 1189 void sth_set_cap(struct sth_board *b, int cap)
322 {
323 1189 ▶ if (b && cap > 0) b->cap = cap;
Branch: 2 branches
BranchTrueFalseTotal
if (b && cap > 0) b->cap = cap; 1189 0 1189
if (b && cap > 0) b->cap = cap; 1189 0 1189
324 1189 }
325  
326 /* ---- the game in the position string (version 5) ---------------------------
327 *
328 * After the seat count and the piece count, a ';' and then, each only when it
329 * is not the default and always in this order, space between them:
330 *
331 * f=2,0 the seats home, in the order they got there
332 * r=1 the seats that resigned, in order
333 * p=37 the ply
334 * k=1 passes in a row, just now
335 * c=500 the cap, when it is not 1,000 a seat
336 * o=1 how the game ended, when it has
337 *
338 * A board fresh from its set-up writes no ';' at all, so every string version
339 * 1 wrote still reads the same.
340 */
341  
342 133053 static int put_dec(char *out, int n, int v)
343 {
344 char digits[12];
345 133053 int k = 0;
346 348107 ▶ do { digits[k++] = (char) ('0' + v % 10); v /= 10; } while (v && k < 11);
Branch: 2 branches
BranchTrueFalseTotal
do { digits[k++] = (char) ('0' + v % 10); v /= 10; } while (v && k < 11); 215054 133053 348107
do { digits[k++] = (char) ('0' + v % 10); v /= 10; } while (v && k < 11); 215054 0 215054
347 481160 ▶ while (k) out[n++] = digits[--k];
Branch: 1 branch
BranchTrueFalseTotal
while (k) out[n++] = digits[--k]; 348107 133053 481160
348 133053 return n;
349 }
350  
351 31449 static int put_seats(char *out, int n, const int *list, int count)
352 {
353 int i;
354 74120 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 42671 31449 74120
355 42671 ▶ if (i) out[n++] = ',';
Branch: 1 branch
BranchTrueFalseTotal
if (i) out[n++] = ','; 11222 31449 42671
356 42671 out[n++] = (char) ('0' + list[i]);
357 }
358 31449 return n;
359 }
360  
361 188307 int sth_game_suffix_write(const struct sth_board *b, char *out, int n)
362 {
363 188307 int any = 0;
364 188307 ▶ if (!b->ply && !b->finished_n && !b->resigned_n && !b->pass_run
Branch: 4 branches
BranchTrueFalseTotal
if (!b->ply && !b->finished_n && !b->resigned_n && !b->pass_run 106433 81874 188307
if (!b->ply && !b->finished_n && !b->resigned_n && !b->pass_run 106433 0 106433
if (!b->ply && !b->finished_n && !b->resigned_n && !b->pass_run 106430 3 106433
if (!b->ply && !b->finished_n && !b->resigned_n && !b->pass_run 106430 0 106430
365 106430 ▶ && b->cap == STH_PLY_CAP_PER_SEAT * b->seats && !b->over)
Branch: 2 branches
BranchTrueFalseTotal
&& b->cap == STH_PLY_CAP_PER_SEAT * b->seats && !b->over) 106110 320 106430
&& b->cap == STH_PLY_CAP_PER_SEAT * b->seats && !b->over) 106110 0 106110
366 106110 return n;
367 82197 out[n++] = ';';
368 #define STH_SEP() do { if (any++) out[n++] = ' '; } while (0)
369 82197 ▶ if (b->finished_n) { STH_SEP(); out[n++] = 'f'; out[n++] = '='; n = put_seats(out, n, b->finished, b->finished_n); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->finished_n) { STH_SEP(); out[n++] = 'f'; out[n++] = '='; n = put_seats(out, n, b->finished, b->finished_n); } 299 81898 82197
if (b->finished_n) { STH_SEP(); out[n++] = 'f'; out[n++] = '='; n = put_seats(out, n, b->finished, b->finished_n); } 0 299 299
370 82197 ▶ if (b->resigned_n) { STH_SEP(); out[n++] = 'r'; out[n++] = '='; n = put_seats(out, n, b->resigned, b->resigned_n); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->resigned_n) { STH_SEP(); out[n++] = 'r'; out[n++] = '='; n = put_seats(out, n, b->resigned, b->resigned_n); } 31150 51047 82197
if (b->resigned_n) { STH_SEP(); out[n++] = 'r'; out[n++] = '='; n = put_seats(out, n, b->resigned, b->resigned_n); } 290 30860 31150
371 82197 ▶ if (b->ply) { STH_SEP(); out[n++] = 'p'; out[n++] = '='; n = put_dec(out, n, b->ply); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->ply) { STH_SEP(); out[n++] = 'p'; out[n++] = '='; n = put_dec(out, n, b->ply); } 81874 323 82197
if (b->ply) { STH_SEP(); out[n++] = 'p'; out[n++] = '='; n = put_dec(out, n, b->ply); } 31156 50718 81874
372 82197 ▶ if (b->pass_run) { STH_SEP(); out[n++] = 'k'; out[n++] = '='; n = put_dec(out, n, b->pass_run); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->pass_run) { STH_SEP(); out[n++] = 'k'; out[n++] = '='; n = put_dec(out, n, b->pass_run); } 2 82195 82197
if (b->pass_run) { STH_SEP(); out[n++] = 'k'; out[n++] = '='; n = put_dec(out, n, b->pass_run); } 2 0 2
373 82197 ▶ if (b->cap != STH_PLY_CAP_PER_SEAT * b->seats) { STH_SEP(); out[n++] = 'c'; out[n++] = '='; n = put_dec(out, n, b->cap); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->cap != STH_PLY_CAP_PER_SEAT * b->seats) { STH_SEP(); out[n++] = 'c'; out[n++] = '='; n = put_dec(out, n, b->cap); } 51177 31020 82197
if (b->cap != STH_PLY_CAP_PER_SEAT * b->seats) { STH_SEP(); out[n++] = 'c'; out[n++] = '='; n = put_dec(out, n, b->cap); } 50857 320 51177
374 82197 ▶ if (b->over) { STH_SEP(); out[n++] = 'o'; out[n++] = '='; out[n++] = (char) ('0' + b->how); }
Branch: 2 branches
BranchTrueFalseTotal
if (b->over) { STH_SEP(); out[n++] = 'o'; out[n++] = '='; out[n++] = (char) ('0' + b->how); } 1332 80865 82197
if (b->over) { STH_SEP(); out[n++] = 'o'; out[n++] = '='; out[n++] = (char) ('0' + b->how); } 1332 0 1332
375 #undef STH_SEP
376 82197 return n;
377 }
378  
379 /* A decimal of one to nine digits, no leading zero unless it is 0. */
380 50032 static int read_dec(const char **s)
381 {
382 50032 const char *p = *s;
383 50032 int v = 0, k = 0;
384 50032 ▶ if (*p < '0' || *p > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (*p < '0' || *p > '9') return -1; 50032 0 50032
if (*p < '0' || *p > '9') return -1; 0 50032 50032
385 50032 ▶ if (*p == '0' && p[1] >= '0' && p[1] <= '9') return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (*p == '0' && p[1] >= '0' && p[1] <= '9') return -1; 2 50030 50032
if (*p == '0' && p[1] >= '0' && p[1] <= '9') return -1; 1 1 2
if (*p == '0' && p[1] >= '0' && p[1] <= '9') return -1; 1 0 1
386 181558 ▶ while (*p >= '0' && *p <= '9' && k < 9) { v = v * 10 + (*p++ - '0'); k++; }
Branch: 3 branches
BranchTrueFalseTotal
while (*p >= '0' && *p <= '9' && k < 9) { v = v * 10 + (*p++ - '0'); k++; } 131527 50031 181558
while (*p >= '0' && *p <= '9' && k < 9) { v = v * 10 + (*p++ - '0'); k++; } 131527 0 131527
while (*p >= '0' && *p <= '9' && k < 9) { v = v * 10 + (*p++ - '0'); k++; } 131527 0 131527
387 50031 ▶ if (*p >= '0' && *p <= '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (*p >= '0' && *p <= '9') return -1; 0 50031 50031
if (*p >= '0' && *p <= '9') return -1; 0 0 0
388 50031 *s = p;
389 50031 return v;
390 }
391  
392 9249 static int read_seats(const char **s, int seats, int *list, int *count, const int *other, int other_n)
393 {
394 9249 const char *p = *s;
395 int seat;
396 9249 *count = 0;
397 for (;;) {
398 12675 ▶ if (*p < '0' || *p > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p < '0' || *p > '9') return 0; 12675 0 12675
if (*p < '0' || *p > '9') return 0; 0 12675 12675
399 12675 seat = *p++ - '0';
400 12675 ▶ if (seat >= seats || in_list(list, *count, seat) || in_list(other, other_n, seat)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (seat >= seats || in_list(list, *count, seat) || in_list(other, other_n, seat)) return 0; 12674 1 12675
if (seat >= seats || in_list(list, *count, seat) || in_list(other, other_n, seat)) return 0; 12673 1 12674
if (seat >= seats || in_list(list, *count, seat) || in_list(other, other_n, seat)) return 0; 1 12672 12673
401 12672 list[(*count)++] = seat;
402 12672 ▶ if (*p != ',') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*p != ',') break; 9246 3426 12672
403 3426 p++;
404 }
405 9246 *s = p;
406 9246 return 1;
407 }
408  
409 /* Reads the part after the ';' into a board whose pieces and seats are set,
410 * and refuses with STH_POS_GAME a suffix that is malformed, out of order, or
411 * that does not agree with itself: a game whose own state says it is over
412 * must say how, and a stated end must be true of the state. */
413 25113 int sth_game_suffix_read(struct sth_board *b, const char *s)
414 {
415 static const char keys[] = "frpkco";
416 25113 int at = 0, v, any = 0, how = STH_ONGOING;
417 84558 ▶ while (*s) {
Branch: 1 branch
BranchTrueFalseTotal
while (*s) { 59455 25103 84558
418 const char *k;
419 59455 ▶ if (any++) { if (*s != ' ') return STH_POS_GAME; s++; }
Branch: 2 branches
BranchTrueFalseTotal
if (any++) { if (*s != ' ') return STH_POS_GAME; s++; } 34343 25112 59455
if (any++) { if (*s != ' ') return STH_POS_GAME; s++; } 0 34343 34343
420 59455 k = strchr(keys + at, *s);
421 59455 ▶ if (!*s || !k || s[1] != '=') return STH_POS_GAME;
Branch: 3 branches
BranchTrueFalseTotal
if (!*s || !k || s[1] != '=') return STH_POS_GAME; 59454 1 59455
if (!*s || !k || s[1] != '=') return STH_POS_GAME; 59451 3 59454
if (!*s || !k || s[1] != '=') return STH_POS_GAME; 0 59451 59451
422 59451 at = (int) (k - keys) + 1;
423 59451 s += 2;
424 59451 switch (*k) {
425 154 case 'f':
426 154 ▶ if (!read_seats(&s, b->seats, b->finished, &b->finished_n, b->resigned, b->resigned_n)) return STH_POS_GAME;
Branch: 1 branch
BranchTrueFalseTotal
if (!read_seats(&s, b->seats, b->finished, &b->finished_n, b->resigned, b->resigned_n)) return STH_POS_GAME; 2 152 154
427 152 break;
428 9095 case 'r':
429 9095 ▶ if (!read_seats(&s, b->seats, b->resigned, &b->resigned_n, b->finished, b->finished_n)) return STH_POS_GAME;
Branch: 1 branch
BranchTrueFalseTotal
if (!read_seats(&s, b->seats, b->resigned, &b->resigned_n, b->finished, b->finished_n)) return STH_POS_GAME; 1 9094 9095
430 9094 break;
431 24941 ▶ case 'p': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->ply = v; break;
Branch: 1 branch
BranchTrueFalseTotal
case 'p': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->ply = v; break; 2 24939 24941
432 3 ▶ case 'k': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->pass_run = v; break;
Branch: 1 branch
BranchTrueFalseTotal
case 'k': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->pass_run = v; break; 0 3 3
433 25088 ▶ case 'c': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->cap = v; break;
Branch: 1 branch
BranchTrueFalseTotal
case 'c': if ((v = read_dec(&s)) <= 0) return STH_POS_GAME; b->cap = v; break; 0 25088 25088
434 170 case 'o':
435 170 ▶ if (*s < '1' || *s > '4') return STH_POS_GAME;
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '1' || *s > '4') return STH_POS_GAME; 170 0 170
if (*s < '1' || *s > '4') return STH_POS_GAME; 1 169 170
436 169 how = *s++ - '0';
437 169 break;
438 }
439 }
440 25103 ▶ if (!any) return STH_POS_GAME;
Branch: 1 branch
BranchTrueFalseTotal
if (!any) return STH_POS_GAME; 1 25102 25103
441 25102 switch (how) {
442 24933 case STH_ONGOING:
443 24933 ▶ if (race_over(b) || b->pass_run >= active_count(b) || b->ply >= b->cap) return STH_POS_GAME;
Branch: 3 branches
BranchTrueFalseTotal
if (race_over(b) || b->pass_run >= active_count(b) || b->ply >= b->cap) return STH_POS_GAME; 24932 1 24933
if (race_over(b) || b->pass_run >= active_count(b) || b->ply >= b->cap) return STH_POS_GAME; 24932 0 24932
if (race_over(b) || b->pass_run >= active_count(b) || b->ply >= b->cap) return STH_POS_GAME; 1 24931 24932
444 24931 ▶ if (!active(b, b->to_move)) return STH_POS_GAME;
Branch: 1 branch
BranchTrueFalseTotal
if (!active(b, b->to_move)) return STH_POS_GAME; 1 24930 24931
445 24930 break;
446 25 case STH_BY_HOME:
447 case STH_BY_RESIGN:
448 25 ▶ if (!race_over(b)) return STH_POS_GAME;
Branch: 1 branch
BranchTrueFalseTotal
if (!race_over(b)) return STH_POS_GAME; 1 24 25
449 24 break;
450 141 case STH_BY_CAP:
451 141 ▶ if (race_over(b) || b->ply < b->cap) return STH_POS_GAME;
Branch: 2 branches
BranchTrueFalseTotal
if (race_over(b) || b->ply < b->cap) return STH_POS_GAME; 141 0 141
if (race_over(b) || b->ply < b->cap) return STH_POS_GAME; 1 140 141
452 140 break;
453 3 case STH_BY_DEADLOCK:
454 3 ▶ if (race_over(b) || b->pass_run < active_count(b)) return STH_POS_GAME;
Branch: 2 branches
BranchTrueFalseTotal
if (race_over(b) || b->pass_run < active_count(b)) return STH_POS_GAME; 3 0 3
if (race_over(b) || b->pass_run < active_count(b)) return STH_POS_GAME; 1 2 3
455 2 break;
456 }
457 25096 b->over = how != STH_ONGOING;
458 25096 b->how = how;
459 25096 return STH_OK;
460 }
« sth_moves.c ↑ Summary sth_search.c »