| 1 |
|
| /* mahjong_decompose.c - every split of a hand, and the waits. |
| 2 |
|
| * |
| 3 |
|
| * Counts over the thirty-four kinds, the melds a number. The standard form |
| 4 |
|
| * is a backtracking walk from the lowest kind held: at that kind the split |
| 5 |
|
| * either takes a pung of it or a chow starting from it, and nothing else can |
| 6 |
|
| * consume it, so every multiset of sets is found exactly once and none twice |
| 7 |
|
| * (the pung count of the lowest kind is 0 or 1, and the chows from it are |
| 8 |
|
| * whatever is left, so the choice is forced by the multiset). The special |
| 9 |
|
| * forms are checked by their shape. |
| 10 |
|
| * |
| 11 |
|
| * The table it reads is mahjong_abi.h's, and nothing here knows a tile's |
| 12 |
|
| * suit or rank except through it. |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #include <string.h> |
| 16 |
|
| #include "mahjong_abi.h" |
| 17 |
|
| #include "mahjong_decompose.h" |
| 18 |
|
| |
| 19 |
|
| static const struct mj_abi *MJ = 0; |
| 20 |
|
| |
| 21 |
|
| #define SUIT(k) (MJ->kind[k].suit) |
| 22 |
|
| #define RANK(k) (MJ->kind[k].rank) |
| 23 |
|
| #define IS_SUIT(k) (MJ->kind[k].flags & MJ_F_SUIT) |
| 24 |
|
| |
| 25 |
|
| /* the three knitted patterns, as rank offsets 1-4-7, 2-5-8, 3-6-9 */ |
| 26 |
|
| static const int KNIT_BASE[3] = { 1, 2, 3 }; |
| 27 |
|
| /* the six assignments of patterns to the suits characters, dots, bamboo */ |
| 28 |
|
| static const int PERM[6][3] = { |
| 29 |
|
| { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } |
| 30 |
|
| }; |
| 31 |
|
| /* the kind of rank r in suit s (1 char, 2 dot, 3 bam) */ |
| 32 |
|
| #define KIND_OF(s, r) (((s) - 1) * 9 + (r)) |
| 33 |
|
| |
| 34 |
|
| /* the thirteen orphans: the six terminals and the seven honours */ |
| 35 |
|
| static const int ORPHAN[13] = { 1, 9, 10, 18, 19, 27, 28, 29, 30, 31, 32, 33, 34 }; |
| 36 |
|
| |
| 37 |
|
| struct walk { |
| 38 |
|
| unsigned char c[MJ_KINDS + 1]; |
| 39 |
|
| int melds; |
| 40 |
|
| int winning; |
| 41 |
|
| struct mj_split *out; |
| 42 |
|
| int max; |
| 43 |
|
| int n; /* splits written */ |
| 44 |
|
| int overflow; |
| 45 |
|
| struct mj_set sets[MJ_MAX_SETS]; |
| 46 |
|
| int nsets; |
| 47 |
|
| int pair; |
| 48 |
|
| }; |
| 49 |
|
| |
| 50 |
5866625 |
| static int total(const unsigned char *c) |
| 51 |
|
| { |
| 52 |
5866625 |
| int i, t = 0; |
| 53 |
205331875 |
▶ | for (i = 1; i <= MJ_KINDS; i++) t += c[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) t += c[i]; |
199465250 |
5866625 |
205331875 |
|
| 54 |
5866625 |
| return t; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* ---- emitting a split, with its placements ---------------------------------- */ |
| 58 |
|
| |
| 59 |
11643 |
| static void emit(struct walk *w, struct mj_split *base) |
| 60 |
|
| { |
| 61 |
11643 |
| int i, placed = 0; |
| 62 |
|
| |
| 63 |
11643 |
▶ | if (!w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->winning) { |
10026 |
1617 |
11643 |
|
| 64 |
10026 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
288 |
9738 |
10026 |
|
| 65 |
9738 |
| w->out[w->n++] = *base; |
| 66 |
9738 |
| return; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* one copy per place the winning tile can sit */ |
| 70 |
6436 |
▶ | for (i = 0; i < base->nsets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < base->nsets; i++) { |
4819 |
1617 |
6436 |
|
| 71 |
4819 |
| const struct mj_set *s = &base->sets[i]; |
| 72 |
4819 |
| int j, in = 0, wait = MJ_WAIT_NONE; |
| 73 |
19276 |
▶ | for (j = 0; j < 3; j++) if (s->tiles[j] == w->winning) in = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < 3; j++) if (s->tiles[j] == w->winning) in = 1; |
2136 |
12321 |
14457 |
| for (j = 0; j < 3; j++) if (s->tiles[j] == w->winning) in = 1; |
14457 |
4819 |
19276 |
|
| 74 |
4819 |
▶ | if (!in) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) continue; |
3163 |
1656 |
4819 |
|
| 75 |
1656 |
▶ | if (s->kind == MJ_SET_PUNG) wait = MJ_WAIT_PUNG; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->kind == MJ_SET_PUNG) wait = MJ_WAIT_PUNG; |
240 |
1416 |
1656 |
|
| 76 |
1416 |
▶ | else if (s->kind == MJ_SET_KNIT) wait = MJ_WAIT_KNIT; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (s->kind == MJ_SET_KNIT) wait = MJ_WAIT_KNIT; |
1 |
1415 |
1416 |
|
| 77 |
|
| else { |
| 78 |
1415 |
| int r = RANK(s->tiles[0]); |
| 79 |
1415 |
▶ | if (w->winning == s->tiles[1]) wait = MJ_WAIT_CLOSED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning == s->tiles[1]) wait = MJ_WAIT_CLOSED; |
276 |
1139 |
1415 |
|
| 80 |
1139 |
▶ | else if (r == 1 && w->winning == s->tiles[2]) wait = MJ_WAIT_EDGE; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (r == 1 && w->winning == s->tiles[2]) wait = MJ_WAIT_EDGE; |
150 |
989 |
1139 |
| else if (r == 1 && w->winning == s->tiles[2]) wait = MJ_WAIT_EDGE; |
63 |
87 |
150 |
|
| 81 |
1076 |
▶ | else if (r == 7 && w->winning == s->tiles[0]) wait = MJ_WAIT_EDGE; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (r == 7 && w->winning == s->tiles[0]) wait = MJ_WAIT_EDGE; |
168 |
908 |
1076 |
| else if (r == 7 && w->winning == s->tiles[0]) wait = MJ_WAIT_EDGE; |
70 |
98 |
168 |
|
| 82 |
1006 |
| else wait = MJ_WAIT_TWO_SIDED; |
| 83 |
|
| } |
| 84 |
1656 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
1656 |
1656 |
|
| 85 |
1656 |
| w->out[w->n] = *base; |
| 86 |
1656 |
| w->out[w->n].place_in = MJ_IN_SET; |
| 87 |
1656 |
| w->out[w->n].place_index = (unsigned char)i; |
| 88 |
1656 |
| w->out[w->n].wait = (unsigned char)wait; |
| 89 |
1656 |
| w->n++; |
| 90 |
1656 |
| placed = 1; |
| 91 |
|
| } |
| 92 |
1617 |
▶ | if (base->pair && base->pair == w->winning) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (base->pair && base->pair == w->winning) { |
1617 |
0 |
1617 |
| if (base->pair && base->pair == w->winning) { |
181 |
1436 |
1617 |
|
| 93 |
181 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
181 |
181 |
|
| 94 |
181 |
| w->out[w->n] = *base; |
| 95 |
181 |
| w->out[w->n].place_in = MJ_IN_PAIR; |
| 96 |
181 |
| w->out[w->n].place_index = 0; |
| 97 |
181 |
| w->out[w->n].wait = MJ_WAIT_PAIR; |
| 98 |
181 |
| w->n++; |
| 99 |
181 |
| placed = 1; |
| 100 |
|
| } |
| 101 |
1617 |
▶ | for (i = 0; i < base->nsingles; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < base->nsingles; i++) { |
0 |
1617 |
1617 |
|
| 102 |
0 |
▶ | if (base->singles[i] != w->winning) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (base->singles[i] != w->winning) continue; |
0 |
0 |
0 |
|
| 103 |
0 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
0 |
0 |
|
| 104 |
0 |
| w->out[w->n] = *base; |
| 105 |
0 |
| w->out[w->n].place_in = MJ_IN_SINGLE; |
| 106 |
0 |
| w->out[w->n].place_index = (unsigned char)i; |
| 107 |
0 |
| w->out[w->n].wait = MJ_WAIT_SINGLE; |
| 108 |
0 |
| w->n++; |
| 109 |
0 |
| placed = 1; |
| 110 |
0 |
| break; /* a kind appears once among the singles */ |
| 111 |
|
| } |
| 112 |
|
| /* the winning tile is in a meld, or not in the hand at all: the caller |
| 113 |
|
| * asked for a placement that does not exist; return the split unplaced |
| 114 |
|
| * rather than nothing, so the hand still counts as complete */ |
| 115 |
1617 |
▶ | if (!placed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!placed) { |
1 |
1616 |
1617 |
|
| 116 |
1 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
1 |
1 |
|
| 117 |
1 |
| w->out[w->n++] = *base; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
11643 |
| static void emit_standard(struct walk *w, int form) |
| 122 |
|
| { |
| 123 |
|
| struct mj_split s; |
| 124 |
11643 |
| memset(&s, 0, sizeof s); |
| 125 |
11643 |
| s.form = (unsigned char)form; |
| 126 |
11643 |
| s.nsets = (unsigned char)w->nsets; |
| 127 |
11643 |
| memcpy(s.sets, w->sets, sizeof(struct mj_set) * (size_t)w->nsets); |
| 128 |
11643 |
| s.pair = (unsigned char)w->pair; |
| 129 |
11643 |
| emit(w, &s); |
| 130 |
11643 |
| } |
| 131 |
|
| |
| 132 |
|
| /* ---- the standard form ------------------------------------------------------ */ |
| 133 |
|
| |
| 134 |
4902155 |
| static void walk_sets(struct walk *w, int from, int form) |
| 135 |
|
| { |
| 136 |
|
| int i; |
| 137 |
21114365 |
▶ | for (i = from; i <= MJ_KINDS && !w->c[i]; i++) ; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = from; i <= MJ_KINDS && !w->c[i]; i++) ; |
21102722 |
11643 |
21114365 |
| for (i = from; i <= MJ_KINDS && !w->c[i]; i++) ; |
16212210 |
4890512 |
21102722 |
|
| 138 |
4902155 |
▶ | if (i > MJ_KINDS) { emit_standard(w, form); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > MJ_KINDS) { emit_standard(w, form); return; } |
11643 |
4890512 |
4902155 |
|
| 139 |
4890512 |
▶ | if (w->nsets >= MJ_MAX_SETS) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->nsets >= MJ_MAX_SETS) return; |
0 |
4890512 |
4890512 |
|
| 140 |
|
| |
| 141 |
4890512 |
▶ | if (w->c[i] >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[i] >= 3) { |
188894 |
4701618 |
4890512 |
|
| 142 |
188894 |
| struct mj_set *s = &w->sets[w->nsets++]; |
| 143 |
188894 |
| s->kind = MJ_SET_PUNG; |
| 144 |
188894 |
| s->tiles[0] = s->tiles[1] = s->tiles[2] = (unsigned char)i; |
| 145 |
188894 |
| w->c[i] -= 3; |
| 146 |
188894 |
| walk_sets(w, i, form); |
| 147 |
188894 |
| w->c[i] += 3; |
| 148 |
188894 |
| w->nsets--; |
| 149 |
|
| } |
| 150 |
4890512 |
▶ | if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
4887997 |
2515 |
4890512 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
4574290 |
313707 |
4887997 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
2267840 |
2306450 |
4574290 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
1199585 |
1068255 |
2267840 |
|
| 151 |
1199585 |
| struct mj_set *s = &w->sets[w->nsets++]; |
| 152 |
1199585 |
| s->kind = MJ_SET_CHOW; |
| 153 |
1199585 |
| s->tiles[0] = (unsigned char)i; |
| 154 |
1199585 |
| s->tiles[1] = (unsigned char)(i + 1); |
| 155 |
1199585 |
| s->tiles[2] = (unsigned char)(i + 2); |
| 156 |
1199585 |
| w->c[i]--; w->c[i + 1]--; w->c[i + 2]--; |
| 157 |
1199585 |
| walk_sets(w, i, form); |
| 158 |
1199585 |
| w->c[i]++; w->c[i + 1]++; w->c[i + 2]++; |
| 159 |
1199585 |
| w->nsets--; |
| 160 |
|
| } |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
1337720 |
| static void standard(struct walk *w) |
| 164 |
|
| { |
| 165 |
1337720 |
| int p, need = 14 - 3 * w->melds; |
| 166 |
1337720 |
▶ | if (total(w->c) != need) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != need) return; |
11 |
1337709 |
1337720 |
|
| 167 |
46819815 |
▶ | for (p = 1; p <= MJ_KINDS; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 1; p <= MJ_KINDS; p++) { |
45482106 |
1337709 |
46819815 |
|
| 168 |
45482106 |
▶ | if (w->c[p] < 2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[p] < 2) continue; |
41968509 |
3513597 |
45482106 |
|
| 169 |
3513597 |
| w->c[p] -= 2; |
| 170 |
3513597 |
| w->pair = p; |
| 171 |
3513597 |
| w->nsets = 0; |
| 172 |
3513597 |
| walk_sets(w, 1, MJ_FORM_STANDARD); |
| 173 |
3513597 |
| w->c[p] += 2; |
| 174 |
|
| } |
| 175 |
1337709 |
| w->pair = 0; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| /* ---- seven pairs ------------------------------------------------------------ */ |
| 179 |
|
| |
| 180 |
1337720 |
| static void seven_pairs(struct walk *w) |
| 181 |
|
| { |
| 182 |
1337720 |
| int i, pairs = 0; |
| 183 |
|
| struct mj_split s; |
| 184 |
2435070 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
240089 |
1097631 |
1337720 |
|
| 185 |
1097631 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
8 |
1097623 |
1097631 |
|
| 186 |
5037707 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
5037373 |
334 |
5037707 |
|
| 187 |
5037373 |
▶ | if (w->c[i] & 1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[i] & 1) return; |
1097289 |
3940084 |
5037373 |
|
| 188 |
3940084 |
| pairs += w->c[i] / 2; |
| 189 |
|
| } |
| 190 |
334 |
▶ | if (pairs != 7) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pairs != 7) return; |
0 |
334 |
334 |
|
| 191 |
334 |
| memset(&s, 0, sizeof s); |
| 192 |
334 |
| s.form = MJ_FORM_SEVEN_PAIRS; |
| 193 |
|
| /* the pairs ride in singles, one entry per pair, a four-of-a-kind twice */ |
| 194 |
11690 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
11356 |
334 |
11690 |
|
| 195 |
|
| int k; |
| 196 |
13694 |
▶ | for (k = 0; k < w->c[i] / 2; k++) s.singles[s.nsingles++] = (unsigned char)i; |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < w->c[i] / 2; k++) s.singles[s.nsingles++] = (unsigned char)i; |
2338 |
11356 |
13694 |
|
| 197 |
|
| } |
| 198 |
|
| /* the winning tile completed one pair: a pair wait */ |
| 199 |
334 |
▶ | if (w->winning && w->c[w->winning]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->winning && w->c[w->winning]) { |
20 |
314 |
334 |
| if (w->winning && w->c[w->winning]) { |
20 |
0 |
20 |
|
| 200 |
20 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
20 |
20 |
|
| 201 |
20 |
| s.place_in = MJ_IN_PAIR; |
| 202 |
20 |
| s.place_index = 0; |
| 203 |
20 |
| s.wait = MJ_WAIT_PAIR; |
| 204 |
20 |
| s.pair = (unsigned char)w->winning; |
| 205 |
20 |
| w->out[w->n++] = s; |
| 206 |
20 |
| return; |
| 207 |
|
| } |
| 208 |
314 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
33 |
281 |
314 |
|
| 209 |
281 |
| w->out[w->n++] = s; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* ---- thirteen orphans ------------------------------------------------------- */ |
| 213 |
|
| |
| 214 |
1337720 |
| static void thirteen_orphans(struct walk *w) |
| 215 |
|
| { |
| 216 |
1337720 |
| int i, pair = 0; |
| 217 |
|
| struct mj_split s; |
| 218 |
2435072 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
240089 |
1097631 |
1337720 |
|
| 219 |
1097631 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
8 |
1097623 |
1097631 |
|
| 220 |
1716074 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
1715789 |
285 |
1716074 |
|
| 221 |
1715789 |
| int orphan = 0, j; |
| 222 |
24021046 |
▶ | for (j = 0; j < 13; j++) if (ORPHAN[j] == i) orphan = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| for (j = 0; j < 13; j++) if (ORPHAN[j] == i) orphan = 1; |
1111611 |
21193646 |
22305257 |
| for (j = 0; j < 13; j++) if (ORPHAN[j] == i) orphan = 1; |
22305257 |
1715789 |
24021046 |
|
| 223 |
1715789 |
▶ | if (!orphan) { if (w->c[i]) return; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!orphan) { if (w->c[i]) return; continue; } |
604178 |
1111611 |
1715789 |
| if (!orphan) { if (w->c[i]) return; continue; } |
282806 |
321372 |
604178 |
|
| 224 |
1111611 |
▶ | if (w->c[i] == 0 || w->c[i] > 2) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->c[i] == 0 || w->c[i] > 2) return; |
309565 |
802046 |
1111611 |
| if (w->c[i] == 0 || w->c[i] > 2) return; |
12450 |
297115 |
309565 |
|
| 225 |
297115 |
▶ | if (w->c[i] == 2) { if (pair) return; pair = i; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->c[i] == 2) { if (pair) return; pair = i; } |
69892 |
227223 |
297115 |
| if (w->c[i] == 2) { if (pair) return; pair = i; } |
36 |
69856 |
69892 |
|
| 226 |
|
| } |
| 227 |
285 |
▶ | if (!pair) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pair) return; |
0 |
285 |
285 |
|
| 228 |
285 |
| memset(&s, 0, sizeof s); |
| 229 |
285 |
| s.form = MJ_FORM_THIRTEEN_ORPHANS; |
| 230 |
285 |
| s.pair = (unsigned char)pair; |
| 231 |
3990 |
▶ | for (i = 0; i < 13; i++) s.singles[s.nsingles++] = (unsigned char)ORPHAN[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 13; i++) s.singles[s.nsingles++] = (unsigned char)ORPHAN[i]; |
3705 |
285 |
3990 |
|
| 232 |
285 |
▶ | if (w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning) { |
6 |
279 |
285 |
|
| 233 |
6 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
6 |
6 |
|
| 234 |
6 |
▶ | if (w->winning == pair) { s.place_in = MJ_IN_PAIR; s.wait = MJ_WAIT_PAIR; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning == pair) { s.place_in = MJ_IN_PAIR; s.wait = MJ_WAIT_PAIR; } |
3 |
3 |
6 |
|
| 235 |
|
| else { |
| 236 |
42 |
▶ | for (i = 0; i < 13; i++) if (ORPHAN[i] == w->winning) s.place_index = (unsigned char)i; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < 13; i++) if (ORPHAN[i] == w->winning) s.place_index = (unsigned char)i; |
3 |
36 |
39 |
| for (i = 0; i < 13; i++) if (ORPHAN[i] == w->winning) s.place_index = (unsigned char)i; |
39 |
3 |
42 |
|
| 237 |
3 |
| s.place_in = MJ_IN_SINGLE; |
| 238 |
3 |
| s.wait = MJ_WAIT_SINGLE; |
| 239 |
|
| } |
| 240 |
6 |
| w->out[w->n++] = s; |
| 241 |
6 |
| return; |
| 242 |
|
| } |
| 243 |
279 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
279 |
279 |
|
| 244 |
279 |
| w->out[w->n++] = s; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| /* ---- honours and knitted tiles (fans 20 and 34) ----------------------------- */ |
| 248 |
|
| |
| 249 |
|
| /* every tile a single; the suit tiles of each suit a subset of the pattern |
| 250 |
|
| * the permutation assigns it; at least the honours to make fourteen */ |
| 251 |
1337720 |
| static void honours_knitted(struct walk *w) |
| 252 |
|
| { |
| 253 |
|
| int i, p; |
| 254 |
1337720 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
240089 |
1097631 |
1337720 |
|
| 255 |
1097631 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
8 |
1097623 |
1097631 |
|
| 256 |
10931582 |
▶ | for (i = 1; i <= MJ_KINDS; i++) if (w->c[i] > 1) return; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i] > 1) return; |
1066577 |
9833959 |
10900536 |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i] > 1) return; |
10900536 |
31046 |
10931582 |
|
| 257 |
216490 |
▶ | for (p = 0; p < 6; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p < 6; p++) { |
185682 |
30808 |
216490 |
|
| 258 |
185682 |
| int ok = 1, s; |
| 259 |
387021 |
▶ | for (s = 1; s <= 3 && ok; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 1; s <= 3 && ok; s++) { |
385344 |
1677 |
387021 |
| for (s = 1; s <= 3 && ok; s++) { |
201339 |
184005 |
385344 |
|
| 260 |
201339 |
| int base = KNIT_BASE[PERM[p][s - 1]], r; |
| 261 |
733215 |
▶ | for (r = 1; r <= 9; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 1; r <= 9; r++) { |
717320 |
15895 |
733215 |
|
| 262 |
717320 |
▶ | if (w->c[KIND_OF(s, r)] && (r - base) % 3 != 0) { ok = 0; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->c[KIND_OF(s, r)] && (r - base) % 3 != 0) { ok = 0; break; } |
275428 |
441892 |
717320 |
| if (w->c[KIND_OF(s, r)] && (r - base) % 3 != 0) { ok = 0; break; } |
185444 |
89984 |
275428 |
|
| 263 |
|
| } |
| 264 |
|
| } |
| 265 |
185682 |
▶ | if (!ok) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) continue; |
185444 |
238 |
185682 |
|
| 266 |
|
| { |
| 267 |
|
| struct mj_split sp; |
| 268 |
238 |
| memset(&sp, 0, sizeof sp); |
| 269 |
238 |
| sp.form = MJ_FORM_HONOURS_KNITTED; |
| 270 |
8330 |
▶ | for (i = 1; i <= MJ_KINDS; i++) if (w->c[i]) sp.singles[sp.nsingles++] = (unsigned char)i; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i]) sp.singles[sp.nsingles++] = (unsigned char)i; |
3332 |
4760 |
8092 |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i]) sp.singles[sp.nsingles++] = (unsigned char)i; |
8092 |
238 |
8330 |
|
| 271 |
238 |
▶ | if (w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning) { |
25 |
213 |
238 |
|
| 272 |
375 |
▶ | for (i = 0; i < sp.nsingles; i++) if (sp.singles[i] == w->winning) sp.place_index = (unsigned char)i; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < sp.nsingles; i++) if (sp.singles[i] == w->winning) sp.place_index = (unsigned char)i; |
25 |
325 |
350 |
| for (i = 0; i < sp.nsingles; i++) if (sp.singles[i] == w->winning) sp.place_index = (unsigned char)i; |
350 |
25 |
375 |
|
| 273 |
25 |
| sp.place_in = MJ_IN_SINGLE; |
| 274 |
25 |
| sp.wait = MJ_WAIT_SINGLE; |
| 275 |
|
| } |
| 276 |
238 |
▶ | if (w->n >= w->max) { w->overflow = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->n >= w->max) { w->overflow = 1; return; } |
0 |
238 |
238 |
|
| 277 |
238 |
| w->out[w->n++] = sp; |
| 278 |
|
| } |
| 279 |
|
| /* one split whatever the assignment: the singles are the same tiles */ |
| 280 |
238 |
| return; |
| 281 |
|
| } |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
|
| /* ---- the knitted straight (fan 35) ------------------------------------------ */ |
| 285 |
|
| |
| 286 |
|
| /* nine tiles making 1-4-7, 2-5-8, 3-6-9 one pattern per suit; the rest a |
| 287 |
|
| * standard hand of (1 - melds) sets and a pair */ |
| 288 |
1337720 |
| static void knitted_straight(struct walk *w) |
| 289 |
|
| { |
| 290 |
1337720 |
| int p, need = 14 - 3 * w->melds; |
| 291 |
1337720 |
▶ | if (w->melds > 1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds > 1) return; |
101708 |
1236012 |
1337720 |
|
| 292 |
1236012 |
▶ | if (total(w->c) != need) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != need) return; |
11 |
1236001 |
1236012 |
|
| 293 |
8652007 |
▶ | for (p = 0; p < 6; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p < 6; p++) { |
7416006 |
1236001 |
8652007 |
|
| 294 |
7416006 |
| int ok = 1, s, k; |
| 295 |
|
| struct mj_set knit[3]; |
| 296 |
15096466 |
▶ | for (s = 1; s <= 3 && ok; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 1; s <= 3 && ok; s++) { |
15093016 |
3450 |
15096466 |
| for (s = 1; s <= 3 && ok; s++) { |
7680460 |
7412556 |
15093016 |
|
| 297 |
7680460 |
| int base = KNIT_BASE[PERM[p][s - 1]]; |
| 298 |
11290605 |
▶ | for (k = 0; k < 3; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < 3; k++) { |
11025815 |
264790 |
11290605 |
|
| 299 |
11025815 |
| int kind = KIND_OF(s, base + 3 * k); |
| 300 |
11025815 |
▶ | if (!w->c[kind]) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->c[kind]) { ok = 0; break; } |
7415670 |
3610145 |
11025815 |
|
| 301 |
3610145 |
| knit[s - 1].tiles[k] = (unsigned char)kind; |
| 302 |
|
| } |
| 303 |
7680460 |
| knit[s - 1].kind = MJ_SET_KNIT; |
| 304 |
|
| } |
| 305 |
7416006 |
▶ | if (!ok) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) continue; |
7415670 |
336 |
7416006 |
|
| 306 |
|
| /* take the nine out, split the rest */ |
| 307 |
4368 |
▶ | for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]--; |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]--; |
3024 |
1008 |
4032 |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]--; |
1008 |
336 |
1344 |
|
| 308 |
|
| { |
| 309 |
|
| int q; |
| 310 |
11760 |
▶ | for (q = 1; q <= MJ_KINDS; q++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (q = 1; q <= MJ_KINDS; q++) { |
11424 |
336 |
11760 |
|
| 311 |
11424 |
▶ | if (w->c[q] < 2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[q] < 2) continue; |
11345 |
79 |
11424 |
|
| 312 |
79 |
| w->c[q] -= 2; |
| 313 |
79 |
| w->pair = q; |
| 314 |
79 |
| w->nsets = 3; |
| 315 |
79 |
| memcpy(w->sets, knit, sizeof knit); |
| 316 |
79 |
| walk_sets(w, 1, MJ_FORM_KNITTED_STRAIGHT); |
| 317 |
79 |
| w->c[q] += 2; |
| 318 |
|
| } |
| 319 |
336 |
| w->pair = 0; |
| 320 |
336 |
| w->nsets = 0; |
| 321 |
|
| } |
| 322 |
4368 |
▶ | for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]++; |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]++; |
3024 |
1008 |
4032 |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]++; |
1008 |
336 |
1344 |
|
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| /* ---- the public three --------------------------------------------------------- */ |
| 327 |
|
| |
| 328 |
1337720 |
| int mj_decompose(const unsigned char *counts, int melds, int winning, |
| 329 |
|
| struct mj_split *out, int max) |
| 330 |
|
| { |
| 331 |
|
| struct walk w; |
| 332 |
1337720 |
▶ | if (!MJ) MJ = mj_abi_table(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!MJ) MJ = mj_abi_table(); |
20 |
1337700 |
1337720 |
|
| 333 |
1337720 |
| memset(&w, 0, sizeof w); |
| 334 |
1337720 |
| memcpy(w.c, counts, MJ_KINDS + 1); |
| 335 |
1337720 |
| w.c[0] = 0; |
| 336 |
1337720 |
| w.melds = melds; |
| 337 |
1337720 |
| w.winning = winning; |
| 338 |
1337720 |
| w.out = out; |
| 339 |
1337720 |
| w.max = max; |
| 340 |
|
| |
| 341 |
1337720 |
| standard(&w); |
| 342 |
1337720 |
| seven_pairs(&w); |
| 343 |
1337720 |
| thirteen_orphans(&w); |
| 344 |
1337720 |
| honours_knitted(&w); |
| 345 |
1337720 |
| knitted_straight(&w); |
| 346 |
|
| |
| 347 |
1337720 |
▶ | return w.overflow ? -1 : w.n; |
Branch: 1 branch
| Branch | True | False | Total |
| return w.overflow ? -1 : w.n; |
1337424 |
296 |
1337720 |
|
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
1312639 |
| int mj_is_complete(const unsigned char *counts, int melds) |
| 351 |
|
| { |
| 352 |
|
| struct mj_split one; |
| 353 |
1312639 |
| int n = mj_decompose(counts, melds, 0, &one, 1); |
| 354 |
1312639 |
| return n != 0 ? 1 : 0; |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
38620 |
| int mj_waits(const unsigned char *counts, int melds, unsigned char *out) |
| 358 |
|
| { |
| 359 |
|
| unsigned char c[MJ_KINDS + 1]; |
| 360 |
38620 |
| int k, n = 0; |
| 361 |
38620 |
▶ | if (!MJ) MJ = mj_abi_table(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!MJ) MJ = mj_abi_table(); |
5 |
38615 |
38620 |
|
| 362 |
38620 |
| memcpy(c, counts, MJ_KINDS + 1); |
| 363 |
1351700 |
▶ | for (k = 1; k <= MJ_KINDS; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 1; k <= MJ_KINDS; k++) { |
1313080 |
38620 |
1351700 |
|
| 364 |
1313080 |
▶ | if (c[k] >= MJ_PER_KIND) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c[k] >= MJ_PER_KIND) continue; |
459 |
1312621 |
1313080 |
|
| 365 |
1312621 |
| c[k]++; |
| 366 |
1312621 |
▶ | if (mj_is_complete(c, melds)) out[n++] = (unsigned char)k; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mj_is_complete(c, melds)) out[n++] = (unsigned char)k; |
10190 |
1302431 |
1312621 |
|
| 367 |
1312621 |
| c[k]--; |
| 368 |
|
| } |
| 369 |
38620 |
| return n; |
| 370 |
|
| } |