| 1 |
|
| /* mahjong_tiles.c - the forty-two kinds, filled in once. |
| 2 |
|
| * |
| 3 |
|
| * The table is written out by hand rather than generated in a loop, so that |
| 4 |
|
| * reading it IS checking it: every row says its code, its suit, its rank and |
| 5 |
|
| * its flags, and t/01-tiles.t holds the same facts in Perl, written from the |
| 6 |
|
| * rulebook and not from this file. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #include <string.h> |
| 10 |
|
| #include "mahjong_abi.h" |
| 11 |
|
| |
| 12 |
|
| #define S (MJ_F_SUIT) |
| 13 |
|
| #define T (MJ_F_SUIT | MJ_F_TERMINAL) |
| 14 |
|
| #define M (MJ_F_SUIT | MJ_F_SIMPLE) |
| 15 |
|
| #define W (MJ_F_HONOUR | MJ_F_WIND) |
| 16 |
|
| #define D (MJ_F_HONOUR | MJ_F_DRAGON) |
| 17 |
|
| #define FL (MJ_F_BONUS | MJ_F_FLOWER) |
| 18 |
|
| #define SE (MJ_F_BONUS | MJ_F_SEASON) |
| 19 |
|
| #define G (MJ_F_GREEN) |
| 20 |
|
| #define R (MJ_F_REVERSIBLE) |
| 21 |
|
| |
| 22 |
|
| static const struct mj_kind KIND[MJ_PATTERNS + 1] = { |
| 23 |
|
| { "", MJ_SUIT_NONE, 0, 0 }, |
| 24 |
|
| |
| 25 |
|
| /* characters, wan. No character is green or reversible. */ |
| 26 |
|
| { "m1", MJ_SUIT_CHAR, 1, T }, |
| 27 |
|
| { "m2", MJ_SUIT_CHAR, 2, M }, |
| 28 |
|
| { "m3", MJ_SUIT_CHAR, 3, M }, |
| 29 |
|
| { "m4", MJ_SUIT_CHAR, 4, M }, |
| 30 |
|
| { "m5", MJ_SUIT_CHAR, 5, M }, |
| 31 |
|
| { "m6", MJ_SUIT_CHAR, 6, M }, |
| 32 |
|
| { "m7", MJ_SUIT_CHAR, 7, M }, |
| 33 |
|
| { "m8", MJ_SUIT_CHAR, 8, M }, |
| 34 |
|
| { "m9", MJ_SUIT_CHAR, 9, T }, |
| 35 |
|
| |
| 36 |
|
| /* dots, tong. Reversible: 1 2 3 4 5 8 9 (fan 40). */ |
| 37 |
|
| { "p1", MJ_SUIT_DOT, 1, T | R }, |
| 38 |
|
| { "p2", MJ_SUIT_DOT, 2, M | R }, |
| 39 |
|
| { "p3", MJ_SUIT_DOT, 3, M | R }, |
| 40 |
|
| { "p4", MJ_SUIT_DOT, 4, M | R }, |
| 41 |
|
| { "p5", MJ_SUIT_DOT, 5, M | R }, |
| 42 |
|
| { "p6", MJ_SUIT_DOT, 6, M }, |
| 43 |
|
| { "p7", MJ_SUIT_DOT, 7, M }, |
| 44 |
|
| { "p8", MJ_SUIT_DOT, 8, M | R }, |
| 45 |
|
| { "p9", MJ_SUIT_DOT, 9, T | R }, |
| 46 |
|
| |
| 47 |
|
| /* bamboo, tiao. Green: 2 3 4 6 8 (fan 3). Reversible: 2 4 5 6 8 9 (fan 40). */ |
| 48 |
|
| { "s1", MJ_SUIT_BAM, 1, T }, |
| 49 |
|
| { "s2", MJ_SUIT_BAM, 2, M | G | R }, |
| 50 |
|
| { "s3", MJ_SUIT_BAM, 3, M | G }, |
| 51 |
|
| { "s4", MJ_SUIT_BAM, 4, M | G | R }, |
| 52 |
|
| { "s5", MJ_SUIT_BAM, 5, M | R }, |
| 53 |
|
| { "s6", MJ_SUIT_BAM, 6, M | G | R }, |
| 54 |
|
| { "s7", MJ_SUIT_BAM, 7, M }, |
| 55 |
|
| { "s8", MJ_SUIT_BAM, 8, M | G | R }, |
| 56 |
|
| { "s9", MJ_SUIT_BAM, 9, T | R }, |
| 57 |
|
| |
| 58 |
|
| /* the winds, in the order of the rounds and the seats */ |
| 59 |
|
| { "we", MJ_SUIT_WIND, 1, W }, |
| 60 |
|
| { "ws", MJ_SUIT_WIND, 2, W }, |
| 61 |
|
| { "ww", MJ_SUIT_WIND, 3, W }, |
| 62 |
|
| { "wn", MJ_SUIT_WIND, 4, W }, |
| 63 |
|
| |
| 64 |
|
| /* the dragons: red, green (fan 3's), white (fan 40's) */ |
| 65 |
|
| { "dr", MJ_SUIT_DRAGON, 1, D }, |
| 66 |
|
| { "dg", MJ_SUIT_DRAGON, 2, D | G }, |
| 67 |
|
| { "dw", MJ_SUIT_DRAGON, 3, D | R }, |
| 68 |
|
| |
| 69 |
|
| /* the flowers, in the rulebook's order: plum, orchid, bamboo, chrysanthemum (3.5.5.1) */ |
| 70 |
|
| { "f1", MJ_SUIT_FLOWER, 1, FL }, |
| 71 |
|
| { "f2", MJ_SUIT_FLOWER, 2, FL }, |
| 72 |
|
| { "f3", MJ_SUIT_FLOWER, 3, FL }, |
| 73 |
|
| { "f4", MJ_SUIT_FLOWER, 4, FL }, |
| 74 |
|
| |
| 75 |
|
| /* the seasons: spring, summer, autumn, winter */ |
| 76 |
|
| { "t1", MJ_SUIT_SEASON, 1, SE }, |
| 77 |
|
| { "t2", MJ_SUIT_SEASON, 2, SE }, |
| 78 |
|
| { "t3", MJ_SUIT_SEASON, 3, SE }, |
| 79 |
|
| { "t4", MJ_SUIT_SEASON, 4, SE }, |
| 80 |
|
| }; |
| 81 |
|
| |
| 82 |
|
| /* A linear scan over forty-two three-byte codes. The Perl side caches ids |
| 83 |
|
| * where it loops, and forty-two compares is cheaper than the hash it would |
| 84 |
|
| * take to beat it. */ |
| 85 |
9190 |
| static int mj_id_of(const char *code) |
| 86 |
|
| { |
| 87 |
|
| int i; |
| 88 |
9190 |
▶ | if (!code || !code[0] || !code[1] || code[2]) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!code || !code[0] || !code[1] || code[2]) return 0; |
9190 |
0 |
9190 |
| if (!code || !code[0] || !code[1] || code[2]) return 0; |
9190 |
0 |
9190 |
| if (!code || !code[0] || !code[1] || code[2]) return 0; |
9190 |
0 |
9190 |
| if (!code || !code[0] || !code[1] || code[2]) return 0; |
1 |
9189 |
9190 |
|
| 89 |
143513 |
▶ | for (i = 1; i <= MJ_PATTERNS; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i <= MJ_PATTERNS; i++) { |
143512 |
1 |
143513 |
|
| 90 |
143512 |
▶ | if (KIND[i].code[0] == code[0] && KIND[i].code[1] == code[1]) return i; |
Branch: 2 branches
| Branch | True | False | Total |
| if (KIND[i].code[0] == code[0] && KIND[i].code[1] == code[1]) return i; |
37108 |
106404 |
143512 |
| if (KIND[i].code[0] == code[0] && KIND[i].code[1] == code[1]) return i; |
9188 |
27920 |
37108 |
|
| 91 |
|
| } |
| 92 |
1 |
| return 0; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| static const struct mj_abi ABI = { |
| 96 |
|
| MJ_ABI_VERSION, |
| 97 |
|
| MJ_KINDS, |
| 98 |
|
| MJ_PATTERNS, |
| 99 |
|
| MJ_PER_KIND, |
| 100 |
|
| MJ_TILES, |
| 101 |
|
| KIND, |
| 102 |
|
| mj_id_of, |
| 103 |
|
| }; |
| 104 |
|
| |
| 105 |
59 |
| const struct mj_abi *mj_abi_table(void) |
| 106 |
|
| { |
| 107 |
59 |
| return &ABI; |
| 108 |
|
| } |