Skip to content

Instantly share code, notes, and snippets.

@Ugrend
Created June 2, 2014 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ugrend/7db3e08f8108236e52cf to your computer and use it in GitHub Desktop.
Save Ugrend/7db3e08f8108236e52cf to your computer and use it in GitHub Desktop.
crappy blackjack
#Crappy Blackjack
import random
import sys
def init_card_deck():
cards = [{11:"ACE OF HEARTS"},
{11:"ACE OF DIAMONDS"},
{11:"ACE OF SPADES"},
{11:"ACE OF CLUBS"},
{10:"KING OF HEARTS"},
{10:"QUEEN OF HEARTS"},
{10:"JACK OF HEARTS"},
{10:"TEN OF HEARTS"},
{9:"NINE OF HEARTS"},
{8:"EIGHT OF HEARTS"},
{7:"SEVEN OF HEARTS"},
{6:"SIX OF HEARTS"},
{5:"FIVE OF HEARTS"},
{4:"FOUR OF HEARTS"},
{3:"THREE OF HEARTS"},
{2:"TWO OF HEARTS"},
{10:"KING OF DIAMONDS"},
{10:"QUEEN OF DIAMONDS"},
{10:"JACK OF DIAMONDS"},
{10:"TEN OF DIAMONDS"},
{9:"NINE OF DIAMONDS"},
{8:"EIGHT OF DIAMONDS"},
{7:"SEVEN OF DIAMONDS"},
{6:"SIX OF DIAMONDS"},
{5:"FIVE OF DIAMONDS"},
{4:"FOUR OF DIAMONDS"},
{3:"THREE OF DIAMONDS"},
{2:"TWO OF DIAMONDS"},
{10:"KING OF CLUBS"},
{10:"QUEEN OF CLUBS"},
{10:"JACK OF CLUBS"},
{10:"TEN OF CLUBS"},
{9:"NINE OF CLUBS"},
{8:"EIGHT OF CLUBS"},
{7:"SEVEN OF CLUBS"},
{6:"SIX OF CLUBS"},
{5:"FIVE OF CLUBS"},
{4:"FOUR OF CLUBS"},
{3:"THREE OF CLUBS"},
{2:"TWO OF CLUBS"},
{10:"KING OF SPADES"},
{10:"QUEEN OF SPADES"},
{10:"JACK OF SPADES"},
{10:"TEN OF SPADES"},
{9:"NINE OF SPADES"},
{8:"EIGHT OF SPADES"},
{7:"SEVEN OF SPADES"},
{6:"SIX OF SPADES"},
{5:"FIVE OF SPADES"},
{4:"FOUR OF SPADES"},
{3:"THREE OF SPADES"},
{2:"TWO OF SPADES"}
]
return cards
def shuffle_cards(cards):
shuffle_count = 0
while shuffle_count < 1000:
random.shuffle(cards)
shuffle_count += 1
def deal_card(player,cards,user_total,user_str_names,print_card=False):
player.append(cards[0])
card_dealt = cards[0]
for key, value in cards[0].iteritems():
user_total.append(key)
user_str_names.append(value)
if print_card:
print "Card Dealt: %s" % value
cards.pop(0)
def calculate_total_count(total):
if sum(total) > 21:
for i in range( len( player_total ) ):
if total[i] == 11:
total[i] = 1
if sum(total) <= 21:
break
return sum(total)
cards = init_card_deck()
shuffle_cards(cards)
dealer_cards = []
player_cards = []
dealer_total = []
player_total = []
dealer_str_names = []
player_str_names = []
deal_card(dealer_cards,cards,dealer_total,dealer_str_names)
deal_card(player_cards,cards,player_total,player_str_names)
deal_card(dealer_cards,cards,dealer_total,dealer_str_names)
deal_card(player_cards,cards,player_total,player_str_names)
print "DEALERS HAND: %s, UNKNOWN" % dealer_str_names[0]
print "YOUR HAND: %s" % ", ".join(player_str_names)
while True:
total_count_for_player = calculate_total_count(player_total)
if total_count_for_player > 21:
print "You have bust!!!! You Lose!!! Total: %s with cards: %s" %(total_count_for_player, ", ".join(player_str_names))
sys.exit(0)
break
hit_me = raw_input("Would you like another card? (Y/N): ")
if hit_me.lower() == "y":
deal_card(player_cards,cards,player_total,player_str_names,True)
else:
break
total_count_for_dealer = calculate_total_count(dealer_total)
while calculate_total_count(dealer_total) < 17:
deal_card(dealer_cards,cards,dealer_total,dealer_str_names)
if calculate_total_count(dealer_total) > 21:
print "Dealer Bust!!! You Win!!!"
elif calculate_total_count(player_total) > calculate_total_count(dealer_total):
print "Score higher than Dealer!! You Win!!!"
elif calculate_total_count(player_total) == calculate_total_count(dealer_total):
print "Score Equal.. Draw!!"
else:
print "Score Lower than Dealer!! You Lose!!!"
print "Your Hand: %s Total: %s" % (", ".join(player_str_names), str(calculate_total_count(player_total)))
print "Dealers Hand: %s Total: %s" % (", ".join(dealer_str_names), str(calculate_total_count(dealer_total)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment