麻雀pythonプログラムの作成②

暗槓を実装。ポンした際に、手番変えるのどうしよう。。。

import random
import sys
sys.path.append("c:/programdata/anaconda3/lib/site-packages")
from mahjong.shanten import Shanten

#計算
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



class Yamahai:
   def __init__(self, ninzuu):
       self.yama = []
       self.dora = []
       kind =("manzu", "pinzu", "souzu", "honors")
       kind_supai = (kind[0], kind[1], kind[2])
       sanma_supai = (kind[1], kind[2])
       value = range(4)
       value_supai = range(9)
       value_jihai = range(7)
       if ninzuu == 4:
           for i in kind_supai:
               for j in value_supai:
                   for k in value:
                       self.yama.append(i + "_" + str(j + 1))
       elif ninzuu ==3:
           for i in [0, 8]:
               for k in value:
                   self.yama.append(kind[0] + "_" + str(i + 1))       
           for i in sanma_supai:
               for j in value_supai:
                   for k in value:
                       self.yama.append(i + "_" + str(j + 1))
       else:
           print("引数が不正です。")
           sys.exit()
       for i in value_jihai:
           for j in value:
               self.yama.append(kind[3] + "_" + str(i + 1))
       # random.shuffle(self.yama)

   def __repr__(self):
       return str(len(self.yama))
   
   def get_dora(self):
       self.dora =  self.yama[0]
       del self.yama[0]


