[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'}
--------------------------------------------------

[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'}
--------------------------------------------------

[Python] 177 麻雀アプリ 01 山の作成

2021年3月16日作成

前から興味のあった麻雀AI作成に取り組んでみます。頓挫必至かと思いますが、取りあえずやってみようかと。

2019年にMicrosoft社の麻雀AIが天鳳10段を達成しています。CUIベースでルールを間違えずに打ち切れるレベルが最初の目標です。

AIに関しては麻雀がテーマでないとモチベーションが上がらないため、少しやる気になった今こそ見切り発車します。

ネット情報を参考にまずは山の作成から。コード例では牌136枚を作成しシャッフルする字牌は風牌と三元牌に分けています。

import random

class Taku:
    def __init__(self):
        # 萬子:m、筒子:p、索子:s、風牌:w、三元牌:z
        shurui = ['m','p','s','w','z']
        num = [9,9,9,4,3]
        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)

taku = Taku()
print(f'枚数 {len(taku.yama)}')
print(taku.yama)
--------------------------------------------------
  出力
--------------------------------------------------
枚数 136
['s3', 'm9', 's2', 'm4', 'p4', 'z1', 'm7', 's5', 'p3', 's3', 's5', 's2', 's4', 's8', 'z3', 's1', 's5', 'p8', 'p1', 's1', 's4', 'm7', 'w2', 'z3', 's6', 'p7', 's9', 'p9', 'p1', 'm1', 'w1', 'p4', 'z2', 'm9', 'z2', 'z2', 'm4', 'p8', 'p9', 'z1', 'p6', 'w4', 's1', 'p5', 'p2', 'p6', 'm2', 'w3', 's7', 'm2', 'm7', 'p9', 's8', 'w3', 'p3', 'z3', 's4', 'm2', 's8', 'm5', 's5', 'p2', 's9', 'w2', 's9', 's7', 'w2', 'm8', 'w1', 'm8', 'm9', 's6', 'p2', 'm2', 'm5', 's9', 'p7', 's3', 'p1', 'p5', 'p6', 'm6', 'm4', 's6', 'm5', 'm3', 'w4', 'm6', 'p3', 'w4', 'p1', 'p4', 'm7', 'z2', 'm1', 'w4', 'p4', 's2', 'm3', 'm6', 's4', 'm6', 'm8', 'p5', 'z1', 'z3', 'p2', 's7', 'w2', 'w3', 'p7', 's8', 's6', 'm1', 'z1', 'm4', 'm3', 'p9', 'p8', 'm3', 's1', 'w3', 'm8', 'p7', 'm5', 'p5', 's3', 'm9', 's2', 's7', 'w1', 'm1', 'p8', 'w1', 'p6', 'p3']
--------------------------------------------------

参考記事