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

お久しぶりです。ごんです!

Python の勉強をしたくなったので、麻雀のプログラムを組んでみたいと思います。ひどいプログラムだと思いますが、頑張ります。
参考にさせていただいている記事

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


class Jampai:
   def __init__(self, kind, value):
       self.kind = kind
       self.value = value

class Yamahai:
   def __init__(self):
       self.yama = []
       kind =("manzu", "pinzu", "souzu", "honors")
       kind_supai = (kind[0],kind[1], kind[2])
       value = range(4)
       value_supai = range(9)
       value_jihai = range(7)
       for i in kind_supai:
           for j in value_supai:
               for k in value:
                   self.yama.append(i + "_" + str(j + 1))
       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))

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

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

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

   def riipai(self, tehai):
       self.calc_manzu = ""
       self.calc_pinzu = ""
       self.calc_souzu = ""
       self.calc_honors = ""
       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]
       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 str(shanten.calculate_shanten(tiles)) + "シャンテンです."

   def tsumo(self, yama):
       tsumohai = yama[0]
       del yama[0]
       self.tehai.append(tsumohai)
       self.tehai = self.riipai(self.tehai)
       if (self.name == "player0"):
           print("ツモ:"+tsumohai)
           self.sute2()
           print(self.syanten(self.tehai))
       else:
           self.sute()

   def sute(self):
       sutehai = self.tehai[13]
       del self.tehai[13]
       self.kawa.append(sutehai)
       # print(self)
       # print(self.tehai)

   def sute2(self):
       print(self.tehai)
       message ="捨てる牌を選んでください"
       sutehaiflg = False
       i = 0
       while (i < 5):
           sentakuhai = input(message)
           if sentakuhai in self.tehai:
               sutehai_position = self.tehai.index(sentakuhai)
               sutehaiflg = True
               break
           i += 1 
       if not (sutehaiflg):
           self.sute()
       sutehai = self.tehai[sutehai_position]
       del self.tehai[sutehai_position]
       self.kawa.append(sutehai)
       # print(self)
       # print(self.tehai)
       print("{}の手配:{}".format(self, self.tehai))


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

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


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



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