sth_moves.c

filter: Statementstmt 100% 97 / 97 Branchbran 75.0% 93 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.9% 190 / 221 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sth_engine.c ↑ Summary sth_rules.c »
1 /* sth_moves.c - one step, one hop, and moving a piece.
2 *
3 * PERL-FREE by design: see include/sth_abi.h.
4 *
5 * THIS FILE'S SCOPE. Version 2: the two atoms a move is made of, a step to a
6 * neighbouring empty hole and a single hop over one neighbouring piece of any
7 * seat into the empty hole straight beyond it. Nothing is captured, so a hop
8 * changes nothing but where the hopping piece is. Moving a piece is a
9 * structure primitive like put, and judges nothing about whose turn it is.
10 * Version 3: the chain built from the atoms, as a reach, a path and the legal
11 * moves of the seat to move.
12 */
13  
14 #include "sth_internal.h"
15  
16 18717194 int sth_steps(const struct sth_board *b, int hole, int *out, int max)
17 {
18 18717194 int d, sq, to, n = 0;
19 18717194 ▶ if (!b || !STH_IS_HOLE(hole)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !STH_IS_HOLE(hole)) return 0; 18717194 0 18717194
if (!b || !STH_IS_HOLE(hole)) return 0; 18717194 0 18717194
if (!b || !STH_IS_HOLE(hole)) return 0; 0 18717194 18717194
20 18717194 sq = STH_SQ_OF[hole];
21 131020358 ▶ for (d = 0; d < STH_DIRS; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < STH_DIRS; d++) { 112303164 18717194 131020358
22 112303164 to = sq + STH_STEP[d];
23 112303164 ▶ if (b->cell[to] != STH_EMPTY) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[to] != STH_EMPTY) continue; 59613869 52689295 112303164
24 52689295 ▶ if (n < max) out[n] = STH_HOLE_AT[to];
Branch: 1 branch
BranchTrueFalseTotal
if (n < max) out[n] = STH_HOLE_AT[to]; 52689295 0 52689295
25 52689295 n++;
26 }
27 18717194 return n;
28 }
29  
30 /* THE MIDDLE IS READ BEFORE THE LANDING. A middle that holds a piece is a
31 * hole, so the landing is one square past a hole at most, inside the margin
32 * whatever the direction. `from` is the hole the moving piece left: in a chain
33 * it is empty, so it can be landed on and cannot be hopped over. */
34 52477138 int sth_hops(const struct sth_board *b, int hole, int from, int *out, int max)
35 {
36 52477138 int d, sq, mid, land, from_sq, c, n = 0;
37 52477138 ▶ if (!b || !STH_IS_HOLE(hole)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !STH_IS_HOLE(hole)) return 0; 52477138 0 52477138
if (!b || !STH_IS_HOLE(hole)) return 0; 52477138 0 52477138
if (!b || !STH_IS_HOLE(hole)) return 0; 0 52477138 52477138
38 52477138 sq = STH_SQ_OF[hole];
39 52477138 ▶ from_sq = STH_IS_HOLE(from) ? STH_SQ_OF[from] : -1;
Branch: 2 branches
BranchTrueFalseTotal
from_sq = STH_IS_HOLE(from) ? STH_SQ_OF[from] : -1; 52477137 1 52477138
from_sq = STH_IS_HOLE(from) ? STH_SQ_OF[from] : -1; 52477137 0 52477137
40 367339966 ▶ for (d = 0; d < STH_DIRS; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < STH_DIRS; d++) { 314862828 52477138 367339966
41 314862828 mid = sq + STH_STEP[d];
42 314862828 ▶ if (mid == from_sq || !STH_IS_PIECE(b->cell[mid])) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (mid == from_sq || !STH_IS_PIECE(b->cell[mid])) continue; 314862827 1 314862828
if (mid == from_sq || !STH_IS_PIECE(b->cell[mid])) continue; 197725241 117137586 314862827
43 117137586 land = mid + STH_STEP[d];
44 117137586 c = b->cell[land];
45 117137586 ▶ if (c != STH_EMPTY && land != from_sq) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c != STH_EMPTY && land != from_sq) continue; 61197942 55939644 117137586
if (c != STH_EMPTY && land != from_sq) continue; 44584036 16613906 61197942
46 72553550 ▶ if (n < max) out[n] = STH_HOLE_AT[land];
Branch: 1 branch
BranchTrueFalseTotal
if (n < max) out[n] = STH_HOLE_AT[land]; 72553550 0 72553550
47 72553550 n++;
48 }
49 52477138 return n;
50 }
51  
52 23449167 int sth_do_move(struct sth_board *b, int from, int to)
53 {
54 int fsq, tsq, seat;
55 23449167 ▶ if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 23449167 0 23449167
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 23449167 0 23449167
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 23449167 0 23449167
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 23449167 0 23449167
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 23449167 0 23449167
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 1 23449166 23449167
56 23449166 fsq = STH_SQ_OF[from];
57 23449166 tsq = STH_SQ_OF[to];
58 23449166 seat = b->cell[fsq];
59 23449166 ▶ if (!STH_IS_PIECE(seat) || b->cell[tsq] != STH_EMPTY) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!STH_IS_PIECE(seat) || b->cell[tsq] != STH_EMPTY) return 0; 23449165 1 23449166
if (!STH_IS_PIECE(seat) || b->cell[tsq] != STH_EMPTY) return 0; 1 23449164 23449165
60 23449164 b->cell[fsq] = STH_EMPTY;
61 23449164 b->cell[tsq] = (unsigned char) seat;
62 23449164 b->key ^= STH_ZOB[seat][from] ^ STH_ZOB[seat][to];
63 23449164 return 1;
64 }
65  
66 /* The move from `from` to `to` taken back: the piece on `to` goes home. */
67 22583954 int sth_undo_move(struct sth_board *b, int from, int to)
68 {
69 int fsq, tsq, seat;
70 22583954 ▶ if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 22583954 0 22583954
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 22583954 0 22583954
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 22583954 0 22583954
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 22583954 0 22583954
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 22583954 0 22583954
if (!b || !STH_IS_HOLE(from) || !STH_IS_HOLE(to) || from == to) return 0; 0 22583954 22583954
71 22583954 fsq = STH_SQ_OF[from];
72 22583954 tsq = STH_SQ_OF[to];
73 22583954 seat = b->cell[tsq];
74 22583954 ▶ if (!STH_IS_PIECE(seat) || b->cell[fsq] != STH_EMPTY) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!STH_IS_PIECE(seat) || b->cell[fsq] != STH_EMPTY) return 0; 22583953 1 22583954
if (!STH_IS_PIECE(seat) || b->cell[fsq] != STH_EMPTY) return 0; 0 22583953 22583953
75 22583953 b->cell[tsq] = STH_EMPTY;
76 22583953 b->cell[fsq] = (unsigned char) seat;
77 22583953 b->key ^= STH_ZOB[seat][to] ^ STH_ZOB[seat][from];
78 22583953 return 1;
79 }
80  
81 /* ---- version 3: the reach, the path and the legal moves ---------------------
82 *
83 * A move is a step, or a chain of hops that may stop after any hop. Nothing
84 * is captured, so the board after a move depends only on where it started and
85 * where it stopped, and the reach is the set of holes a piece can stop on.
86 *
87 * WALKS AND SIMPLE PATHS REACH THE SAME HOLES. While a chain runs only the
88 * hopping piece moves, and its start is empty throughout, so the board it hops
89 * over never changes: the hops form a fixed graph, and anything a walk round
90 * that graph reaches, a path with no hole twice reaches too. So a breadth-first
91 * search that visits each hole once finds every stop, and the first time it
92 * finds a hole is by a shortest chain.
93 */
94  
95 struct sth_bfs {
96 int parent[STH_HOLES]; /* the hole a chain hopped from, or -1 */
97 int depth[STH_HOLES]; /* hops to get here; 0 for the start */
98 int found[STH_HOLES]; /* 1 for every hole a chain can stop on */
99 };
100  
101 18344081 static void hop_search(const struct sth_board *b, int from, struct sth_bfs *s)
102 {
103 18344081 int queue[STH_HOLES], head = 0, tail = 0, i, n, at, land[STH_HOPS_MAX];
104 2237977882 ▶ for (i = 0; i < STH_HOLES; i++) { s->parent[i] = -1; s->depth[i] = -1; s->found[i] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_HOLES; i++) { s->parent[i] = -1; s->depth[i] = -1; s->found[i] = 0; } 2219633801 18344081 2237977882
105 18344081 s->depth[from] = 0;
106 18344081 queue[tail++] = from;
107 69383016 ▶ while (head < tail) {
Branch: 1 branch
BranchTrueFalseTotal
while (head < tail) { 51038935 18344081 69383016
108 51038935 at = queue[head++];
109 51038935 n = sth_hops(b, at, from, land, STH_HOPS_MAX);
110 120955127 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 69916192 51038935 120955127
111 69916192 ▶ if (s->depth[land[i]] >= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (s->depth[land[i]] >= 0) continue; 37221338 32694854 69916192
112 32694854 s->depth[land[i]] = s->depth[at] + 1;
113 32694854 s->parent[land[i]] = at;
114 32694854 s->found[land[i]] = 1;
115 32694854 queue[tail++] = land[i];
116 }
117 }
118 18344081 }
119  
120 18647297 static int movable(const struct sth_board *b, int from)
121 {
122 18647297 ▶ return b && STH_IS_HOLE(from) && STH_IS_PIECE(b->cell[STH_SQ_OF[from]]);
Branch: 4 branches
BranchTrueFalseTotal
return b && STH_IS_HOLE(from) && STH_IS_PIECE(b->cell[STH_SQ_OF[from]]); 18647297 0 18647297
return b && STH_IS_HOLE(from) && STH_IS_PIECE(b->cell[STH_SQ_OF[from]]); 18647297 0 18647297
return b && STH_IS_HOLE(from) && STH_IS_PIECE(b->cell[STH_SQ_OF[from]]); 18647297 0 18647297
return b && STH_IS_HOLE(from) && STH_IS_PIECE(b->cell[STH_SQ_OF[from]]); 18647295 2 18647297
123 }
124  
125 18125728 int sth_reach(const struct sth_board *b, int from, int *out, int max)
126 {
127 struct sth_bfs s;
128 18125728 int step[STH_STEPS_MAX], i, ns, n = 0;
129 18125728 ▶ if (!movable(b, from)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!movable(b, from)) return 0; 1 18125727 18125728
130 18125727 hop_search(b, from, &s);
131 18125727 ns = sth_steps(b, from, step, STH_STEPS_MAX);
132 68877238 ▶ for (i = 0; i < ns; i++) s.found[step[i]] = 1;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ns; i++) s.found[step[i]] = 1; 50751511 18125727 68877238
133 18125727 s.found[from] = 0;
134 2211338694 ▶ for (i = 0; i < STH_HOLES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_HOLES; i++) { 2193212967 18125727 2211338694
135 2193212967 ▶ if (!s.found[i]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!s.found[i]) continue; 2111049601 82163366 2193212967
136 82163366 ▶ if (n < max) out[n] = i;
Branch: 1 branch
BranchTrueFalseTotal
if (n < max) out[n] = i; 82163366 0 82163366
137 82163366 n++;
138 }
139 18125727 return n;
140 }
141  
142 /* A STEP IS SHOWN AS A STEP. A hole a step reaches is shown as the step, even
143 * when a chain reaches it too: the board ends the same, and one move is one
144 * move. Otherwise the BFS parents, which is a shortest chain, and among the
145 * shortest the one whose hops were found first in direction order. */
146 521569 int sth_path(const struct sth_board *b, int from, int to, int *out, int max)
147 {
148 struct sth_bfs s;
149 int step[STH_STEPS_MAX], i, ns, n, at;
150 521569 ▶ if (!movable(b, from) || !STH_IS_HOLE(to) || to == from) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!movable(b, from) || !STH_IS_HOLE(to) || to == from) return 0; 521568 1 521569
if (!movable(b, from) || !STH_IS_HOLE(to) || to == from) return 0; 521568 0 521568
if (!movable(b, from) || !STH_IS_HOLE(to) || to == from) return 0; 521568 0 521568
if (!movable(b, from) || !STH_IS_HOLE(to) || to == from) return 0; 2 521566 521568
151 521566 ns = sth_steps(b, from, step, STH_STEPS_MAX);
152 1503441 ▶ for (i = 0; i < ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ns; i++) { 1285087 218354 1503441
153 1285087 ▶ if (step[i] != to) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (step[i] != to) continue; 981875 303212 1285087
154 303212 ▶ if (max > 0) out[0] = from;
Branch: 1 branch
BranchTrueFalseTotal
if (max > 0) out[0] = from; 303212 0 303212
155 303212 ▶ if (max > 1) out[1] = to;
Branch: 1 branch
BranchTrueFalseTotal
if (max > 1) out[1] = to; 303212 0 303212
156 303212 return 2;
157 }
158 218354 hop_search(b, from, &s);
159 218354 ▶ if (!s.found[to]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s.found[to]) return 0; 6 218348 218354
160 218348 n = s.depth[to] + 1;
161 905061 ▶ for (at = to, i = n - 1; i >= 0; i--, at = s.parent[at])
Branch: 1 branch
BranchTrueFalseTotal
for (at = to, i = n - 1; i >= 0; i--, at = s.parent[at]) 686713 218348 905061
162 686713 ▶ if (i < max) out[i] = at;
Branch: 1 branch
BranchTrueFalseTotal
if (i < max) out[i] = at; 686713 0 686713
163 218348 return n;
164 }
165  
166 1698923 int sth_legal(const struct sth_board *b, int *out_from, int *out_to, int max)
167 {
168 1698923 int h, i, k, n = 0, to[STH_HOLES];
169 1698923 ▶ if (!b) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return 0; 0 1698923 1698923
170 207268606 ▶ for (h = 0; h < STH_HOLES; h++) {
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) { 205569683 1698923 207268606
171 205569683 ▶ if (b->cell[STH_SQ_OF[h]] != b->to_move) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[STH_SQ_OF[h]] != b->to_move) continue; 188614976 16954707 205569683
172 16954707 k = sth_reach(b, h, to, STH_HOLES);
173 91802797 ▶ for (i = 0; i < k; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < k; i++) { 74848090 16954707 91802797
174 74848090 ▶ if (n < max) { out_from[n] = h; out_to[n] = to[i]; }
Branch: 1 branch
BranchTrueFalseTotal
if (n < max) { out_from[n] = h; out_to[n] = to[i]; } 70204961 4643129 74848090
175 74848090 n++;
176 }
177 }
178 1698923 return n;
179 }
« sth_engine.c ↑ Summary sth_rules.c »