2021年6月2日 星期三

樸克牌模擬 python

 import random

ds = 'A23456789TJQK'
fs = 'SHDC'
r = []
for i in ds:
    for j in fs:
        r.append(j+i)

sd = {}
n = 1000000
for j in range(n):
    ud = []
    for i in range(5):
        u = random.randint(0,51)
        ud.append(r[u])
    print(j,ud,end=' ')

    sameCheck = [i[1for i in ud]
    sameCheck1 = [sameCheck.count(ifor i in sameCheck]
  
    nSame = int(sameCheck1.count(2)/2)
    nSame3 = sameCheck1.count(3)

    msg = ''

    if nSame3 ==1 : msg = 'Three of a kind'
    elif nSame ==2 : msg = 'Two Pair'
    elif nSame == 1:msg = 'One Pair'

    checkFlag = [fs.find(i[0]) for i in ud]
    checkFlag.sort()  

    if checkFlag[0] == checkFlag[1] == checkFlag[2] == checkFlag[3] == checkFlag[4]:
        msg = 'Flush'

    checkSame = [ds.find(i[1]) for i in ud]
    checkSame.sort()
    if (checkSame[0] == checkSame[1] == checkSame[2] == checkSame[3]
    or  checkSame[1] == checkSame[2] == checkSame[3] == checkSame[4]):
        msg = 'Four of a kind' 
    if (checkSame[0] == checkSame[1and checkSame[1] == checkSame[2and checkSame[3] == checkSame[4]
    or checkSame[0] == checkSame[1and checkSame[2] == checkSame[3and checkSame[3] == checkSame[4]):
        msg = 'FullHouse'  
    if checkSame[0] == checkSame[1]-1 and checkSame[1] == checkSame[2]-1  and checkSame[2] == checkSame[3]-1 and checkSame[3] == checkSame[4]-1:
        msg = 'Straight'  
    if (checkFlag[0] == checkFlag[1] == checkFlag[2] == checkFlag[3] == checkFlag[4]
       and checkSame[0] == checkSame[1]-1 and checkSame[1] == checkSame[2]-1  and checkSame[2] == checkSame[3]-1 and checkSame[3] == checkSame[4]-1):
        msg = 'Straight Flush' 
    
    print(nSame,msg)
    
    if msg in sdsd[msg]+=1
    else        : sd[msg]=1 

print(n'times :')
for k,v in sd.items():
    if k=='':print('%-20s : %d'%('No...',v))
    else:    print('%-20s : %d'%(k,v))

1000000 times : No... : 457108 Two Pair : 68882 One Pair : 460935 Four of a kind : 2114 Straight : 2852 Flush : 3894 FullHouse : 4201 Straight Flush : 14