In this milestone project you will be creating a Complete BlackJack Card Game in Python. Here are the requirements: You need to create a simple text-based BlackJack game The game needs to have one player versus an automated dealer. The player can stand or hit. The player must be able to pick their betting amount. You need to keep track of the players total money. You need to alert the player of wins, losses, or busts, etc... And most importantly: You must use OOP and classes in some portion of your game. You can not just use functions in your game. Use classes to help you define the Deck and the Player's hand. There are many right ways to do this, so explore it well! Feel free to expand this game-try including multiple players. Try adding in Double-Down and card splits! Remember, you are free to use any resources you want and as always: HAVE FUN!
In [20]:
# modules to import
import random
from IPython.display import clear_output
In [21]:
# class for each blackjack player
class Gambler(object):
def __init__(self,name='Player 1',bank=100,sex='M',hand=[],bet=5):
self.name = name
self.bank = bank
self.sex = sex
self.hand = hand
self.bet = bet
return
def setName(self):
self.name = input('Enter your name: ')
return
def anteUp(self):
if self.bank == 0:
return 1
self.bet = input('Your bank balance is %s. Enter your bet: ' %self.bank)
self.bet = int_check(self.bet)
while self.bet > self.bank:
print('bet is too large. you have insufficient funds.')
return self.anteUp()
while self.bet < 1:
print('bet must be greater than 0')
return self.anteUp()
self.bank -= self.bet
return
def hand_status(self):
print(self.name,'Hand:',self.hand,'Current Sum:',self.current_sum())
def current_sum(self):
y = 0
for i in self.hand:
z = str(i[:-1])
ace = 0
if z == 'J' or z == 'Q' or z == 'K':
z = 10
y += z
elif z == 'A':
ace = 1
z = 11
y += int(z)
else:
y += int(z)
x = ace_test(y,ace)
return x
def win(self):
self.bank += self.bet*2
return
def tie(self):
self.bank += self.bet
return
# class for the deck
class Deck(object):
def __init__(self):
self.spades = ['2\u2660','3\u2660','4\u2660','5\u2660','6\u2660','7\u2660','8\u2660','9\u2660','10\u2660','J\u2660','Q\u2660','K\u2660','A\u2660']
self.hearts = ['2\u2665','3\u2665','4\u2665','5\u2665','6\u2665','7\u2665','8\u2665','9\u2665','10\u2665','J\u2665','Q\u2665','K\u2665','A\u2665']
self.diamonds = ['2\u2666','3\u2666','4\u2666','5\u2666','6\u2666','7\u2666','8\u2666','9\u2666','10\u2666','J\u2666','Q\u2666','K\u2666','A\u2666']
self.clubs = ['2\u2663','3\u2663','4\u2663','5\u2663','6\u2663','7\u2663','8\u2663','9\u2663','10\u2663','J\u2663','Q\u2663','K\u2663','A\u2663']
self.full_deck = self.spades + self.hearts + self.diamonds + self.clubs
return
def shuffle(self):
random.shuffle(self.full_deck)
return
def next_card(self):
return self.full_deck.pop()
In [22]:
# error proofing and quitting funcitons
def keep_playing(str_text):
i = 1
while i == 1:
i = 0
continue_playing = input(str_text)
if continue_playing == 'Y':
return 1
elif continue_playing == 'N':
return 0
else:
print('invalid entry. enter only Y or N')
i = 1
def int_check(val):
while True:
try:
val = int(val)
return val
except:
print('That\'s not a number!')
val = input("Please enter a number: ")
continue
else:
break
def ace_test(y,ace):
if y > 21 and ace == 1:
return y-10
else:
return y
def status_check(x):
if x > 21:
return 0
elif x == 21:
return 1
else:
return 2
In [25]:
def blackjack():
for num in range(2):
deal_to_player()
deal_to_dealer()
player.hand_status()
dealer.hand_status()
if status_check(player.current_sum()) == 1:
print('blackjack! you win!')
player.bank += player.bet * 2
break
if status_check(dealer.current_sum()) == 1:
print('blackjack! dealer wins!')
break
while 1:
if keep_playing('Hit? (Y/N): ') == 1:
deal_to_player()
if status_check(player.current_sum()) == 0:
print('you busted!')
break
elif status_check(player.current_sum()) == 1:
print('blackjack! you win!')
player.bank += player.bet * 2
break
# deal to the dealer and check if bust
if dealer.current_sum() < 17:
deal_to_dealer()
dealer.hand_status()
if dealer.current_sum() > 21:
print('dealer busted. you win!')
break
elif dealer.current_sum() == 21:
print('blackjack! dealer win!')
break
else:
continue
# check to see if anyone has a blackjack
# check to see if player wants to hit
# check to see if plaer has a blackjack
# check to see if player busts
# check to see if delaer wants to hit
# check to see if dealer has a blackjack
# check to see if dealer busts
# reprint hand status
return
def deal_to_player():
x = cards.full_deck.pop()
player.hand.append(x)
return
def deal_to_dealer():
x = cards.full_deck.pop()
dealer.hand.append(x)
return
In [26]:
# actual loop for the program
while 1:
# Burnside Blackjack
print('Welcome to the Burnside Casino. It\'s time for Blackjack!')
# initialize deck and shuffle it
cards = Deck()
cards.shuffle()
print(cards.full_deck)
# initialize the player and dealer
player = Gambler('Player1',bank=100,sex='M',hand=[])
player.setName()
dealer = Gambler('Dealer',bank=1000000,sex='M',hand=[])
broke = player.anteUp()
if broke == 1:
print('you loser. you are broke! you are kicked out of the casino!')
break
blackjack()
# test to see if we should continue playing
if keep_playing('Continue playing? (Y/N): ') == 1:
clear_output()
continue
else:
# clear_output()
print('thanks for playing')
break
In [ ]:
print(cards.full_deck)
In [6]:
print(player.name)
In [ ]: