vee-eye
12-26-2001, 08:45 PM
I started coding blackjack in Python yesterday for fun. It worked nicely, but you could only obtain cards from 1 (ace) to 10. I then added functionality for face cards, and even aces (so aces would be either 1 or 11). I was very surprised when I was able to simulate a real deck (ie. no more than four ace cards could be in play) by adding only four lines of code. Here it is, uncommented, for your tweaking pleasure!
#!/usr/bin/env python
import random
import sys
def randomCard():
card = 1+int(random.random()*13)
while deck[card] == 4:
card = 1+int(random.random()*13)
deck[card] = deck[card] + 1
if card == 1:
return "A"
elif card == 11:
return "J"
elif card == 12:
return "Q"
elif card == 13:
return "K"
return card
def checkFace(card):
if card == "A":
return 11
elif card in ["J","Q","K"]:
return 10
return card
def aceTotalDown(thetotal):
thetotal = thetotal - 10
print "The ace (11) was changed to a 1"
print "The new total is", thetotal
return thetotal
def endGame():
playAgain = raw_input("\nContinue playing? ")
if not (playAgain in ["YES","Y","yes","y"]):
sys.exit()
def startGame():
aceDeal = 0
aceHit = 0
card1 = randomCard()
card2 = randomCard()
total = checkFace(card1) + checkFace(card2)
if card1 == "A" or card2 == "A":
aceDeal = 1
if card1 == "A" and card2 == "A":
total = 12
print "Your cards are", card1, "and", card2
print "Your total is", total
if total == 21:
print "Blackjack! You win!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
while hit in ["YES","Y","yes","y"]:
newcard = randomCard()
if newcard == "A":
aceHit = 1
total = total + checkFace(newcard)
print "Your card is", newcard
print "Your new total is", total
if total > 21:
if aceDeal or aceHit:
if aceDeal:
aceDeal = 0
elif aceHit:
aceHit = 0
total = aceTotalDown(total)
if total > 21:
print "Busted!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
continue
print "Busted!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
dealerAceDeal = 0
dealerAceHit = 0
dealer1 = randomCard()
dealer2 = randomCard()
dealertotal = checkFace(dealer1) + checkFace(dealer2)
if dealer1 == "A" or dealer2 == "A":
dealerAceDeal = 1
if dealer1 == "A" and dealer2 == "A":
dealertotal = 12
print "The dealer's cards are", dealer1, "and", dealer2
print "The dealer's total is", dealertotal
while dealertotal < 17:
print "The dealer chooses to hit"
dealernew = randomCard()
if dealernew == "A":
dealerAceHit = 1
dealertotal = dealertotal + checkFace(dealernew)
print "\nThe dealer's card is", dealernew
print "The dealer's new total is", dealertotal
if dealertotal > 21:
if dealerAceDeal or dealerAceHit:
if dealerAceDeal:
dealerAceDeal = 0
elif dealerAceHit:
dealerAceHit = 0
dealertotal = aceTotalDown(dealertotal)
continue
print "The dealer is busted!"
print "You win!"
endGame()
return
print "The dealer chooses to stand"
print "\nYour total is", total
print "The dealer's total is", dealertotal
if total < dealertotal:
print "You lose!"
elif total == dealertotal:
print "You tied!"
else:
print "You win!"
endGame()
return
print "\nLet's play Blackjack!"
try:
while 1:
deck = [0]*14
startGame()
except EOFError:
print " "
except KeyboardInterrupt:
print " "
Sorry, it's 168 lines. I'm sure the length could have been halved if I had used object orientated programming, but I don't know how to do it in Python. :)
The board totally screwed up the indentation, so it won't work if you copy and paste. I'll try and upload a file containing the code on the internet for you guys to download
Here's the source: blackjack.py (http://hometown.aol.com/magicii646/blackjack.py)
[ 27 December 2001: Message edited by: vee-eye ]
#!/usr/bin/env python
import random
import sys
def randomCard():
card = 1+int(random.random()*13)
while deck[card] == 4:
card = 1+int(random.random()*13)
deck[card] = deck[card] + 1
if card == 1:
return "A"
elif card == 11:
return "J"
elif card == 12:
return "Q"
elif card == 13:
return "K"
return card
def checkFace(card):
if card == "A":
return 11
elif card in ["J","Q","K"]:
return 10
return card
def aceTotalDown(thetotal):
thetotal = thetotal - 10
print "The ace (11) was changed to a 1"
print "The new total is", thetotal
return thetotal
def endGame():
playAgain = raw_input("\nContinue playing? ")
if not (playAgain in ["YES","Y","yes","y"]):
sys.exit()
def startGame():
aceDeal = 0
aceHit = 0
card1 = randomCard()
card2 = randomCard()
total = checkFace(card1) + checkFace(card2)
if card1 == "A" or card2 == "A":
aceDeal = 1
if card1 == "A" and card2 == "A":
total = 12
print "Your cards are", card1, "and", card2
print "Your total is", total
if total == 21:
print "Blackjack! You win!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
while hit in ["YES","Y","yes","y"]:
newcard = randomCard()
if newcard == "A":
aceHit = 1
total = total + checkFace(newcard)
print "Your card is", newcard
print "Your new total is", total
if total > 21:
if aceDeal or aceHit:
if aceDeal:
aceDeal = 0
elif aceHit:
aceHit = 0
total = aceTotalDown(total)
if total > 21:
print "Busted!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
continue
print "Busted!"
endGame()
return
hit = raw_input("\nHit? ")
print " "
dealerAceDeal = 0
dealerAceHit = 0
dealer1 = randomCard()
dealer2 = randomCard()
dealertotal = checkFace(dealer1) + checkFace(dealer2)
if dealer1 == "A" or dealer2 == "A":
dealerAceDeal = 1
if dealer1 == "A" and dealer2 == "A":
dealertotal = 12
print "The dealer's cards are", dealer1, "and", dealer2
print "The dealer's total is", dealertotal
while dealertotal < 17:
print "The dealer chooses to hit"
dealernew = randomCard()
if dealernew == "A":
dealerAceHit = 1
dealertotal = dealertotal + checkFace(dealernew)
print "\nThe dealer's card is", dealernew
print "The dealer's new total is", dealertotal
if dealertotal > 21:
if dealerAceDeal or dealerAceHit:
if dealerAceDeal:
dealerAceDeal = 0
elif dealerAceHit:
dealerAceHit = 0
dealertotal = aceTotalDown(dealertotal)
continue
print "The dealer is busted!"
print "You win!"
endGame()
return
print "The dealer chooses to stand"
print "\nYour total is", total
print "The dealer's total is", dealertotal
if total < dealertotal:
print "You lose!"
elif total == dealertotal:
print "You tied!"
else:
print "You win!"
endGame()
return
print "\nLet's play Blackjack!"
try:
while 1:
deck = [0]*14
startGame()
except EOFError:
print " "
except KeyboardInterrupt:
print " "
Sorry, it's 168 lines. I'm sure the length could have been halved if I had used object orientated programming, but I don't know how to do it in Python. :)
The board totally screwed up the indentation, so it won't work if you copy and paste. I'll try and upload a file containing the code on the internet for you guys to download
Here's the source: blackjack.py (http://hometown.aol.com/magicii646/blackjack.py)
[ 27 December 2001: Message edited by: vee-eye ]