亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2017年09月26日 11:14:14   作者:Xjng  
這篇文章主要介紹了Python計算斗牛游戲概率算法,簡單介紹了斗牛游戲的原理并結合具體實例形式分析了相關的游戲概率算法,需要的朋友可以參考下

本文實例講述了Python計算斗牛游戲概率算法。分享給大家供大家參考,具體如下:

過年回家,都會約上親朋好友聚聚會,會上經(jīng)常會打麻將,斗地主,斗牛。在這些游戲中,斗牛是最受歡迎的,因為可以很多人一起玩,而且沒有技術含量,都是看運氣(專業(yè)術語是概率)。

斗牛的玩法是:

1. 把牌中的JQK都拿出來
2. 每個人發(fā)5張牌
3. 如果5張牌中任意三張加在一起是10的 倍數(shù),就是有牛。剩下兩張牌的和的10的余數(shù)就是牛數(shù)。

牌的大小:

4條 > 3條 > 牛十 > 牛九 > …… > 牛一 >沒有牛

而這些牌出現(xiàn)的概率是有多少呢?

由于只有四十張牌,所以采用了既簡單,又有效率的方法枚舉來計算。

計算的結果:

所有牌的組合數(shù):658008
出現(xiàn)四條的組合數(shù):360,概率 :0.05%
出現(xiàn)三條的組合數(shù):25200,概率 :3.83%
出現(xiàn)牛十的組合數(shù):42432,概率 :6.45%
出現(xiàn)牛九或牛八的組合數(shù):87296,概率 :13.27%
出現(xiàn)牛一到牛七的組合數(shù):306112,概率 :46.52%
出現(xiàn)沒有牛的組合數(shù):196608,概率 :29.88%

所以有七成的概率是有牛或以上的,所以如果你經(jīng)常遇到?jīng)]有牛,說明你的運氣非常差或者本來是有牛的,但是你沒有找出來。

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 # 牌數(shù)
    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 '所有牌的組合數(shù):%d' % total_num
  print '出現(xiàn)四條的組合數(shù):%d,概率 :%.2f%%' % (four_num, four_num * 100 / total_num)
  print '出現(xiàn)三條的組合數(shù):%d,概率 :%.2f%%' % (three_num, three_num * 100 / total_num)
  print '出現(xiàn)牛十的組合數(shù):%d,概率 :%.2f%%' % (ten_num, ten_num * 100 / total_num)
  print '出現(xiàn)牛九或牛八的組合數(shù):%d,概率 :%.2f%%' % (eight_nine_num, eight_nine_num * 100 / total_num)
  print '出現(xiàn)牛一到牛七的組合數(shù):%d,概率 :%.2f%%' % (common_num, common_num * 100 / total_num)
  print '出現(xiàn)沒有牛的組合數(shù):%d,概率 :%.2f%%' % (no_num, no_num * 100 / total_num)
def main():
  game_pokers = get_all_game_poker() # 658008種
  print_rate(game_pokers)
main()

如果有錯誤,歡迎指正。

更多關于Python相關內容可查看本站專題:《Python游戲開發(fā)技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • Python中的數(shù)學運算操作符使用進階

    Python中的數(shù)學運算操作符使用進階

    這篇文章主要介紹了Python中的數(shù)學運算操作符使用進階,也包括運算賦值操作符等基本知識的小結,需要的朋友可以參考下
    2016-06-06
  • 使用pandas的box_plot去除異常值

    使用pandas的box_plot去除異常值

    今天小編就為大家分享一篇使用pandas的box_plot去除異常值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python實現(xiàn)按學生年齡排序的實際問題詳解

    Python實現(xiàn)按學生年齡排序的實際問題詳解

    這篇文章主要給大家介紹了關于Python實現(xiàn)按學生年齡排序實際問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。
    2017-08-08
  • 如何基于python實現(xiàn)腳本加密

    如何基于python實現(xiàn)腳本加密

    這篇文章主要介紹了如何基于python實現(xiàn)腳本加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Python利用遞歸和walk()遍歷目錄文件的方法示例

    Python利用遞歸和walk()遍歷目錄文件的方法示例

    在日常開發(fā)中經(jīng)常需要檢查一個“目錄或文件夾”內部有沒有我們想要的文件或者文件夾,下面這篇文章主要給大家介紹了關于Python利用遞歸和walk()遍歷目錄文件的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • 提升Python程序性能的7個習慣

    提升Python程序性能的7個習慣

    這篇文章主要介紹了提升Python程序性能的7個習慣,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • C++通過內嵌解釋器調用Python及間接調用Python三方庫

    C++通過內嵌解釋器調用Python及間接調用Python三方庫

    本文主要介紹了C++通過內嵌解釋器調用Python及間接調用Python三方庫,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Python EOL while scanning string literal問題解決方法

    Python EOL while scanning string literal問題解決方法

    這篇文章主要介紹了Python EOL while scanning string literal問題解決方法,本文總結出是數(shù)據(jù)庫數(shù)據(jù)出現(xiàn)問題導致這個問題,需要的朋友可以參考下
    2015-04-04
  • python中Requests請求的安裝與常見用法

    python中Requests請求的安裝與常見用法

    Requests是一常用的http請求庫,它使用python語言編寫,可以方便地發(fā)送http請求,以及方便地處理響應結果,下面這篇文章主要給大家介紹了關于python中Requests請求的安裝與常見用法的相關資料,需要的朋友可以參考下
    2022-07-07
  • Pytest框架之fixture的詳細使用教程

    Pytest框架之fixture的詳細使用教程

    這篇文章主要介紹了Pytest框架之fixture的詳細使用教程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04

最新評論