---
# System prepended metadata

title: 程式設計(一) 第十二週 共筆

---

程式設計(一) 第十二週 共筆
===

## 麻將規則

- 萬 1~9
- 筒 1~9
- 條 1~9
- 字牌 東南西北中發白
- 兩個一樣的稱為 "將"
- 三個一樣 "刻"
- 萬筒條連續三張的 稱為 "順"

:::success
勝利條件 若干組「刻、順」加上一組「將」即為“胡牌”
:::

## 上課程式

```python=
# give 3n+2 card, check if can hu
# eg, 1112345678999, 胡了嗎?
# card: number of each card, 萬9筒9條9字7, 一共34種牌
# n: card裡面一共有幾張牌
# needMJ: 需要選一對當MJ?
def canHu(card, n, needMJ = True) :
    if n == 0 :
        return True
    if needMJ == True :
        for i in range(0, 34) :
            if card[i] >= 2 :
                card[i] -= 2
                ans = canHu(card, n - 2, False)
                card[i] += 2
                if ans == True :
                    return True
    # check 3 cards
    for i in range(0, 34) :
        if card[i] >= 3 :
            card[i] -= 3
            ans = canHu(card, n - 3, needMJ)
            card[i] += 3
            if ans == True :
                return True
        if i < 27 and i % 9 < 7 and card[i] > 0 and card[i+1] > 0 and card[i+2] > 0 :
            card[i] -= 1
            card[i+1] -= 1
            card[i+2] -= 1
            ans = canHu(card, n - 3, needMJ)
            card[i] += 1
            card[i+1] += 1
            card[i+2] += 1
            if ans == True :
                return True
    return False
def main() :
    card = [4,1,1,1,1,1,1,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    print(canHu(card,14))
main()
```


送大家寫功課的好東西
```python=
lis =['一萬','二萬','三萬','四萬','五萬','六萬','七萬','八萬','九萬','一筒','二筒','三筒','四筒','五筒','六筒','七筒','八筒','九筒','一條','二條','三條','四條','五條','六條','七條','八條','九條','東','南','西','北','中','發','白']
``

這樣就不用自己打了