实现麻将牌发牌操作

import random


class Card:
    # 创建5个列表,总牌堆和四名玩家各一个牌堆
    cards = []
    player1 = []
    player2 = []
    player3 = []
    player4 = []

    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    # 使用类方法创建一副麻将牌(初始化牌堆)
    @classmethod
    def init_cards(cls):
        # 创建万字牌元祖
        wan = ("一万", "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万",
            "一万", "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万",
            "一万", "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万",
            "一万", "二万", "三万", "四万", "五万", "六万", "七万", "八万", "九万")
        # 创建筒字牌元祖
        tong = ("幺鸡", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",
               "幺鸡", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",
                "幺鸡", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",
                "幺鸡", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒",
                "幺鸡", "二筒", "三筒", "四筒", "五筒", "六筒", "七筒", "八筒", "九筒")
        # 创建条字牌元祖
        tiao = ("一条", "二条", "三条", "四条", "五条", "六条", "七条", "八条", "九条",
            "一条", "二条", "三条", "四条", "五条", "六条", "七条", "八条", "九条",
            "一条", "二条", "三条", "四条", "五条", "六条", "七条", "八条", "九条",
            "一条", "二条", "三条", "四条", "五条", "六条", "七条", "八条", "九条")
        # 创建风牌元祖
        wind = ("东风", "西风", "南风", "北风", "东风", "西风", "南风", "北风",
                "东风", "西风", "南风", "北风", "东风", "西风", "南风", "北风",
                "东风", "西风", "南风", "北风", "东风", "西风", "南风", "北风",
                "东风", "西风", "南风", "北风", "东风", "西风", "南风", "北风")
        word = ("红中", "发财", "白板", "红中", "发财", "白板",
             "红中", "发财", "白板", "红中", "发财", "白板",
             "红中", "发财", "白板", "红中", "发财", "白板",
             "红中", "发财", "白板", "红中", "发财", "白板")

        # 分别遍历5种花色牌,为所有136张牌创建单张牌对象,并添加到总牌堆列表中
        for wa in wan:
            a = Card(wa)
            cls.cards.append(a)
        for to in tong:
            b = Card(to)
            cls.cards.append(b)
        for ti in tiao:
            c = Card(ti)
            cls.cards.append(c)
        for wi in wind:
            d = Card(wi)
            cls.cards.append(d)
        for wo in word:
            e = Card(wo)
            cls.cards.append(e)

    # 展示总牌堆
    @classmethod
    def show_cards(cls):
        for card in cls.cards:
            print(card, end=" ")
        print()

    # 洗牌
    @classmethod
    def wash_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
    def send_cards(cls):
        # 发牌模式为每人轮流一次性拿4张牌,循环3次,最后每人轮流拿1张牌,每人拿到13张牌
        x = random.randint(2, 12)   # 生成随机数,完成掷色子的动作,从而决定拿牌位置
        for _ in range(3):
            # 每个玩家一次性拿4张牌
            for _ in range(4):
                cls.player1.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player2.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player3.append(cls.cards.pop(2 * x))
            for _ in range(4):
                cls.player4.append(cls.cards.pop(2 * x))
        # 每个玩家各拿1张牌
        cls.player1.append(cls.cards.pop(2 * x))
        cls.player2.append(cls.cards.pop(2 * x))
        cls.player3.append(cls.cards.pop(2 * x))
        cls.player4.append(cls.cards.pop(2 * x))

    @classmethod
    def show_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()

复制代码

你可能感兴趣的:(实现麻将牌发牌操作)