|
| 1 | +Python 3.9+ is supported. |
| 2 | + |
| 3 | +If you need Python 2 support, you can use the v1.1.11 version of the library. |
| 4 | + |
| 5 | +The library contains various tools (shanten, agari, hand calculation) |
| 6 | +for the Japanese version of mahjong (riichi mahjong). |
| 7 | + |
| 8 | +## Riichi mahjong hands calculation |
| 9 | + |
| 10 | +It supports optional features like: |
| 11 | + |
| 12 | +| Feature | Keyword parameter | Default value | |
| 13 | +|-------------------------------------------------------------------------------------------|---------------------------|-----------------------------| |
| 14 | +| Disable or enable open tanyao yaku | has_open_tanyao | False | |
| 15 | +| Disable or enable aka dora in the hand | has_aka_dora | False | |
| 16 | +| Disable or enable double yakuman (like suuanko tanki) | has_double_yakuman | True | |
| 17 | +| Settings for different kazoe yakuman calculation (it сan be an yakuman or a sanbaiman) | kazoe_limit | HandConstants.KAZOE_LIMITED | |
| 18 | +| Support kiriage mangan | kiriage | False | |
| 19 | +| Allow to disable additional +2 fu in open hand (you can make 1-20 hand with that setting) | fu_for_open_pinfu | True | |
| 20 | +| Disable or enable pinfu tsumo | fu_for_pinfu_tsumo | False | |
| 21 | +| Counting renhou as 5 han or yakuman | renhou_as_yakuman | False | |
| 22 | +| Disable or enable Daisharin yakuman | has_daisharin | False | |
| 23 | +| Disable or enable Daisharin in other suits (Daisuurin, Daichikurin) | has_daisharin_other_suits | False | |
| 24 | +| Disable or enable yakuman for dealing into open hands | has_sashikomi_yakuman | False | |
| 25 | +| Limit yakuman calculation to 6 (maximum score 192000) | limit_to_sextuple_yakuman | True | |
| 26 | +| Disable or enable extra yakuman for all honors 7 pairs | has_daichisei | False | |
| 27 | +| Disable or enable paarenchan without any yaku | paarenchan_needs_yaku | True | |
| 28 | + |
| 29 | +The code was validated on tenhou.net phoenix replays in total on |
| 30 | +**11,120,125 hands**. |
| 31 | + |
| 32 | +So, we can say that our hand calculator works the same way that |
| 33 | +tenhou.net hand calculation. |
| 34 | + |
| 35 | +## How to use |
| 36 | + |
| 37 | +You can find more examples here: https://github.com/MahjongRepository/mahjong/blob/v1.4.0/doc/examples.py |
| 38 | + |
| 39 | +Let's calculate how much will cost this hand: |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +### Tanyao hand by ron |
| 44 | + |
| 45 | +```python |
| 46 | +from mahjong.hand_calculating.hand import HandCalculator |
| 47 | +from mahjong.tile import TilesConverter |
| 48 | +from mahjong.hand_calculating.hand_config import HandConfig |
| 49 | +from mahjong.meld import Meld |
| 50 | + |
| 51 | +calculator = HandCalculator() |
| 52 | + |
| 53 | +# we had to use all 14 tiles in that array |
| 54 | +tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444') |
| 55 | +win_tile = TilesConverter.string_to_136_array(sou='4')[0] |
| 56 | + |
| 57 | +result = calculator.estimate_hand_value(tiles, win_tile) |
| 58 | + |
| 59 | +print(result.han, result.fu) |
| 60 | +print(result.cost['main']) |
| 61 | +print(result.yaku) |
| 62 | +for fu_item in result.fu_details: |
| 63 | + print(fu_item) |
| 64 | +``` |
| 65 | + |
| 66 | +Output: |
| 67 | +``` |
| 68 | +1 40 |
| 69 | +1300 |
| 70 | +[Tanyao] |
| 71 | +{'fu': 30, 'reason': 'base'} |
| 72 | +{'fu': 4, 'reason': 'closed_pon'} |
| 73 | +{'fu': 4, 'reason': 'closed_pon'} |
| 74 | +{'fu': 2, 'reason': 'open_pon'} |
| 75 | +``` |
| 76 | + |
| 77 | +### How about tsumo? |
| 78 | + |
| 79 | +```python |
| 80 | +result = calculator.estimate_hand_value(tiles, win_tile, config=HandConfig(is_tsumo=True)) |
| 81 | + |
| 82 | +print(result.han, result.fu) |
| 83 | +print(result.cost['main'], result.cost['additional']) |
| 84 | +print(result.yaku) |
| 85 | +for fu_item in result.fu_details: |
| 86 | + print(fu_item) |
| 87 | +``` |
| 88 | + |
| 89 | +Output: |
| 90 | +``` |
| 91 | +4 40 |
| 92 | +4000 2000 |
| 93 | +[Menzen Tsumo, Tanyao, San Ankou] |
| 94 | +{'fu': 20, 'reason': 'base'} |
| 95 | +{'fu': 4, 'reason': 'closed_pon'} |
| 96 | +{'fu': 4, 'reason': 'closed_pon'} |
| 97 | +{'fu': 4, 'reason': 'closed_pon'} |
| 98 | +{'fu': 2, 'reason': 'tsumo'} |
| 99 | +``` |
| 100 | + |
| 101 | +### What if we add open set? |
| 102 | + |
| 103 | +```python |
| 104 | +melds = [Meld(meld_type=Meld.PON, tiles=TilesConverter.string_to_136_array(man='444'))] |
| 105 | + |
| 106 | +result = calculator.estimate_hand_value(tiles, win_tile, melds=melds, config=HandConfig(options=OptionalRules(has_open_tanyao=True))) |
| 107 | + |
| 108 | +print(result.han, result.fu) |
| 109 | +print(result.cost['main']) |
| 110 | +print(result.yaku) |
| 111 | +for fu_item in result.fu_details: |
| 112 | + print(fu_item) |
| 113 | +``` |
| 114 | + |
| 115 | +Output: |
| 116 | +``` |
| 117 | +1 30 |
| 118 | +1000 |
| 119 | +[Tanyao] |
| 120 | +{'fu': 20, 'reason': 'base'} |
| 121 | +{'fu': 4, 'reason': 'closed_pon'} |
| 122 | +{'fu': 2, 'reason': 'open_pon'} |
| 123 | +{'fu': 2, 'reason': 'open_pon'} |
| 124 | +``` |
| 125 | + |
| 126 | +Shanten calculation |
| 127 | +=================== |
| 128 | + |
| 129 | +```python |
| 130 | +from mahjong.shanten import Shanten |
| 131 | + |
| 132 | +shanten = Shanten() |
| 133 | +tiles = TilesConverter.string_to_34_array(man='13569', pin='123459', sou='443') |
| 134 | +result = shanten.calculate_shanten(tiles) |
| 135 | + |
| 136 | +print(result) |
| 137 | +``` |
| 138 | + |
| 139 | +Aotenjou scoring rules |
| 140 | +====================== |
| 141 | + |
| 142 | +```python |
| 143 | +tiles = TilesConverter.string_to_136_array(honors='111133555566667777') |
| 144 | +win_tile = TilesConverter.string_to_136_array(honors='3')[0] |
| 145 | + |
| 146 | +melds = [ |
| 147 | + Meld(meld_type=Meld.KAN, tiles=TilesConverter.string_to_136_array(honors='1111'), opened=False), |
| 148 | + Meld(meld_type=Meld.KAN, tiles=TilesConverter.string_to_136_array(honors='5555'), opened=False), |
| 149 | + Meld(meld_type=Meld.KAN, tiles=TilesConverter.string_to_136_array(honors='6666'), opened=False), |
| 150 | + Meld(meld_type=Meld.KAN, tiles=TilesConverter.string_to_136_array(honors='7777'), opened=False), |
| 151 | +] |
| 152 | + |
| 153 | +result = calculator.estimate_hand_value(tiles, win_tile, melds=melds, dora_indicators=TilesConverter.string_to_136_array(honors='44447777'), |
| 154 | + scores_calculator_factory=Aotenjou, config=HandConfig(is_riichi=True, is_tsumo=True, is_ippatsu=True, is_haitei=True, player_wind=EAST, round_wind=EAST)) |
| 155 | + |
| 156 | +print(result.han, result.fu) |
| 157 | +print(result.cost['main']) |
| 158 | +print(result.yaku) |
| 159 | +for fu_item in result.fu_details: |
| 160 | + print(fu_item) |
| 161 | +``` |
| 162 | + |
| 163 | +Output: |
| 164 | +``` |
| 165 | +103 160 |
| 166 | +12980742146337069071326240823050300 |
| 167 | +[Menzen Tsumo, Riichi, Ippatsu, Haitei Raoyue, Yakuhai (wind of place), Yakuhai (wind of round), Daisangen, Suu Kantsu, Tsuu Iisou, Suu Ankou Tanki, Dora 32] |
| 168 | +{'fu': 32, 'reason': 'closed_terminal_kan'} |
| 169 | +{'fu': 32, 'reason': 'closed_terminal_kan'} |
| 170 | +{'fu': 32, 'reason': 'closed_terminal_kan'} |
| 171 | +{'fu': 32, 'reason': 'closed_terminal_kan'} |
| 172 | +{'fu': 20, 'reason': 'base'} |
| 173 | +{'fu': 2, 'reason': 'pair_wait'} |
| 174 | +{'fu': 2, 'reason': 'tsumo'} |
| 175 | +``` |
0 commit comments