0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ポーカーの役ができる確率を計算してみた(ジョーカー1枚) (2)

Last updated at Posted at 2024-02-16

前回、各役の判定条件を関数にして役の判定をしていましたが、
「フラッシュ」「ストレート」と名の付く役を先に判定して、それ以外は数字毎の枚数で判定することにします。

  • 変更部分
def deal(hand, k):
    global nPatterns
    global cntFiveOfAKind
    global cntRoyalStraightFlush
    global cntStraightFlush
    global cntFourOfAKind
    global cntFullHouse
    global cntFlush
    global cntStraight
    global cntThreeOfAKind
    global cntTwoPair
    global cntOnePair
    global cntNoPair

    if len(hand)<5:
        for i in range(k,len(CARDS)):
            hand.append(CARDS[i])
            deal(hand,i+1)
            hand.pop()
    else:
        for i in range(1,len(hand)):
            assert(hand[i-1][1]<=hand[i][1])
        
        nPatterns+=1

ここまでは前回と同じです。
この後の判定条件で、「フラッシュ」「ストレート」と名の付く役から判定します。

        if isRoyalStraightFlush(hand):
            cntRoyalStraightFlush+=1
        elif isStraightFlush(hand):
            cntStraightFlush+=1
        elif isFlush(hand):
            cntFlush+=1
        elif isStraight(hand):
            cntStraight+=1

この後、数字毎に枚数を集約して判定します。

        else:
            #数字毎の枚数を管理
            numnum=[0]*14
            for h in hand:
                numnum[h[1]]+=1

            #枚数の降順にソート
            #numnum[0]はジョーカーの有無(ジョーカーの枚数)が
            #格納されているので、numnum[1]以降をソート
            numnum_sorted=sorted(numnum[1:],reverse=True)
            
            #ジョーカーがあれば一番多い枚数の数字として扱う
            numnum_sorted[0]+=numnum[0]
            
            #0枚の部分を捨てる(1枚以下の部分を捨ててもよい)
            del numnum_sorted[numnum_sorted.index(0):]

            #役の判定
            if numnum_sorted==[5]:
                cntFiveOfAKind+=1
            elif numnum_sorted==[4,1]:
                cntFourOfAKind+=1
            elif numnum_sorted==[3,2]:
                cntFullHouse+=1
            elif numnum_sorted==[3,1,1]:
                cntThreeOfAKind+=1
            elif numnum_sorted==[2,2,1]:
                cntTwoPair+=1
            elif numnum_sorted==[2,1,1,1]:
                cntOnePair+=1
            else:
                cntNoPair+=1 
  • 実行結果
Five of a Kind       :      13(  0.000453%)
Royal Straight Flush :      24(  0.000836%)
Straight Flush       :     180(  0.006272%)
Four of a Kind       :    3120(  0.108723%)
Full House           :    6552(  0.228318%)
Flush                :    7804(  0.271946%)
Straight             :   20532(  0.715479%)
Three of a Kind      :  137280(  4.783800%)
Two Pair             :  123552(  4.305420%)
One Pair             : 1268088( 44.189101%)
No Pair              : 1302540( 45.389651%)
-------------------------------------------
Total                : 2869685(100.000000%)

前回の結果と一致いたしました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?