本篇文章為大家展示了使用python怎么編寫一個(gè)扎金花小程序,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站專注于大興企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,購(gòu)物商城網(wǎng)站建設(shè)。大興網(wǎng)站建設(shè)公司,為大興等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)先生成一付完整的撲克牌
給5個(gè)玩家隨機(jī)發(fā)牌
統(tǒng)一開(kāi)牌,比大小,輸出贏家是誰(shuí)
主要思路:
把各種牌用積分來(lái)計(jì)算,最后加上三張牌的基礎(chǔ)積分。即:最終積分=基礎(chǔ)積分+牌型積分。最后比較最紅積分,誰(shuí)的積分大,誰(shuí)排名就靠前。 附上源碼:
# -*- coding: utf-8 -*- """ # @Time : 2021-01-07 14:55 # @author : jianwei # @Software : PyCharm """ import random import operator puke = [] # 存儲(chǔ)撲克牌 num_list = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] hua_list = ['梅花', '紅桃', '黑桃', '方塊'] sotr_dic = {'2': 0, '3': 1, '4': 2, '5': 3, '6': 4, '7': 5, '8': 6, '9': 7, '10': 8, 'J': 9, 'Q': 10, 'K': 11, 'A': 12, '對(duì)子': 15, '順子': 30, '順金': 60, '豹子': 100} count_new_list = [] # 存儲(chǔ)玩家分?jǐn)?shù)和排序后排名 count_dic = {} # 存儲(chǔ)玩家分?jǐn)?shù) # 準(zhǔn)備52張撲克 for hua in hua_list: for num in num_list: a = hua + num puke.append(a) player_dic = {'玩家1': [], '玩家2': [], '玩家3': [], '玩家4': [], '玩家5': []} # 隨機(jī)給五個(gè)玩家發(fā)牌 # print(len(puke)) for key, value in player_dic.items(): for i in range(3): plate = random.sample(puke, 3) player_dic[key] = plate for i in plate: puke.remove(i) # print(player_dic) # 獲取玩家的牌型 def paixing(list1): num = [] huase = [] for i in list1: a = i[2:] b = i[:2] num.append(a) huase.append(b) return num, huase # 對(duì)數(shù)字的牌型進(jìn)行排序 def sotr(num): new_num = [] sort_list2 = [] list1 = [] for i in num: new_num.append(sotr_dic[i]) new_num = sorted(new_num) for new in new_num: sort_list2.append([k for k, v in sotr_dic.items() if v == new]) for m in sort_list2: for n in m: list1.append(n) return list1 # 對(duì)玩家的牌形統(tǒng)計(jì)分?jǐn)?shù) def count(num, huase): a = 0 base_count = sotr_dic[num[0]] + sotr_dic[num[1]] + sotr_dic[num[2]] if num[0] == num[1] and num[1] == num[2]: paixing = '豹子' a = base_count + sotr_dic[paixing] # print(paixing, a) elif (sotr_dic[num[0]] + 1 == sotr_dic[num[1]] and sotr_dic[num[2]] - 1 == sotr_dic[num[1]]) and (huase[0] == huase[ 1] and huase[1] == huase[2]): paixing = '順金' a = base_count + sotr_dic[paixing] # print(paixing, a) elif (sotr_dic[num[0]] + 1 == sotr_dic[num[1]]) and (sotr_dic[num[2]] - 1 == sotr_dic[num[1]]) and ( huase[0] != huase[ 1] or huase[1] != huase[2]): paixing = '順子' a = base_count + sotr_dic[paixing] # print(paixing, a) elif (num[0] == num[1] and num[1] != num[2]) or (num[1] == num[2] and num[0] != num[1]) or ( num[0] == num[2] and num[1] != num[0]): paixing = '對(duì)子' a = base_count + sotr_dic[paixing] # print(paixing, a) else: a = base_count return a # 對(duì)存儲(chǔ)玩家分?jǐn)?shù)的字典進(jìn)行排序 def compare(count_dic): d = list(zip(count_dic.values(), count_dic.keys())) return sorted(d, reverse=True) for key, value in player_dic.items(): # print(key,value) num, huase = paixing(value) # print(num,huase) num = sotr(num) # print(num, huase) count1 = count(num, huase) # print(count1) count_dic[key] = count1 print(key + "的牌為:" + str(value)) count_new_list = compare(count_dic) # print(count_new_list) print('最終排名:' + "\t" + count_new_list[0][1] + "第一名" + "\t" + count_new_list[1][1] + "第二名" + "\t" + count_new_list[2][ 1] + "第三名" + "\t" + count_new_list[3][1] + "第四名" + "\t" + count_new_list[4][1] + "第五名")
上述內(nèi)容就是使用python怎么編寫一個(gè)扎金花小程序,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。