Skip to content

Instantly share code, notes, and snippets.

@buriy
Created April 21, 2009 17:25
Show Gist options
  • Select an option

  • Save buriy/99257 to your computer and use it in GitHub Desktop.

Select an option

Save buriy/99257 to your computer and use it in GitHub Desktop.
blackjack.py
from random import *
from math import *
#GLOBAL VARIABLES
cards = range(0,52)
def randRange(in_lower, in_upper):
temp_range = in_upper - in_lower
return int(round((temp_range+0.5)*random() + (in_lower - 0.5)))
def cardAsString(in_card):
value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
suit = ["hearts","diamonds","spades","clubs"]
return value[in_card % 13]+ " of " + suit[in_card / 13]
def popRandArray(in_list):
return in_list.pop(randRange(0, len(in_list) - 1))
def realDealCard():
global cards
if len(cards)==0:
print "new deck"
cards = range(0, 52)
return popRandArray(cards)
def cardScore(in_card):
score = in_card % 13 + 1
if score > 10:
score = 10
return score
def calculate_score(cards):
score = 0
first_ace = False
for card in cards:
card_score = cardScore(card)
if card_score == 1 and first_ace == False: # first ace can be 1 or 11
first_ace = True
# skip first ace now
else:
score += card_score
if first_ace == True:
# if score + ace <= 21 then first ace can be 11, otherwise it's 1.
if score + 11 <= 21:
score += 11
else:
score += 1
return score
def do_dealer_turn(cards):
current_score = calculate_score(cards)
# dealer takes one more card if score is < 17
if current_score < 17:
return realDealCard()
else:
return None
def do_player_turn(cards):
current_score = calculate_score(cards)
print 'Your current score is', current_score
if current_score >= 21:
return None
if len(cards) >= 2:
more = raw_input('Should I give you one more card? (yes/no) ')
if more == 'no':
return None
if more != 'yes':
print "Neither 'no' nor 'yes' means 'yes'!"
# give user first 2 cards without user interaction
card = realDealCard()
print "Dealer gives you", cardAsString(card)
return card
def play_single_set():
dealer_cards = []
player_cards = []
player_continue = True
dealer_continue = True
while player_continue or dealer_continue:
if dealer_continue:
card = do_dealer_turn(dealer_cards)
if card != None:
dealer_cards.append(card)
else:
dealer_continue = False
if player_continue:
card = do_player_turn(player_cards)
if card != None:
player_cards.append(card)
else:
player_continue = False
player_score = calculate_score(player_cards)
dealer_score = calculate_score(dealer_cards)
print "Player score:", player_score, "Dealer score:", dealer_score
print "Dealer has the following cards:"
for card in dealer_cards:
print cardAsString(card)
# "rank" is a way to compare scores
# if player has score > 21 (loser), then let rank = 0
# else let rank = score
if player_score > 21:
player_rank = 0
else:
player_rank = player_score
if dealer_score > 21:
dealer_rank = 0
else:
dealer_rank = dealer_score
# after this transform, the better rank means the better cards.
if player_rank == dealer_rank:
print "Draw"
elif player_rank > dealer_rank:
if player_rank == 21:
print "Wow! Black jack! You win with black jack!"
else:
print "You win!"
else:
print "You lose!"
def game():
sets = int(raw_input('how many sets do you want to play? '))
for i in range(sets): # for (i<0; i<sets; i++)
play_single_set()
if __name__ == "__main__":
game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment