Question
hi, i want to create a blackjack game. And i have written my own code with the test code below importing the code from the
hi, i want to create a blackjack game. And i have written my own code with the test code below importing the code from the above pictures. But my code doesn't work. My code will be given below. I want the code fixed and i want the extra credit implemented as well.
Thank you.
from lab9 import Card
from lab9 import ChipBank
from random import shuffle
# Import lab9 Classes and random shuffle method.
# BlackjackHand class creates the deck of cards.
class BlackjackHand:
def __init__(self):
self.list_of_cards = []
def add_card(self, new_card):
self.list_of_cards.append(new_card)
def __str__(self):
card_string = ""
for card in self.list_of_cards:
card_string += str(card)
card_string += ", "
card_string = card_string.strip()
return card_string.strip(",")
def get_value(self):
card_values = 0
number_of_aces = 0
for card in self.list_of_cards:
card_values += card.get_value()
if card.get_rank() == "Ace":
number_of_aces += 1
if card_values > 21:
for number in range(number_of_aces):
card_values -= 10
return self.card_values
class Blackjack:
def __init__(self, starting_dollars):
self.bank = ChipBank(starting_dollars)
#print("Your remaining chips are" + self.player_bank)
deck_of_cards = []
cards_created = 0
# reshuffles deck when all cards have been used
cards_used = 0
cards_used_list = []
#while cards_used
# if cards_used > 0:
# print(card)
# shuffled_deck = shuffled_deck - card
#cards_used += 1
player_hand = ""
player_value = 0
player_status = ""
dealer_hand = ""
dealer_value = 0
dealer_status = ""
# while dealer_hand
# dealer_hand.replace(card)
# dealer_value = card.get_value()
# print(dealer_hand)
# if player_value and dealer_value == 21:
# player_status = push
# print("push")
# elif player_value == 21:
# if dealer_value != 21:
# print("player wins")
# elif dealer_value == 21:
# if player_value != 21:
# print("dealer wins")
def draw(self):
card = self.deck_of_cards.pop()
self.cards_used_list.append(card)
card.face_up()
return card
def start_hand(self, wager):
self.bank.withdraw(wager)
self.player_hand = blackjackhand()
self.dealer_hand = blackjackhand()
self.player_hand.add_card(card)
self.dealer_hand.add_card(card)
player_card_1 = self.draw()
self.player_hand.add_card(player_card_1)
player_card_2 = self.draw()
self.player_hand.add_card(player_card_2)
print("The player has a" + player_card_1 + player_card_2)
dealer_card_facedown = self.draw()
self.dealer_hand(dealer_card_facedown).face_down
dealer_card_faceup = self.draw()
self.dealer_hand.add_card(dealer_card_faceup)
print("The dealer has a" + dealer_card_faceup)
if player_hand.get_value == 21 and dealer_hand.get_value == 21:
print("The player and the dealer tie!")
elif player_hand.get_value == 21 and dealer_hand.get_value != 21:
print("The player wins!")
elif dealer_hand.get_value == 21 and player_hand.get_value != 21:
print("The dealer wins!")
def hit(self):
while player_hand
choice = input("Would you like to hit again? YES/NO ")
if choice.upper() == "YES":
if cards_used
player_hand.replace(card)
player_value = card.get_value()
print(player_hand)
else:
shuffled_deck = shuffled_deck - card
cards_used += 1
print(player_value)
if player_hand == 21:
player_status = stand
elif player_hand > 21:
player_staus = bust
def stand(self):
print(dealer_hand)
while dealer_hand
dealer_hand.replace(card)
dealer_value = card.get_value()
print(dealer_hand)
if dealer_value > 21:
player_status = win
end_hand("win")
elif dealer_value == 21:
player_status = lose
end_hand("lose")
elif player_value and dealer_value == 21:
player_status = push
end_hand("push")
elif player_value == 21:
if dealer_value != 21:
end_hand("win")
def end_hand(outcome):
if outcome == "win":
self.bank.deposit(wager * 2)
print('You win')
elif outcome == "lose":
print('You lose')
elif outcome == "push":
print('You tied')
self.bank.deposit(wager)
def game_active():
if dealer_hand != '':
return True
else:
return False
if __name__== "__main__":
blackjack = Blackjack(250)
while blackjack.bank.value > 0:
print("Your remaining chips: "+ str(blackjack.bank))
wager = int(input('How much would you like to wager? '))
blackjack.start_hand(wager)
while blackjack.game_active():
choice = input("STAND or HIT: ").upper()
if choice == "STAND":
blackjack.stand()
elif choice == "HIT":
blackjack.hit()
print()
print("Out of money! The casino wins!")
1 $class Card: This is the header for the class Card. Any new card objects created will use the Card(card_num) method, which will be used to create card objects with a suit, rank, and value. Cards start facing down. # class member variables - shared by all objects suits ["Spades", "Hearts", "Clubs", "Diamonds" ranks ["Jack", "Queen", "King" ] def _init (self, card_num): init will be run whenever a new card object is created ie : Card () Is executed Takes in 2 parameters, card_num (any number between e and 51) , self (giving the object the ability to call it's own methods) # Sets the Card's instance variables: # Cards start facing down self._facing_upFalse 20 21 # Sets its "suit" using the shared suits list. # Integer division of the initial card num determines the suit. # 1-13 spades, 14-26 hearts, 27-39 clubs, 40-52 diamonds self._sut = Card.suts[card-num // 13] 23 24 25 26 27 28 29 # Use modules 13 to determine the card's "rank" (e.g., Ace, King, Queen, Jack, 1, 2, # and point "value" (2-10,11) num = cardnum % 13 # If numk13 == , its an Ace ) - 31 self._rank "Ace" self._value11 # Jack if 10, Queen if 11, King if 12 elif num in [10, 11, 121: 35 self.-rank = Card. ranks [num- 10] # access shared rank list to set ranks of J, Q, and K self.value = 10 37 38 39 - # Else rank and value is it's number +1 (card 1, refers to 2 of spades worth 2 points) else: self._rank - str num + 1) self._valuenum1 1 $class Card: This is the header for the class Card. Any new card objects created will use the Card(card_num) method, which will be used to create card objects with a suit, rank, and value. Cards start facing down. # class member variables - shared by all objects suits ["Spades", "Hearts", "Clubs", "Diamonds" ranks ["Jack", "Queen", "King" ] def _init (self, card_num): init will be run whenever a new card object is created ie : Card () Is executed Takes in 2 parameters, card_num (any number between e and 51) , self (giving the object the ability to call it's own methods) # Sets the Card's instance variables: # Cards start facing down self._facing_upFalse 20 21 # Sets its "suit" using the shared suits list. # Integer division of the initial card num determines the suit. # 1-13 spades, 14-26 hearts, 27-39 clubs, 40-52 diamonds self._sut = Card.suts[card-num // 13] 23 24 25 26 27 28 29 # Use modules 13 to determine the card's "rank" (e.g., Ace, King, Queen, Jack, 1, 2, # and point "value" (2-10,11) num = cardnum % 13 # If numk13 == , its an Ace ) - 31 self._rank "Ace" self._value11 # Jack if 10, Queen if 11, King if 12 elif num in [10, 11, 121: 35 self.-rank = Card. ranks [num- 10] # access shared rank list to set ranks of J, Q, and K self.value = 10 37 38 39 - # Else rank and value is it's number +1 (card 1, refers to 2 of spades worth 2 points) else: self._rank - str num + 1) self._valuenum1
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