国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python計算斗牛游戲概率算法實例分析

2020-02-16 10:19:11
字體:
來源:轉載
供稿:網友

本文實例講述了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 osimport cPicklefrom copy import copyfrom collections import Counterimport 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_NIUdef 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 pokersdef 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_pokersdef 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()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿坝县| 南安市| 广州市| 丹棱县| 中山市| 营口市| 鹤山市| 新余市| 保靖县| 呼伦贝尔市| 马尔康县| 交城县| 上栗县| 夏邑县| 聂荣县| 赣榆县| 江山市| 溧阳市| 常熟市| 柯坪县| 衢州市| 郴州市| 新疆| 弥勒县| 灌阳县| 宽甸| 饶平县| 蕉岭县| 永丰县| 于都县| 公主岭市| 台前县| 徐汇区| 枞阳县| 高碑店市| 历史| 敖汉旗| 平湖市| 滨州市| 余庆县| 三原县|