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_poke(cls):
flowers = ("♠", "♥", "♣", "♦")
nums = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
kings = {"big": "大王", "small": "小王"}
for f in flowers:
for n in nums:
p = Poke(f, n)
cls.pokes.append(p)
cls.pokes.append(kings["big"])
cls.pokes.append(kings["small"])
# 洗牌 第一种方法 从牌的第一张开始,随机生成一张互换位置,相当于互换54次
@classmethod
def wash_poke(cls):
for i in range(53):
num1 = random.randint(0, 53)
Poke.pokes, Poke.pokes[num1] = Poke.pokes[num1], Poke.pokes
# 洗牌 第二种方法 牌分成两部分,第一部分的选择一张随机插入到后面一部分
@classmethod
def wash1_poke(cls):
for i in range(28, 54):
Poke.pokes.insert(i, Poke.pokes.pop(0))
# 发牌
@classmethod
def send_poke(cls):
while True:
if len(Poke.pokes) <= 3:
break
Poke.player1.append(Poke.pokes.pop(0))
Poke.player2.append(Poke.pokes.pop(0))
Poke.player3.append(Poke.pokes.pop(0))
Poke.last = tuple(Poke.pokes) # 因为底牌不变,做成元组会比较好一点
# 显示牌的序列状态,在没有发牌以前临时使用
@classmethod
def show(cls):
for a in cls.pokes:
print(a, end="\t")
print()
# 显示玩家以及底牌
@classmethod
def player_show(cls):
print("第一位玩家的牌是:", end="")
for a in cls.player1:
print(a, end="\t")
print()
print("第二位玩家的牌是:", end="")
for a in cls.player2:
print(a, end="\t")
print()
print("第三位玩家的牌是:", end="")
for a in cls.player3:
print(a, end="\t")
print()
print("最后的底牌是:", end="")
for a in cls.last:
print(a, end="\t")
print()
Poke.init_poke()
Poke.show()
# Poke.wash_poke() # 第一种洗牌方式
Poke.wash1_poke() # 第二种洗牌方式
Poke.show()
Poke.send_poke()
Poke.player_show()
|
|