from random import randint, random, randrange, shuffle, choice,choices
# 1.写一个函数实现斗地主发牌功能
def deal_cards():
# 1)准备一副牌
colors = ['♥', '♦', '♠', '♣']
nums = list(range(2,11)) + ['J', 'Q', 'K', 'A']
pokers = []
for num in nums:
for c in colors:
pokers.append(f'{c}{num}')
pokers.extend(['joker', 'JOKER'])
# 2)洗牌
# shuffle(列表) - 将列表中的元素随机打乱顺序
shuffle(pokers)
# 3)发牌
player1 = []
player2 = []
player3 = []
pokers = iter(pokers)
for _ in range(17):
player1.append(next(pokers))
player2.append(next(pokers))
player3.append(next(pokers))
# 4)理牌
table = {'J': 11, 'Q': 12, 'K': 13, 'A':14, '2': 15, 'oker': 16, 'OKER': 17}
table.update({str(x):x for x in range(3, 11)})
player1.sort(key=lambda item: table[item[1:]], reverse=True)
player2.sort(key=lambda item: table[item[1:]], reverse=True)
player3.sort(key=lambda item: table[item[1:]], reverse=True)
return player1, player2, player3, list(pokers)
p1, p2, p3, di = deal_cards()
print(p1)
print(p2)
print(p3)
print(di)
斗地主发牌功能
最新推荐文章于 2024-08-29 15:58:04 发布