python用列表,元组,和类完成斗地主发牌游戏

博客仅提及运行结果,但未给出具体内容。推测可能围绕某程序或系统的运行结果展开,不过缺乏关键信息,难以进一步概括。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"""
斗地主
分析:
1.扑克牌作为对象呈现
2.创建未发牌的牌堆的列表
3.创建三个玩家牌堆的列表
4.创建底牌的元组
5.最原始的牌堆初始化,将54张牌加入到牌堆
6.创建洗牌操作
7.创建发牌操作
"""
import random
class Poke:
    pokes = []
    player1 = []
    player2 = []
    player3 = []
    last = None
    def __init__(self,flower,num):
        self.flower = flower
        self.num = num

    def __str__(self):
        return "%s%s" % (self.flower,self.num)

     #初始化牌
    @classmethod
    def init_pokes(cls):
        flowers = ("♠","♥","♦","♣")
        nums = ("1","2","3","4","5","6","7","8","9","10","J","Q","K")
        kings = {"big":"大王","small":"小王"}
        for flower_ in flowers:
            for num_ in nums:
                p = Poke(flower_,num_)
                cls.pokes.append(p)
        cls.pokes.append(Poke(kings["big"],""))
        cls.pokes.append(Poke(kings["small"],""))
    #洗牌
    @classmethod
    def wash(cls):
        #洗牌是从牌堆中找出一张固定的牌,与随机的一张牌交换位置
        #迭代牌堆,找出一张牌
        #产生随机数,作为被交换牌
        #交换
        for idx in range(54):
            idxx = random.randint(0,53)
            cls.pokes[idx],cls.pokes[idxx] = cls.pokes[idxx],cls.pokes[idx]

    #发牌
    @classmethod
    def send_poke(cls):
        for _ in range(0,17):
            cls.player1.append(cls.pokes.pop(0))
            cls.player2.append(cls.pokes.pop(0))
            cls.player3.append(cls.pokes.pop(0))
        #将剩余的三张牌做成底牌
        cls.last = tuple(cls.pokes)
    #临时方法:展示牌堆
    @classmethod
    def show(cls):
        for poke in cls.pokes:
            print(poke,end=" ")
        print()
    @classmethod
    def show_player(cls):
        print("玩家1",end = ":")
        for poke in cls.player1:
            print(poke,end = " ")
        print()
        print("玩家2", end=":")
        for poke in cls.player2:
            print(poke, end=" ")
        print()
        print("玩家3", end=":")
        for poke in cls.player3:
            print(poke, end=" ")
        print()
        print("底牌", end=":")
        for poke in cls.last:
            print(poke, end=" ")
        print()


Poke.init_pokes()
Poke.wash()
Poke.show()
Poke.send_poke()
Poke.show_player()

 

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值