Python3 模拟斗地主发牌

Python3 模拟斗地主发牌

案例斗地主

分析:
1.扑克牌作为对象呈现
2.创建未发牌的牌堆的列表
3.创建三个玩家牌堆的列表
4.创建底牌的元组
5.最原始的牌堆初始化,将54张牌加入到牌堆
6.创建洗牌操作
7.创建发牌操作

功能实现

import random
class Poke:
    poke = []  # 扑克牌牌堆
    p1 = []   # 玩家一牌堆
    p2 = []   #  玩家二牌堆
    p3 = []    #  玩家三牌堆
    last = None   # 底牌牌堆
    def __init__(self,f,num):      # 初始化牌堆
        self.flower = f     # 花色
        self.num = num    #  点数
    def __str__(self):
        return "%s%s" % (self.flower,self.num)     # 返回牌值
    @classmethod
    def init(cls):   # 定义牌堆
        ph = ("♥","♠","♣","♦")                    # 花色元组
        pnum = ("2","3","4","5","6","7","8","9","10","J","Q","K","A")  # 点数元组
        king = {"big":"大王","small":"小王"}        # 大小王
        for p in ph:                   # 循环遍历花色
            for _nump in pnum:    #  循环遍历点数
                cls.poke.append(Poke(p,_nump))  # 装牌
        cls.poke.append(Poke(king["big"],""))   # 装大王
        cls.poke.append(Poke(king["small"],""))  # 装小王
    @classmethod
    def wash(cls):     # 洗牌
        random.shuffle(cls.poke)
    @classmethod
    def send(cls):    #  发牌
        for _ in range(0,17): # 三个人每人发17张牌 循环
            cls.p1.append(cls.poke.pop(0))   # 玩家一发牌
            cls.p2.append(cls.poke.pop(0))
            cls.p3.append(cls.poke.pop(0))
        cls.last= tuple(cls.poke)            # 最后三张牌做底牌  不能修改做元组
    @classmethod
    def show(cls):    # 展示牌
        print("gamer1:")
        for pokes in cls.p1:
            print(pokes,end = " ")
        print()
        print("gamer2:")
        for pokes in cls.p2:
            print(pokes, end=" ")
        print()
        print("gamer3:")
        for pokes in cls.p3:
            print(pokes, end=" ")
        print()
        print("ending:")
        for pokes in cls.last:
            print(pokes, end=" ")
        print()
Poke.init()
Poke.wash()
Poke.send()     #  必须先发牌后才能展示牌
Poke.show()

你可能感兴趣的:(Python小实例)