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

Blackjack Simulation Previously in this chapter you saw the card_dealer.py progr

ID: 3850194 • Letter: B

Question

Blackjack Simulation

Previously in this chapter you saw the card_dealer.py program that simulates cards being dealt from a deck. Enhance the program so it simulates a simplified version of the game of

Blackjack between two virtual players. The cards have the following values:

Numeric cards are assigned the value they have printed on them. For example, the value of the 2 of spades is 2, and the value of the 5 of diamonds is 5.

Jacks, queens, and kings are valued at 10.

Aces are valued at 1 or 11, depending on the player’s choice.

The program should deal cards to each player until one player’s hand is worth more than

21 points. When that happens, the other player is the winner. (It is possible that both players’ hands will simultaneously exceed 21 points, in which case neither player wins.) The program should repeat until all the cards have been dealt from the deck.

If a player is dealt an ace, the program should decide the value of the card according to the following rule: The ace will be worth 11 points, unless that makes the player’s hand exceed

21 points. In that case, the ace will be worth 1 point.

Sample Output:

>>>

Player 1 was dealt 8 of Hearts

Player 2 was dealt 4 of Hearts

Player 1 was dealt 5 of Diamonds

Player 2 was dealt 7 of Diamonds

Player 1 was dealt 10 of Clubs

Player 2 was dealt 9 of Clubs

Player 2 wins.

>>>

cardealer.py program:

# This program uses a dictionary as a deck of cards.

def main():
# Create a deck of cards.
deck = create_deck()

# Get the number of cards to deal.
num_cards = int(input('How many cards should I deal? '))

# Deal the cards.
deal_cards(deck, num_cards)

# The create_deck function returns a dictionary
# representing a deck of cards.
def create_deck():
# Create a dictionary with each card and its value
# stored as key-value pairs.
deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
'4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
'7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
'10 of Spades':10, 'Jack of Spades':10,
'Queen of Spades':10, 'King of Spades': 10,
  
'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
'4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
'7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
'10 of Hearts':10, 'Jack of Hearts':10,
'Queen of Hearts':10, 'King of Hearts': 10,
  
'Ace of Clubs':1, '2 of Clubs':2, '3 of Clubs':3,
'4 of Clubs':4, '5 of Clubs':5, '6 of Clubs':6,
'7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
'10 of Clubs':10, 'Jack of Clubs':10,
'Queen of Clubs':10, 'King of Clubs': 10,
  
'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
'4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
'7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
'10 of Diamonds':10, 'Jack of Diamonds':10,
'Queen of Diamonds':10, 'King of Diamonds': 10}

# Return the deck.
return deck

# The deal_cards function deals a specified number of cards
# from the deck.

def deal_cards(deck, number):
# Initialize an accumulator for the hand value.
hand_value = 0

# Make sure the number of cards to deal is not
# greater than the number of cards in the deck.
if number > len(deck):
number = len(deck)

# Deal the cards and accumulate their values.
for count in range(number):
card, value = deck.popitem()
print(card)
hand_value += value

# Display the value of the hand.
print('Value of this hand:', hand_value)

# Call the main function.
main()

Explanation / Answer

Modified code for blacjack simulation:

# This program uses a dictionary as a deck of cards.
def main():
   deck = create_deck()
   # Get the number of cards to deal.
   # num_cards = int(input('How many cards should I deal? '))
   # Deal the cards.
   deal_cards(deck)
  
# The create_deck function returns a dictionary
# representing a deck of cards.

def create_deck():
   # Create a dictionary with each card and its value
   # stored as key-value pairs.
   deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
   '4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
   '7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
   '10 of Spades':10, 'Jack of Spades':10,
   'Queen of Spades':10, 'King of Spades': 10,
  
   'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
   '4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
   '7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
   '10 of Hearts':10, 'Jack of Hearts':10,
   'Queen of Hearts':10, 'King of Hearts': 10,
  
   'Ace of Clubs':1, '2 of Clubs':2, '3 of Clubs':3,
   '4 of Clubs':4, '5 of Clubs':5, '6 of Clubs':6,
   '7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
   '10 of Clubs':10, 'Jack of Clubs':10,
   'Queen of Clubs':10, 'King of Clubs': 10,
  
   'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
   '4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
   '7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
   '10 of Diamonds':10, 'Jack of Diamonds':10,
   'Queen of Diamonds':10, 'King of Diamonds': 10}
   # Return the deck.
   return deck

# The deal_cards function deals a specified number of cards
# from the deck.
def deal_cards(deck):
   # Initialize an accumulator for the hand value.
   P1hand_value = 0
   P2hand_value = 0

   # Deal the cards and accumulate their values.
   while len(deck)!=0:

#halting conditions
       if P1hand_value>=21 and P2hand_value>=21: #tie
           print ("No one wins. There's a tie.")
           break   #break the loop and halt the program
       elif P1hand_value>=21: #Player2 wins
           print ("Player 2 wins.")
           break #break the loop and halt the program
       elif P2hand_value>=21: #Player1 wins
           print("Player 1 wins.")
           break   #break the loop and halt the program

       card, value = deck.popitem()
       print("Player 1 was dealt with " + card)
       if(value==1 and P1hand_value+value>=21):
           value=11 #for the special case of Ace, 11 pts.
       P1hand_value += value

       card, value = deck.popitem()
       print("Player 2 was dealt with " + card)
       if(value==1 and P2hand_value+value>=21):
           value=11 #for the special case of Ace, 11 pts.
       P2hand_value += value

       print ""

   # Call the main function.

main()