用python语言实现斗地主基础版-案例

Talk is cheap, show me code!(屁话少说,放码过来)

"""

案例斗地主分析:

1.扑克牌作为对象呈现

2.创建未发牌的牌堆的列表

3.创建三个玩家牌堆的列表

4.创建底牌的元组

5.最原始的牌堆初始化,将54张牌加入到牌堆

6.创建洗牌操作

7.创建发牌操作

"""

import random

class Poke:

pokes = []
player1 = []
player2 = []
player3 = []
last = []

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):#初始化牌
    print("初始化牌堆:")
    flowers = ("♠", "♥", "♣", "♦")
    nums = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
    kings = {"big": "大王", "small": "小王"}

    for flowers_ in flowers:
        for num_ in nums:
            cls.pokes.append(Poke(flowers_, num_))
    cls.pokes.append(Poke(kings["big"], ""))
    cls.pokes.append(Poke(kings["small"], ""))

@classmethod
def wash(cls):#洗牌
    print("洗牌: ")
    for idx in range(54):
        idxx = random.randint(0, 53)
        Poke.pokes[idx], Poke.pokes[idxx] = Poke.pokes[idxx], Poke.pokes[idx]

@classmethod
def send(cls):#发牌
    for _ in range(0, 17):
        Poke.player1.append(cls.pokes.pop(0))
        Poke.player2.append(cls.pokes.pop(0))
        Poke.player3.append(cls.pokes.pop(0))
    Poke.last = tuple(Poke.pokes)

@classmethod#展示牌堆
def show(cls):
    for poke in Poke.pokes:
        print(poke, end=" ")
    print()

@classmethod
def show_player(cls):#展示玩家手里的牌和底牌
    print("玩家1的牌: ", end="")
    for poke in Poke.player1:
        print(poke, end=" ")
    print()

    print("玩家2的牌: ", end="")
    for poke in Poke.player2:
        print(poke, end=" ")
    print()

    print("玩家3的牌: ", end="")
    for poke in Poke.player3:
        print(poke, end=" ")
    print()

    print("底牌: ", end="")
    for poke in Poke.last:
        print(poke, end=" ")
    print()
复制代码

Poke.init_pokes()

Poke.show()

Poke.wash()

Poke.show()

Poke.wash()

Poke.show()

Poke.send()

Poke.show()

Poke.show_player()

转载于:https://juejin.im/post/5ca470b1e51d4564eb78d0c5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值