import random
import sys
cards = [
'2','3','4','5','6','7','8','9','10','J','Q','K','A',
'2','3','4','5','6','7','8','9','10','J','Q','K','A',
'2','3','4','5','6','7','8','9','10','J','Q','K','A',
'2','3','4','5','6','7','8','9','10','J','Q','K','A',
]
class Player():
def __init__(self, name, hand, score):
self.name = name
self.hand = hand
self.score = score
def game_intro(self):
print(" \n ========= Welcome to Blackjack ====== " )
print(self.name, "Will be dealt first")
def game_logic(self):
self.game_intro()
self.deal_cards()
self.table_score()
player1.player_decision()
def dealer_logic(self):
print(" \n Dealers Turn... ")
if self.score <= 16:
print(" \n ________ <> Dealer Hits <> __________")
self.hit()
self.get_score(self.hand)
self.dealer_logic()
elif self.score == 21:
self.final_score()
print(" \n Dealer Black Jack - Dealer WINs")
elif self.score > 21:
self.final_score()
print(" \n Dealer Busts ..... ", player1.name, "Wins!" )
else:
print(" \n Dealer Stays ")
self.tally_up_score()
# This is Final tally after both turns are taken w/ no busts to see who has a better score
def tally_up_score(self):
player1.get_score(self.hand)
dealer.get_score(self.hand)
if player1.score > dealer.score:
self.final_score()
print("Sam wins! ")
sys.exit()
elif player1.score < dealer.score:
self.final_score()
print("Sam Loses")
sys.exit()
elif player1.score == dealer.score:
self.final_score()
print("Tie score, dealer wins ")
sys.exit()
def player_decision(self):
ask = input(str( " \n It's your turn: H to hit S to Stay ... "))
if (str(ask) == "H"):
print("\n _____________<>", self.name, "Hits <>____________")
self.hit()
if (str(ask) == "S"):
print("\n ", self.name, "decides to stay")
if self.score == 21:
self.final_score()
print(" * BLACKJACK * , You win !")
sys.exit()
else:
self.dealer_turn()
def check_hand(self):
if self.score == 21:
self.final_score()
print(" * BLACKJACK * ", self.name, "WINS ")
sys.exit()
elif self.score > 21:
self.final_score()
print( self.name, "Busted! ")
sys.exit()
else:
if self.name == player1.name:
self.player_decision()
elif self.name == dealer.name:
self.dealer_logic()
def hit(self):
self.hand.append(cards.pop())
print(" \n TABLE CARDS:", player1.name, player1.hand,"=====", dealer.name, "[{}][?]".format(dealer.hand[0]) )
self.table_score()
self.check_hand()
def dealer_turn(self):
if self.name == "sam":
Player.dealer_logic(dealer)
#Table Score calls get score, and just returns a string of the table score
def table_score(self):
player1.get_score(self.hand)
dealer.get_score(self.hand)
print (" \n TABLE SCORE:", player1.score, "[?]")
def final_score(self):
player1.get_score(self.hand)
dealer.get_score(self.hand)
print("CARDS Revealed:", player1.name, player1.hand,"=====", dealer.name, dealer.hand )
print (" \n FINAL SCORE:", player1.score, dealer.score)
def deal_cards(self):
random.shuffle(cards)
player1.hand = []
dealer.hand = []
player1.hand.append(cards.pop())
dealer.hand.append(cards.pop())
player1.hand.append(cards.pop())
dealer.hand.append(cards.pop())
print("CARDS on the table:", player1.name, player1.hand,"=====", dealer.name, "[{}][?]".format(dealer.hand[0]) )
return player1.hand, dealer.hand
def get_score(self, hand):
self.score = 0
for card in self.hand:
if card in ('12345678910'):
self.score += int(card)
elif card in ('JQK'):
self.score += 10
elif card == 'A':
if self.score <= 10:
self.score += 11
else:
self.score += 1
return self.score
#Create Players Here
player1 = Player("sam", 0, 0)
dealer = Player("dealer", 0, 0)
#Start Game with Player1 Here
Player.game_logic(player1)