2
3

More than 3 years have passed since last update.

(Python)100万ハンド分析してみた〜スターティングハンド集計〜

Last updated at Posted at 2020-08-14

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

早速ですが、有名なポーカーサイト"PokerStars"で私がプレーした約100万ハンドを分析します。
今回はテキサスホールデムのスターティングハンドを集計しました。

スターティングハンドとはトランプの全カード54枚のうち、jokerを除いた52枚の中からランダムに2枚配られたものをいいます。

ソースコードも記載しているので、是非みなさまも利用してみてください。
プログラミングは私自身初心者レベルなので、アドバイス等いただけると嬉しいです。
  
↓100万ハンド集計結果
スクリーンショット 2020-08-02 17.39.05.png

ハンドログを集計すると、各ハンドの出現率が収束しているように見えました。
各ハンドの出現率はこちらをご覧ください。 ※近日公開予定です。

PokerStarsのハンドログは、プレーヤーの皆さまのPC内に保存されています。
ハンドログの抽出方法はこちらをご覧ください。 ※近日公開予定です。

このログデータは非常にデータの加工がしやすいので便利です。

HoldemManager2.2020-07-31-09-22-31.PS.Hand1-1000.txt
PokerStars Zoom Hand #155136535392:  Hold'em No Limit ($0.05/$0.10) - 2016/06/24 9:17:52 ET
Table 'Aludra' 6-max Seat #1 is the button
Seat 1: xxx ($8.18 in chips)
Seat 2: xxx ($9.90 in chips)
Seat 3: pbirdyellow ($10 in chips)
Seat 4: xxx ($17.30 in chips)
Seat 5: xxx ($21.93 in chips)
Seat 6: xxx ($10 in chips)
ccc: posts small blind $0.05
pbirdyellow: posts big blind $0.10
*** HOLE CARDS ***
Dealt to pbirdyellow [Qc Ad]
xxx: folds
xxx: folds
xxx: raises $0.15 to $0.25
xxx: folds
xxx: folds
pbirdyellow: raises $0.55 to $0.80
xxx: folds
Uncalled bet ($0.55) returned to pbirdyellow
pbirdyellow collected $0.55 from pot
pbirdyellow: doesn't show hand
*** SUMMARY ***
Total pot $0.55 | Rake $0
Seat 1: xxx (button) folded before Flop

・・・(以下続く)

※プレーヤー情報は隠しています。

以下がソースコードになります。"path"の値を設定すると動くはずです。
プログラミングはまだまだ初心者レベルですので、どんどんご指摘をください。
コードの内容で疑問等ございましたら、qiitaアカウントもしくはtwitterアカウントよりDM等でご連絡ください。

twitter:@pbirdyellow

pokermain.py
from holdcards import Holdcards 
from plotgraph import Plotgraph
import os
import glob

path='ここにlogのパスを記述'
filelist = glob.glob(os.path.join(path,"*.txt"),recursive=True)
totcards = []
graphdata = []
for item in filelist:
    with open(item) as f:
        data = f.readlines()
        card = Holdcards()
        h_cards = card.find_holdcards(data)
        totcards += h_cards

cardscount = card.count_holdcards(totcards)
graph= Plotgraph()
graph.writegraph(cardscount,card.handlist,card.hands)
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.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
                            rowhandlist.append(hand)
                            j += 1
                     self.handlist.append(rowhandlist)
                     totlist.append(rowlist)
                     i += 1
              return totlist

Plotgraph.py
import numpy as np
import matplotlib.pyplot as plt

class Plotgraph:
       def __init__(self):
              pass

       def writegraph(self,graphlist,handlist,hands):

              column_labels = list('AKQJT98765432')
              row_labels = list('AKQJT98765432')
              data = np.array(graphlist)
              fig,ax = plt.subplots()
              heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
              ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
              ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
              ax.invert_yaxis()
              ax.xaxis.tick_top()
              ax.set_xticklabels(row_labels, minor=False)
              ax.set_yticklabels(column_labels, minor=False)
              i = 0
              while i < 13:
                     j = 0
                     while j < 13:
                            plt.text(0.25+j,0.75+i,str(handlist[i][j]))
                            j += 1
                     i += 1
              plt.title("totalhands = "+str(hands), y=-0.1)

              fig.colorbar(heatmap, ax=ax)
              plt.show()

2
3
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
2
3