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

# generate deck and declare variables
num_iterations = 20 #--- Number of times to try.
cards_seen = 7
#decklist = {"C": 9, "D": 8, "X": 1, "S": 22}
decklist = {"L": 17, "C": 10, "D": 7, "S": 6}
# L = Lands,
# S = Spells except creatures,
# C = Creatures with 3 or less mana cost,
# D = Creatures with 4 or more mana cost.

def Do_you_keep(hand):
    flag = True
    if hand["L"] <= 1:
        flag = False
    elif hand["L"] >= 6:
        flag = False
    elif hand["C"] <= 0:
        flag = False
    elif hand["L"] <= 2 and hand["C"] <= 1:
        flag = False
    return flag


deck = []
for card in decklist.keys():
    deck += [card] * decklist[card]
#--- Print your deck.
print("Desk =", deck)
print("-------")
for card, number in decklist.items():
#    print(card + ": " + str(number) + "\t", end="")
    print(card + ": " + str(number))
print("-------")

#--- Simulation loop: Start <28001>
count_keep = 0
count_total = 0
for _ in range(num_iterations):
    Check = "Ke----ep"
    init = {"L": 0, "C": 0, "D": 0, "S": 0}
    hand = {**init, **dict(Counter(random.sample(deck, cards_seen)))}
    if Do_you_keep(hand) == False:
        Check = "Mulligan"

    print(Check, ", with hand =", hand)
#    count_CCDD += (  min(draw["C"],2)+min(draw["D"],2)+draw["X"] >= 4)
    count_keep  += (Do_you_keep(hand) == True)
    count_total += (1 >= 0)
#--- End <28001>
print("-------")

#--- print results
print("Times of iterations:", count_total)
print("Times of keeps:", count_keep)

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

