(Python)100万ハンド分析してみた〜AAが配られる回数は？？〜


　　
閲覧いただき、ありがとうございます。
pbirdyellowです。

今回は"1万ハンドごと"に"AA"が配られる回数を集計してみました。
AAが極端に来る日もあれば全くこない日もあるのですが、実際のところどうなのでしょう・・・


↓分析結果
![スクリーンショット 2020-08-15 19.17.33.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/659636/3c5d6491-6393-7b46-42d4-24a16cd9e448.png)

この表の見方は
・x軸は1万ハンド毎(~10000,~20000,~30000,・・・)
・y軸はAAが配られた回数
です。

maxで67回、minで31回とかなり分散が激しいですね。。。

KKも調べてみました。
↓分析結果
![スクリーンショット 2020-08-15 19.36.38.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/659636/71a36f22-b137-b192-0b32-00ad1b0a5a29.png)


以下がソースコードになります。
プログラムは完全初心者なので、より良いコードの書き方等ありましたら是非ご指摘ください！！

```python:pokermain.py
from holdcards import Holdcards 
from plotgraph import Plotgraph
import os
import glob
import re

path='ここにパスを記述'
hand = "AA" #調べたいハンド：自由に変更可能
count = 10000　#調べたいハンド毎：自由に変更可能


num = lambda val : int(re.sub("\\D", "", val))
filelist = sorted(glob.glob(os.path.join(path,"*.txt"),recursive=True),key = num)
totcards = []
graphdata = []
countdata = []
counthands = []
for item in filelist:
    print(item)
    with open(item) as f:
        data = f.readlines()
        card = Holdcards()
        h_cards = card.find_holdcards(data)
        totcards += h_cards


i = 0
while len(totcards[count*i:count*(i+1)]) == count:
    graphdata.append(totcards[count*i:count*(i+1)])
    #counthands.append(str(count*i+1)+"~"+str(count*i+len(totcards[count*i:count*(i+1)])))
    i += 1

for item in graphdata:
    countdata.append(item.count(hand))

print(len(graphdata))
print(countdata)

graph= Plotgraph()
graph.writegraph(countdata,hand,count,len(graphdata)*count)



```


```python：holdcards.py

class Holdcards:
       def __init__(self):
              self.trump={"A":"14","K":"13","Q":"12","J":"11","T":"10","9":"9","8":"8","7":"7","6":"6","5":"5","4":"4","3":"3","2":"2"}
              self.r_trump={"14":"A","13":"K","12":"Q","11":"J","10":"T","9":"9","8":"8","7":"7","6":"6","5":"5","4":"4","3":"3","2":"2"}
              self.hands = 0
              self.tothands = 0
              self.handlist = []


       def find_holdcards(self,data):
              holdcards = []
              for item in data:
                     if 'Dealt to' in item:
                            item = item[-7:-2]
                            if item[1] == item[4]:
                                   if int(self.trump.get(item[0])) > int(self.trump.get(item[3])):
                                          item = item[0] + item[3] + 's'
                                   else:
                                          item = item[3] + item[0] + 's'
                            else:
                                   if int(self.trump.get(item[0])) > int(self.trump.get(item[3])):
                                          item = item[0] + item[3] + 'o'
                                   elif item[0] == item[3]:
                                          item = item[0] + item[3]
                                   else:
                                          item = item[3] + item[0] + 'o'
                            
                            holdcards.append(item)
              return holdcards

       def count_holdcards(self,list):
              
              totlist = []
              i = 0
              while i < 13:
                     j=0
                     rowlist = []
                     rowhandlist = []
                     while j < 13:
                            if i < j:
                                   hand = (self.r_trump.get(str(14-i))+self.r_trump.get(str(14-j))+"s")
                                   count = list.count(hand)
                                   rowlist.append(count)
                            elif i == j:
                                   hand = (self.r_trump.get(str(14-i))+self.r_trump.get(str(14-j)))
                                   count = list.count(hand)
                                   rowlist.append(count)    
                            else:
                                   hand = (self.r_trump.get(str(14-j))+self.r_trump.get(str(14-i))+"o")
                                   count = list.count(hand)
                                   rowlist.append(count)                                                                  
                            self.hands += count
                            #print(hand + ":" + str(count))
                            rowhandlist.append(hand)
                            j += 1
                     self.handlist.append(rowhandlist)
                     totlist.append(rowlist)
                     i += 1
              return totlist


```

```python:plotgraph.py
import numpy as np
import matplotlib.pyplot as plt

class Plotgraph:
       def __init__(self):
              pass




       def writegraph(self,countlist,hand,count,tothands):
              x = np.arange(len(countlist))
              width = 0.75
              rects = plt.bar(x, countlist, width)
              plt.xlabel("i × "+str(count)+"hands")
              plt.ylabel("count of "+hand)
              plt.title("hand = "+hand+" , totalhands = "+str(tothands))
              for rect in rects:
                     height = rect.get_height()
                     plt.annotate('{}'.format(height),
                            xy=(rect.get_x() + rect.get_width() / 2, height),
                            size=7,
                            xytext=(0, 3),
                            textcoords="offset points",
                            ha='center', va='bottom')
              plt.show()


```