實現麻將牌發牌操作

2021-09-24 08:02:08 字數 3266 閱讀 9795

import random

class

card:

# 建立5個列表,總牌堆和四名玩家各乙個牌堆

cards =

player1 =

player2 =

player3 =

player4 =

def__init__

(self, name):

self.name = name

def__str__

(self):

return self.name

# 使用類方法建立一副麻將牌(初始化牌堆)

@classmethod

definit_cards

(cls):

# 建立萬字牌元祖

wan = ("一萬", "二萬", "三萬", "四萬", "五萬", "六萬", "七萬", "八萬", "九萬",

"一萬", "二萬", "三萬", "四萬", "五萬", "六萬", "七萬", "八萬", "九萬",

"一萬", "二萬", "三萬", "四萬", "五萬", "六萬", "七萬", "八萬", "九萬",

"一萬", "二萬", "三萬", "四萬", "五萬", "六萬", "七萬", "八萬", "九萬")

# 建立筒字牌元祖

tong = ("么雞", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",

"么雞", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",

"么雞", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",

"么雞", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",

"么雞", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒")

# 建立條字牌元祖

tiao = ("一條", "二條", "三條", "四條", "五條", "六條", "七條", "八條", "九條",

"一條", "二條", "三條", "四條", "五條", "六條", "七條", "八條", "九條",

"一條", "二條", "三條", "四條", "五條", "六條", "七條", "八條", "九條",

"一條", "二條", "三條", "四條", "五條", "六條", "七條", "八條", "九條")

# 建立風牌元祖

wind = ("東風", "西風", "南風", "北風", "東風", "西風", "南風", "北風",

"東風", "西風", "南風", "北風", "東風", "西風", "南風", "北風",

"東風", "西風", "南風", "北風", "東風", "西風", "南風", "北風",

"東風", "西風", "南風", "北風", "東風", "西風", "南風", "北風")

word = ("紅中", "發財", "白板", "紅中", "發財", "白板",

"紅中", "發財", "白板", "紅中", "發財", "白板",

"紅中", "發財", "白板", "紅中", "發財", "白板",

"紅中", "發財", "白板", "紅中", "發財", "白板")

# 分別遍歷5種花色牌,為所有136張牌建立單張牌物件,並新增到總牌堆列表中

for wa in wan:

a = card(wa)

for to in tong:

b = card(to)

for ti in tiao:

c = card(ti)

for wi in wind:

d = card(wi)

for wo in word:

e = card(wo)

# 展示總牌堆

@classmethod

defshow_cards

(cls):

for card in cls.cards:

print(card, end=" ")

print()

# 洗牌

@classmethod

defwash_cards

(cls):

idxx = random.randint(1, 136)

for idx in range(136):

cls.cards[idx], cls.cards[idxx] = cls.cards[idxx], cls.cards[idx]

@classmethod

defsend_cards

(cls):

# 發牌模式為每人輪流一次性拿4張牌,迴圈3次,最後每人輪流拿1張牌,每人拿到13張牌

x = random.randint(2, 12) # 生成隨機數,完成擲色子的動作,從而決定拿牌位置

for _ in range(3):

# 每個玩家一次性拿4張牌

for _ in range(4):

for _ in range(4):

for _ in range(4):

for _ in range(4):

# 每個玩家各拿1張牌

@classmethod

defshow_player

(cls):

print("玩家一:", end="")

for card in cls.player1:

print(card, end=" ")

print()

print("玩家二:", end="")

for card in cls.player2:

print(card, end=" ")

print()

print("玩家三:", end="")

for card in cls.player3:

print(card, end=" ")

print()

print("玩家四:", end="")

for card in cls.player4:

print(card, end=" ")

print()

card.init_cards()

card.show_cards()

card.wash_cards()

card.show_cards()

card.send_cards()

card.show_player()

複製**

麻將牌和牌問題

在知乎上看到乙個問題,如何用程式判斷麻將牌是否和牌。和牌的規則為 14張麻將牌當中,必須要有乙個對子,即兩張相同的牌,除去對子後,剩下的牌可以組合成 a,a,a 或者 a,a 1,a 2 兩種模式的組合,其中a為某種花色的某張牌。我的解法是 首先定義一下麻將牌在程式中的表示方式 把萬,餅,條分別用連...

Python撲克牌發牌(用類實現)

2.題解 編寫程式,4名牌手打牌,計算機隨機將52張牌 不含大小鬼 發給4名牌手,在螢幕上顯示每位牌手的牌。設計出3個類 card類 hand類和poke類。card類代表一張牌,其中facenum欄位指出是牌面數字1 13,suit欄位指出的是花色,值 梅 為梅花,方 為方塊,紅 為紅心,黑 為黑...

python3 實現麻將胡牌問題

題目描述 清一色是麻將番種之一,指由一種花色的序數牌組成的和牌.數字1 9,每個數字最多有4張牌 我們不考慮具體花色,我們只看數字組合。刻子 三張一樣的牌 如 111,222,333,999 順子 三張連續的牌 如 123,234,345,789 對子 兩張相同的牌 如 11,22,33,99 需要...