Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Blackjack Project
- import random
- def symbol_repl(card_str):
- return card_str.replace('S','\u2660').replace('H','\u2665').replace('C','\u2663').replace('D','\u2666')
- def create_deck():
- deck =[]
- suits = ['C','D','H','S']
- card_val = ['A','J','K','Q','T','9','8','7','6','5','4','3','2']
- for suit in suits:
- for card in card_val:
- deck += [symbol_repl(card + suit)]
- return deck
- def eval(hand):
- score = 0
- ace = False
- card_val = {'A':11,'K':10,'Q':10,'J':10,'T':10,'9':9,'8':8,'7':7,'6':6,'5':5,'4':4,'3':3,'2':2}
- for card in hand:
- score += card_val[card[0]]
- for card in hand:
- if('A' in card):
- ace = True
- if(score > 21 and ace == True):
- score -= 10
- return score
- # def count(players):
- # players = int(input("How many players are there? (1-3): "))
- # if(players == 1):
- # player1_hand = []
- # elif(players == 2):
- # player2_hand = []
- # elif(players == 3):
- # player3_hand = []
- deck = create_deck()
- random.shuffle(deck)
- dealer_hand = []
- player1_hand = []
- cardnum = 0
- replay = True
- while(replay):
- # initial card deal
- player1_hand += [deck[cardnum]]
- cardnum += 1
- dealer_hand += [deck[cardnum]]
- cardnum += 1
- player1_hand += [deck[cardnum]]
- cardnum += 1
- dealer_hand += [deck[cardnum]]
- cardnum += 1
- print("Player 1's hand:", player1_hand[0], player1_hand[1], "\nDealer's hand: ??", dealer_hand[1])
- print("\n***************", "\nYour score:", eval(player1_hand))
- # split
- # if(player1_hand[0][0] == [1][0]):
- # split = input("Do you want to split your hand? (y/n): ")
- # if(split == 'y'):
- #TODO: work on splitting hands
- # blackjack detection
- blackjack = False
- if(eval(player1_hand) == 21):
- print("Natural blackjack!")
- blackjack = True
- elif(eval(dealer_hand) == 21):
- print("Dealer blackjack!")
- blackjack = True
- # player hit or stand
- while(blackjack == False and eval(player1_hand) <= 21):
- hit_stand = input("Do you want to hit or stand?: ").lower()
- print("\n***************")
- if(hit_stand[0] == 'h'):
- player1_hand += [deck[cardnum]]
- cardnum += 1
- if(eval(player1_hand) > 21):
- print("Player 1's hand:", player1_hand, "\n***************")
- break
- print("Player 1's hand:", player1_hand, "\nDealer's hand: ??", dealer_hand[1] )
- print("\n***************", "\nYour score:", eval(player1_hand))
- elif(hit_stand[0] == 's'):
- break
- # dealer hit or stand
- while(blackjack == False and eval(dealer_hand) < 17 ):
- print("Dealer hits.")
- dealer_hand += [deck[cardnum]]
- cardnum += 1
- print("Dealer's hand:", dealer_hand, "\n***************")
- # hand evaluations
- if(eval(player1_hand) > eval(dealer_hand) and eval(player1_hand) <= 21):
- print("You win!", "\nYour hand:", player1_hand,"\nDealer's hand:", dealer_hand)
- elif(eval(player1_hand) < eval(dealer_hand) and eval(dealer_hand) <= 21):
- print("Dealer wins!","\nDealer's hand:", dealer_hand)
- elif(eval(player1_hand) > 21 and eval(dealer_hand) <= 21):
- print("You bust! Dealer won!", "\nYour hand:", player1_hand,"\nDealer's hand:", dealer_hand)
- elif(eval(player1_hand) <= 21 and eval(dealer_hand) > 21):
- print("Player hand:", player1_hand, "\nYou win! Dealer bust!")
- elif(eval(player1_hand) > 21 and eval(dealer_hand) > 21):
- print("Both busted!")
- elif(eval(player1_hand) == eval(dealer_hand)):
- print("Player's hand:", player1_hand,"\nDealer's hand:", dealer_hand, "\nPush!")
- replay = input("Do you want to play again? (y/n): ").lower()
- print("\n***************")
- deck = create_deck()
- random.shuffle(deck)
- dealer_hand = []
- player1_hand = []
- cardnum = 0
- if(replay != 'y'):
- replay = False
- #TODO: figure out splitting cards
- #TODO: figure out multiple players
Add Comment
Please, Sign In to add comment