Python计算斗牛游戏的概率
Python计算斗牛游戏的概率
过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率
)。
斗牛的玩法是:
- 把牌中的JQK都拿出来
- 每个人发5张牌
- 如果5张牌中任意三张加在一起是10的 倍数,就是
有牛
。剩下两张牌的和的10的余数就是牛数。
牌的大小:
4条 > 3条 > 牛十 > 牛九 > …… > 牛一 >没有牛
而这些牌出现的概率是有多少呢?
由于只有四十张牌,所以采用了既简单,又有效率的方法枚举
来计算。
计算的结果:
所有牌的组合数:658008
出现四条的组合数:360,概率 :0.05%
出现三条的组合数:25200,概率 :3.83%
出现牛十的组合数:42432,概率 :6.45%
出现牛九或牛八的组合数:87296,概率 :13.27%
出现牛一到牛七的组合数:306112,概率 :46.52%
出现没有牛的组合数:196608,概率 :29.88%
所以有七成的概率是有牛或以上的,所以如果你经常遇到没有牛,说明你的运气非常差或者本来是有牛的,但是你没有找出来。
Python源代码:
# encoding=utf-8
__author__ = 'kevinlu1010@qq.com'
import os
import cPickle
from copy import copy
from collections import Counter
import itertools
'''
计算斗牛游戏的概率
'''
class Poker():
'''
一张牌
'''
def __init__(self, num, type):
self.num = num # 牌数
self.type = type # 花色
class GamePoker():
'''
一手牌,即5张Poker
'''
COMMON_NIU = 1 # 普通的牛,即牛一-牛七
NO_NIU = 0 # 没有牛
EIGHT_NINE_NIU = 2 # 牛九或牛八
TEN_NIU = 3 # 牛十
THREE_SAME = 4 # 三条
FOUR_SAME = 5 # 四条
def __init__(self, pokers):
assert len(pokers) == 5
self.pokers = pokers
self.num_pokers = [p.num for p in self.pokers]
# self.weight = None # 牌的权重,权重大的牌胜
# self.money_weight = None # 如果该牌赢,赢钱的权重
self.result = self.sumary()
def is_niu(self):
'''
是否有牛
:return:
'''
# if self.is_three_same():
# return 0
for three in itertools.combinations(self.num_pokers, 3):
if sum(three) % 10 == 0:
left = copy(self.num_pokers)
for item in three:
left.remove(item)
point = sum(left) % 10
return 10 if point == 0 else point
return 0
def is_three_same(self):
'''
是否3条
:return:
'''
# if self.is_four_same():
# return 0
count = Counter([p.num for p in self.pokers])
for num in count:
if count[num] == 3:
return num
return 0
def is_four_same(self):
'''
是否4条
:return:
'''
count = Counter([p.num for p in self.pokers])
for num in count:
if count[num] == 4:
return num
return 0
def sumary(self):
'''
计算牌
'''
if self.is_four_same():
return GamePoker.FOUR_SAME
if self.is_three_same():
return GamePoker.THREE_SAME
niu_point = self.is_niu()
if niu_point in (8, 9):
return GamePoker.EIGHT_NINE_NIU
elif niu_point == 10:
return GamePoker.TEN_NIU
elif niu_point > 0:
return GamePoker.COMMON_NIU
else:
return GamePoker.NO_NIU
def get_all_pokers():
'''
生成所有的Poker,共四十个
:return:
'''
pokers = []
for i in range(1, 11):
for j in ('A', 'B', 'C', 'D'):
pokers.append(Poker(i, j))
return pokers
def get_all_game_poker(is_new=0):
'''
生成所有game_poker
:param pokers:
:return:
'''
pokers = get_all_pokers()
game_pokers = []
if not is_new and os.path.exists('game_pokers'):
with open('game_pokers', 'r') as f:
return cPickle.loads(f.read())
for pokers in itertools.combinations(pokers, 5): # 5代表五张牌
game_pokers.append(GamePoker(pokers))
with open('game_pokers', 'w') as f:
f.write(cPickle.dumps(game_pokers))
return game_pokers
def print_rate(game_pokers):
total_num = float(len(game_pokers))
four_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.FOUR_SAME])
three_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.THREE_SAME])
ten_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.TEN_NIU])
eight_nine_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.EIGHT_NINE_NIU])
common_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.COMMON_NIU])
no_num = len([game_poker for game_poker in game_pokers if game_poker.result == GamePoker.NO_NIU])
print '所有牌的组合数:%d' % total_num
print '出现四条的组合数:%d,概率 :%.2f%%' % (four_num, four_num * 100 / total_num)
print '出现三条的组合数:%d,概率 :%.2f%%' % (three_num, three_num * 100 / total_num)
print '出现牛十的组合数:%d,概率 :%.2f%%' % (ten_num, ten_num * 100 / total_num)
print '出现牛九或牛八的组合数:%d,概率 :%.2f%%' % (eight_nine_num, eight_nine_num * 100 / total_num)
print '出现牛一到牛七的组合数:%d,概率 :%.2f%%' % (common_num, common_num * 100 / total_num)
print '出现没有牛的组合数:%d,概率 :%.2f%%' % (no_num, no_num * 100 / total_num)
def main():
game_pokers = get_all_game_poker() # 658008种
print_rate(game_pokers)
main()
如果有错误,欢迎指正。
Python计算斗牛游戏的概率的更多相关文章
- 原生JS实战:写了个斗牛游戏,分享给大家一起玩!
本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5869953.html 该程序是本人的个人作品,写的不好,未经本人允许,请 ...
- 12岁的少年教你用Python做小游戏
首页 资讯 文章 频道 资源 小组 相亲 登录 注册 首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...
- Python菜鸟快乐游戏编程_pygame(1)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- python猜数字游戏快速求解解决方案
#coding=utf-8 def init_set(): r10=range(10) return [(i, j, k, l) for i in r10 for j in r10 for k in ...
- [转载] python 计算字符串长度
本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...
- BZOJ_3191_[JLOI2013]卡牌游戏_概率DP
BZOJ_3191_[JLOI2013]卡牌游戏_概率DP Description N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随 ...
- Python菜鸟快乐游戏编程_pygame(6)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(5)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(4)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
随机推荐
- jQuery源码dom ready分析
一.前言 在平时开发web项目时,我们使用jquery框架时,可能经常这样来使用$(document).ready(fn),$(function(){}),这样使用的原因是在浏览器把DOM树渲染好之前 ...
- hdu 3652 打表
思路:直接打表 #include<cstdio> #include<vector> #include<cmath> #include<iostream> ...
- HTML常见标签
标题:h1.h2.h3.h4.h5.... 段落:p 换行:br 容器:div.span(用来容纳其他标签) 表格:table.tr.td 列表:ul.ol.li 图片:img 表单:input 链接 ...
- MongoDB - The mongo Shell, Write Scripts for the mongo Shell
You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform a ...
- Win7如何快速修复系统
Windows 7可能是微软迄 今为止最好的桌面操作系统,但是偶尔出现一些问题还是在所难免的.这里教你如何快速修复出现问题的Windows 7系统,每个操作系统都有重新安装的可能,Windows 7也 ...
- Android ViewFlipper用法浅析
在Android应用开发中,我们经常会需要实现左右切换视图的功能,这通常需要在LinearLayout.RelativeLayout等布局中添加ImageView来实现.如果每次只需展示一张图片,并可 ...
- 熔断器设计模式<转>
熔断器设计模式 如果大家有印象的话,尤其是夏天,如果家里用电负载过大,比如开了很多家用电器,就会”自动跳闸”,此时电路就会断开.在以前更古老的一种方式是”保险丝”,当负载过大,或者电路发生故障或异常时 ...
- JavaEE是什么?
曾经有那么两次被问到JavaEE是什么东西.做了这么久的程序员了,这个概念还说不清楚,真的感觉有点惭愧呀. 下面摘抄网络上的一些概念,以悼念傻逼的自己. Java EE,Java平台企业版(Java ...
- 简单的表单验证插件(Jquery)
在做web开发的时候经常遇到表单验证问题,表单验证一般有客户端验证和服务器端验证,这个验证插件仅仅能满足我的项目中的基本需求的. Validate_Tools.js function Validate ...
- Swift 概述及Swift运算符和表达式
Swift 是用于设计 iOS 及 Mac OS X 应用的一门新 语言. Swift 特点 • Swift 保留了 C 与 Objective-C 的优点,并摒弃 其为了兼容 C 语言所 ...