class Janshi:
   def __init__(self, name):
       self.tehai = []
       self.int = []
       self.kawa = []
       self.kan_pon_aida  =["","","",]
       self.kan_pon =[]
       self.name = "player"+ str(name)
       self.ponflg = False

   def __repr__(self):
       return str(self.name)

   def get_haipai(self, yama):
       self.tehai =  yama[0:13]
       self.tehai =  ["manzu_1","manzu_1","manzu_1","manzu_2","manzu_2","manzu_2","manzu_3","manzu_3","manzu_3","manzu_4","manzu_4","souzu_1","souzu_1",]
       del yama[0:13]
       print(sorted(self.tehai))
       return sorted(self.tehai)
   

   def create_tehaiInt(self, calc):
       if calc:
           for i in range(1, 10):
               self.tehaiInt += str(calc.count(str(i)))
       else:
           for i in range(1, 10):
               self.tehaiInt += '0'
       

   def riipai(self, tehai):
       self.calc_manzu = ""
       self.calc_pinzu = ""
       self.calc_souzu = ""
       self.calc_honors = ""
       self.tehaiInt = ""
       self.manzu = []
       self.pinzu = []
       self.souzu = []
       self.honors = []

       sorted(tehai)
       for i in range(len(tehai)):
           if tehai[i][:3] == "hon":
               self.honors.append(tehai[i])
               self.calc_honors += tehai[i][7]
           if tehai[i][:3] == "man":
               self.manzu.append(tehai[i])
               self.calc_manzu += tehai[i][6]
           if tehai[i][:3] == "pin":
               self.pinzu.append(tehai[i])
               self.calc_pinzu += tehai[i][6]
           if tehai[i][:3] == "sou":
               self.souzu.append(tehai[i])
               self.calc_souzu += tehai[i][6]
       self.create_tehaiInt(sorted(self.calc_manzu))
       self.create_tehaiInt(sorted(self.calc_pinzu))
       self.create_tehaiInt(sorted(self.calc_souzu))
       self.create_tehaiInt(sorted(self.calc_honors))
       if self.ponflg:
           self.int 
       else:
           self.tehaiInt = self.tehaiInt[0:34]

       return sorted(self.manzu) + sorted(self.pinzu) + sorted(self.souzu) + sorted(self.honors)

   def syanten(self, tehai):
       # self.tehai = self.riipai(tehai)
       #Shanten(シャンテン数計算用クラス)のインスタンスを生成
       shanten = Shanten()
       #手牌14枚
       tiles = TilesConverter.string_to_34_array(man=self.calc_manzu, pin=self.calc_pinzu, sou=self.calc_souzu, honors=self.calc_honors)
       #計算
       return shanten.calculate_shanten(tiles)

   def tsumo(self, yama):
       self.annkanflg = False
       self.tsumohai = yama[0]
       del yama[0]
       self.tehai.append(self.tsumohai)
       self.tehai = self.riipai(self.tehai)
       if (self.name == "player0"):
           print("ツモ:'{}'".format(self.tsumohai))
           if self.kan_pon:
               self.tehai_kan_pon = self.kan_pon_aida + self.kan_pon
           else:
               self.tehai_kan_pon = self.kan_pon
           if self.syanten(self.tehai + self.kan_pon) == 0:
               print("テンパイ")
               self.sute()
           elif self.syanten(self.tehai + self.kan_pon) == -1:
               self.agaricheck()
           else:
               print("{}シャンテンです".format(self.syanten(self.tehai + self.kan_pon)))
               self.sute()
       else:
           self.sute()

   def sute(self):
       if (self.name == "player0"):
           print(self.tehai + self.tehai_kan_pon)
           if "4" in self.tehaiInt:
               self.annkan()
           if self.annkanflg:
               self.tsumo(yamahai.yama)
           else:
               message ="捨てる牌を選んでください"
               self.sutehaiFlg = False
               i = 0
               while (i < 5):
                   sentakuhai = input(message)
                   if sentakuhai in self.tehai:
                       sutehai_position = self.tehai.index(sentakuhai)
                       self.sutehaiFlg = True
                       break
                   i += 1 
               if not (self.sutehaiFlg):
                   self.sute()
               self.sutehai = self.tehai[sutehai_position]
               del self.tehai[sutehai_position]
               self.pon()
               self.kawa.append(self.sutehai)
               print("捨て牌:{}".format(self.sutehai))
               # print(self)
               # print(self.tehai)
               print("{}の手配:{}".format(self, self.tehai + self.tehai_kan_pon))
       else:
           self.sutehai = self.tehai[13]
           del self.tehai[13]
           print("捨て牌:{}".format(self.sutehai))
           self.pon()
           self.kawa.append(self.sutehai)


   def pon(self):
       self.sutehaiList = []
       self.otherPlayerNo = [0,1,2]
       self.ponflg = True
       self.sutehaiList.append(self.sutehai)
       self.riipai(self.sutehaiList)
       
       self.tehaiInt.find("1")
       
       self.otherPlayerNo.remove(StartGame.playerNo)
       for i in self.otherPlayerNo:
           if StartGame.players[i].tehai.count(self.sutehai)== 2:
               message = "{}をポンしますか(YorN)".format(self.sutehai)
               pon = input(message)
               if pon =="y":
                   StartGame.players[i].tehai.append(pon)
                   StartGame.players[i].sute()
                   #ポンした際に手版を変わる。。。。。
               elif pon == "n":
                   pass
               else:
                   self.pon()
       self.ponflg = True


   def annkan(self):
       self.annkanflg = False
       message = "槓する牌を選んでください??(orN)"
       kan = input(message)
       if kan in self.tehai and self.tehai.count(kan)== 4:
           for i in range(4):
               self.kan_pon.append(kan)
               self.tehai.remove(kan)
           self.annkanflg = True
       elif kan == "n":
           pass
       else:
           self.annkan()

   def agaricheck(self):
       message = "和了しますか(YorN)"
       agari_yn = input(message)
       if agari_yn == "y":
           self.agari()
       elif agari_yn == "n":
           self.sute()
       else:
           self.agaricheck()

   def agari(self):
       print("和了")
       self.print_hand_result()
       sys.exit(0)

   #結果出力用
   def print_hand_result(self):
       self.calc_agari_manzu = ""
       self.calc_agari_pinzu = ""
       self.calc_agari_souzu = ""
       self.calc_agari_honors = ""
       if self.tsumohai[:3] == "hon":
           self.calc_agari_honors += self.tsumohai[7]
           win_tile = TilesConverter.string_to_136_array(hon=self.calc_agari_honors)[0]
       if self.tsumohai[:3] == "man":
           self.calc_agari_manzu += self.tsumohai[6]
           win_tile = TilesConverter.string_to_136_array(man=self.calc_agari_manzu)[0]
       if self.tsumohai[:3] == "pin":
           self.calc_agari_pinzu += self.tsumohai[6]
           win_tile = TilesConverter.string_to_136_array(pin=self.calc_agari_pinzu)[0]
       if self.tsumohai[:3] == "sou":
           self.calc_agari_souzu += self.tsumohai[6]
           win_tile = TilesConverter.string_to_136_array(sou=self.calc_agari_souzu)[0]

       #アガリ形(man=マンズ, pin=ピンズ, sou=ソーズ, honors=字牌)
       tiles = TilesConverter.string_to_136_array(man=self.calc_manzu, pin=self.calc_pinzu, sou=self.calc_souzu, honors=self.calc_honors)
       #鳴き(なし)
       melds = None
       #ドラ(なし)
       dora_indicators = None
       #オプション(なし)
       config = None
       #HandCalculator(計算用クラス)のインスタンスを生成
       calculator = HandCalculator()
       result = calculator.estimate_hand_value(tiles, win_tile, melds, dora_indicators, config)
   
       # #翻数, 符数
       # print(result.han, result.fu)
       #点数(ツモアガリの場合[左:親失点, 右:子失点], ロンアガリの場合[左:放銃者失点, 右:0])
       print(result.cost['main'], result.cost['additional'])
       #役
       print(result.yaku)

class StartGame:
   players = {}
   playerNo = 0
   def haipai(self, MemberCount):
       for i in range(MemberCount):
           StartGame.players[i] = Janshi(i)
           haipai = StartGame.players[i].get_haipai(yamahai.yama)
           yamahai.get_dora()
           # print(players[i].syanten(haipai))
           # print(players[i].tehai)

   def tsumo(self, MemberCount):
       for i in range(MemberCount):
           playerNo = i
           self.players[playerNo].tsumo(yamahai.yama) 
       print(yamahai)


print("STR")
print()
ninzuu = 3
yamahai = Yamahai(ninzuu)
StartGame().haipai(ninzuu)
while len(yamahai.yama) > 3:
   StartGame().tsumo(ninzuu)
print()
print("END")

この記事が気に入ったらサポートをしてみませんか?