mahjong_shanten.c

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 164 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 291 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mahjong_decompose.c ↑ Summary mahjong_tiles.c »
1 /* mahjong_shanten.c - the distance to ready.
2 *
3 * Per suit: a recursive grouping of the nine counts into sets, partials and
4 * at most one head, reduced to two 25-bit masks (sets 0..4 x partials 0..4,
5 * with and without a head) and memoised by the base-5 value of the counts.
6 * The honours group trivially. The hand is the best combination.
7 */
8  
9 #include <string.h>
10 #include "mahjong_abi.h"
11 #include "mahjong_shanten.h"
12  
13 #define BASE5_MAX 1953125 /* 5^9 */
14  
15 /* memo[head][index]: a 25-bit mask of reachable (sets, partials); 0 means
16 * unfilled (a filled entry always has at least the (0, 0) bit set) */
17 static unsigned int MEMO[2][BASE5_MAX];
18  
19 #define BIT(s, p) (1u << ((s) * 5 + (p)))
20  
21 struct suit_walk {
22 unsigned char c[9];
23 unsigned int mask[2];
24 };
25  
26 0 static void walk(struct suit_walk *w, int i, int sets, int partials, int head)
27 {
28 int c;
29 0 ▶ while (i < 9 && !w->c[i]) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < 9 && !w->c[i]) i++; 0 0 0
while (i < 9 && !w->c[i]) i++; 0 0 0
30 0 ▶ if (i >= 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 9) { 0 0 0
31 0 ▶ if (sets <= 4 && partials <= 4) w->mask[head] |= BIT(sets, partials);
Branch: 2 branches
BranchTrueFalseTotal
if (sets <= 4 && partials <= 4) w->mask[head] |= BIT(sets, partials); 0 0 0
if (sets <= 4 && partials <= 4) w->mask[head] |= BIT(sets, partials); 0 0 0
32 0 return;
33 }
34 0 c = w->c[i];
35  
36 /* a pung of i */
37 0 ▶ if (c >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 3) { 0 0 0
38 0 w->c[i] -= 3;
39 0 walk(w, i, sets + 1, partials, head);
40 0 w->c[i] += 3;
41 }
42 /* a chow from i */
43 0 ▶ if (i <= 6 && w->c[i + 1] && w->c[i + 2]) {
Branch: 3 branches
BranchTrueFalseTotal
if (i <= 6 && w->c[i + 1] && w->c[i + 2]) { 0 0 0
if (i <= 6 && w->c[i + 1] && w->c[i + 2]) { 0 0 0
if (i <= 6 && w->c[i + 1] && w->c[i + 2]) { 0 0 0
44 0 w->c[i]--; w->c[i + 1]--; w->c[i + 2]--;
45 0 walk(w, i, sets + 1, partials, head);
46 0 w->c[i]++; w->c[i + 1]++; w->c[i + 2]++;
47 }
48 /* a pair of i as the head, once */
49 0 ▶ if (c >= 2 && !head) {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 2 && !head) { 0 0 0
if (c >= 2 && !head) { 0 0 0
50 0 w->c[i] -= 2;
51 0 walk(w, i, sets, partials, 1);
52 0 w->c[i] += 2;
53 }
54 /* a pair of i as a partial */
55 0 ▶ if (c >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 2) { 0 0 0
56 0 w->c[i] -= 2;
57 0 walk(w, i, sets, partials + 1, head);
58 0 w->c[i] += 2;
59 }
60 /* two adjacent */
61 0 ▶ if (i <= 7 && w->c[i + 1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (i <= 7 && w->c[i + 1]) { 0 0 0
if (i <= 7 && w->c[i + 1]) { 0 0 0
62 0 w->c[i]--; w->c[i + 1]--;
63 0 walk(w, i, sets, partials + 1, head);
64 0 w->c[i]++; w->c[i + 1]++;
65 }
66 /* two a gap apart */
67 0 ▶ if (i <= 6 && w->c[i + 2]) {
Branch: 2 branches
BranchTrueFalseTotal
if (i <= 6 && w->c[i + 2]) { 0 0 0
if (i <= 6 && w->c[i + 2]) { 0 0 0
68 0 w->c[i]--; w->c[i + 2]--;
69 0 walk(w, i, sets, partials + 1, head);
70 0 w->c[i]++; w->c[i + 2]++;
71 }
72 /* leave one i ungrouped */
73 0 w->c[i]--;
74 0 walk(w, i, sets, partials, head);
75 0 w->c[i]++;
76 }
77  
78 0 static void suit_masks(const unsigned char *nine, unsigned int *out)
79 {
80 0 unsigned int idx = 0;
81 int i;
82 0 ▶ for (i = 0; i < 9; i++) idx = idx * 5 + nine[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 9; i++) idx = idx * 5 + nine[i]; 0 0 0
83 0 ▶ if (!MEMO[0][idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!MEMO[0][idx]) { 0 0 0
84 struct suit_walk w;
85 0 memcpy(w.c, nine, 9);
86 0 w.mask[0] = w.mask[1] = 0;
87 0 walk(&w, 0, 0, 0, 0);
88 0 MEMO[0][idx] = w.mask[0];
89 0 MEMO[1][idx] = w.mask[1];
90 }
91 0 out[0] = MEMO[0][idx];
92 0 out[1] = MEMO[1][idx];
93 0 }
94  
95 /* the honours: every kind with three or more is a set, with two a partial
96 * or the head */
97 0 static void honour_masks(const unsigned char *counts, unsigned int *out)
98 {
99 0 int k, sets = 0, pairs = 0;
100 0 ▶ for (k = 28; k <= 34; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 28; k <= 34; k++) { 0 0 0
101 0 ▶ if (counts[k] >= 3) sets++;
Branch: 1 branch
BranchTrueFalseTotal
if (counts[k] >= 3) sets++; 0 0 0
102 0 ▶ else if (counts[k] == 2) pairs++;
Branch: 1 branch
BranchTrueFalseTotal
else if (counts[k] == 2) pairs++; 0 0 0
103 }
104 0 out[0] = out[1] = 0;
105 0 ▶ if (sets <= 4 && pairs <= 4) out[0] |= BIT(sets, pairs);
Branch: 2 branches
BranchTrueFalseTotal
if (sets <= 4 && pairs <= 4) out[0] |= BIT(sets, pairs); 0 0 0
if (sets <= 4 && pairs <= 4) out[0] |= BIT(sets, pairs); 0 0 0
106 0 ▶ if (pairs && sets <= 4) out[1] |= BIT(sets, pairs - 1);
Branch: 2 branches
BranchTrueFalseTotal
if (pairs && sets <= 4) out[1] |= BIT(sets, pairs - 1); 0 0 0
if (pairs && sets <= 4) out[1] |= BIT(sets, pairs - 1); 0 0 0
107 0 ▶ if (!out[0]) out[0] = BIT(0, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (!out[0]) out[0] = BIT(0, 0); 0 0 0
108 0 }
109  
110 0 static int standard(const unsigned char *counts, int melds)
111 {
112 unsigned int m[4][2];
113 0 int best = 99, g, s0, p0, s1, p1, s2, p2, s3, p3, h0, h1, h2, h3;
114 unsigned char nine[9];
115 0 ▶ for (g = 0; g < 3; g++) {
Branch: 1 branch
BranchTrueFalseTotal
for (g = 0; g < 3; g++) { 0 0 0
116 0 memcpy(nine, counts + 1 + 9 * g, 9);
117 0 suit_masks(nine, m[g]);
118 }
119 0 honour_masks(counts, m[3]);
120  
121 /* every combination of one (sets, partials) from each group, at most
122 * one head among them */
123 0 ▶ for (h0 = 0; h0 < 2; h0++) for (s0 = 0; s0 <= 4; s0++) for (p0 = 0; p0 <= 4; p0++) {
Branch: 3 branches
BranchTrueFalseTotal
for (h0 = 0; h0 < 2; h0++) for (s0 = 0; s0 <= 4; s0++) for (p0 = 0; p0 <= 4; p0++) { 0 0 0
for (h0 = 0; h0 < 2; h0++) for (s0 = 0; s0 <= 4; s0++) for (p0 = 0; p0 <= 4; p0++) { 0 0 0
for (h0 = 0; h0 < 2; h0++) for (s0 = 0; s0 <= 4; s0++) for (p0 = 0; p0 <= 4; p0++) { 0 0 0
124 0 ▶ if (!(m[0][h0] & BIT(s0, p0))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(m[0][h0] & BIT(s0, p0))) continue; 0 0 0
125 0 ▶ for (h1 = 0; h1 < 2; h1++) { if (h1 && h0) continue;
Branch: 3 branches
BranchTrueFalseTotal
for (h1 = 0; h1 < 2; h1++) { if (h1 && h0) continue; 0 0 0
for (h1 = 0; h1 < 2; h1++) { if (h1 && h0) continue; 0 0 0
for (h1 = 0; h1 < 2; h1++) { if (h1 && h0) continue; 0 0 0
126 0 ▶ for (s1 = 0; s1 <= 4; s1++) for (p1 = 0; p1 <= 4; p1++) {
Branch: 2 branches
BranchTrueFalseTotal
for (s1 = 0; s1 <= 4; s1++) for (p1 = 0; p1 <= 4; p1++) { 0 0 0
for (s1 = 0; s1 <= 4; s1++) for (p1 = 0; p1 <= 4; p1++) { 0 0 0
127 0 ▶ if (!(m[1][h1] & BIT(s1, p1))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(m[1][h1] & BIT(s1, p1))) continue; 0 0 0
128 0 ▶ for (h2 = 0; h2 < 2; h2++) { if (h2 && (h0 || h1)) continue;
Branch: 4 branches
BranchTrueFalseTotal
for (h2 = 0; h2 < 2; h2++) { if (h2 && (h0 || h1)) continue; 0 0 0
for (h2 = 0; h2 < 2; h2++) { if (h2 && (h0 || h1)) continue; 0 0 0
for (h2 = 0; h2 < 2; h2++) { if (h2 && (h0 || h1)) continue; 0 0 0
for (h2 = 0; h2 < 2; h2++) { if (h2 && (h0 || h1)) continue; 0 0 0
129 0 ▶ for (s2 = 0; s2 <= 4; s2++) for (p2 = 0; p2 <= 4; p2++) {
Branch: 2 branches
BranchTrueFalseTotal
for (s2 = 0; s2 <= 4; s2++) for (p2 = 0; p2 <= 4; p2++) { 0 0 0
for (s2 = 0; s2 <= 4; s2++) for (p2 = 0; p2 <= 4; p2++) { 0 0 0
130 0 ▶ if (!(m[2][h2] & BIT(s2, p2))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(m[2][h2] & BIT(s2, p2))) continue; 0 0 0
131 0 ▶ for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue;
Branch: 5 branches
BranchTrueFalseTotal
for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue; 0 0 0
for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue; 0 0 0
for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue; 0 0 0
for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue; 0 0 0
for (h3 = 0; h3 < 2; h3++) { if (h3 && (h0 || h1 || h2)) continue; 0 0 0
132 0 ▶ for (s3 = 0; s3 <= 4; s3++) for (p3 = 0; p3 <= 4; p3++) {
Branch: 2 branches
BranchTrueFalseTotal
for (s3 = 0; s3 <= 4; s3++) for (p3 = 0; p3 <= 4; p3++) { 0 0 0
for (s3 = 0; s3 <= 4; s3++) for (p3 = 0; p3 <= 4; p3++) { 0 0 0
133 int S, P, head, d;
134 0 ▶ if (!(m[3][h3] & BIT(s3, p3))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(m[3][h3] & BIT(s3, p3))) continue; 0 0 0
135 0 S = melds + s0 + s1 + s2 + s3;
136 0 P = p0 + p1 + p2 + p3;
137 0 ▶ head = h0 || h1 || h2 || h3;
Branch: 4 branches
BranchTrueFalseTotal
head = h0 || h1 || h2 || h3; 0 0 0
head = h0 || h1 || h2 || h3; 0 0 0
head = h0 || h1 || h2 || h3; 0 0 0
head = h0 || h1 || h2 || h3; 0 0 0
138 0 ▶ if (S > 4) S = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (S > 4) S = 4; 0 0 0
139 0 ▶ if (S + P > 4) P = 4 - S;
Branch: 1 branch
BranchTrueFalseTotal
if (S + P > 4) P = 4 - S; 0 0 0
140 0 d = 8 - 2 * S - P - head;
141 0 ▶ if (d < best) best = d;
Branch: 1 branch
BranchTrueFalseTotal
if (d < best) best = d; 0 0 0
142 } }
143 } }
144 } }
145 }
146 0 return best;
147 }
148  
149 0 static int seven_pairs(const unsigned char *counts, int melds)
150 {
151 0 int k, pairs = 0, kinds = 0;
152 0 ▶ if (melds) return 99;
Branch: 1 branch
BranchTrueFalseTotal
if (melds) return 99; 0 0 0
153 0 ▶ for (k = 1; k <= MJ_KINDS; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k <= MJ_KINDS; k++) { 0 0 0
154 0 ▶ if (counts[k]) kinds++;
Branch: 1 branch
BranchTrueFalseTotal
if (counts[k]) kinds++; 0 0 0
155 0 ▶ if (counts[k] >= 2) pairs++;
Branch: 1 branch
BranchTrueFalseTotal
if (counts[k] >= 2) pairs++; 0 0 0
156 }
157 0 ▶ return 6 - pairs + (kinds < 7 ? 7 - kinds : 0);
Branch: 1 branch
BranchTrueFalseTotal
return 6 - pairs + (kinds < 7 ? 7 - kinds : 0); 0 0 0
158 }
159  
160 static const int ORPHAN[13] = { 1, 9, 10, 18, 19, 27, 28, 29, 30, 31, 32, 33, 34 };
161  
162 0 static int thirteen_orphans(const unsigned char *counts, int melds)
163 {
164 0 int i, have = 0, pair = 0;
165 0 ▶ if (melds) return 99;
Branch: 1 branch
BranchTrueFalseTotal
if (melds) return 99; 0 0 0
166 0 ▶ for (i = 0; i < 13; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 13; i++) { 0 0 0
167 0 ▶ if (counts[ORPHAN[i]]) have++;
Branch: 1 branch
BranchTrueFalseTotal
if (counts[ORPHAN[i]]) have++; 0 0 0
168 0 ▶ if (counts[ORPHAN[i]] >= 2) pair = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (counts[ORPHAN[i]] >= 2) pair = 1; 0 0 0
169 }
170 0 return 13 - have - pair;
171 }
172  
173 static const int KNIT_BASE[3] = { 1, 2, 3 };
174 static const int PERM[6][3] = {
175 { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 }
176 };
177  
178 0 static int honours_knitted(const unsigned char *counts, int melds)
179 {
180 0 int p, k, honours = 0, best = 0;
181 0 ▶ if (melds) return 99;
Branch: 1 branch
BranchTrueFalseTotal
if (melds) return 99; 0 0 0
182 0 ▶ for (k = 28; k <= 34; k++) if (counts[k]) honours++;
Branch: 2 branches
BranchTrueFalseTotal
for (k = 28; k <= 34; k++) if (counts[k]) honours++; 0 0 0
for (k = 28; k <= 34; k++) if (counts[k]) honours++; 0 0 0
183 0 ▶ for (p = 0; p < 6; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 0; p < 6; p++) { 0 0 0
184 0 int s, usable = honours;
185 0 ▶ for (s = 0; s < 3; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < 3; s++) { 0 0 0
186 0 int base = KNIT_BASE[PERM[p][s]], r;
187 0 ▶ for (r = base; r <= 9; r += 3) if (counts[1 + 9 * s + r - 1]) usable++;
Branch: 2 branches
BranchTrueFalseTotal
for (r = base; r <= 9; r += 3) if (counts[1 + 9 * s + r - 1]) usable++; 0 0 0
for (r = base; r <= 9; r += 3) if (counts[1 + 9 * s + r - 1]) usable++; 0 0 0
188 }
189 0 ▶ if (usable > best) best = usable;
Branch: 1 branch
BranchTrueFalseTotal
if (usable > best) best = usable; 0 0 0
190 }
191 0 return 13 - best;
192 }
193  
194 0 int mj_shanten_forms(const unsigned char *counts, int melds, int *out)
195 {
196 int min;
197 0 out[MJ_FORM_S_STANDARD] = standard(counts, melds);
198 0 out[MJ_FORM_S_PAIRS] = seven_pairs(counts, melds);
199 0 out[MJ_FORM_S_ORPHANS] = thirteen_orphans(counts, melds);
200 0 out[MJ_FORM_S_KNITTED] = honours_knitted(counts, melds);
201 0 min = out[0];
202 0 ▶ if (out[1] < min) min = out[1];
Branch: 1 branch
BranchTrueFalseTotal
if (out[1] < min) min = out[1]; 0 0 0
203 0 ▶ if (out[2] < min) min = out[2];
Branch: 1 branch
BranchTrueFalseTotal
if (out[2] < min) min = out[2]; 0 0 0
204 0 ▶ if (out[3] < min) min = out[3];
Branch: 1 branch
BranchTrueFalseTotal
if (out[3] < min) min = out[3]; 0 0 0
205 0 return min;
206 }
207  
208 0 int mj_shanten(const unsigned char *counts, int melds)
209 {
210 int forms[MJ_FORM_S_COUNT];
211 0 return mj_shanten_forms(counts, melds, forms);
212 }
213  
214 0 int mj_ukeire(const unsigned char *counts, int melds, unsigned char *out)
215 {
216 unsigned char c[MJ_KINDS + 1];
217 0 int k, n = 0, now;
218 0 memcpy(c, counts, MJ_KINDS + 1);
219 0 now = mj_shanten(c, melds);
220 0 ▶ for (k = 1; k <= MJ_KINDS; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k <= MJ_KINDS; k++) { 0 0 0
221 0 ▶ if (c[k] >= MJ_PER_KIND) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c[k] >= MJ_PER_KIND) continue; 0 0 0
222 0 c[k]++;
223 0 ▶ if (mj_shanten(c, melds) < now) out[n++] = (unsigned char)k;
Branch: 1 branch
BranchTrueFalseTotal
if (mj_shanten(c, melds) < now) out[n++] = (unsigned char)k; 0 0 0
224 0 c[k]--;
225 }
226 0 return n;
227 }
« mahjong_decompose.c ↑ Summary mahjong_tiles.c »