Attached below, is the full program for the simple, text-based blackjack game that I have recently finished developing for Python. Upon review, I was wondering if anyone out there would be able to offer any suggestions for any improvements to my code that could improve functionality or perhaps reduce the length of the program.
#J.Corbett
#Blackjack game prototype - text based
#04/09/17
#import lines
import time
import random
import sys
#set default integer values
PLYRcardValue = 0
DLRcardValue = 0
PLYRcard1 = 0
PLYRcard2 = 0
PLYRcard3 = 0
DLRcard1 = 0
DLRcard2 = 0
DLRcard3 = 0
#set True/False statements
play = True
PLYR_ace1 = False
PLYR_ace2 = False
PLYR_ace3 = False
DLR_ace1 = False
DLR_ace2 = False
DLR_ace3 = False
print("Blackjack Protoype - Version: 101\n\n")
#"open file" function - for reading
def readFile(filename):
myList = []
file = open(filename,"r")
for line in file:
currentLine = line.strip().split(",")
myList.append(currentLine[0])
return myList
#open external file "deck" and assign to array
deck = readFile("deck.txt")
#"check if a card is an Ace" function
def checkAce (card,deck,ace):
if card == deck[0] or card == deck[13] or card == deck[26] or card == deck[39]:
ace = True
else:
ace = False
return card, ace
#calling function example: PLYRcard1, PLYR_ace1 = checkAce(PLYRcard1, deck, PLYR_ace1)
#"calculate value of dealt hand" function
def HandValueCalc(card,hand,deck):
card = deck[random.randint(0,51)]
if card == deck[0]:
hand += 1
elif card == deck[1]:
hand += 2
elif card == deck[2]:
hand += 3
elif card == deck[3]:
hand += 4
elif card == deck[4]:
hand += 5
elif card == deck[5]:
hand += 6
elif card == deck[6]:
hand += 7
elif card == deck[7]:
hand += 8
elif card == deck[8]:
hand += 9
elif card == deck[9]:
hand += 10
elif card == deck[10]:
hand += 10
elif card == deck[11]:
hand += 10
elif card == deck[12]:
hand += 10
elif card == deck[13]:
hand += 1
elif card == deck[14]:
hand += 2
elif card == deck[15]:
hand += 3
elif card == deck[16]:
hand += 4
elif card == deck[17]:
hand += 5
elif card == deck[18]:
hand += 6
elif card == deck[19]:
hand += 7
elif card == deck[20]:
hand += 8
elif card == deck[21]:
hand += 9
elif card == deck[22]:
hand += 10
elif card == deck[23]:
hand += 10
elif card == deck[24]:
hand += 10
elif card == deck[25]:
hand += 10
elif card == deck[26]:
hand += 1
elif card == deck[27]:
hand += 2
elif card == deck[28]:
hand += 3
elif card == deck[29]:
hand += 4
elif card == deck[30]:
hand += 5
elif card == deck[31]:
hand += 6
elif card == deck[32]:
hand += 7
elif card == deck[33]:
hand += 8
elif card == deck[34]:
hand += 9
elif card == deck[35]:
hand += 10
elif card == deck[36]:
hand += 10
elif card == deck[37]:
hand += 10
elif card == deck[38]:
hand += 10
elif card == deck[39]:
hand += 1
elif card == deck[40]:
hand += 2
elif card == deck[41]:
hand += 3
elif card == deck[42]:
hand += 4
elif card == deck[43]:
hand += 5
elif card == deck[44]:
hand += 6
elif card == deck[45]:
hand += 7
elif card == deck[46]:
hand += 8
elif card == deck[47]:
hand += 9
elif card == deck[48]:
hand += 10
elif card == deck[49]:
hand += 10
elif card == deck[50]:
hand += 10
else:
hand += 10
return card, hand
#calling function example: PLYRcard1, PLYRcardValue = HandValueCalc(PLYRcard1,PLYRcardValue,deck)
#"check if hand is bust" function
def checkBust (handValue):
if handValue > 21:
bust = True
print("Gone bust!")
else:
bust = False
return bust
#calling function example: PLYRbust = checkBust(PLYRcardValue)
#initiate game
if play == True:
#setvalues for the players cards, and check if any are aces
PLYRcard1, PLYRcardValue = HandValueCalc(PLYRcard1, PLYRcardValue, deck)
PLYRcard2, PLYRcardValue = HandValueCalc(PLYRcard2, PLYRcardValue, deck)
PLYRcard1, PLYR_ace1 = checkAce(PLYRcard1, deck, PLYR_ace1)
PLYRcard2, PLYR_ace2 = checkAce(PLYRcard2, deck, PLYR_ace2)
time.sleep(1)
print("You have been dealt these two cards:",PLYRcard1,"and",PLYRcard2)
time.sleep(1)
print("The value of your hand is:",PLYRcardValue)
time.sleep(1)
#let player choose value for Ace, if an ace is present (potential for a function?)
if PLYR_ace1 == True or PLYR_ace2 == True:
ace_choice = input("You have been dealt an ace. The ace can be valued one, or eleven. \nWhich value do you want?:")
if ace_choice == 1:
PLYRcardValue += 1
elif ace_choice == 11:
PLYRcardValue += 11
else:
print("Unknown Error: Please restart the program.")
sys.exit()
print("The value of your hand is now:",PLYRcardValue)
choice = input("\nWould you like to stick or split?:")
if choice == "split":
#set value for the players third card, and check if an ace is present
PLYRcard3, PLYRcardValue = HandValueCalc(PLYRcard3, PLYRcardValue, deck)
PLYRcard3, PLYR_ace3 = checkAce(PLYRcard3, deck, PLYR_ace3)
print("You drew:", PLYRcard3)
time.sleep(0.5)
#choice for value of ace
if PLYR_ace3 == True:
ace_choice = input("You have been dealt an ace. The ace can be valued one, or eleven. \nWhich value do you want?:")
if ace_choice == 1:
PLYRcardValue += 1
elif ace_choice == 11:
PLYRcardValue += 11
else:
print("Unknown Input: Please restart the program.")
sys.exit()
print("Your hand is now worth:",PLYRcardValue)
time.sleep(1)
#check if the hand is bust
PLYRbust = checkBust(PLYRcardValue)
#CPU's turn
#set values for dealers hand, check if aces
DLRcard1, DLRcardValue = HandValueCalc(DLRcard1, DLRcardValue, deck)
DLRcard2, DLRcardValue = HandValueCalc(DLRcard2, DLRcardValue, deck)
DLRcard1, DLR_ace1 = checkAce(DLRcard1, deck, DLR_ace1)
DLRcard2, DLR_ace2 = checkAce(DLRcard2, deck, DLR_ace2)
time.sleep(1)
#random choice for ace value
if DLR_ace1 == True or DLR_ace2 == True:
ace_DLR = random.randint(1,2)
if ace_DLR == 1:
DLRcardValue+= 1
print("The dealer has decided to value the Ace at: 1")
elif ace_DLR == 2:
DLRcardValue += 11
print("The dealer has decided to value the Ace at: 11")
else:
print("Unknown Error: Please restart the program.")
sys.exit()
print("\nThe Dealer's hand is worth:",DLRcardValue)
#random choice for dealer splitting
DLRchoice = random.randint(1,3)
time.sleep(3)
if DLRchoice == 3:
print("\nThe dealer has decided to split.")
#set value for dealers third card, check if ace
DLRcard3, DLRcardValue = HandValueCalc(DLRcard3, DLRcardValue, deck)
DLRcard3, DLR_ace3 = checkAce(DLRcard3, deck, DLR_ace3)
#ace's value, random choice
if DLR_ace3 == True:
ace_DLR = random.randint(1,2)
if ace_DLR == 1:
DLRcardValue+= 1
elif ace_DLR == 2:
DLRcardValue += 11
else:
print("Unknown Error: Please restart the program.")
sys.exit()
print("The Dealer's hand is now worth:",DLRcardValue)
else:
print("\nThe dealer has decided to stick.")
time.sleep(1)
#check if dealer's hand is bust
DLRbust = checkBust(DLRcardValue)
time.sleep(2)
#Decide winner
if PLYRbust == True and DLRbust == True and DLRcardValue < PLYRcardValue:
print("\nThe Dealer is closer to 21! \nDealer wins!")
elif PLYRbust == True and DLRbust == True and DLRcardValue > PLYRcardValue:
print("\nThe Player is closer to 21! \nPlayer wins!")
elif PLYRbust == True and DLRbust == False:
print("\nThe Dealer is closer to 21! \nDealer wins!")
elif PLYRbust == False and DLRbust == True:
print("\nThe Player is closer to 21! \nPlayer wins!")
elif PLYRcardValue > DLRcardValue and PLYRcardValue < 21:
print("\nThe Player is closer to 21! \nPlayer wins!")
elif PLYRcardValue < DLRcardValue and DLRcardValue < 21:
print("\nThe Dealer is closer to 21! \nDealer wins!")
elif PLYRcardValue == DLRcardValue:
print("It's a draw!")
else:
print("\nUnknown error, please Restart the program.")