from itertools import permutations as IP
import math
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter as SMF
import numpy as np
import random
import seaborn as sns
deck = list(range(1,11))
decks = list(IP(deck))
#How many wins?
def riddler(decks):
wins = 0
#Loop through all 10! decks.
for d in decks:
tot = 0
#As long as there are cards remaining.
while len(d) > 0 :
#Place the top card in your hand and update the deck.
tot += d[-1]
d = d[:-1]
#What is the outcome?
if tot == 21:
wins += 1
break
elif tot > 21:
break
else :
continue
return(wins)
#For Plot.
def ec1(decks):
#Only scenarios possible.
wins = {3: 0,
4: 0,
5: 0}
passe = {2: 0,
3: 0,
4: 0,
5: 0,}
#Loop through all 10! decks.
for d in decks:
tot = 0
drawn = []
#As long as there are cards remaining.
while len(d) > 0 :
#Place the top card in your hand and update the deck.
tot += d[-1]
d = d[:-1]
#If you play safe, you will never lose, only pass.
#Win, save the count corresponding to the appropriate hand size in dictionary.
if tot == 21 :
wins[10 - len(d)] += 1
break
#Pass, save the count corresponding to the appropriate hand size in dictionary.
elif max(d) > 21 - tot :
passe[10 - len(d)] += 1
break
return(wins, passe)
#Check win combinations.
def ec2(decks):
wins = {}
#Loop through all 10! decks.
for d in decks:
tot = 0
hand = []
#As long as there are cards remaining.
while len(d) > 0 :
#Place the top card in your hand and update the deck.
tot += d[-1]
hand.append(d[-1])
hand.sort()
d = d[:-1]
#If you win, what cards are in your hand?
if tot == 21 :
if str(hand) in wins.keys():
wins[str(hand)] += 1
else :
wins[str(hand)] = 1
break
elif max(d) > 21 - tot :
break
return(wins)
riddler(decks)
635040
dicts = ec1(decks)
print(dicts)
({3: 50400, 4: 21600, 5: 2880}, {2: 1532160, 3: 1562400, 4: 427680, 5: 31680})
wins, passe = dicts
sns.set()
fig = plt.figure(figsize = (15, 8))
ax = fig.add_subplot()
#Title setup.
ax.set_title('Risk Averse, Non-Traditional Blackjack', fontsize = 24)
#X-axis setup.
ax.set_xlabel('Number of Cards in Hand', fontsize = 22)
ax.set_xticks(ticks = range(2,6))
#Y-axis setup.
ax.set_ylabel('Frequency', fontsize = 22)
ax.tick_params(axis = 'both', which = 'major', labelsize = 18)
ax.yaxis.set_major_formatter(SMF('{x:,.0f}'))
#Pass.
bars0 = plt.bar(x = passe.keys(),
height = np.array(list(passe.values())) + np.array([0] + list(wins.values())),
color = '#21908CFF',
width = 1,
edgecolor = 'grey',
label = 'Pass')
#Wins.
bars1 = plt.bar(x = wins.keys(),
height = wins.values(),
color = '#404688FF',
width = 1,
edgecolor = 'grey',
label = 'Win')
for i in range(4) :
#Pass.
plt.text(x = bars0[i].get_xy()[0] + 0.45,
y = bars0[i].get_height() + 25000,
s = format(int(bars0[i].get_height()), ','),
ha = 'center',
size = 'xx-large',
weight = 'bold',
c = '#21908CFF',)
#Wins.
if i < 3 :
plt.text(x = bars1[i].get_xy()[0] + 0.45,
y = bars1[i].get_height() + 10000,
s = format(int(bars1[i].get_height()), ','),
ha = 'center',
size = 'large',
weight = 'bold',
c = '#404688FF')
plt.legend(loc = 5,
fontsize = 'x-large')
fig.savefig("2024.05.24.png", bbox_inches = 'tight')
ec2(decks)
{'[5, 6, 10]': 10080, '[4, 7, 10]': 10080, '[3, 8, 10]': 10080, '[2, 9, 10]': 20160, '[2, 4, 5, 10]': 4320, '[2, 3, 6, 10]': 4320, '[1, 4, 6, 10]': 4320, '[1, 3, 7, 10]': 4320, '[1, 2, 8, 10]': 4320, '[1, 2, 3, 5, 10]': 2880}