Text based command line blackjack. Hit and Stand are the only available options. Clean, commented code. Comment out any "clear()" statements you see if you want to run it in Idle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #Blackjack by Mike McGowan
#It's Monday, June 25, 2007 as of now, but I'm pretty sure I finished this
#a week or two ago. I cleaned it up yesterday.
import random as r
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]*4
dealer = []
player = []
c = 'y'
#Clear works only if you run it from command line
def clear():
import os
if os.name == 'nt':
os.system('CLS') #Pass CLS to cmd
if os.name == 'posix':
os.system('clear') #Pass clear to terminal
def showHand():
hand = 0
for i in player: hand += i #Tally up the total
print "The dealer is showing a %d" % dealer[0]
print "Your hand totals: %d (%s)" % (hand, player)
#Gives player and dealer their cards
def setup():
for i in range(2):
dealDealer = deck[r.randint(1, len(deck)-1)]
dealPlayer = deck[r.randint(1, len(deck)-1)]
dealer.append(dealDealer)
player.append(dealPlayer)
deck.pop(dealDealer)
deck.pop(dealPlayer)
setup()
while c != 'q':
showHand()
c = raw_input("[H]it [S]tand [Q]uit: ").lower()
clear()
if c == 'h':
dealPlayer = deck[r.randint(1, len(deck)-1)]
player.append(dealPlayer)
deck.pop(dealPlayer)
hand = 0
for i in dealer: hand += i
if not hand > 17: #Dealer strategy.
dealDealer = deck[r.randint(1, len(deck)-1)]
dealer.append(dealDealer)
deck.pop(dealDealer)
hand = 0
for i in player: hand += i
if hand > 21:
print "BUST!"
player = [] #Clear player hand
dealer = [] #Clear dealer's hand
setup() #Run the setup again
hand = 0
for i in dealer: hand +=i
if hand > 21:
print "Dealer Busts!"
player = []
dealer = []
setup()
elif c == 's':
dHand = 0 #Dealer's hand total
pHand = 0 #Player's hand total
for i in dealer: dHand += i
for i in player: pHand += i
if pHand > dHand:
print "FTW!" #If playerHand (pHand) is greater than dealerHand (dHand) you win...
dealer = []
player = []
setup()
else:
print "FTL!" #...otherwise you loose.
dealer = []
player = []
setup()
else:
if c == 'q':
gb = raw_input("Toodles. [Hit Enter]")
else:
print "Invalid choice."
|
Why text blackjack? I wanted to make my first complicated program. I was watching Pirates of Silicon Valley and I got to the scene where one guy goes "I just made blackjack!" and then I wanted to make blackjack.
Issues: *Occasional IndexError. A try/except statement should help fix that.
an oo version?? i think it would be easier to understand and even funner to program if you used an object oriented aproach with class's give me a couple days and i'll submit one :)
and pirates of silicon valley rocked :)
Fun game. I think there is a problem in the following sequence.
dealDealer is a card value, but the list pop() is an offset, and here refers the position of the card in the deck. The effect is that the card that is pop'ed is not the one that has been delt.
I enjoyed the game (thanks Mike) so has a go at a re-write:
(comment continued...)
(...continued from previous comment)