Click to See Complete Forum and Search --> : More Python Blackjack!


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 ]

Gnu/Vince
12-26-2001, 09:33 PM
I would be interested to get the source vee-eye (guess why? :))

vee-eye
12-26-2001, 10:32 PM
Originally posted by Gnu/Vince:
<STRONG>I would be interested to get the source vee-eye (guess why? :))</STRONG>

Hehe. Just try doing this in Ruby. ;)

blackjack.py (http://hometown.aol.com/magicii646/blackjack.py)

I don't have a domain name or a web server, but fortunately AOL provides free web space without any screwy agreements, like with geocities. Not that I use AOL as my ISP or anything...

inkedmn
12-27-2001, 08:22 PM
very impressive my friend :)

kmj
12-27-2001, 09:13 PM
very cool; if you'd like, you can comment it up and I'll post it at CCAE. :D :D :D

vee-eye
12-28-2001, 12:28 AM
Originally posted by kmj:
<STRONG>very cool; if you'd like, you can comment it up and I'll post it at CCAE. :D :D :D</STRONG>

I actually just started commenting it for the heck of it. Maybe I'll send it to ya. ;)

I also tried adding support for suit symbols (Spades, Clubs, Hearts, and Diamonds). I got it working with symbols appearing randomly, but I decided that the symbols were kind of distracting and not necessary at all, so I took them out.

Haha! I've gotten so used to vim that I actually started using vim commands while typing this.

:cool: Vim (http://www.vim.org) :cool:

[ 27 December 2001: Message edited by: vee-eye ]

Gnu/Vince
12-28-2001, 01:04 AM
Originally posted by vee-eye:
<STRONG>

:cool: Vim (http://www.vim.org) :cool:

</STRONG>

Hell Yeah!