Python计算斗牛游戏的概率

过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛。在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率)。

斗牛的玩法是:

  1. 把牌中的JQK都拿出来
  2. 每个人发5张牌
  3. 如果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计算斗牛游戏的概率的更多相关文章

  1. 原生JS实战:写了个斗牛游戏,分享给大家一起玩!

    本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5869953.html 该程序是本人的个人作品,写的不好,未经本人允许,请 ...

  2. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  3. Python菜鸟快乐游戏编程_pygame(1)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  4. 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 ...

  5. [转载] python 计算字符串长度

    本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...

  6. BZOJ_3191_[JLOI2013]卡牌游戏_概率DP

    BZOJ_3191_[JLOI2013]卡牌游戏_概率DP Description   N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随 ...

  7. Python菜鸟快乐游戏编程_pygame(6)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  8. Python菜鸟快乐游戏编程_pygame(5)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  9. Python菜鸟快乐游戏编程_pygame(4)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

随机推荐

  1. 【Android Studio使用教程6】Execution failed for task ':×××:compileReleaseAidl'

    使用Android Studio运行项目时候,经常会报一些错,比如 Execution failed for task ':×××:processReleaseResources' Execution ...

  2. Android 拍照 代码实例

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 这是我做的一个简单的利用Android手机的摄像头进行拍照的实例. 在这里我实现了基本的拍照.照片的存储 ...

  3. c#代码使用ResourceDictionary样式

    对于ResourceDictionary样式代码: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006 ...

  4. mount: unknown filesystem type 'LVM2_member'解决方案

    系统启动到request_module: runaway loop modprobe binfmt-464c挂起 利用U盘系统,挂载硬盘出现:mount: unknown filesystem typ ...

  5. 使用开源库MagicalRecord操作CoreData

      1. 将 MagicalRecord 文件夹拖入到工程文件中,引入 CoreData.frame 框架 2. 在 .pch 文件中引入头文件 CoreData+MagicalRecord.h 注: ...

  6. spring三大核心学习(一)---控制反转

    记得当年大学时候,java的企业级框架还是ssh的天下(spring,struts和hibernate),但是现在,感觉spring已经完全把那两个框架甩在后边了.用spring的人越来越多,用str ...

  7. Oracle中的内置函数在sql中的转换整理

    程序里面经常会即支持Oracle数据库,又支持sql数据库.而有些Oracle内置函数用的比较多,但在sql中语法有些不同,我做了些整理,希望可以帮助大家.... 1.oracle中的内置函数:ora ...

  8. DWZ(JUI) 教程 中如何整合第三方jQuery插件

    Query插件一般是$(document).ready()中初始化 $(document).ready(function(){  // 文档就绪,初始化jQuery插件| });  // 或者或缩写形 ...

  9. Java之组合数组2

    编写函数Fun,其功能是将两个两位数的正整数A.B合并为一个整数放在C中,将A数的十位和个位一次放在C的个位和十位上,A数的十位和个位一次放在C的百位和千位上.例如,当 A=16,B=35,调用该函数 ...

  10. server——小记

    问题 Step 1   Start the server in Directory Services Restore Mode   Windows Server 2003/2008 Directory ...