I'm very new to python (been coding for about two days) and have created a programme that simulates blackjack games so that I can work out optimal strategy over many iterations.
The problem is that when I run it, my CPU goes to about 99.8% and it freezes (I run in terminal) - Does anybody have any suggestions on how I can change my code so that it is less CPU intensive? - I'm looking to learn about what I've done wrong, the code is just a bit of fun!
As I said I'm new to this and as such the code isn't very elegant, but it works just fine.
import random
q = 0
#simple counter that will record the score of games over time
win_count = 0
draw_count = 0
lose_count = 0
bust_count = 0
#User input for when the game is run, it will allow them to choose the number of games to be played (iterations) and also the score at which the player will hold.
iterations = input('Number of iterations: ')
hold_score = input('Hold score for the player: ')
#defining the card values, suits and decks.
for x in range(0, iterations):
s2 = 2
s3 = 3
s4 = 4
s5 = 5
s6 = 6
s7 = 7
s8 = 8
s9 = 9
s10 = 10
sj = 10
sq = 10
sk = 10
sa = 11
c2 = 2
c3 = 3
c4 = 4
c5 = 5
c6 = 6
c7 = 7
c8 = 8
c9 = 9
c10 = 10
cj = 10
cq = 10
ck = 10
ca = 11
d2 = 2
d3 = 3
d4 = 4
d5 = 5
d6 = 6
d7 = 7
d8 = 8
d9 = 9
d10 = 10
dj = 10
dq = 10
dk = 10
da = 11
h2 = 2
h3 = 3
h4 = 4
h5 = 5
h6 = 6
h7 = 7
h8 = 8
h9 = 9
h10 = 10
hj = 10
hq = 10
hk = 10
ha = 11
spades = [s2, s3, s4, s5, s6, s7, s8, s9, s10, sj, sq, sk, sa]
clubs = [c2, c3, c4, c5, c6, c7, c7, c8, c9, c10, cj, cq, ck, ca]
diamonds = [d2, d3, d4, d5, d6, d7, d8, d9, d10, dj, dq, dk, da]
hearts = [h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk, ha]
deck = [s2, s3, s4, s5, s6, s7, s8, s9, s10, sj, sq, sk, sa, c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck, ca, d2, d3, d4, d5, d6, d7, d7, d8, d9, d10, dj, dq, dk, da, h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk, ha]
deck_standard = [s2, s3, s4, s5, s6, s7, s8, s9, s10, sj, sq, sk, sa, c2, c3, c4, c5, c6, c7, c8, c9, c10, cj, cq, ck, ca, d2, d3, d4, d5, d6, d7, d7, d8, d9, d10, dj, dq, dk, da, h2, h3, h4, h5, h6, h7, h8, h9, h10, hj, hq, hk, ha]
#List for the cards the dealer and player holds
dealer = []
user_1 = []
#Randomly gives the player and dealer two cards each - and then remove these cards from the deck of cards so it cannot be used again.
card_1 = random.choice(deck)
deck.remove(card_1)
user_1.append(card_1)
card_2 = random.choice(deck)
deck.remove(card_2)
dealer.append(card_2)
card_3 = random.choice(deck)
deck.remove(card_3)
user_1.append(card_3)
card_4 = random.choice(deck)
deck.remove(card_4)
dealer.append(card_4)
#Calculates the number of cards the player/dealer has - also defines their scores to be zero.
dealer_card_count = len(dealer)
user_1_card_count = len(user_1)
dealer_score = 0
user_1_score = 0
#Looks at the two cards each player has and calculates their score - then adds this score to their score.
for x in range(0, dealer_card_count):
dealer_score = dealer_score + int(dealer[x])
for x in range(0, user_1_card_count):
user_1_score = user_1_score + int(user_1[x])
#code to say that if the user has less cards than the pre-defined hold score - give them another card and add that card to their score.
while (int(user_1_score) <= int(hold_score)):
if (int(user_1_card_count)<6):
card_5 = random.choice(deck)
deck.remove(card_5)
user_1.append(card_5)
user_1_score = 0
user_1_card_count = len(user_1)
for x in range(0, user_1_card_count):
user_1_score = user_1_score + int(user_1[x])
# sets the hold score for the dealer as that of the user (so that the dealer knows how high he has to aim for)
hold_score_dealer = user_1_score
# Same as above - but for the dealer.
if (int(user_1_score) < int(21)):
while (int(dealer_score) <= int(hold_score_dealer)):
if (int(dealer_card_count)<6):
card_6 = random.choice(deck)
deck.remove(card_6)
dealer.append(card_6)
dealer_score = 0
dealer_card_count = len(dealer)
for x in range(0, dealer_card_count):
dealer_score = dealer_score + int(dealer[x])
#code so that if the user or the dealer have 5 cards they win automatically (5 card rule)
if (int(user_1_card_count) ==5):
if (int(dealer_card_count) !=5):
if (int(user_1_score)) <22:
win_count = win_count + 1
if (int(user_1_card_count) == 5):
if (int(dealer_card_count) == 5):
if (int(user_1_score) < 22):
if (int(dealer_score) <22):
draw_count = draw_count + 1
elif (int(user_1_score) > 22):
bust_count = bust_count + 1
if (int(dealer_card_count) == 5):
if(int(user_1_card_count) != 5):
if (int(dealer_score) <22):
lose_count = lose_count + 1
#Code to deal with all possible outcomes (assuming they don't have 5 cards) - and add to the counter.
if (int(user_1_card_count) != 5):
if (int(dealer_card_count) != 5):
if (int(user_1_score) > int(dealer_score)):
if (int(user_1_score) < int(22)):
win_count = win_count + 1
elif (int(user_1_score) > int(21)):
if (int(dealer_score) < int(22)):
lose_count = lose_count + 1
elif (int(user_1_score) > int(21)):
if (int(dealer_score) > int (21)):
draw_count = draw_count + 1
bust_count = bust_count + 1
elif (int(user_1_score) < int(dealer_score)):
if(int(user_1_score) > 21):
draw_count = draw_count + 1
bust_count = bust_count + 1
if (int(user_1_card_count) != 5):
if (int(dealer_card_count) != 5):
if (int(user_1_score) == int(dealer_score)):
if (int(user_1_score)) < int(22):
draw_count = draw_count + 1
elif (int(user_1_score) > 21):
draw_count = draw_count + 1
bust_count = bust_count + 1
if (int(user_1_card_count) != 5):
if (int(dealer_card_count) != 5):
if (int(user_1_score) < int(dealer_score)):
if (int(dealer_score) < int(22)):
lose_count = lose_count + 1
elif (int(dealer_score) > int(21)):
if (int(user_1_score) < int(22)):
win_count = win_count + 1
#Code to print the progress of the iterations over time
if (q == iterations * 0.2):
print "20%"
if (q == iterations * 0.4):
print "40%"
if (q == iterations * 0.6):
print "60%"
if (q == iterations * 0.8):
print "80%"
if (q == iterations * 1):
print "100%"
gc.collect()
print q
q = q + 1
# Code to print the final scores after all iterations are complete.
print "Total iterations: " + str(iterations)
print "*****"
print "Win count : " + str(win_count) +" ("+ str((float(win_count))/float(iterations)*100) +"%)"
print "Draw count : " + str(draw_count) +" ("+ str((float(draw_count))/float(iterations)*100) +"%)"
print "Lose count : " + str(lose_count) +" ("+ str((float(lose_count))/float(iterations)*100) +"%)"
print "Bust count : " + str(bust_count) +" ("+ str((float(bust_count))/float(iterations)*100) +"%)"
print ""