目录
- 前言
- 一、思路
- 二、解决方案
- 三、总结
前言
《诈金花》又叫三张牌,是在全国广泛流传的一种民间多人纸牌游戏。比如JJ比赛中的诈金花(赢三张),具有独特的比牌规则。游戏过程中需要考验玩家的胆略和智慧。--《百度百科》 前几天在交流群里边,有个叫【^-^】的粉丝分享了一道扑克牌诈金花的题目,要求用python实现,题目如下:
自己写一个程序,实现发牌、比大小判断输赢。
游戏规则:
一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。
有以下几种牌:
豹子:三张一样的牌,如3张6.
顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7
顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子
对子:2张牌一样
单张:单张最大的是A
这几种牌的大小顺序为, 豹子>顺金>顺子>对子>单张
一、思路
针对这个问题,首先需要构造一副扑克牌,根据扑克牌的属性特征,进行不断的拆解,然后构造玩家用户,这里经常会用到字典和列表来存储信息,屡试不爽。
二、解决方案
针对该问题,粉丝【^-^】给出了解决方法,直接上代码如下:
# -*- coding: utf-8 -*- import random puke = [] # 存储扑克牌 num_list = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] hua_list = ['梅花', '红桃', '黑桃', '方块'] sort_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, '对子': 15, '顺子': 30, '顺金': 60, '豹子': 100} count_new_list = [] # 存储玩家分数和排序后排名 count_dic = {} # 存储玩家分数 # 准备52张扑克 for hua in hua_list: for num in num_list: a = hua + num puke.append(a) player_dic = {'玩家1': [], '玩家2': [], '玩家3': [], '玩家4': [], '玩家5': []} # 随机给五个玩家发牌 # 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 # sort_dic = {'2': 0, '3': 1, '4': 2, '5': 3, '6': 4} # 对数字编程客栈的牌型进行排序 def sort(num): new_num = [] sort_list2 = [] list1 = [] for i in num: new_num.append(sort_dic[i]) new_num = sorted(new_num) # 排序后是[2, 4, 7] for new in new_num: sort_list2.append([k for k, v in sort_dic.items() if v == new]) for m in sort_list2: for n in m: list1.append(n) return list1 # 对玩家的牌形统计分数 def count(num, huase): a = 0 base_count = sort_dic[num[0]] + sort_dic[num[1]] + sort_dic[num[2]] if num[0] == num[1] and num[1] == num[2]: paixing = '豹子' a = base_count + sort_dic[paixing] elif (sort_dic[num[0]] + 1 == sort_dic[num[1]] and sort_dic[num[2]] - 1 == sort_dic[num[1]]) and (huase[0] == huase[ 1] and huase[1] == huase[2]): paixing = '顺金' a = base_count + sort_dic[paixing] elif (sort_dic[num[0]] + 1 == sort_dic[num[1]]) and (sort_dic[num[2]] - 1 == sort_dic[num[1]]) and ( huase[0] != huase[ 1] or huase[1] != huase[2]): paixing = '顺子' a = base_count + sort_dic[paixing] elif (num[0] == num[1] and num[1] != num[2]) or (num[1] == num[2] and num[0] != num[1]) or ( num[gKDbfK0] == num[2] and num[1] != num[0]): paixing = '对子' a = base_count + sort_dic[paixing] else: a = base_count return a # 对存储玩家分数的字典进行排序 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(): num, huase = paixing(value) num = sort(num) count1 = count(num, huase) 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] + "第五名")
代码看上去确实挺多的,超过了100行,需要花点时间去读,不过涉及的知识点并不复杂,基本上有点Python基础,也可以理解。代码运行之后,可以看到效果如下:
不过后来我在读取这份代码的时候,发现中间有个地方写的着实有些冗余,稍微修改下,代码方面简洁一些,一些函数和变量命名加了一些对应的现实意义的单词,可读性强了一丢丢,代码如下:
# -*- coding: utf-8 -*- import random puke = [] # 存储扑克牌 num_list = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] hua_list = ['梅花', '红桃', '黑桃', '方块'] sort_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, '对子': 15, '顺子': 30, '顺金': 60, '豹子': 100} count_new_list = [] # 存储玩家分数和排序后排名 count_dic = {} # 存储玩家分数 # 准备52张扑克 for hua in hua_list: for num in num_list: a = hua + num puke.append(a) player_dic = {'玩家1': [], '玩家2': [], '玩家3': [], '玩家4': [], '玩家5': []} # 随机给五个玩家发牌 print(len(puke)) for key, value in player_dic.items(): for i in range(3): plate = random.sample(puke, 3) player_dic[key] = platewww.cppcns.com for i in plate: puke.remove(i) print(player_dic) # 获取玩家的牌型 def paixing(list1): num = [] huase = [] for data in list1: huase_type = data[:2] pai_number = data[2:] num.append(pai_number) huase.append(huase_type) return num, huase # 对玩家的牌形统计分数 def get_score(num, huase): base_count = sort_dic[num[0]] + sort_dic[num[1]] + sort_dic[num[2]] if num[0] == num[1] and num[1] == num[2]: paixing = '豹子' score = base_count + sort_dic[paixing] elif (sort_dic[num[0]] + 1 == sort_dic[num[1]] and sort_dic[num[2]] - 1 == sort_dic[num[1]]) and (huase[0] == huase[ 1] and huase[1] == huase[2]): paixing = '顺金' score = base_count + sort_dic[paixing] elif (sort_dic[num[0]] + 1 == sort_dic[num[1]]) and (sort_dic[num[2]] - 1 == sort_dic[num[1]]) and ( huase[0] != huase[ 1] or huase[1] != huase[2]): paixing = '顺子' score = base_count + sort_dic[paixing] 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 = '对子' score = base_count + sort_dic[paixing] else: score = base_count return score if __name__ == '__main__': for key, value in player_dic.items(): num, huase = paixing(value) # 对数字的牌型进行排序 num = sorted(num) score = get_score(num, huase) count_dic[key] = score print(key + "的牌为:" + str(value)) # 对存储玩家分数的字典进行排序 count_new_list = sorted(zip(count_dic.values(), count_dic.keys()), reverse=True) print("最终排名:") for i in range(len(count_new_list)): print(count_new_list[i][1] + '\t', end='')
三、总结
我是Python进阶者。本文实际生活中的诈金花游戏,基于Python编程,使用Python基础知识中的列表、字典、函数等,实现了在线诈金花的过程。
到此这篇关于基于Python制作炸金花游戏的过程详解的文章就介绍到这了,更多相关Python炸金花游戏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论