---
# System prepended metadata

title: 麻雀清一色待ち判定機

---

# 麻雀清一色待ち判定機

## コード

```python:main.py
#main.py
import haipai
import tiletxt 
import hantei
import sys
import time

if __name__=="__main__":
    while True:
        tehai = haipai.randhaipai()
        machi = hantei.machi(tehai)
        if machi.count(0) != 9:
            break
        break

    #tehai = [3,1,1,1,1,1,1,1,3]
    print("配牌を1~9の文字13個で構成してください。")
    txt = input()
    if len(txt) != 13:
        print("配牌が13牌ではありません。{}牌になっています。".format(len(txt)) )
        sys.exit()

    start = time.time()

    tehai = tiletxt.tiletxt_input(txt)
    machi = hantei.machi(tehai)

    tehai_txt = tiletxt.tiletxt(tehai)
    machi_normal_txt = tiletxt.tiletxt(machi[0])
    machi_imas_txt = tiletxt.tiletxt(machi[1])

    end = time.time()

    print("手牌　　　　: " + tehai_txt)
    print("普通待ち　　: " + machi_normal_txt)
    print("アイマス待ち: " + machi_imas_txt)
    print("実行時間　　: " + str(end-start))
```

```python:tiletxt.py
#tiletxt.py
def tiletxt(tehai):
    txt = ""
    for i in range(9):
        if tehai[i] > 0:
            txt += (str(i+1) * tehai[i])
    return txt

def tiletxt_input(txt):
    tehai = [0]*9
    for i in range(len(txt)):
        n = int(txt[i])
        tehai[n-1] += 1
    
    return tehai
```

```python:hantei.py
#hantei.py
import copy
def machi(tehai):
    mentsu_list_normal = []
    for i in range(9):
        mentsu = []
        for j in range(9):
            if j==i:
                mentsu.append(3)
            else:
                mentsu.append(0)
        mentsu_list_normal.append(mentsu)
    for i in range(7):
        mentsu = []
        for j in range(9):
            if j==i or j-1==i or j-2==i:
                mentsu.append(1)
            else:
                mentsu.append(0)
        mentsu_list_normal.append(mentsu)
    
    mentsu_list_imas = [
        [0,0,0,0,1,1,1,0,0], #765
        [0,0,0,0,0,1,1,1,0], #876
        [0,0,1,1,0,1,0,0,0], #346
        [1,0,1,0,1,0,0,0,0], #315
        [0,1,1,0,0,0,0,1,0], #283
        [1,0,0,0,0,1,0,0,1], #961 
    ]
    machihai_normal = []
    machihai_imas = []
    for i in range(9):
        #print("待ち牌" + str(i+1))
        machiflag = [0,0]
        tehai_tsumo = copy.copy(tehai)
        if tehai_tsumo[i] <= 3: #該当の持ち牌が3枚以下であることが条件 
            tehai_tsumo[i] = tehai_tsumo[i] + 1
        else:
            machihai_normal.append(0)
            continue
        
        if tehai_tsumo.count(2) == 7:
            machiflag[0] = 1
        if mentsukei(tehai_tsumo,mentsu_list_normal,0,4):
            machiflag[0] = 1
        if mentsukei(tehai_tsumo,mentsu_list_imas,0,4):
            machiflag[1] = 1
    
        
        machihai_normal.append(machiflag[0])
        machihai_imas.append(machiflag[1])
        
    return [machihai_normal,machihai_imas]
        

            
# tehai ... 14枚の手牌
# mentsu_list ...  面子リスト 刻子が[3,0,0,0,0,0,0,0,0]など 順子が[1,1,1,0,0,0,0,0,0]などの形で保管
# num ... 面子リストの何個目まで確認したか
# times ... 面子が何個分残っているか        
            
def mentsukei(tehai,mentsu_list,num,times):
    #print(str(tehai) + " num=" + str(num) +  " times=" + str(times))
    if times == 0 and tehai.count(2) == 1: #雀頭が残ってる つまり[2,0,0,0,0,0,0,0,0]とかだったらTrue
        return True
    elif times == 0:
        return False
    elif num == len(mentsu_list): #面子リスト
        return False

    mentsu = mentsu_list[num]
    flag_a = False
    if tehai_minus_hantei(tehai,mentsu): #tehai-mentsuをすることができるか？
        new_tehai = tehai_minus(tehai,mentsu)
        flag_a = mentsukei(new_tehai,mentsu_list,num,times-1)
    flag_b = mentsukei(tehai,mentsu_list,num+1,times)
        
    if flag_a or flag_b:
        return True
    """
    for mentsu in mentsu_list[num:]:
        if tehai_minus_hantei(tehai,mentsu): #tehai-mentsuをすることができるか？
            new_tehai = tehai_minus(tehai,mentsu)
            flag_a = mentsukei(new_tehai,mentsu_list,num,times-1)
            flag_b = mentsukei(tehai,mentsu_list,num+1,times)
            
            if flag_a or flag_b:
                return True

        num += 1
        
    return False
    """
    
    

def tehai_minus(tehai , mentsu):
    a=[]
    for i in range(9):
        a.append(tehai[i] - mentsu[i])
    return a

def tehai_minus_hantei(tehai , mentsu):
    flag = True
    for i in range(9):
        if tehai[i] < mentsu[i]:
            flag = False
    return flag



```

```python:haipai.py
#haipai.py
import random


def randhaipai():
    tehai = [0]*9
    for times in range(13):
        while True:
            x = random.randint(0, 8)
            if tehai[x] <= 3:
                tehai[x] += 1
                break
    return tehai

def randhaipai_shuffle():
    tehai = [0]*9
    randpai = list(range(9))*4
    for _ in range(13):
        n =random.randrange(len(randpai))
        tehai[randpai[n]] += 1
        del randpai[n]
    
    return tehai

```