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 3195495 static int ru_rules_named(int name, struct ru_rules *r)
20 {
21 int i;
22 3195495 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 3195495 3195495
23 15977475 ▶ for (i = 0; i < 4; i++) r->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) r->reserved[i] = 0; 12781980 3195495 15977475
24 3195495 ▶ if (name == RU_RULES_FINKEL) {
Branch: 1 branch
BranchTrueFalseTotal
if (name == RU_RULES_FINKEL) { 3158064 37431 3195495
25 3158064 r->route = RU_ROUTE_SHORT;
26 3158064 r->dice = 4;
27 3158064 r->zero_rolls = 0;
28 3158064 r->safe_rosettes = 1;
29 3158064 r->pieces = 7;
30 3158064 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 13162889 static int ru_rules_ok(const struct ru_rules *r)
44 {
45 13162889 ▶ if (!r) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return 0; 0 13162889 13162889
46 13162889 ▶ 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; 6111729 7051160 13162889
if (r->route != RU_ROUTE_SHORT && r->route != RU_ROUTE_LONG) return 0; 0 6111729 6111729
47 13162889 ▶ if (r->dice != 3 && r->dice != 4) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->dice != 3 && r->dice != 4) return 0; 7465874 5697015 13162889
if (r->dice != 3 && r->dice != 4) return 0; 2 7465872 7465874
48 13162887 ▶ 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; 5697016 7465871 13162887
if (r->zero_rolls != 0 && r->zero_rolls != 4) return 0; 1 5697015 5697016
49 13162886 ▶ 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; 7106610 6056276 13162886
if (r->safe_rosettes != 0 && r->safe_rosettes != 1) return 0; 0 7106610 7106610
50 13162886 ▶ 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; 13162885 1 13162886
if (r->pieces < 1 || r->pieces > RU_PIECES_MAX) return 0; 1 13162884 13162885
51 13162884 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 11796457 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 11796457 int len = (RU->route_len)(r->route);
82 11796457 int to = from_step + roll;
83 int cell, standing;
84  
85 11796457 ▶ if (to > len + 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (to > len + 1) return 0; 600938 11195519 11796457
86  
87 11195519 m->from_step = from_step;
88 11195519 m->to_step = to;
89 11195519 ▶ 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); 8365814 2829705 11195519
90 11195519 m->to_cell = -1;
91 11195519 m->captures = 0;
92 11195519 m->rosette = 0;
93 11195519 m->home = 0;
94 11195519 m->reserved[0] = m->reserved[1] = 0;
95  
96 11195519 ▶ if (to == len + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (to == len + 1) { 393037 10802482 11195519
97 393037 m->home = 1;
98 393037 return 1;
99 }
100  
101 10802482 cell = (RU->route_cell)(r->route, side, to);
102 10802482 standing = (RU->at)(b, cell);
103 10802482 ▶ if (standing == RU_PIECE_OF(side)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (standing == RU_PIECE_OF(side)) return 0; 2413654 8388828 10802482
104 8388828 ▶ if (standing == RU_PIECE_OF(RU_OTHER(side))) {
Branch: 2 branches
BranchTrueFalseTotal
if (standing == RU_PIECE_OF(RU_OTHER(side))) { 4198386 4190442 8388828
if (standing == RU_PIECE_OF(RU_OTHER(side))) { 1130783 7258045 8388828
105 1130783 ▶ if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0; 615954 514829 1130783
if (r->safe_rosettes && (RU->is_rosette)(cell)) return 0; 80930 535024 615954
106 1049853 m->captures = 1;
107 }
108 8307898 m->to_cell = cell;
109 8307898 m->rosette = (RU->is_rosette)(cell) ? 1 : 0;
110 8307898 return 1;
111 }
112  
113 3418038 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 3418038 int side, len, step, n = 0;
119  
120 3418038 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 3418038 3418038
121 3418038 ▶ if (roll < 0 || roll > RU_ROLL_MAX) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (roll < 0 || roll > RU_ROLL_MAX) return -1; 3418038 0 3418038
if (roll < 0 || roll > RU_ROLL_MAX) return -1; 0 3418038 3418038
122 3418038 ▶ 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 3418038 3418038
123 3418038 ▶ if (!ru_rules_ok(rules)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!ru_rules_ok(rules)) return -1; 0 3418038 3418038
124 3418038 ▶ if (roll == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (roll == 0) return 0; 360373 3057665 3418038
125  
126 3057665 side = (RU->side)(b);
127 3057665 len = (RU->route_len)(rules->route);
128  
129 3057665 ▶ 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)) { 2829705 227960 3057665
if ((RU->hand)(b, side) > 0 && consider(b, rules, side, 0, roll, &m)) { 1845078 984627 2829705
130 1845078 ▶ if (out && n < max) out[n] = m;
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) out[n] = m; 1845078 0 1845078
if (out && n < max) out[n] = m; 1845078 0 1845078
131 1845078 n++;
132 }
133 48779529 ▶ for (step = 1; step <= len; step++) {
Branch: 1 branch
BranchTrueFalseTotal
for (step = 1; step <= len; step++) { 45721864 3057665 48779529
134 45721864 int cell = (RU->route_cell)(rules->route, side, step);
135 45721864 ▶ if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->at)(b, cell) != RU_PIECE_OF(side)) continue; 36755112 8966752 45721864
136 8966752 ▶ if (!consider(b, rules, side, step, roll, &m)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!consider(b, rules, side, step, roll, &m)) continue; 2110895 6855857 8966752
137 6855857 ▶ if (out && n < max) out[n] = m;
Branch: 2 branches
BranchTrueFalseTotal
if (out && n < max) out[n] = m; 6855857 0 6855857
if (out && n < max) out[n] = m; 6855857 0 6855857
138 6855857 n++;
139 }
140 3057665 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 7053666 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 7053666 ▶ if (!u) u = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) u = &local; 0 7053666 7053666
199 7053666 u->kind = RU_UNDO_NONE;
200 35268330 ▶ for (i = 0; i < 4; i++) u->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) u->reserved[i] = 0; 28214664 7053666 35268330
201 7053666 ▶ if (!b || !m) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !m) return 0; 7053666 0 7053666
if (!b || !m) return 0; 0 7053666 7053666
202  
203 7053666 side = (RU->side)(b);
204 7053666 enemy = RU_OTHER(side);
205  
206 7053666 ▶ if (m->from_step == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->from_step == 0) { 1552146 5501520 7053666
207 1552146 ▶ if ((RU->hand)(b, side) < 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->hand)(b, side) < 1) return 0; 1 1552145 1552146
208 }
209 5501520 ▶ 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 5501519 5501520
210  
211 7053664 u->kind = RU_UNDO_MOVE;
212 7053664 u->side = side;
213 7053664 u->ply = (RU->ply)(b);
214 7053664 u->from_step = m->from_step;
215 7053664 ▶ 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; 5501519 1552145 7053664
216 7053664 ▶ u->to_cell = m->home ? -1 : m->to_cell;
Branch: 1 branch
BranchTrueFalseTotal
u->to_cell = m->home ? -1 : m->to_cell; 6758321 295343 7053664
217 7053664 u->captured = 0;
218  
219 7053664 ▶ 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); 1552145 5501519 7053664
220 5501519 else (RU->lift)(b, m->from_cell);
221  
222 7053664 ▶ if (m->home) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->home) { 295343 6758321 7053664
223 295343 (RU->set_home)(b, side, (RU->home)(b, side) + 1);
224 }
225 else {
226 6758321 ▶ 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)) { 822423 5935898 6758321
227 822423 u->captured = 1;
228 822423 (RU->set_hand)(b, enemy, (RU->hand)(b, enemy) + 1);
229 }
230 6758321 (RU->put)(b, m->to_cell, RU_PIECE_OF(side));
231 }
232  
233 7053664 ▶ 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); 6758321 295343 7053664
if (m->home || !(RU->is_rosette)(m->to_cell)) (RU->set_side)(b, enemy); 5300098 1458223 6758321
234 7053664 (RU->set_ply)(b, u->ply + 1);
235 7053664 return 1;
236 }
237  
238 366626 static void ru_forfeit(struct ru_board *b, struct ru_undo *u)
239 {
240 struct ru_undo local;
241 int i;
242 366626 ▶ if (!u) u = &local;
Branch: 1 branch
BranchTrueFalseTotal
if (!u) u = &local; 0 366626 366626
243 366626 u->kind = RU_UNDO_NONE;
244 1833130 ▶ for (i = 0; i < 4; i++) u->reserved[i] = 0;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) u->reserved[i] = 0; 1466504 366626 1833130
245 366626 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 366626 366626
246 366626 u->kind = RU_UNDO_FORFEIT;
247 366626 u->side = (RU->side)(b);
248 366626 u->ply = (RU->ply)(b);
249 366626 u->from_step = 0;
250 366626 u->from_cell = u->to_cell = -1;
251 366626 u->captured = 0;
252 366626 (RU->set_side)(b, RU_OTHER(u->side));
253 366626 (RU->set_ply)(b, u->ply + 1);
254 }
255  
256 7185803 static void ru_unapply(struct ru_board *b, const struct ru_undo *u)
257 {
258 int side, enemy;
259 7185803 ▶ if (!b || !u || u->kind == RU_UNDO_NONE) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !u || u->kind == RU_UNDO_NONE) return; 7185803 0 7185803
if (!b || !u || u->kind == RU_UNDO_NONE) return; 7185803 0 7185803
if (!b || !u || u->kind == RU_UNDO_NONE) return; 0 7185803 7185803
260 7185803 side = u->side;
261 7185803 enemy = RU_OTHER(side);
262  
263 7185803 ▶ if (u->kind == RU_UNDO_MOVE) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->kind == RU_UNDO_MOVE) { 6829838 355965 7185803
264 6829838 ▶ if (u->to_cell < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->to_cell < 0) { 281629 6548209 6829838
265 281629 (RU->set_home)(b, side, (RU->home)(b, side) - 1);
266 }
267 6548209 ▶ else if (u->captured) {
Branch: 1 branch
BranchTrueFalseTotal
else if (u->captured) { 796231 5751978 6548209
268 796231 (RU->put)(b, u->to_cell, RU_PIECE_OF(enemy));
269 796231 (RU->set_hand)(b, enemy, (RU->hand)(b, enemy) - 1);
270 }
271 else {
272 5751978 (RU->lift)(b, u->to_cell);
273 }
274 6829838 ▶ 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); 1507954 5321884 6829838
275 5321884 else (RU->put)(b, u->from_cell, RU_PIECE_OF(side));
276 }
277 7185803 (RU->set_side)(b, side);
278 7185803 (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 14482331 static int ru_winner(const struct ru_board *b, const struct ru_rules *rules)
288 {
289 struct ru_rules standard;
290 14482331 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 14482331 14482331
291 14482331 ▶ 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 14482331 14482331
292 14482331 ▶ 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; 5193 14477138 14482331
293 14477138 ▶ 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; 5495 14471643 14477138
294 14471643 return -1;
295 }
296  
297 8408600 static int ru_how(const struct ru_board *b, const struct ru_rules *rules)
298 {
299 8408600 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 8408600 8408600
300 8408600 ▶ if (ru_winner(b, rules) >= 0) return RU_BY_HOME;
Branch: 1 branch
BranchTrueFalseTotal
if (ru_winner(b, rules) >= 0) return RU_BY_HOME; 7943 8400657 8408600
301 8400657 ▶ 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 8400650 8400657
302 8400650 return 0;
303 }
304  
305 8406004 static int ru_status(const struct ru_board *b, const struct ru_rules *rules)
306 {
307 8406004 int how = ru_how(b, rules);
308 8406004 ▶ if (how == RU_BY_HOME) return RU_WON;
Branch: 1 branch
BranchTrueFalseTotal
if (how == RU_BY_HOME) return RU_WON; 5351 8400653 8406004
309 8400653 ▶ if (how == RU_BY_PLY_CAP) return RU_DRAWN;
Branch: 1 branch
BranchTrueFalseTotal
if (how == RU_BY_PLY_CAP) return RU_DRAWN; 5 8400648 8400653
310 8400648 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 96 void ru_moves_install(struct ru_abi *t)
352 {
353 96 RU = t;
354 96 t->rules_named = ru_rules_named;
355 96 t->rules_ok = ru_rules_ok;
356 96 t->moves = ru_moves;
357 96 t->count_positions = ru_count_positions;
358 96 t->apply = ru_apply;
359 96 t->forfeit = ru_forfeit;
360 96 t->unapply = ru_unapply;
361 96 t->status = ru_status;
362 96 t->how = ru_how;
363 96 t->winner = ru_winner;
364 96 t->ply_cap = ru_ply_cap;
365 96 t->walk = ru_walk;
366 96 }
« RoyalUr.xs ↑ Summary ru_search.c »