[Python] 179 麻雀アプリ 03 鳴きなしツモ和了りのみの麻雀ゲーム

2021年3月16日作成

鳴きやドラのない単純な麻雀ゲームを作成しました。手牌のリストから牌を頭に浮かべてプレイします。


自分しか和了れないので、動作確認程度で動かしてみてください。


和了りやすくするため、索子と字牌を除いた72枚構成にしました。点数計算は自動です。

コード例
点数がロン和了りになっていますがそのままにしています。

import random,sys
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
from mahjong.shanten import Shanten

calculator = HandCalculator()

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

        # このゲームは萬子と筒子のみ
        shurui = ['m','p']
        num = [9,9]

        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)

class Janshi:
    def __init__(self,position):
        self.position = position
        self.tehai = []
        self.sutehai = []

    def haipai(self, yama):
        self.tehai =  yama[0:13]
        del yama[0:13]
        return self.tehai

    def ripai(self):
        self.tehai = sorted(self.tehai)
        man = [hai for hai in self.tehai if 'm' in hai]
        pin = [hai for hai in self.tehai if 'p' in hai]
        sou = [hai for hai in self.tehai if 's' in hai]
        zi = [hai for hai in self.tehai if 'z' in hai]
        self.tehai = man + pin + sou + zi

    def tsumo(self, yama):
        hai = yama[0]
        del yama[0]
        self.tehai.append(hai)
        return hai

    def dahai(self,hai):
        hai_i = self.tehai.index(hai)
        del self.tehai[hai_i]
        self.sutehai.append(hai)
        return hai

    def shanten(self, tehai):
        self.tehai = sorted(self.tehai)
        tiles_man = [hai[1] for hai in self.tehai if 'm' in hai]
        tiles_pin = [hai[1] for hai in self.tehai if 'p' in hai]
        tiles_sou = [hai[1] for hai in self.tehai if 's' in hai]
        tiles_honors = [hai[1] for hai in self.tehai if 'z' in hai]

        # Shantenのインスタンス生成
        shanten = Shanten()
        tiles = TilesConverter.string_to_34_array(man=tiles_man, pin=tiles_pin, sou=tiles_sou, honors=tiles_honors)

        return str(shanten.calculate_shanten(tiles))

def keisan(tehai,tsumohai):
    tiles_man = [hai[1] for hai in tehai if 'm' in hai]
    tiles_pin = [hai[1] for hai in tehai if 'p' in hai]
    tiles_sou = [hai[1] for hai in tehai if 's' in hai]
    tiles_honors = [hai[1] for hai in tehai if 'z' in hai]

    tsumohai_l = [tsumohai]
    print(tsumohai_l)
    end_man = [hai[1] for hai in tsumohai_l if 'm' in hai]
    end_pin = [hai[1] for hai in tsumohai_l if 'p' in hai]
    end_sou = [hai[1] for hai in tsumohai_l if 's' in hai]
    end_honors = [hai[1] for hai in tsumohai_l if 'z' in hai]

    # 和了牌姿
    tiles = TilesConverter.string_to_136_array(man=tiles_man, pin=tiles_pin, sou=tiles_sou, honors=tiles_honors)
    # 和了牌(最後のツモ牌)
    win_tile = TilesConverter.string_to_136_array(man=end_man, pin=end_pin, sou=end_sou, honors=end_honors)[0]
    # 鳴きなし
    melds = None
    # ドラなし
    dora_indicators = None
    # オプションなし
    config = None
    # 計算
    result = calculator.estimate_hand_value(tiles, win_tile, melds, dora_indicators, config)
    return result

# 結果出力
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()
yama = taku.yama
print(f'枚数 {len(yama)}')

print(f'初期状態の山\n{yama}\n')

janshi1 = Janshi('自家')
janshi1.haipai(yama)
print(f'自家手牌\n{janshi1.tehai}\n')

janshi1.ripai()
print(f'埋牌後自家手牌\n{janshi1.tehai}\n')

for i in range(18):
    print(f'{i+1}順目 手牌')
    tsumohai = janshi1.tsumo(yama)
    print(f'ツモ牌 {tsumohai}')
    janshi1.ripai()
    print(f'ツモ後手牌\n{janshi1.tehai}\n')
    print(f'向聴数 {janshi1.shanten(janshi1.tehai)}\n')

    if int(janshi1.shanten(janshi1.tehai)) < 0:
        print('和了!')
        result = keisan(janshi1.tehai,tsumohai)
        print_hand_result(result)
        sys.exit()

    print('捨て牌を選んでください')
    dahai = input()
    dahai = janshi1.dahai(dahai)

    print(f'打牌 {dahai}')
    print(f'打牌後手牌\n{janshi1.tehai}\n')
    print(f'捨て牌\n{janshi1.sutehai}\n')
--------------------------------------------------

  出力
--------------------------------------------------
枚数 72
初期状態の山
['m2', 'p1', 'm5', 'm9', 'm4', 'p3', 'm3', 'm7', 'p5', 'p7', 'p6', 'm6', 'p2', 'p1', 'p9', 'p4', 'p1', 'm7', 'p8', 'p4', 'p6', 'p9', 'm9', 'm7', 'm2', 'm5', 'p2', 'p7', 'm1', 'm4', 'm3', 'm5', 'm3', 'm8', 'p8', 'm5', 'm8', 'm1', 'm1', 'm6', 'm3', 'p3', 'p7', 'p8', 'm4', 'p3', 'p2', 'm9', 'p7', 'm9', 'p9', 'm6', 'm4', 'm8', 'm7', 'm8', 'p1', 'm6', 'p5', 'p8', 'm2', 'p2', 'p5', 'p9', 'p3', 'p6', 'p5', 'm1', 'p6', 'p4', 'm2', 'p4']

自家手牌
['m2', 'p1', 'm5', 'm9', 'm4', 'p3', 'm3', 'm7', 'p5', 'p7', 'p6', 'm6', 'p2']

埋牌後自家手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm9', 'p1', 'p2', 'p3', 'p5', 'p6', 'p7']

1順目 手牌
ツモ牌 p1
ツモ後手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm9', 'p1', 'p1', 'p2', 'p3', 'p5', 'p6', 'p7']
向聴数 0
捨て牌を選んでください
m9
打牌 m9
打牌後手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'p1', 'p1', 'p2', 'p3', 'p5', 'p6', 'p7']
捨て牌
['m9']

2順目 手牌
ツモ牌 p9
ツモ後手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'p1', 'p1', 'p2', 'p3', 'p5', 'p6', 'p7', 'p9']
向聴数 0
捨て牌を選んでください
p9
打牌 p9
打牌後手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'p1', 'p1', 'p2', 'p3', 'p5', 'p6', 'p7']
捨て牌
['m9', 'p9']

3順目 手牌
ツモ牌 p4
ツモ後手牌
['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'p1', 'p1', 'p2', 'p3', 'p4', 'p5', 'p6', 'p7']
向聴数 -1
和了!
['p4']
1 30
1000 0
[Pinfu]
{'fu': 30, 'reason': 'base'}
--------------------------------------------------