案例:鬥地主——發牌案例
""" 案例鬥地主 分析: 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()
複製代碼