python实现打扑克方法
# 游戏规则:
# 一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。
#
# 有以下几种牌:
# 豹子:三张一样的牌,如3张6.
# 同花顺:即3张同样花色的顺子, 如红桃 5、6、7
# 顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子
# 对子:2张牌一样
# 单张:单张最大的是A
# 这几种牌的大小顺序为, 豹子>同花顺>同花>顺子>对子>单张
# 需程序实现的点:
# 1. 先生成一付完整的扑克牌
# 2. 给5个玩家随机发牌
# 3. 统一开牌,比大小,输出赢家是谁
# 3.1 单牌之间如何比较大小 (权重)
# 3.2 其他牌型如何比较大小
# A:红桃J 红桃K 黑桃A 1.1 13 140 = 154.1
#
# B:方片2 方片2 梅花3 0.2 2 30 = 32.2*5 = 161
# 3.3 判断玩家手里是什么牌(不同的牌型有不同的判断方法)高内聚 低耦合
# val = [2, 2, 3] # SET
# if len(set(val)) == 2: # {2, 3}
# print("对子")
#
# if len(set(val)) == 1:
# print("豹子")
# 代码实现
import random
# 1. 生成牌
def alex():
poke_types = ['', '', '', '']
poke_nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
poke_list = []
for p_type in poke_types:
count = 2
for p_num in poke_nums:
card = [f"{p_type}{p_num}", count]
poke_list.append(card)
count += 1
# print(poke_list)
return poke_list
pokeList = alex()
# 2. 发牌
players = ['木木', '鬼鬼', '神经蛤蟆', 'error', '裕', '感叹号']
def blackGirl(pl, pk, pn):
player_dic = {}
for p_name in pl:
p_cards = random.sample(pk, pn)
for card in p_cards:
pk.remove(card)
player_dic[p_name] = p_cards
print(f"为玩家【{p_name}】生成了牌:{p_cards}")
return player_dic
playerDic = blackGirl(players, pokeList, 3)
# 3. 写好每种牌型的判断函数
# 冒泡排序
def sortList(dataList):
length = len(dataList)
for i in range(length):
for j in range(length - i - 1):
if dataList[j][1] > dataList[j + 1][1]:
dataList[j], dataList[j + 1] = dataList[j + 1], dataList[j]
return dataList
# 单张
def calculate_single(p_cards, score):
# 初始化
# score = 0
# 算分 0.1 1 10
# 先排序
p_cards = sortList(p_cards)
weight_val = [0.1, 1, 10]
count = 0
for card in p_cards:
score += card[1] * weight_val[count]
count += 1
print(f"计算单牌的结果是:{score}")
return score
# 对子
def calculate_pair(p_cards, score):
p_cards = sortList(p_cards)
# 列表推导式
card_val = [i[1] for i in p_cards]
if len(set(card_val)) == 2:
if card_val[0] == card_val[1]: # aab
score = (card_val[0] + card_val[1]) * 50 + card_val[2]
else: # abb
score = (card_val[1] + card_val[2]) * 50 + card_val[0]
print(f"计算对子的结果是:{score}")
return score
# 顺子
def calculate_straight(p_cards, score):
p_cards = sortList(p_cards)
card_val = [i[1] for i in p_cards]
a, b, c = card_val
if b - a == 1 and c - b == 1:
score *= 100
print(f"计算顺子的结果是:{score}")
return score
# 同花
def calculate_same_color(p_cards, score):
color_val = [i[0][0] for i in p_cards]
if len(set(color_val)) == 1:
score *= 1000
print(f"计算同花的结果是:{score}")
return score
# 同花顺
def calculate_same_color_straight(p_cards, score):
# 同花
color_val = [i[0][0] for i in p_cards]
if len(set(color_val)) == 1:
# 顺子
p_cards = sortList(p_cards)
card_val = [i[1] for i in p_cards]
a, b, c = card_val
if b - a == 1 and c - b == 1:
score *= 0.1
print(f"计算同花顺的结果是:{score}")
return score
# 豹子
def calculate_leopard(p_cards, score):
card_val = {i[1] for i in p_cards}
if len(card_val) == 1:
score *= 100000
print(f"计算豹子的结果是:{score}")
return score
# 4. 比对
calc_func_orders = [
calculate_single,
calculate_pair,
calculate_straight,
calculate_same_color,
calculate_same_color_straight,
calculate_leopard
]
player_score = []
for p_name, p_cards in playerDic.items():
print(f"开始计算玩家【{p_name}】的牌:{p_cards}")
score = 0
for calc_func in calc_func_orders:
score = calc_func(p_cards, score)
player_score.append([p_name, score])
winner = sortList(player_score)[-1]
print(f"恭喜最后获胜的玩家是【{winner[0]}】,得分是:{winner[1]}")
python实现打扑克方法的更多相关文章
- python 类属性与方法
Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...
- Python执行系统命令的方法 os.system(),os.popen(),commands
os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html 1.__init__() 创建对 ...
- python 字典内置方法get应用
python字典内置方法get应用,如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这 ...
- [转] python程序的调试方法
qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...
- Python prettytable的使用方法
Python prettytable的使用方法 prettytable可以整齐地输出一个表格信息: +-----------+------+------------+----------------- ...
- Python多线程及其使用方法
[Python之旅]第六篇(三):Python多线程及其使用方法 python 多线程 多线程使用方法 GIL 摘要: 1.Python中的多线程 执行一个程序,即在操作系统中开启了一个进 ...
- Python学习笔记4-如何快速的学会一个Python的模块、方法、关键字
想要快速的学会一个Python的模块和方法,两个函数必须要知道,那就是dir()和help() dir():能够快速的以集合的型式列出该模块下的所有内容(类.常量.方法)例: #--encoding: ...
- Python生成随机数的方法
这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...
随机推荐
- 记录--居中为什么要使用 transform?
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 引言 居中是我们在前端布局中经常会遇到的问题,其中包括水平居中和垂直居中.居中的方法很多,比如说水平居中可以使用text-align: c ...
- 性能测试系列:Jmeter使用记录
jmeter配置环境变量vi /etc/profileexport PATH=$PATH:/tmp/jmeter/apache-jmeter-5.4.1/binsource /etc/profile ...
- cyc_to_led
Entity: cyc_to_led File: cyc_to_led.v Diagram Generics Generic name Type Value Description MD_SIM_AB ...
- KingbaseES 基于SQL的函数过程
什么是SQL函数? SQL函数包体是一些可执行的SQL语言.同时包含1条以上的查询,但是函数只返回最后一个查询(必须是SELECT)的结果. 除非SQL函数声明为返回void,否则最后一条语句必须是S ...
- #费马小定理#JZOJ 4015 数列
题目 给出\(x_n=(ax_{n-1}^2+bx_{n-1}+c)\bmod m\) 给出\(x_0.a,b,c,n,m\),求\(x_n\) \(\text{Subtask 1:}n\leq 10 ...
- #线段树分治,凸壳#洛谷 5416 [CTSC2016]时空旅行
题目链接 题目大意 有 \(n\) 个平行宇宙,由某些平行宇宙添加或删除一个点(三维坐标)得到, 现在有 \(m\) 组询问,形如对于某个平行宇宙,费用为从该平行宇宙内某个点出发 到达指定 \(x\) ...
- OpenHarmony技术日圆满举行丨3.1 Release版本重磅发布,生态落地初具规模
4 月 25 日,"共建新技术,开拓新领域"OpenAtom OpenHarmony(以下简称"OpenHarmony")技术日在深圳顺利召开.活动现场,Ope ...
- SQL LIKE 运算符:用法、示例和通配符解释
SQL中的LIKE运算符用于在WHERE子句中搜索列中的指定模式.通常与LIKE运算符一起使用的有两个通配符: 百分号 % 代表零个.一个或多个字符. 下划线 _ 代表一个单个字符. 以下是LIKE运 ...
- 关于openGauss中的虚拟索引
关于 openGauss 中的虚拟索引 作为曾经的 Oracle 资深使用者,对于 Oracle 11gR2 版本推出的 invisible Index 感觉一直很良好:因为这对于大部分情况下做优化是 ...
- Prometheus 性能调优-水平分片
简介 之前笔者有连续 2 篇文章: Prometheus 性能调优 - 什么是高基数问题以及如何解决? 如何精简 Prometheus 的指标和存储占用 陆续介绍了一些 Prometheus 的性能调 ...