Skip to main content

Mahjong hands calculation

Project description

https://travis-ci.org/MahjongRepository/tenhou-js-bot.svg?branch=master

For now only Python 3.5+ is supported.

Riichi mahjong hands calculation

This library can calculate hand cost (han, fu with details, yaku and scores) for riichi mahjong (japanese version).

It supports features like:

  • Disable or enable aka dora in the hand

  • Disable or enable open tanyao yaku

  • Disable or enable double yakuman (like suuanko tanki)

The code was validated on tenhou.net phoenix replays in total on 11,120,125 hands.

So, we can say that our own hand calculator works the same way that tenhou.net hand calculation.

Right now it supports only japanese version (riichi mahjong). MCR (chinese version) is in plans.

Project repository: https://github.com/MahjongRepository/mahjong

How to install

pip install mahjong

How to use

Let’s calculate how much will cost this hand:

https://user-images.githubusercontent.com/475367/30796350-3d30431a-a204-11e7-99e5-aab144c82f97.png

Tanyao hand by ron

from mahjong.hand_calculating.hand import HandCalculator
from mahjong.tile import TilesConverter

calculator = HandCalculator()

# we had to use all 14 tiles in that array
tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444')
win_tile = TilesConverter.string_to_136_array(sou='4')[0]

result = calculator.estimate_hand_value(tiles, win_tile)

print(result.han, result.fu)
print(result.cost['main'])
print(result.yaku)
for fu_item in result.fu_details:
    print(fu_item)

Output:

1 40
1300
[Tanyao]
{'fu': 30, 'reason': 'base'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 2, 'reason': 'open_pon'}

How about tsumo?

result = calculator.estimate_hand_value(tiles, win_tile, is_tsumo=True)

print(result.han, result.fu)
print(result.cost['main'], result.cost['additional'])
print(result.yaku)
for fu_item in result.fu_details:
    print(fu_item)

Output:

4 40
4000 2000
[Menzen Tsumo, Tanyao, San Ankou]
{'fu': 20, 'reason': 'base'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 2, 'reason': 'tsumo'}

What if we add open set?

from mahjong.meld import Meld

meld = Meld()
meld.type = Meld.PON
meld.tiles = TilesConverter.string_to_136_array(man='444')

result = calculator.estimate_hand_value(tiles, win_tile, melds=[meld], has_open_tanyao=True)

print(result.han, result.fu)
print(result.cost['main'])
print(result.yaku)
for fu_item in result.fu_details:
    print(fu_item)

Output:

1 30
1000
[Tanyao]
{'fu': 20, 'reason': 'base'}
{'fu': 4, 'reason': 'closed_pon'}
{'fu': 2, 'reason': 'open_pon'}
{'fu': 2, 'reason': 'open_pon'}

Shanten calculation

from mahjong.shanten import Shanten
from mahjong.tile import TilesConverter

shanten = Shanten()
tiles = TilesConverter.string_to_34_array(man='13569', pin='123459', sou='443')
result = shanten.calculate_shanten(tiles)

print(result)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mahjong-1.0.5.tar.gz (21.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page