大家好,欢迎来到IT知识分享网。
python – 扎金花游戏
话不多说,直接上代码
import random from faker import Faker Aiosle = Faker(locale='zh_CN') color = ['♠', '♦', '♥', '♣'] num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K','A'] num_peo = int(input("请输入参与游戏的人数:")) #扑克牌生成函数 def card(color,num): card = [] for i in color: count = 2 for j in num: cards = [f"{
i}{
j}", count] count+=1 card.append(cards) random.shuffle(card) return card #向每个用户发三张牌 def card_for_peo(card): peo_card = {
} person = [] #存放玩家姓名的列表 # num_cards = [] #存放发出的牌的列表 for k in range(1,num_peo+1): person.append(Aiosle.name()) for peo in person: brand = random.sample(card,3) for b in brand: card.remove(b) # num_cards.append(brand) peo_card[peo]=brand return peo_card #三张牌排序 def card_sort(lis): l = len(lis) # 冒泡排序 for i in range(0, l): for j in range(0, l - i - 1): if (lis[j][1] > lis[j + 1][1]): lis[j], lis[j + 1] = lis[j + 1], lis[j] return lis # 单牌比大小 def card_one_score(p_card,score): p_card = card_sort(p_card) weight_val = [0.1,1,10] count = 0 for card in p_card: score += card[1] * weight_val[count] count +=1 # print("计算单牌",p_card,score) return score #对子比大小 def card_double_score(p_card,score): p_card = card_sort(p_card) card_val = [i[1] for i in p_card] if len(set(card_val)) == 2: if card_val[0] == card_val[1]: score = (card_val[0]+card_val[1]) *50 + card_val[2] else: score = (card_val[1]+card_val[2]) *50 + card_val[0] return score #顺子比大小 def card_even_score(p_card,score): p_card = card_sort(p_card) card_val = [i[1] for i in p_card] if card_val[1]-card_val[0]==1 and card_val[2]-card_val[1]==1: score = score*100 else: score = score return score #同花 def card_samecolor_score(p_card,score): color_set = {
i[0][0] for i in p_card} if len(color_set) == 1: score*1000 else: score=score return score #同花顺比大小 def card_Flush_score(p_card,score): p_card = card_sort(p_card) card_val = [i[1] for i in p_card] color_set =[i[0][0] for i in p_card] if len(set(color_set)) == 1: if card_val[0] + card_val[2] == 2 * card_val[1]: score *= 0.1 return score #豹子比大小 def card_leopard_score(p_card,score): p_card = card_sort(p_card) card_val = {
i[1] for i in p_card} if len(card_val) == 1: score *=10000 return score score = 0 #生成扑克牌 card=card(color,num) #发牌 peo_card = card_for_peo(card) calc_orders = [ card_one_score, card_double_score, card_even_score, card_samecolor_score, card_Flush_score, card_leopard_score ] peo_score = [] for p_name,p_card in peo_card.items(): score = 0 for fun in calc_orders: score = fun(p_card,score) peo_score.append([p_name,p_card,score]) for i in peo_score: print(i,'\n') winner = sorted(peo_score,key=lambda x:x[2])[-1] print(f"赢家是{
winner}!")
运行结果:
请输入参与游戏的人数:3 ['李云', [['♦6', 6], ['♠6', 6], ['♠J', 11]], 611] ['叶金凤', [['♦4', 4], ['♥6', 6], ['♣10', 10]], 106.4] ['张阳', [['♠7', 7], ['♠10', 10], ['♦Q', 12]], 130.7] 赢家是['李云', [['♦6', 6], ['♠6', 6], ['♠J', 11]], 611]! Process finished with exit code 0
第一次写,欢迎各位大佬指出不足!!!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/106512.html