Question
Step 1 - I need Console BLACKJACK! Blackjack payout is 3:2 Money: 100.0 Bet amount: 10 DEALER'S SHOW CARD: 9 of Diamonds YOUR CARDS: 5
Step 1 - I need
Console BLACKJACK! Blackjack payout is 3:2 Money: 100.0 Bet amount: 10 DEALER'S SHOW CARD: 9 of Diamonds YOUR CARDS: 5 of Hearts 5 of Clubs Hit or stand? (hit/stand): hit YOUR CARDS: 5 of Hearts 5 of Clubs 7 of Diamonds Hit or stand? (hit/stand): stand DEALER'S CARDS: 9 of Diamonds Jack of Hearts YOUR POINTS: 17 DEALER'S POINTS: 19 Sorry. You lose. Money: 90.0 Play again? (y/n): n Come back soon! Bye!
Step 2 - I need to improve to this
Section 2: Improve the program. This should improve the appearance of the console and the readability of the code. Console BLACKJACK! Blackjack payout is 3:2 Start time: 11:43:03 AM Money: $110.00 Bet amount: 10 DEALER'S SHOW CARD: 6 of Clubs YOUR CARDS: 9 of Clubs Jack of Clubs Hit or stand? (hit/stand): stand DEALER'S CARDS: 6 of Clubs Queen of Hearts Queen of Diamonds YOUR POINTS: 19 DEALER'S POINTS: 26 Yay! The dealer busted. You win! Money: $120.00 Play again? (y/n): n Stop time: 11:43:20 AM Elapsed time: 00:00:16 Come back soon! Bye!
Step 3 - Section 3: object-oriented program Convert the Blackjack program from procedural to object-oriented. This shouldn't change the functionality of the code, but it should make the code more modular, reusable, and easier to maintain. Console BLACKJACK! Blackjack payout is 3:2 Start time: 11:43:03 AM Money: $110.00 Bet amount: 10 DEALER'S SHOW CARD: 6 of Clubs YOUR CARDS: 9 of Clubs Jack of Clubs Hit or stand? (hit/stand): stand DEALER'S CARDS: 6 of Clubs Queen of Hearts Queen of Diamonds YOUR POINTS: 19 DEALER'S POINTS: 26 Yay! The dealer busted. You win! Money: $120.00 Play again? (y/n): n Stop time: 11:43:20 AM Elapsed time: 00:00:16 Come back soon! Bye!
Here is what I have so far. What am I missing?
import random, os, sys
name_of_card = { 1: 'Ace', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 11: 'Jack', 12: 'Queen', 13: 'King' }
suit_of_card = { 'c': 'Clubs', 'h': 'Hearts', 's': 'Spades', 'd': 'Diamonds' }
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return(name_of_card[self.rank]+" Of "+suit_of_card[self.suit])
def getRank(self):
return(self.rank)
def getSuit(self):
return(self.suit)
def blackjack_score(self):
if self.rank > 9:
return(10)
else:
return(self.rank)
def show_of_hands(hand):
for card in hand:
print(card)
def show_count(hand):
print("Count: "+str(count_hand(hand)))
def count_hand(hand):
count_hand=0
for card in hand:
count_hand += card.blackjack_score()
return(count_hand)
def game_over():
print("Come back soon!")
sys.exit(0)
card_deck= []
suits = [ 'c','h','d','s' ]
hand= { 'dealer': [],'human': [] }
for suit in suits:
for rank in range(1,14):
card_deck.append(Card(rank,suit))
keepPlaying = True
while keepPlaying:
os.system('clear')
random.shuffle(card_deck)
random.shuffle(card_deck)
random.shuffle(card_deck)
#Deal Cards
hand['human'].append(card_deck.pop(0))
hand['dealer'].append(card_deck.pop(0))
hand['human'].append(card_deck.pop(0))
hand['dealer'].append(card_deck.pop(0))
human_turn = True
human_turn_end = False
print("Blackjack! ")
print()
while human_turn:
print('DEALER\'S CARDS : '+ str(hand['dealer'][-1]))
print()
print('YOUR CARDS:')
show_of_hands(hand['human'])
show_count(hand['human'])
print()
human_choice = True
user_input = ''
while human_choice:
user_input = input("Hit or Stand? (hit/stand) ")
if user_input == 'hit' or 'stand':
human_choice = False
if user_input == 'hit':
hand['human'].append(card_deck.pop(0))
if count_hand(hand['human']) > 21:
human_turn = False
human_turn_end = True
elif user_input == 'stand':
human_turn = False
dealer_turn = True
dealer_turn_end = False
while not human_turn_end and dealer_turn:
if count_hand(hand['dealer'])<17:
hand['dealer'].append(card_deck.pop(0))
else:
dealer_turn = False
if count_hand(hand['dealer'])>21:
dealer_turn = False
dealer_turn_end = True
print()
print('Dealer Hand:')
show_of_hands(hand['dealer'])
show_count(hand['dealer'])
print()
print('Player Hand:')
show_of_hands(hand['human'])
show_count(hand['human'])
print()
if human_turn_end:
print('PLAYER BUSTED')
elif dealer_turn_end:
print('DEALER BUSTED')
elif count_hand(hand['human']) > count_hand(hand['dealer']):
print('PLAYER WINS')
else:
print('DEALER WINS')
print()
if input("Play again (y/n)") == 'n':
game_over()
card_deck.extend(hand['dealer'])
card_deck.extend(hand['human'])
del hand['dealer'][:]
del hand['human'][:]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started