需求:
1. 先生成一付完整的扑克牌
2. 给5个玩家随机发牌
3. 统一开牌,比大小,输出赢家是谁
import random
# 生成扑克牌
list01 = ['红桃', '黑桃', '梅花', '方片']
list02 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
cards = []
for a in list01:
for b in list02:
card = [f'{a}', b]
cards.append(card)
# print(cards)
# 给玩家随机发牌
players = ['张三', '李四', '赵五', '宋六', '田七']
dic_score = {}
for player in players:
pcards = random.sample(cards, 3)
t = [pcards[0][1], pcards[1][1], pcards[2][1]]
t.sort()
for pcard in pcards:
cards.remove(pcard)
# 判断牌型 比较大小 这几种牌的大小顺序为, 豹子 > 同花顺 > 同花 > 顺子 > 对子 > 单张
if t[0] == t[1] == t[2]: # 三张牌大小一样
card_list = '豹子' # 最小豹子222 大于 204879
score_coe = 10000
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
elif pcards[0][0] == pcards[1][0] == pcards[2][0]: # 三张牌花色以致
if t[0] + 1 == t[1] and t[1] + 1 == t[2]: # 三张牌大小按顺序
card_list = '同花顺' # 最小同花顺234 43coe +2 > 57539 coe = 1339
score_coe = 1339 # 最大同花顺QKA 153coe+12 = 204879
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
else:
card_list = '同花' # 最小同花235 53coe+2 > 19902 所以coe = 376
score_coe = 376 # 最大同花AKJ=153coe + 11 =57539
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
elif t[0] + 1 == t[1] and t[1] + 1 == t[2]:
card_list = '顺子' # 最小顺子234= 43coe+2 > 最大对子AAK=5557,coe = 130
score_coe = 130 # 最大顺子为QKA = 140*130 + 13*130 + 12 = 19902 分
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
elif t[0] == t[1] or t[1] == t[2]:
card_list = '对子' # 最小为对2一个3, 22coe + 3 >J k A= 140*5 + 13*5 +11 = 776 得coe = 36 最大AAK = 5557
score_coe = 36
if t[0] == t[1]:
score = t[0] * score_coe * 10 + t[1] * score_coe + t[2]
else:
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
else:
card_list = '单张' # A42 要大于 KQ10 140coe +4coe+2 >130coe + 12coe + 10 --- coe > 4
score_coe = 5
score = t[0] + t[1] * score_coe + t[2] * score_coe * 10
for i in range(len(pcards)): # 将卡牌11,12,13,14替换成卡牌J、Q、K、A
if 11 == pcards[i][1]:
pcards[i][1] = 'J'
elif 12 == pcards[i][1]:
pcards[i][1] = 'Q'
elif 13 == pcards[i][1]:
pcards[i][1] = 'K'
elif 14 == pcards[i][1]:
pcards[i][1] = 'A'
else:
pcards[i][1] = pcards[i][1]
print(f'{player}的牌为:{pcards},牌型为:{card_list},分数为:{score}')
dic_score[player] = score
a = zip(dic_score.values(), dic_score.keys())
print(f'本次胜出的为{max(a)[1]}')