[Python] 178 麻雀アプリ 02 点数計算ライブラリmahjong

2021年3月16日作成

mahjongという点数計算ライブラリを使って九連宝燈の点数計算をさせてみました。

これはかなり便利です。

import random
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.tile import TilesConverter
from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules
from mahjong.meld import Meld
from mahjong.constants import EAST, SOUTH, WEST, NORTH

calculator = HandCalculator()

class Taku:
    def __init__(self):
        # 萬子:m,筒子:p,索子:s,字牌:z
        shurui = ['m','p','s','z']
        num = [9,9,9,7]

        self.yama = []
        for s,n in zip(shurui,num):
            for i in range(1,n+1):
                hai = s + str(i)
                self.yama.append(hai)
        
        self.yama = self.yama * 4
        random.shuffle(self.yama)

#結果出力
def print_hand_result(hand_result):
     #翻数, 符数
     print(hand_result.han, hand_result.fu)
     #点数(親(放銃者),子)
     print(hand_result.cost['main'], result.cost['additional'])
     #役
     print(hand_result.yaku)
     #符数内容
     for fu_item in hand_result.fu_details:
          print(fu_item)

taku = Taku()
print(f'枚数 {len(taku.yama)}')
# print(taku.yama)

# 中略

# 九連宝燈で和了
my_tehai = ['m1','m1','m1','m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm9', 'm9', 'm9']

tiles_man = [hai[1] for hai in my_tehai if 'm' in hai]
print(tiles_man)
tiles_pin = [hai[1] for hai in my_tehai if 'p' in hai]
print(tiles_pin)
tiles_sou = [hai[1] for hai in my_tehai if 's' in hai]
print(tiles_sou)
tiles_honors = [hai[1] for hai in my_tehai if 'z' in hai]
print(tiles_honors)

# 和了牌姿
tiles = TilesConverter.string_to_136_array(man=tiles_man, pin=tiles_pin, sou=tiles_sou, honors=tiles_honors)
#和了牌(萬子の1)
win_tile = TilesConverter.string_to_136_array(man='1')[0]
#鳴きなし
melds = None
#ドラなし
dora_indicators = None
#オプションなし
config = None
#計算
result = calculator.estimate_hand_value(tiles, win_tile, melds, dora_indicators, config)
print_hand_result(result)
--------------------------------------------------

  出力
--------------------------------------------------
枚数 136
['1', '1', '1', '2', '3', '4', '5', '6', '7', '8', '9', '9', '9', '9']
[]
[]
[]
13 40
32000 0
[Chuuren Poutou]
{'fu': 30, 'reason': 'base'}
{'fu': 8, 'reason': 'closed_terminal_pon'}
--------------------------------------------------