Python编写咋金花小程序
需求:
1. 先生成一付完整的扑克牌
2. 给5个玩家随机发牌
3. 统一开牌,比大小,输出赢家是谁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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]}' ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)