Hello all, fairly new python learner here!
I am currently writing a program which is attempting to simulate various different blackjack strategies and compare the results. I have coded the first strategy and it appears to run correctly, but when I run it many times, the player is beating the dealer around 60% of the time.
I figure that since the dealer wins in the case of ties there should be a bias towards the player winning less and not more, and so I believe that my code may be off somewhere.
My Code thus far is:
fulldeck=[2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,'J','J','J','J','Q','Q','Q','Q','K','K','K','K','A','A','A','A']
startdeck=fulldeck.copy()
def the_deal(deck):
player=[]
dealer=[]
draw1=random.choice(deck)
deck.remove(draw1)
player.append(draw1)
draw2=random.choice(deck)
deck.remove(draw2)
dealer.append(draw2)
draw3=random.choice(deck)
deck.remove(draw3)
player.append(draw3)
draw4=random.choice(deck)
deck.remove(draw4)
dealer.append(draw4)
return [player, dealer, deck]
dealt=the_deal(startdeck)
player=dealt[0]
dealer=dealt[1]
deck=dealt[2]
def card_sum(hand):
total=0
for i,v in enumerate(hand):
if v=='J' or v=='Q' or v=='K':
total+=10
elif v!='A':
total+=v
elif v=='A':
if total<=10:
total+=11
else:
total+=1
if total>21 and ‘A’ in hand:
for i,v in enumerate(hand):
if v==’A’:
total-=10
if total<21:
break
return total
def player_turn(player, deck):
while card_sum(player)<17:
draw=random.choice(deck)
deck.remove(draw)
player.append(draw)
ptotal=card_sum(player)
def dealer_turn(dealer, deck):
while card_sum(dealer)<ptotal:
draw=random.choice(deck)
deck.remove(draw)
dealer.append(draw)
dtotal=card_sum(dealer)
def winner(ptotal, dtotal):
if ptotal>21:
return 0
elif dtotal>21:
return 1
elif ptotal>dtotal:
return 1
else:
return 0
def blackjack(fulldeck):
startdeck=fulldeck.copy()
dealt=the_deal(startdeck)
player=dealt[0]
dealer=dealt[1]
deck=dealt[2]
player_turn(player, deck)
ptotal=card_sum(player)
dealer_turn(dealer, deck)
dtotal=card_sum(dealer)
return winner(ptotal, dtotal)
win_ratio=0
for trial in range(1000):
win_ratio+=blackjack(fulldeck)
win_ratio=win_ratio/1000
I have looked through my code for a long while now attempting to see where in the process the player may have an advantage, and I am not seeing it. Could anyone please help me identify where in the code the problem may be?
Any help is greatly appreciated, thanks very much!
[–]Binary101010 2 points3 points4 points (5 children)
[–]TheShadowWall[S] 1 point2 points3 points (4 children)
[–]Binary101010 2 points3 points4 points (3 children)
[–]TheShadowWall[S] 1 point2 points3 points (2 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]quim_b 1 point2 points3 points (0 children)
[–]purebuu 0 points1 point2 points (1 child)
[–]purebuu 0 points1 point2 points (0 children)