友意白雑記帳

だいたい自分用の覚え書き

Pythonにマリガンチェックを20000回やらせてみた。

MTGにおいて、マリガン判断は最も(運ではなく)実力によって勝敗をコントロールできる部分の一つでしょう。すべてのMTGプレイヤーはマリガン判断に迷うもの。しかし実際のところ、自分のキープ基準に対して、マリガンするべき割合の適正値を計算して知るのはかなり難しい。そこでPythonでMTGマリガン再現機を作りました。

今回はモダンのマーフォークデッキをサンプルにしてあります。以下のソースコードをVer.3.6.9以降のPythonで動かせば、手作業なら数十時間はかかるであろう回数のマリガンチェックを数秒で行うことができます。

説明動画(YouTube)

www.youtube.com

 

以下がソースコードになります。

"Mulligan_Simulator.py"

#==============================================================

#--- Code in Python 3.6.9 or after:
from __future__ import print_function
from collections import Counter
import random

num_iterations = 20000 #--- Number of times to try.

# generate deck and declare variables
decklist = {"L": 20, "V": 4, "C1": 8, "C2": 22, "C3": 6}
# L = Lands,
# V = AEther Vial,
# CX = Others.
cards_seen = 7 #--- Number of cards in opening hand

def Do_you_keep(hand):
    flag = True
    if hand["V"] <= 0:# and hand["C1"] <= 0:
        flag = False
    elif hand["L"] <= 1:
        flag = False
    elif hand["L"] >= 4:
        flag = False
    elif hand["L"] <= 2 and hand["C3"] >= 3:
        flag = False
    return flag

deck = [] #--- Initial deck = empty.
num_cards = 0
for card in decklist.keys():
    deck += [card] * decklist[card] #--- Copy your deck list.
print("Desk =", deck)#--- Print your deck.
print("-------")
for card, number in decklist.items():
    print(card + ": " + str(number))
    num_cards += number
print("Total cards in deck = ", num_cards)
print("-------")

#--- Simulation loop: Start <28001>
count_keep = 0
count_total = 0
for _ in range(num_iterations):
    Check = "Ke----ep"
    init = {"L": 0, "V": 0, "C1": 0, "C2": 0, "C3": 0}
    hand = {**init, **dict(Counter(random.sample(deck, cards_seen)))}
    if Do_you_keep(hand) == False:
       Check = "Mulligan"
#    print(Check, ", with hand =", hand)#--- To print out the full results
    count_keep  += (Do_you_keep(hand) == True)
    count_total += (1 >= 0)
#--- End <28001>

#--- print results
print("Times of iteration:", count_total)
print("Times of keeping hand:", count_keep)

x = 1.0000000001 - float(count_keep) / float(num_iterations)
print("Mulligan probability:",  int(x*100.0), "%")
print("-------")

#==============================================================

 

尚、今回の内容は以下の記事を大いに参考にさせていただきました。興味の湧いた方はぜひこちらもご一読下さい。

マナベースとモンテカルロ法
https://article.hareruyamtg.com/article/article_5462/