ru_moves.c

filter: Statementstmt 99.4% 190 / 191 Branchbran 82.5% 142 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.4% 332 / 363 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« RoyalUr.xs ↑ Summary ru_search.c »
1 /* ru_moves.c - what a roll allows.
2 *
3 * PERL-FREE, like ru_engine.c, and it reaches the board only through the
4 * table: this file does not know how a position is stored.
5 *
6 * THIS FILE'S SCOPE. The rule set as a struct, the moves a roll allows the
7 * side to move, a count of the positions a rule set has, and then a move MADE:
8 * apply and its undo, the forfeit, whose turn it is afterwards, and how a game
9 * ends.
10 */
11  
12 #include <stddef.h>
13 #include "ru_abi.h"
14  
15 static const struct ru_abi *RU = NULL;
16  
17 /* ---- the rule set ------------------------------------------------------------- */
18  
19 3119811 static int ru_rules_named(int name, struct ru_rules *r)
20 {
21 int i;
22 3119811 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 3119811 3119811
23 15599055 ▶ for (i = 0; i < 4; i++) r->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) r->reserved[i] = 0; 12479244 3119811 15599055
24 3119811 ▶ if (name == RU_RULES_FINKEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (name == RU_RULES_FINKEL) { 3082380 37431 3119811
25 3082380 r->route = RU_ROUTE_SHORT;
26 3082380 r->dice = 4;
27 3082380 r->zero_rolls = 0;
28 3082380 r->safe_rosettes = 1;
29 3082380 r->pieces = 7;
30 3082380 return 1;
31 }
32 37431 ▶ if (name == RU_RULES_MASTERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (name == RU_RULES_MASTERS) { 37431 0 37431
33 37431 r->route = RU_ROUTE_LONG;
34 37431 r->dice = 3;
35 37431 r->zero_rolls = 4;
36 37431 r->safe_rosettes = 0;
37 37431 r->pieces = 7;
38 37431 return 1;
39 }
40 0 return 0;
41 }
42  
43 13071393 static int ru_rules_ok(const struct ru_rules *r)
44 {
45 13071393 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 13071393 13071393
46 13071393 ▶ if (r->route != RU_ROUTE_SHORT && r->route != RU_ROUTE_LONG) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->route != RU_ROUTE_SHORT && r->route != RU_ROUTE_LONG) return 0; 6060822 7010571 13071393
if (r->route != RU_ROUTE_SHORT && r->route != RU_ROUTE_LONG) return 0; 0 6060822 6060822
47 13071393 ▶ if (r->dice != 3 && r->dice != 4) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->dice != 3 && r->dice != 4) return 0; 7425285 5646108 13071393
if (r->dice != 3 && r->dice != 4) return 0; 2 7425283 7425285
48 13071391 ▶ if (r->zero_rolls != 0 && r->zero_rolls != 4) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->zero_rolls != 0 && r->zero_rolls != 4) return 0; 5646109 7425282 13071391
if (r->zero_rolls != 0 && r->zero_rolls != 4) return 0; 1 5646108 5646109
49 13071390 ▶ if (r->safe_rosettes != 0 && r->safe_rosettes != 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->safe_rosettes != 0 && r->safe_rosettes != 1) return 0; 7066028 6005362 13071390
if (r->safe_rosettes != 0 && r->safe_rosettes != 1) return 0; 0 7066028 7066028
50 13071390 ▶ if (r->pieces < 1 || r->pieces > RU_PIECES_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->pieces < 1 || r->pieces > RU_PIECES_MAX) return 0; 13071389 1 13071390
if (r->pieces < 1 || r->pieces > RU_PIECES_MAX) return 0; 1 13071388 13071389
51 13071388 return 1;
52 }
53  
54 /* ---- the generator --------------------------------------------------------------
55 *
56 * The candidates are the hand, when it holds a piece, and then the side's
57 * pieces in ascending order of STEP. That order is part of the contract.
58 *
59 * For each, the landing step is the step it stands on plus the roll, and:
60 *
61 * past home no move: a piece leaves on an exact roll
62 * home a move
63 * a cell holding the side's own no move
64 * an enemy on a rosette that is safe no move
65 * an enemy anywhere else a move, and a capture
66 * an empty cell a move
67 *
68 * NOTHING BETWEEN THE TWO STEPS IS LOOKED AT. A piece passes over any piece of
69 * either side, an enemy on a safe rosette included: the rosette refuses a
70 * landing, it does not close the route.
71 *
72 * THE LANDING CELL IS ASKED OF THE MOVER'S ROUTE, and what stands on it is
73 * asked of the CELL. On the long route one cell is two different steps, one
74 * for each side, so "is there an enemy on my step 12" is not a question about
75 * the enemy's step 12.
76 */
77  
78 11756714 static int consider(const struct ru_board *b, const struct ru_rules *r, int side,
79 int from_step, int roll, struct ru_move *m)
80 {
81 11756714 int len = (RU->route_len)(r->route);
82 11756714 int to = from_step + roll;
83 int cell, standing;
84  
85 11756714 ▶ if (to > len + 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (to > len + 1) return 0; 599636 11157078 11756714
86  
87 11157078 m->from_step = from_step;
88 11157078 m->to_step = to;
89 11157078 ▶ m->from_cell = from_step == 0 ? -1 : (RU->route_cell)(r->route, side, from_step);
Branch: 1 branch
BranchTrueFalseTotal
m->from_cell = from_step == 0 ? -1 : (RU->route_cell)(r->route, side, from_step); 8339978 2817100 11157078
90 11157078 m->to_cell = -1;
91 11157078 m->captures = 0;
92 11157078 m->rosette = 0;
93 11157078 m->home = 0;
94 11157078 m->reserved[0] = m->reserved[1] = 0;
95  
96 11157078 ▶ if (to == len + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (to == len + 1) { 391795 10765283 11157078
97 391795 m->home = 1;
98 391795 return 1;
99 }
100  
101 10765283 cell = (RU->route_cell)(r->route, side, to);
102 10765283 standing = (RU->at)(b, cell);
103 10765283 ▶ if (standing == RU_PIECE_OF(side)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (standing == RU_PIECE_OF(side)) return 0; 2407226 8358057 10765283
104 8358057 ▶ if (standing == RU_PIECE_OF(RU_OTHER(side))) {
Branch: 2 branches
BranchTrueFalseTotal
if (standing == RU_PIECE_OF(RU_OTHER(side))) { 4182235 4175822 8358057
if (standing == RU_PIECE_OF(RU_OTHER(side))) { 1127794 7230263 8358057
105 1127794 ▶ if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0; 615110 512684 1127794
if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0; 80859 534251 615110
106 1046935 m->captures = 1;
107 }
108 8277198 m->to_cell = cell;
109 8277198 m->rosette = (RU->is_rosette)(cell) ? 1 : 0;
110 8277198 return 1;
111 }
112  
113 3403748 static int ru_moves(const struct ru_board *b, const struct ru_rules *rules, int roll,
114 struct ru_move *out, int max)
115 {
116 struct ru_rules standard;
117 struct ru_move m;
118 3403748 int side, len, step, n = 0;
119  
120 3403748 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 3403748 3403748
121 3403748 ▶ if (roll < 0 || roll > RU_ROLL_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (roll < 0 || roll > RU_ROLL_MAX) return -1; 3403748 0 3403748
if (roll < 0 || roll > RU_ROLL_MAX) return -1; 0 3403748 3403748
122 3403748 ▶ if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; } 0 3403748 3403748
123 3403748 ▶ if (!ru_rules_ok(rules)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ru_rules_ok(rules)) return -1; 0 3403748 3403748
124 3403748 ▶ if (roll == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (roll == 0) return 0; 360086 3043662 3403748
125  
126 3043662 side = (RU->side)(b);
127 3043662 len = (RU->route_len)(rules->route);
128  
129 3043662 ▶ if ((RU->hand)(b, side) > 0 && consider(b, rules, side, 0, roll, &m)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((RU->hand)(b, side) > 0 && consider(b, rules, side, 0, roll, &m)) { 2817100 226562 3043662
if ((RU->hand)(b, side) > 0 && consider(b, rules, side, 0, roll, &m)) { 1835202 981898 2817100
130 1835202 ▶ if (out && n < max) out[n] = m;
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) out[n] = m; 1835202 0 1835202
if (out && n < max) out[n] = m; 1835202 0 1835202
131 1835202 n++;
132 }
133 48553674 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 45510012 3043662 48553674
134 45510012 int cell = (RU->route_cell)(rules->route, side, step);
135 45510012 ▶ if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue; 36570398 8939614 45510012
136 8939614 ▶ if (!consider(b, rules, side, step, roll, &m)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!consider(b, rules, side, step, roll, &m)) continue; 2105823 6833791 8939614
137 6833791 ▶ if (out && n < max) out[n] = m;
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) out[n] = m; 6833791 0 6833791
if (out && n < max) out[n] = m; 6833791 0 6833791
138 6833791 n++;
139 }
140 3043662 return n;
141 }
142  
143 /* ---- how many positions a rule set has -------------------------------------------
144 *
145 * Every way of standing each side's pieces on cells of ITS OWN route, no two
146 * on one cell, with what is left split between hand and home, for each side to
147 * move. It is a count of positions that are consistent, not of positions play
148 * can reach, and a finished game is counted.
149 *
150 * Walked cell by cell and not computed, so that it can be held against a
151 * formula that was.
152 */
153  
154 1004600157 static unsigned long long count_from(const struct ru_rules *r, int cell, int light, int dark)
155 {
156 unsigned long long total;
157 1004600157 ▶ if (cell == RU_CELLS)
Branch: 1 branch
BranchTrueFalseTotal
if (cell == RU_CELLS) 540309158 464290999 1004600157
158 540309158 return (unsigned long long) (r->pieces - light + 1)
159 540309158 * (unsigned long long) (r->pieces - dark + 1);
160 464290999 total = count_from(r, cell + 1, light, dark);
161 464290999 ▶ if (light < r->pieces && (RU->route_step)(r->route, RU_SIDE_LIGHT, cell) > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (light < r->pieces && (RU->route_step)(r->route, RU_SIDE_LIGHT, cell) > 0) 354230423 110060576 464290999
if (light < r->pieces && (RU->route_step)(r->route, RU_SIDE_LIGHT, cell) > 0) 164027719 190202704 354230423
162 164027719 total += count_from(r, cell + 1, light + 1, dark);
163 464290999 ▶ if (dark < r->pieces && (RU->route_step)(r->route, RU_SIDE_DARK, cell) > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (dark < r->pieces && (RU->route_step)(r->route, RU_SIDE_DARK, cell) > 0) 376282398 88008601 464290999
if (dark < r->pieces && (RU->route_step)(r->route, RU_SIDE_DARK, cell) > 0) 376281413 985 376282398
164 376281413 total += count_from(r, cell + 1, light, dark + 1);
165 464290999 return total;
166 }
167  
168 26 static unsigned long long ru_count_positions(const struct ru_rules *rules)
169 {
170 struct ru_rules standard;
171 26 ▶ if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; } 0 26 26
172 26 ▶ if (!ru_rules_ok(rules)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ru_rules_ok(rules)) return 0; 0 26 26
173 26 return 2ULL * count_from(rules, 0, 0, 0);
174 }
175  
176 /* ---- a move made ------------------------------------------------------------------
177 *
178 * WHOSE TURN IT IS AFTERWARDS is decided here and nowhere else:
179 *
180 * a move to an ordinary cell the other side
181 * a move to a rosette the SAME side
182 * a capture on an ordinary cell the other side
183 * a capture on a rosette the same side
184 * a move home the other side: home is not a cell
185 * a forfeit the other side, always
186 *
187 * A CAPTURED PIECE GOES TO ITS OWNER'S HAND. Not to its owner's home, not to
188 * the mover's hand, and not back onto the board at its first step.
189 */
190  
191 7046523 static int ru_apply(struct ru_board *b, const struct ru_rules *rules,
192 const struct ru_move *m, struct ru_undo *u)
193 {
194 struct ru_undo local;
195 int side, enemy, i;
196  
197 (void) rules;
198 7046523 ▶ if (!u) u = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) u = &local; 0 7046523 7046523
199 7046523 u->kind = RU_UNDO_NONE;
200 35232615 ▶ for (i = 0; i < 4; i++) u->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) u->reserved[i] = 0; 28186092 7046523 35232615
201 7046523 ▶ if (!b || !m) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !m) return 0; 7046523 0 7046523
if (!b || !m) return 0; 0 7046523 7046523
202  
203 7046523 side = (RU->side)(b);
204 7046523 enemy = RU_OTHER(side);
205  
206 7046523 ▶ if (m->from_step == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->from_step == 0) { 1550428 5496095 7046523
207 1550428 ▶ if ((RU->hand)(b, side) < 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->hand)(b, side) < 1) return 0; 1 1550427 1550428
208 }
209 5496095 ▶ else if ((RU->at)(b, m->from_cell) != RU_PIECE_OF(side)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if ((RU->at)(b, m->from_cell) != RU_PIECE_OF(side)) return 0; 1 5496094 5496095
210  
211 7046521 u->kind = RU_UNDO_MOVE;
212 7046521 u->side = side;
213 7046521 u->ply = (RU->ply)(b);
214 7046521 u->from_step = m->from_step;
215 7046521 ▶ u->from_cell = m->from_step == 0 ? -1 : m->from_cell;
Branch: 1 branch
BranchTrueFalseTotal
u->from_cell = m->from_step == 0 ? -1 : m->from_cell; 5496094 1550427 7046521
216 7046521 ▶ u->to_cell = m->home ? -1 : m->to_cell;
Branch: 1 branch
BranchTrueFalseTotal
u->to_cell = m->home ? -1 : m->to_cell; 6751530 294991 7046521
217 7046521 u->captured = 0;
218  
219 7046521 ▶ if (m->from_step == 0) (RU->set_hand)(b, side, (RU->hand)(b, side) - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (m->from_step == 0) (RU->set_hand)(b, side, (RU->hand)(b, side) - 1); 1550427 5496094 7046521
220 5496094 else (RU->lift)(b, m->from_cell);
221  
222 7046521 ▶ if (m->home) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->home) { 294991 6751530 7046521
223 294991 (RU->set_home)(b, side, (RU->home)(b, side) + 1);
224 }
225 else {
226 6751530 ▶ if ((RU->at)(b, m->to_cell) == RU_PIECE_OF(enemy)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, m->to_cell) == RU_PIECE_OF(enemy)) { 821782 5929748 6751530
227 821782 u->captured = 1;
228 821782 (RU->set_hand)(b, enemy, (RU->hand)(b, enemy) + 1);
229 }
230 6751530 (RU->put)(b, m->to_cell, RU_PIECE_OF(side));
231 }
232  
233 7046521 ▶ if (m->home || !(RU->is_rosette)(m->to_cell)) (RU->set_side)(b, enemy);
Branch: 2 branches
BranchTrueFalseTotal
if (m->home || !(RU->is_rosette)(m->to_cell)) (RU->set_side)(b, enemy); 6751530 294991 7046521
if (m->home || !(RU->is_rosette)(m->to_cell)) (RU->set_side)(b, enemy); 5294907 1456623 6751530
234 7046521 (RU->set_ply)(b, u->ply + 1);
235 7046521 return 1;
236 }
237  
238 366272 static void ru_forfeit(struct ru_board *b, struct ru_undo *u)
239 {
240 struct ru_undo local;
241 int i;
242 366272 ▶ if (!u) u = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) u = &local; 0 366272 366272
243 366272 u->kind = RU_UNDO_NONE;
244 1831360 ▶ for (i = 0; i < 4; i++) u->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) u->reserved[i] = 0; 1465088 366272 1831360
245 366272 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 366272 366272
246 366272 u->kind = RU_UNDO_FORFEIT;
247 366272 u->side = (RU->side)(b);
248 366272 u->ply = (RU->ply)(b);
249 366272 u->from_step = 0;
250 366272 u->from_cell = u->to_cell = -1;
251 366272 u->captured = 0;
252 366272 (RU->set_side)(b, RU_OTHER(u->side));
253 366272 (RU->set_ply)(b, u->ply + 1);
254 }
255  
256 7184130 static void ru_unapply(struct ru_board *b, const struct ru_undo *u)
257 {
258 int side, enemy;
259 7184130 ▶ if (!b || !u || u->kind == RU_UNDO_NONE) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !u || u->kind == RU_UNDO_NONE) return; 7184130 0 7184130
if (!b || !u || u->kind == RU_UNDO_NONE) return; 7184130 0 7184130
if (!b || !u || u->kind == RU_UNDO_NONE) return; 0 7184130 7184130
260 7184130 side = u->side;
261 7184130 enemy = RU_OTHER(side);
262  
263 7184130 ▶ if (u->kind == RU_UNDO_MOVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->kind == RU_UNDO_MOVE) { 6828289 355841 7184130
264 6828289 ▶ if (u->to_cell < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->to_cell < 0) { 281602 6546687 6828289
265 281602 (RU->set_home)(b, side, (RU->home)(b, side) - 1);
266 }
267 6546687 ▶ else if (u->captured) {
Branch: 1 branch
BranchTrueFalseTotal
else if (u->captured) { 796081 5750606 6546687
268 796081 (RU->put)(b, u->to_cell, RU_PIECE_OF(enemy));
269 796081 (RU->set_hand)(b, enemy, (RU->hand)(b, enemy) - 1);
270 }
271 else {
272 5750606 (RU->lift)(b, u->to_cell);
273 }
274 6828289 ▶ if (u->from_step == 0) (RU->set_hand)(b, side, (RU->hand)(b, side) + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (u->from_step == 0) (RU->set_hand)(b, side, (RU->hand)(b, side) + 1); 1507572 5320717 6828289
275 5320717 else (RU->put)(b, u->from_cell, RU_PIECE_OF(side));
276 }
277 7184130 (RU->set_side)(b, side);
278 7184130 (RU->set_ply)(b, u->ply);
279 }
280  
281 /* ---- how a game stands --------------------------------------------------------------
282 *
283 * Asked in this order, because the move that brings the last piece home can
284 * also be the ply that reaches the cap: home first.
285 */
286  
287 14422652 static int ru_winner(const struct ru_board *b, const struct ru_rules *rules)
288 {
289 struct ru_rules standard;
290 14422652 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 14422652 14422652
291 14422652 ▶ if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; } 0 14422652 14422652
292 14422652 ▶ if ((RU->home)(b, RU_SIDE_LIGHT) >= rules->pieces) return RU_SIDE_LIGHT;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->home)(b, RU_SIDE_LIGHT) >= rules->pieces) return RU_SIDE_LIGHT; 5177 14417475 14422652
293 14417475 ▶ if ((RU->home)(b, RU_SIDE_DARK) >= rules->pieces) return RU_SIDE_DARK;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->home)(b, RU_SIDE_DARK) >= rules->pieces) return RU_SIDE_DARK; 5336 14412139 14417475
294 14412139 return -1;
295 }
296  
297 8350631 static int ru_how(const struct ru_board *b, const struct ru_rules *rules)
298 {
299 8350631 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 8350631 8350631
300 8350631 ▶ if (ru_winner(b, rules) >= 0) return RU_BY_HOME;
Branch: 1 branch
BranchTrueFalseTotal
if (ru_winner(b, rules) >= 0) return RU_BY_HOME; 7816 8342815 8350631
301 8342815 ▶ if ((RU->ply)(b) >= RU_PLY_CAP) return RU_BY_PLY_CAP;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->ply)(b) >= RU_PLY_CAP) return RU_BY_PLY_CAP; 7 8342808 8342815
302 8342808 return 0;
303 }
304  
305 8348065 static int ru_status(const struct ru_board *b, const struct ru_rules *rules)
306 {
307 8348065 int how = ru_how(b, rules);
308 8348065 ▶ if (how == RU_BY_HOME) return RU_WON;
Branch: 1 branch
BranchTrueFalseTotal
if (how == RU_BY_HOME) return RU_WON; 5254 8342811 8348065
309 8342811 ▶ if (how == RU_BY_PLY_CAP) return RU_DRAWN;
Branch: 1 branch
BranchTrueFalseTotal
if (how == RU_BY_PLY_CAP) return RU_DRAWN; 5 8342806 8342811
310 8342806 return RU_ONGOING;
311 }
312  
313 1 static int ru_ply_cap(void) { return RU_PLY_CAP; }
314  
315 /* ---- the walk ------------------------------------------------------------------------ */
316  
317 1102876 static unsigned long long ru_walk(struct ru_board *b, const struct ru_rules *rules, int depth)
318 {
319 struct ru_rules standard;
320 struct ru_move m[RU_MOVES_MAX];
321 struct ru_undo u;
322 1102876 unsigned long long total = 0;
323 int rolls, i, k, n, roll, weight, denominator;
324  
325 1102876 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 1102876 1102876
326 1102876 ▶ if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rules) { ru_rules_named(RU_RULES_FINKEL, &standard); rules = &standard; } 0 1102876 1102876
327 1102876 ▶ if (!ru_rules_ok(rules)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ru_rules_ok(rules)) return 0; 0 1102876 1102876
328 1102876 ▶ if (depth <= 0 || ru_status(b, rules) != RU_ONGOING) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (depth <= 0 || ru_status(b, rules) != RU_ONGOING) return 1; 90444 1012432 1102876
if (depth <= 0 || ru_status(b, rules) != RU_ONGOING) return 1; 12 90432 90444
329  
330 90432 rolls = (RU->chance_count)(rules->dice, rules->zero_rolls);
331 517951 ▶ for (i = 0; i < rolls; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rolls; i++) { 427519 90432 517951
332 427519 (RU->chance)(rules->dice, rules->zero_rolls, i, &roll, &weight, &denominator);
333 427519 n = ru_moves(b, rules, roll, m, RU_MOVES_MAX);
334 427519 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 66872 360647 427519
335 66872 ru_forfeit(b, &u);
336 66872 total += ru_walk(b, rules, depth - 1);
337 66872 ru_unapply(b, &u);
338 66872 continue;
339 }
340 1396167 ▶ for (k = 0; k < n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) { 1035520 360647 1396167
341 1035520 ru_apply(b, rules, &m[k], &u);
342 1035520 total += ru_walk(b, rules, depth - 1);
343 1035520 ru_unapply(b, &u);
344 }
345 }
346 90432 return total;
347 }
348  
349 /* ---- the table ------------------------------------------------------------------- */
350  
351 73 void ru_moves_install(struct ru_abi *t)
352 {
353 73 RU = t;
354 73 t->rules_named = ru_rules_named;
355 73 t->rules_ok = ru_rules_ok;
356 73 t->moves = ru_moves;
357 73 t->count_positions = ru_count_positions;
358 73 t->apply = ru_apply;
359 73 t->forfeit = ru_forfeit;
360 73 t->unapply = ru_unapply;
361 73 t->status = ru_status;
362 73 t->how = ru_how;
363 73 t->winner = ru_winner;
364 73 t->ply_cap = ru_ply_cap;
365 73 t->walk = ru_walk;
366 73 }
« RoyalUr.xs ↑ Summary ru_search.c »