Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in Python - Modify the Blackjack program. There are some potential error

ID: 3719135 • Letter: T

Question

This is in Python

- Modify the Blackjack program. There are some potential errors that can occur. One of them is to check to see if there are enough cards left to deal a round. If not, repopulate and reshuffle the deck.

- Find one additional place that you think needs error checking and detail in the #comments what you are doing.

- Find a second additional error check for this program and detail it and you could earn an additional 5 bonus points.

# Blackjack
# From 1 to 7 players compete against a dealer

import cards, games

class BJ_Card(cards.Card):
""" A Blackjack Card. """
ACE_VALUE = 1

@property
def value(self):
if self.is_face_up:
v = BJ_Card.RANKS.index(self.rank) + 1
if v > 10:
v = 10
else:
v = None
return v

class BJ_Deck(cards.Deck):
""" A Blackjack Deck. """
def populate(self):
for suit in BJ_Card.SUITS:
for rank in BJ_Card.RANKS:
self.cards.append(BJ_Card(rank, suit))
  

class BJ_Hand(cards.Hand):
""" A Blackjack Hand. """
def __init__(self, name):
super(BJ_Hand, self).__init__()
self.name = name

def __str__(self):
rep = self.name + ": " + super(BJ_Hand, self).__str__()  
if self.total:
rep += "(" + str(self.total) + ")"   
return rep

@property
def total(self):
# if a card in the hand has value of None, then total is None
for card in self.cards:
if not card.value:
return None
  
# add up card values, treat each Ace as 1
t = 0
for card in self.cards:
t += card.value

# determine if hand contains an Ace
contains_ace = False
for card in self.cards:
if card.value == BJ_Card.ACE_VALUE:
contains_ace = True
  
# if hand contains Ace and total is low enough, treat Ace as 11
if contains_ace and t <= 11:
# add only 10 since we've already added 1 for the Ace
t += 10
  
return t

def is_busted(self):
return self.total > 21


class BJ_Player(BJ_Hand):
""" A Blackjack Player. """
def is_hitting(self):
response = games.ask_yes_no(" " + self.name + ", do you want a hit? (Y/N): ")
return response == "y"

def bust(self):
print(self.name, "busts.")
self.lose()

def lose(self):
print(self.name, "loses.")

def win(self):
print(self.name, "wins.")

def push(self):
print(self.name, "pushes.")

  
class BJ_Dealer(BJ_Hand):
""" A Blackjack Dealer. """
def is_hitting(self):
return self.total < 17

def bust(self):
print(self.name, "busts.")

def flip_first_card(self):
first_card = self.cards[0]
first_card.flip()


class BJ_Game(object):
""" A Blackjack Game. """
def __init__(self, names):   
self.players = []
for name in names:
player = BJ_Player(name)
self.players.append(player)

self.dealer = BJ_Dealer("Dealer")

self.deck = BJ_Deck()
self.deck.populate()
self.deck.shuffle()

@property
def still_playing(self):
sp = []
for player in self.players:
if not player.is_busted():
sp.append(player)
return sp

def __additional_cards(self, player):
while not player.is_busted() and player.is_hitting():
self.deck.deal([player])
print(player)
if player.is_busted():
player.bust()

def play(self):
# deal initial 2 cards to everyone
self.deck.deal(self.players + [self.dealer], per_hand = 2)
self.dealer.flip_first_card() # hide dealer's first card
for player in self.players:
print(player)
print(self.dealer)

# deal additional cards to players
for player in self.players:
self.__additional_cards(player)

self.dealer.flip_first_card() # reveal dealer's first

if not self.still_playing:
# since all players have busted, just show the dealer's hand
print(self.dealer)
else:
# deal additional cards to dealer
print(self.dealer)
self.__additional_cards(self.dealer)

