案例:斗地主——发牌案例
"""
案例斗地主
分析:
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 = ("2","3","4","5","6","7","8","9","10","J","Q","K","A")
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 a in range(54):
b = random.randint(0,53)
cls.pokes[a],cls.pokes[b] = cls.pokes[b],cls.pokes[a]
@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.show()
Poke.wash()
Poke.show()
Poke.send_poke()
Poke.show_player()
复制代码