| 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 |
0 |
| static int total(const unsigned char *c) |
| 51 |
|
| { |
| 52 |
0 |
| int i, t = 0; |
| 53 |
0 |
▶ | 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]; |
0 |
0 |
0 |
|
| 54 |
0 |
| return t; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* ---- emitting a split, with its placements ---------------------------------- */ |
| 58 |
|
| |
| 59 |
0 |
| static void emit(struct walk *w, struct mj_split *base) |
| 60 |
|
| { |
| 61 |
0 |
| int i, placed = 0; |
| 62 |
|
| |
| 63 |
0 |
▶ | if (!w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->winning) { |
0 |
0 |
0 |
|
| 64 |
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 |
|
| 65 |
0 |
| w->out[w->n++] = *base; |
| 66 |
0 |
| return; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* one copy per place the winning tile can sit */ |
| 70 |
0 |
▶ | for (i = 0; i < base->nsets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < base->nsets; i++) { |
0 |
0 |
0 |
|
| 71 |
0 |
| const struct mj_set *s = &base->sets[i]; |
| 72 |
0 |
| int j, in = 0, wait = MJ_WAIT_NONE; |
| 73 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (j = 0; j < 3; j++) if (s->tiles[j] == w->winning) in = 1; |
0 |
0 |
0 |
|
| 74 |
0 |
▶ | if (!in) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) continue; |
0 |
0 |
0 |
|
| 75 |
0 |
▶ | 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; |
0 |
0 |
0 |
|
| 76 |
0 |
▶ | 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; |
0 |
0 |
0 |
|
| 77 |
|
| else { |
| 78 |
0 |
| int r = RANK(s->tiles[0]); |
| 79 |
0 |
▶ | 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; |
0 |
0 |
0 |
|
| 80 |
0 |
▶ | 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; |
0 |
0 |
0 |
| else if (r == 1 && w->winning == s->tiles[2]) wait = MJ_WAIT_EDGE; |
0 |
0 |
0 |
|
| 81 |
0 |
▶ | 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; |
0 |
0 |
0 |
| else if (r == 7 && w->winning == s->tiles[0]) wait = MJ_WAIT_EDGE; |
0 |
0 |
0 |
|
| 82 |
0 |
| else wait = MJ_WAIT_TWO_SIDED; |
| 83 |
|
| } |
| 84 |
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 |
|
| 85 |
0 |
| w->out[w->n] = *base; |
| 86 |
0 |
| w->out[w->n].place_in = MJ_IN_SET; |
| 87 |
0 |
| w->out[w->n].place_index = (unsigned char)i; |
| 88 |
0 |
| w->out[w->n].wait = (unsigned char)wait; |
| 89 |
0 |
| w->n++; |
| 90 |
0 |
| placed = 1; |
| 91 |
|
| } |
| 92 |
0 |
▶ | if (base->pair && base->pair == w->winning) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (base->pair && base->pair == w->winning) { |
0 |
0 |
0 |
| if (base->pair && base->pair == w->winning) { |
0 |
0 |
0 |
|
| 93 |
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 |
|
| 94 |
0 |
| w->out[w->n] = *base; |
| 95 |
0 |
| w->out[w->n].place_in = MJ_IN_PAIR; |
| 96 |
0 |
| w->out[w->n].place_index = 0; |
| 97 |
0 |
| w->out[w->n].wait = MJ_WAIT_PAIR; |
| 98 |
0 |
| w->n++; |
| 99 |
0 |
| placed = 1; |
| 100 |
|
| } |
| 101 |
0 |
▶ | for (i = 0; i < base->nsingles; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < base->nsingles; i++) { |
0 |
0 |
0 |
|
| 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 |
0 |
▶ | if (!placed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!placed) { |
0 |
0 |
0 |
|
| 116 |
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 |
|
| 117 |
0 |
| w->out[w->n++] = *base; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
0 |
| static void emit_standard(struct walk *w, int form) |
| 122 |
|
| { |
| 123 |
|
| struct mj_split s; |
| 124 |
0 |
| memset(&s, 0, sizeof s); |
| 125 |
0 |
| s.form = (unsigned char)form; |
| 126 |
0 |
| s.nsets = (unsigned char)w->nsets; |
| 127 |
0 |
| memcpy(s.sets, w->sets, sizeof(struct mj_set) * (size_t)w->nsets); |
| 128 |
0 |
| s.pair = (unsigned char)w->pair; |
| 129 |
0 |
| emit(w, &s); |
| 130 |
0 |
| } |
| 131 |
|
| |
| 132 |
|
| /* ---- the standard form ------------------------------------------------------ */ |
| 133 |
|
| |
| 134 |
0 |
| static void walk_sets(struct walk *w, int from, int form) |
| 135 |
|
| { |
| 136 |
|
| int i; |
| 137 |
0 |
▶ | 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++) ; |
0 |
0 |
0 |
| for (i = from; i <= MJ_KINDS && !w->c[i]; i++) ; |
0 |
0 |
0 |
|
| 138 |
0 |
▶ | 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; } |
0 |
0 |
0 |
|
| 139 |
0 |
▶ | if (w->nsets >= MJ_MAX_SETS) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->nsets >= MJ_MAX_SETS) return; |
0 |
0 |
0 |
|
| 140 |
|
| |
| 141 |
0 |
▶ | if (w->c[i] >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[i] >= 3) { |
0 |
0 |
0 |
|
| 142 |
0 |
| struct mj_set *s = &w->sets[w->nsets++]; |
| 143 |
0 |
| s->kind = MJ_SET_PUNG; |
| 144 |
0 |
| s->tiles[0] = s->tiles[1] = s->tiles[2] = (unsigned char)i; |
| 145 |
0 |
| w->c[i] -= 3; |
| 146 |
0 |
| walk_sets(w, i, form); |
| 147 |
0 |
| w->c[i] += 3; |
| 148 |
0 |
| w->nsets--; |
| 149 |
|
| } |
| 150 |
0 |
▶ | 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]) { |
0 |
0 |
0 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
0 |
0 |
0 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
0 |
0 |
0 |
| if (IS_SUIT(i) && RANK(i) <= 7 && w->c[i + 1] && w->c[i + 2]) { |
0 |
0 |
0 |
|
| 151 |
0 |
| struct mj_set *s = &w->sets[w->nsets++]; |
| 152 |
0 |
| s->kind = MJ_SET_CHOW; |
| 153 |
0 |
| s->tiles[0] = (unsigned char)i; |
| 154 |
0 |
| s->tiles[1] = (unsigned char)(i + 1); |
| 155 |
0 |
| s->tiles[2] = (unsigned char)(i + 2); |
| 156 |
0 |
| w->c[i]--; w->c[i + 1]--; w->c[i + 2]--; |
| 157 |
0 |
| walk_sets(w, i, form); |
| 158 |
0 |
| w->c[i]++; w->c[i + 1]++; w->c[i + 2]++; |
| 159 |
0 |
| w->nsets--; |
| 160 |
|
| } |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
0 |
| static void standard(struct walk *w) |
| 164 |
|
| { |
| 165 |
0 |
| int p, need = 14 - 3 * w->melds; |
| 166 |
0 |
▶ | if (total(w->c) != need) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != need) return; |
0 |
0 |
0 |
|
| 167 |
0 |
▶ | for (p = 1; p <= MJ_KINDS; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 1; p <= MJ_KINDS; p++) { |
0 |
0 |
0 |
|
| 168 |
0 |
▶ | if (w->c[p] < 2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[p] < 2) continue; |
0 |
0 |
0 |
|
| 169 |
0 |
| w->c[p] -= 2; |
| 170 |
0 |
| w->pair = p; |
| 171 |
0 |
| w->nsets = 0; |
| 172 |
0 |
| walk_sets(w, 1, MJ_FORM_STANDARD); |
| 173 |
0 |
| w->c[p] += 2; |
| 174 |
|
| } |
| 175 |
0 |
| w->pair = 0; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| /* ---- seven pairs ------------------------------------------------------------ */ |
| 179 |
|
| |
| 180 |
0 |
| static void seven_pairs(struct walk *w) |
| 181 |
|
| { |
| 182 |
0 |
| int i, pairs = 0; |
| 183 |
|
| struct mj_split s; |
| 184 |
0 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
0 |
0 |
0 |
|
| 185 |
0 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
0 |
0 |
0 |
|
| 186 |
0 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
0 |
0 |
0 |
|
| 187 |
0 |
▶ | if (w->c[i] & 1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[i] & 1) return; |
0 |
0 |
0 |
|
| 188 |
0 |
| pairs += w->c[i] / 2; |
| 189 |
|
| } |
| 190 |
0 |
▶ | if (pairs != 7) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pairs != 7) return; |
0 |
0 |
0 |
|
| 191 |
0 |
| memset(&s, 0, sizeof s); |
| 192 |
0 |
| s.form = MJ_FORM_SEVEN_PAIRS; |
| 193 |
|
| /* the pairs ride in singles, one entry per pair, a four-of-a-kind twice */ |
| 194 |
0 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
0 |
0 |
0 |
|
| 195 |
|
| int k; |
| 196 |
0 |
▶ | 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; |
0 |
0 |
0 |
|
| 197 |
|
| } |
| 198 |
|
| /* the winning tile completed one pair: a pair wait */ |
| 199 |
0 |
▶ | if (w->winning && w->c[w->winning]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->winning && w->c[w->winning]) { |
0 |
0 |
0 |
| if (w->winning && w->c[w->winning]) { |
0 |
0 |
0 |
|
| 200 |
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 |
|
| 201 |
0 |
| s.place_in = MJ_IN_PAIR; |
| 202 |
0 |
| s.place_index = 0; |
| 203 |
0 |
| s.wait = MJ_WAIT_PAIR; |
| 204 |
0 |
| s.pair = (unsigned char)w->winning; |
| 205 |
0 |
| w->out[w->n++] = s; |
| 206 |
0 |
| return; |
| 207 |
|
| } |
| 208 |
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 |
|
| 209 |
0 |
| w->out[w->n++] = s; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| /* ---- thirteen orphans ------------------------------------------------------- */ |
| 213 |
|
| |
| 214 |
0 |
| static void thirteen_orphans(struct walk *w) |
| 215 |
|
| { |
| 216 |
0 |
| int i, pair = 0; |
| 217 |
|
| struct mj_split s; |
| 218 |
0 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
0 |
0 |
0 |
|
| 219 |
0 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
0 |
0 |
0 |
|
| 220 |
0 |
▶ | for (i = 1; i <= MJ_KINDS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_KINDS; i++) { |
0 |
0 |
0 |
|
| 221 |
0 |
| int orphan = 0, j; |
| 222 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (j = 0; j < 13; j++) if (ORPHAN[j] == i) orphan = 1; |
0 |
0 |
0 |
|
| 223 |
0 |
▶ | if (!orphan) { if (w->c[i]) return; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!orphan) { if (w->c[i]) return; continue; } |
0 |
0 |
0 |
| if (!orphan) { if (w->c[i]) return; continue; } |
0 |
0 |
0 |
|
| 224 |
0 |
▶ | 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; |
0 |
0 |
0 |
| if (w->c[i] == 0 || w->c[i] > 2) return; |
0 |
0 |
0 |
|
| 225 |
0 |
▶ | 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; } |
0 |
0 |
0 |
| if (w->c[i] == 2) { if (pair) return; pair = i; } |
0 |
0 |
0 |
|
| 226 |
|
| } |
| 227 |
0 |
▶ | if (!pair) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pair) return; |
0 |
0 |
0 |
|
| 228 |
0 |
| memset(&s, 0, sizeof s); |
| 229 |
0 |
| s.form = MJ_FORM_THIRTEEN_ORPHANS; |
| 230 |
0 |
| s.pair = (unsigned char)pair; |
| 231 |
0 |
▶ | 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]; |
0 |
0 |
0 |
|
| 232 |
0 |
▶ | if (w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning) { |
0 |
0 |
0 |
|
| 233 |
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 |
|
| 234 |
0 |
▶ | 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; } |
0 |
0 |
0 |
|
| 235 |
|
| else { |
| 236 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (i = 0; i < 13; i++) if (ORPHAN[i] == w->winning) s.place_index = (unsigned char)i; |
0 |
0 |
0 |
|
| 237 |
0 |
| s.place_in = MJ_IN_SINGLE; |
| 238 |
0 |
| s.wait = MJ_WAIT_SINGLE; |
| 239 |
|
| } |
| 240 |
0 |
| w->out[w->n++] = s; |
| 241 |
0 |
| return; |
| 242 |
|
| } |
| 243 |
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 |
|
| 244 |
0 |
| 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 |
0 |
| static void honours_knitted(struct walk *w) |
| 252 |
|
| { |
| 253 |
|
| int i, p; |
| 254 |
0 |
▶ | if (w->melds) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds) return; |
0 |
0 |
0 |
|
| 255 |
0 |
▶ | if (total(w->c) != 14) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != 14) return; |
0 |
0 |
0 |
|
| 256 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i] > 1) return; |
0 |
0 |
0 |
|
| 257 |
0 |
▶ | for (p = 0; p < 6; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p < 6; p++) { |
0 |
0 |
0 |
|
| 258 |
0 |
| int ok = 1, s; |
| 259 |
0 |
▶ | for (s = 1; s <= 3 && ok; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 1; s <= 3 && ok; s++) { |
0 |
0 |
0 |
| for (s = 1; s <= 3 && ok; s++) { |
0 |
0 |
0 |
|
| 260 |
0 |
| int base = KNIT_BASE[PERM[p][s - 1]], r; |
| 261 |
0 |
▶ | for (r = 1; r <= 9; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 1; r <= 9; r++) { |
0 |
0 |
0 |
|
| 262 |
0 |
▶ | 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; } |
0 |
0 |
0 |
| if (w->c[KIND_OF(s, r)] && (r - base) % 3 != 0) { ok = 0; break; } |
0 |
0 |
0 |
|
| 263 |
|
| } |
| 264 |
|
| } |
| 265 |
0 |
▶ | if (!ok) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) continue; |
0 |
0 |
0 |
|
| 266 |
|
| { |
| 267 |
|
| struct mj_split sp; |
| 268 |
0 |
| memset(&sp, 0, sizeof sp); |
| 269 |
0 |
| sp.form = MJ_FORM_HONOURS_KNITTED; |
| 270 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (i = 1; i <= MJ_KINDS; i++) if (w->c[i]) sp.singles[sp.nsingles++] = (unsigned char)i; |
0 |
0 |
0 |
|
| 271 |
0 |
▶ | if (w->winning) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->winning) { |
0 |
0 |
0 |
|
| 272 |
0 |
▶ | 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; |
0 |
0 |
0 |
| for (i = 0; i < sp.nsingles; i++) if (sp.singles[i] == w->winning) sp.place_index = (unsigned char)i; |
0 |
0 |
0 |
|
| 273 |
0 |
| sp.place_in = MJ_IN_SINGLE; |
| 274 |
0 |
| sp.wait = MJ_WAIT_SINGLE; |
| 275 |
|
| } |
| 276 |
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 |
|
| 277 |
0 |
| w->out[w->n++] = sp; |
| 278 |
|
| } |
| 279 |
|
| /* one split whatever the assignment: the singles are the same tiles */ |
| 280 |
0 |
| 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 |
0 |
| static void knitted_straight(struct walk *w) |
| 289 |
|
| { |
| 290 |
0 |
| int p, need = 14 - 3 * w->melds; |
| 291 |
0 |
▶ | if (w->melds > 1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->melds > 1) return; |
0 |
0 |
0 |
|
| 292 |
0 |
▶ | if (total(w->c) != need) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total(w->c) != need) return; |
0 |
0 |
0 |
|
| 293 |
0 |
▶ | for (p = 0; p < 6; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p < 6; p++) { |
0 |
0 |
0 |
|
| 294 |
0 |
| int ok = 1, s, k; |
| 295 |
|
| struct mj_set knit[3]; |
| 296 |
0 |
▶ | for (s = 1; s <= 3 && ok; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = 1; s <= 3 && ok; s++) { |
0 |
0 |
0 |
| for (s = 1; s <= 3 && ok; s++) { |
0 |
0 |
0 |
|
| 297 |
0 |
| int base = KNIT_BASE[PERM[p][s - 1]]; |
| 298 |
0 |
▶ | for (k = 0; k < 3; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < 3; k++) { |
0 |
0 |
0 |
|
| 299 |
0 |
| int kind = KIND_OF(s, base + 3 * k); |
| 300 |
0 |
▶ | if (!w->c[kind]) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->c[kind]) { ok = 0; break; } |
0 |
0 |
0 |
|
| 301 |
0 |
| knit[s - 1].tiles[k] = (unsigned char)kind; |
| 302 |
|
| } |
| 303 |
0 |
| knit[s - 1].kind = MJ_SET_KNIT; |
| 304 |
|
| } |
| 305 |
0 |
▶ | if (!ok) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) continue; |
0 |
0 |
0 |
|
| 306 |
|
| /* take the nine out, split the rest */ |
| 307 |
0 |
▶ | 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]]--; |
0 |
0 |
0 |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]--; |
0 |
0 |
0 |
|
| 308 |
|
| { |
| 309 |
|
| int q; |
| 310 |
0 |
▶ | for (q = 1; q <= MJ_KINDS; q++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (q = 1; q <= MJ_KINDS; q++) { |
0 |
0 |
0 |
|
| 311 |
0 |
▶ | if (w->c[q] < 2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->c[q] < 2) continue; |
0 |
0 |
0 |
|
| 312 |
0 |
| w->c[q] -= 2; |
| 313 |
0 |
| w->pair = q; |
| 314 |
0 |
| w->nsets = 3; |
| 315 |
0 |
| memcpy(w->sets, knit, sizeof knit); |
| 316 |
0 |
| walk_sets(w, 1, MJ_FORM_KNITTED_STRAIGHT); |
| 317 |
0 |
| w->c[q] += 2; |
| 318 |
|
| } |
| 319 |
0 |
| w->pair = 0; |
| 320 |
0 |
| w->nsets = 0; |
| 321 |
|
| } |
| 322 |
0 |
▶ | 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]]++; |
0 |
0 |
0 |
| for (s = 0; s < 3; s++) for (k = 0; k < 3; k++) w->c[knit[s].tiles[k]]++; |
0 |
0 |
0 |
|
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| /* ---- the public three --------------------------------------------------------- */ |
| 327 |
|
| |
| 328 |
0 |
| int mj_decompose(const unsigned char *counts, int melds, int winning, |
| 329 |
|
| struct mj_split *out, int max) |
| 330 |
|
| { |
| 331 |
|
| struct walk w; |
| 332 |
0 |
▶ | if (!MJ) MJ = mj_abi_table(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!MJ) MJ = mj_abi_table(); |
0 |
0 |
0 |
|
| 333 |
0 |
| memset(&w, 0, sizeof w); |
| 334 |
0 |
| memcpy(w.c, counts, MJ_KINDS + 1); |
| 335 |
0 |
| w.c[0] = 0; |
| 336 |
0 |
| w.melds = melds; |
| 337 |
0 |
| w.winning = winning; |
| 338 |
0 |
| w.out = out; |
| 339 |
0 |
| w.max = max; |
| 340 |
|
| |
| 341 |
0 |
| standard(&w); |
| 342 |
0 |
| seven_pairs(&w); |
| 343 |
0 |
| thirteen_orphans(&w); |
| 344 |
0 |
| honours_knitted(&w); |
| 345 |
0 |
| knitted_straight(&w); |
| 346 |
|
| |
| 347 |
0 |
▶ | return w.overflow ? -1 : w.n; |
Branch: 1 branch
| Branch | True | False | Total |
| return w.overflow ? -1 : w.n; |
0 |
0 |
0 |
|
| 348 |
|
| } |
| 349 |
|
| |
| 350 |
0 |
| int mj_is_complete(const unsigned char *counts, int melds) |
| 351 |
|
| { |
| 352 |
|
| struct mj_split one; |
| 353 |
0 |
| int n = mj_decompose(counts, melds, 0, &one, 1); |
| 354 |
0 |
| return n != 0 ? 1 : 0; |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
0 |
| int mj_waits(const unsigned char *counts, int melds, unsigned char *out) |
| 358 |
|
| { |
| 359 |
|
| unsigned char c[MJ_KINDS + 1]; |
| 360 |
0 |
| int k, n = 0; |
| 361 |
0 |
▶ | if (!MJ) MJ = mj_abi_table(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!MJ) MJ = mj_abi_table(); |
0 |
0 |
0 |
|
| 362 |
0 |
| memcpy(c, counts, MJ_KINDS + 1); |
| 363 |
0 |
▶ | for (k = 1; k <= MJ_KINDS; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 1; k <= MJ_KINDS; k++) { |
0 |
0 |
0 |
|
| 364 |
0 |
▶ | if (c[k] >= MJ_PER_KIND) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c[k] >= MJ_PER_KIND) continue; |
0 |
0 |
0 |
|
| 365 |
0 |
| c[k]++; |
| 366 |
0 |
▶ | 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; |
0 |
0 |
0 |
|
| 367 |
0 |
| c[k]--; |
| 368 |
|
| } |
| 369 |
0 |
| return n; |
| 370 |
|
| } |