if self.dealer.is_busted():
# everyone still playing wins
for player in self.still_playing:
player.win()   
else:
# compare each player still playing to dealer
for player in self.still_playing:
if player.total > self.dealer.total:
player.win()
elif player.total < self.dealer.total:
player.lose()
else:
player.push()

# remove everyone's cards
for player in self.players:
player.clear()
self.dealer.clear()
  

def main():
print(" Welcome to Blackjack! ")
  
names = []
number = games.ask_number("How many players? (1 - 7): ", low = 1, high = 8)
for i in range(number):
name = input("Enter player name: ")
names.append(name)
print()
  
game = BJ_Game(names)

again = None
while again != "n":
game.play()
again = games.ask_yes_no(" Do you want to play again?: ")


main()
input(" Press the enter key to exit.")

Explanation / Answer

import os

import random

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4

def deal(deck):

hand = []

for i in range(2):

random.shuffle(deck)

card = deck.pop()

if card == 11:card = "J"

if card == 12:card = "Q"

if card == 13:card = "K"

if card == 14:card = "A"

hand.append(card)

return hand

def play_again():

again = raw_input("Do you want to play again? (Y/N) : ").lower()

if again == "y":

dealer_hand = []

player_hand = []

deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4

game()

else:

print "Bye!"

exit()

def total(hand):

total = 0

for card in hand:

if card == "J" or card == "Q" or card == "K":

total+= 10

elif card == "A":

if total >= 11: total+= 1

else: total+= 11

else:

total += card

return total

def hit(hand):

card = deck.pop()

if card == 11:card = "J"

if card == 12:card = "Q"

if card == 13:card = "K"

if card == 14:card = "A"

hand.append(card)

return hand

def clear():

if os.name == 'nt':

os.system('CLS')

if os.name == 'posix':

os.system('clear')

def print_results(dealer_hand, player_hand):

clear()

print "The dealer has a " + str(dealer_hand) + " for a total of " + str(total(dealer_hand))

print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))

def blackjack(dealer_hand, player_hand):

if total(player_hand) == 21:

print_results(dealer_hand, player_hand)

print "Congratulations! You got a Blackjack! "

play_again()

elif total(dealer_hand) == 21:

print_results(dealer_hand, player_hand)

print "Sorry, you lose. The dealer got a blackjack. "

play_again()

def score(dealer_hand, player_hand):

if total(player_hand) == 21:

print_results(dealer_hand, player_hand)

print "Congratulations! You got a Blackjack! "

elif total(dealer_hand) == 21:

print_results(dealer_hand, player_hand)

print "Sorry, you lose. The dealer got a blackjack. "

elif total(player_hand) > 21:

print_results(dealer_hand, player_hand)

print "Sorry. You busted. You lose. "

elif total(dealer_hand) > 21:

print_results(dealer_hand, player_hand)

print "Dealer busts. You win! "

elif total(player_hand) < total(dealer_hand):

print_results(dealer_hand, player_hand)

print "Sorry. Your score isn't higher than the dealer. You lose. "

elif total(player_hand) > total(dealer_hand):

print_results(dealer_hand, player_hand)

print "Congratulations. Your score is higher than the dealer. You win "

def game():

choice = 0

clear()

print "WELCOME TO BLACKJACK! "

dealer_hand = deal(deck)

player_hand = deal(deck)

while choice != "q":

print "The dealer is showing a " + str(dealer_hand[0])

print "You have a " + str(player_hand) + " for a total of " + str(total(player_hand))

blackjack(dealer_hand, player_hand)

choice = raw_input("Do you want to [H]it, [S]tand, or [Q]uit: ").lower()

clear()

if choice == "h":

hit(player_hand)

while total(dealer_hand) < 17:

hit(dealer_hand)

score(dealer_hand, player_hand)

play_again()

elif choice == "s":

while total(dealer_hand) < 17:

hit(dealer_hand)

score(dealer_hand, player_hand)

play_again()

elif choice == "q":

print "Bye!"

exit()

if __name__ == "__main__":

game()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote