Blackjack card game with hit and stand.
I wasn't allowed to use functions outside of classes in the code.
I had some difficulty with __str__
dunder method on Dealer
and Player
classes (I got weird solutions for the time being)
I would appreciate any feedback on how to improve readability and make the code more efficient.
Code
#Blackjack
#imports
import random
import time
from IPython.display import clear_output
#dictionaries
ranks = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
suits = ['Diamonds', 'Spades', 'Clubs', 'Hearts']
values = {'Ace':1, 'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10}
#card class
class Card():
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.value = values[rank]
def __str__(self):
return f'{self.rank} of {self.suit}'
#deck class
class Deck():
def __init__(self):
self.deck_cards = []
for rank in ranks:
for suit in suits:
self.deck_cards.append(Card(rank,suit))
def shuffle(self):
random.shuffle(self.deck_cards)
def dealCards(self):
return self.deck_cards.pop(0)
def __str__(self):
for item in self.deck_cards:
print(item)
return 'Joker'
#hand class
class Player():
def __init__(self):
self.hand = []
def addCard(self, card):
self.hand.append(card)
def handSum(self):
card_sum = 0
for card in self.hand:
card_sum += card.value
return card_sum
def __str__(self):
print('------------\nPlayer Cards\n------------')
for item in self.hand:
print(item)
print(f"{player_name}'s card sum: {player1.handSum()}")
return('')
#chip class
class Chips():
def __init__(self, player_name):
self.player_name = player_name
self.chip_count = 3
def betChip(self,num):
self.chip_count -= num
return num
def addChip(self,num):
self.chip_count += num
def subtractChip(self,num):
self.chip_count -= num
def __str__(self):
if self.chip_count == 1:
return f'{self.player_name} has {self.chip_count} chip!'
else:
return f'{self.player_name} has {self.chip_count} chips!'
#dealer class
class Dealer(Player):
def __str__(self):
print('------------\nDealer Cards\n------------')
for item in self.hand:
if item == self.hand[-1]:
pass
else:
print(item)
print('**********')
return str('')
#variable initialization
player_name = input('What is your name?: ')
running = False
game_on = True
gameDeck = Deck()
gameDeck.shuffle()
player1 = Player()
player1_chips = Chips(player_name)
dealer = Dealer()
#game loop
while game_on:
if player1_chips.chip_count == 0:
time.sleep(3)
clear_output()
print('You ran out of chips\nThanks for playing')
break
else:
pass
gameDeck = Deck()
gameDeck.shuffle()
player1 = Player()
dealer = Dealer()
ready_game = input('Ready for a round of BlackJack?(Y/N) ')
if ready_game == 'Y' or ready_game == 'y':
clear_output()
running = True
elif ready_game == 'N' or ready_game == 'n':
clear_output()
print('Thanks for playing!')
break
else:
clear_output()
print("Please input 'Y' or 'N'")
continue
while running:
#placing bets
bet = input(f'You have {player1_chips.chip_count} chips. How much would you like to bet? ')
clear_output()
if bet.isdigit() == True:
bet = int(bet)
if bet > player1_chips.chip_count:
print('Your bet is above your total chip count!')
time.sleep(1)
clear_output()
continue
else:
print(f'You have bet {bet} chips')
time.sleep(1)
clear_output()
else:
print('Your bet is not a number!')
time.sleep(1)
clear_output()
continue
#card initialization
print(f'Distributing card to {player_name}...')
player1.addCard(gameDeck.dealCards())
time.sleep(.85)
clear_output()
print('Distributing card to Dealer...')
dealer.addCard(gameDeck.dealCards())
time.sleep(.85)
clear_output()
print(f'Distributing card to {player_name}...')
player1.addCard(gameDeck.dealCards())
time.sleep(.85)
clear_output()
print('Distributing card to Dealer...')
dealer.addCard(gameDeck.dealCards())
time.sleep(.85)
clear_output()
print('Cards distributed')
clear_output()
print(player1)
print(dealer)
#check for 21:
if player1.handSum() == 21:
print(f'You win and get {bet * 1.5} chips')
player1_chips.addChip(bet * 1.5)
time.sleep(2)
clear_output()
running = False
else:
pass
temp_var = True
#gameplay
while temp_var:
choice = input('Hit(H) or Stand(S)? ')
if choice == 'H' or choice == 'h':
clear_output()
print(f'Distributing card to {player_name}...')
player1.addCard(gameDeck.dealCards())
time.sleep(1)
temp_var = False
elif choice == 'S' or choice == 's':
temp_var = False
clear_output()
else:
clear_output()
print("Please input 'H' or 'S'")
time.sleep(.5)
continue
#printing updated hands
print('Dealer will reveal their cards')
time.sleep(1.3)
clear_output()
print(player1)
print('')
print('------------\nDealer Cards\n------------')
[print(item) for item in dealer.hand[0::]]
print(f"Dealer's card sum: {dealer.handSum()}")
print(' \n \n ')
time.sleep(.9)
#no more user input/game continues
if player1.handSum() == 21:
if dealer.handSum() == 21:
print('You have tied with the dealer')
time.sleep(1)
print('You won no chips. Your total remains the same')
break
else:
print('You win!')
time.sleep(.9)
player1_chips.addChip(bet)
print(f'You have won {bet} chips. Bringing your total up to {player1_chips.chip_count} chips')
break
elif player1.handSum() > 21:
print('You have BUSTED\nYou Lose!')
time.sleep(1)
player1_chips.subtractChip(bet)
print(f'You lost {bet} chips. Bringing your total down to {player1_chips.chip_count} chips')
break
elif dealer.handSum() <= 16:
dealer.addCard(gameDeck.dealCards())
print('Dealer is adding a card to their hand as they have a 16 or less....')
time.sleep(3)
clear_output()
if dealer.handSum() > 21:
#printing hands
##################################################
print(player1)
print('------------\nDealer Cards\n------------')
[print(item) for item in dealer.hand[0::]]
print(f"Dealer's card sum: {dealer.handSum()}")
time.sleep(1)
##################################################
print('You win! Dealer Busts!')
time.sleep(.9)
player1_chips.addChip(bet)
print(f'You have won {bet} chips. Bringing your total up to {player1_chips.chip_count} chips')
break
elif dealer.handSum() > player1.handSum():
#printing hands
##################################################
print(player1)
print('------------\nDealer Cards\n------------')
[print(item) for item in dealer.hand[0::]]
print(f"Dealer's card sum: {dealer.handSum()}")
time.sleep(1)
##################################################
print('You Lose! Dealer got a higher sum of cards!')
time.sleep(.9)
player1_chips.subtractChip(bet)
print(f'You lost {bet} chips. Bringing your total down to {player1_chips.chip_count} chips')
break
else:
if dealer.handSum() > player1.handSum():
print('You Lose! Dealer got a higher sum of cards!')
time.sleep(1)
player1_chips.subtractChip(bet)
print(f'You lost {bet} chips. Bringing your total down to {player1_chips.chip_count} chips')
break
else:
print('You win! Dealer got a lesser sum of cards!')
time.sleep(1)
player1_chips.addChip(bet)
print(f'You have won {bet} chips. Bringing your total up to {player1_chips.chip_count} chips')
break