You are on page 1of 3

12/8/2020 Blackjack

In [ ]: import random

#list of possible suits and values for card


suits = ('Spades', 'Hearts', 'Clubs', 'Diamonds')
name_vals = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
'Ten', 'Jack', 'Queen', 'King', 'Ace')
num_vals = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eigh
t':8, 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}

playing = True

#created card class to match suits to values


class Card:
def __init__(self, suit, name):
self.suit = suit
self.name = name
def __str__(self):
return self.name + ' of ' + self.suit

#created class to create 2 deck of cards to shuffle and deal out


class Deck:
def __init__(self):
self.deck = []
for suit in suits:
for name in name_vals:
self.deck.append(Card(suit, name))
self.deck.append(Card(suit, name))
def __str__(self):
deck_contains = ''
for card in self.deck:
deck_contains += '\n' + card.__str__()
return 'The deck has:' + deck_contains
def shuffle_deck(self):
random.shuffle(self.deck)
def deal_deck(self):
single_card = self.deck.pop()
return single_card

#hand class to add cards to empty list and caluculate value of hand
class Hand:
def __init__(self, dealer_hand=False):
self.deck = []
self.value = 0
self.aces = 0
def add_card(self,card):
self.deck.append(card)
self.value += num_vals[card.name]
def get_value(self):
if player_hand.deck(name_vals) == 'Ace':
self.aces += 1
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1
return self.value

localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 2/4
12/8/2020 Blackjack

player_chips = 10

#while playing:, add 'while true' fucntion here to start new round until quit
while playing:
print('Welcome to a new game of Blackjack!')

deck = Deck()
deck.shuffle_deck()

while True:
try:
print('You have', player_chips, 'chips.', end=' ')
bet_amount = int(input('How many would you like to bet? \n'))
if bet_amount <= 0:
print('Sorry, please enter a postitive integer!')
elif bet_amount > player_chips:
print('Sorry, you do not have enough chips')
elif bet_amount in range(0,player_chips):
player_chips -= bet_amount
print('\nYou bet', bet_amount,'chips. You have', player_chips,
'chips left.')
break
except ValueError:
print('Sorry, please enter an integer!')

print('Dealer will deal cards now...\n', '~'*30)

player_hand = Hand()
player_hand.add_card(deck.deal_deck())
player_hand.add_card(deck.deal_deck())

dealer_hand = Hand()
dealer_hand.add_card(deck.deal_deck())
dealer_hand.add_card(deck.deal_deck())

print('Dealer has', dealer_hand.deck[0], 'and one hidden card.')


print('You have', player_hand.deck[0], 'and', player_hand.deck[1],'.')

#game play starts, dealers asks player to hit or stand


while True:
user_input = str(input('Do you want to Hit or Stand?\n'))
if user_input not in ['Hit','Stand','H','h','hit','S','s','stand']:
user_input = input('Wrong choice, enter again.\n')
elif user_input in ['Hit','H','h','hit']:
player_hand.add_card(deck.deal_deck())
hand_len = len(player_hand.deck)
print('You have', end=' ')
for x in range(0, hand_len):
print(player_hand.deck[x], end=', ')
print('in your hand.')
elif user_input in ['Stand','S','s','stand']:
print('\nYou choose to stand. Your hand adds up to', player_hand.g
et_value(), '.')
break

#game play ends, calculate results

localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 3/4
12/8/2020 Blackjack

print('~'*30, '\nNow revealing dealer cards...')


print('Dealer has', dealer_hand.deck[0], 'and', dealer_hand.deck[1])
print('Dealer hand adds up to', dealer_hand.get_value(), '.')

player_score = player_hand.get_value()
dealer_score = dealer_hand.get_value()

if dealer_score <= 16:


print('Dealer hand is 16 or less, dealing one more card to dealer...')
dealer_hand.add_card(deck.deal_deck())
dealer_len = len(dealer_hand.deck)
print('Dealer has', end=' ')
for x in range(0, dealer_len):
print(dealer_hand.deck[x], end=', ')
print('in their hand.')
print('Dealer hand now adds up to', dealer_hand.get_value(), '.')
else:
pass

if (player_score > 21) and (dealer_score > 21):


print('Both hands are a bust! You lose!')
print('You have', player_chips, 'chips left.')
elif player_score > 21:
print('Your hand in a bust! You lose!')
print('You have', player_chips, 'chips left.')
elif player_score < dealer_score:
print('Dealer hand is higher! You lose!')
print('You have', player_chips, 'chips left.')
elif player_score > dealer_score:
print('Congrats! You win!')
player_chips = player_chips + (bet_amount*2)
print('You have', player_chips, 'chips left.')
elif player_score == dealer_score:
print('A tie! No one loses!')
player_chips = player_chips + bet_amount
print('You have', player_chips, 'chips.')

while True:
try:
restart = input('Would you like to play another game? Yes or no?\n
')
if restart in ['No', 'no', 'n']:
playing = False
break
elif restart in ['Yes', 'yes', 'y']:
break
continue
except ValueError:
print('Sorry, that is not a valid input!')

print('Thank you for playing Blackjack! Goodbye!')

localhost:8888/nbconvert/html/Code/Blackjack.ipynb?download=false 4/4

You might also like