107k views
3 votes
Create the program Create a program that allows the user to play Blackjack against a computer dealer. 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: DEALER's POINTS: 19 Sorry. You lose. Money: 90.0 Play again? ( y/n ) : n Come back soon! Bye! Specifications - If necessary, learn the rules of Blackjack by researching it on the web. For this program:

a) The dealer must continue taking cards until the dealer has at least 17 points.
b) Don't allow a player to "split" a hand or "double down."
c) The program should accept integer or float entries for the bet amount.
d) Getting a blackjack pays out 3:2, which is 1.5 times the bet amount.
e) The program should round the blackjack payout to a maximum of two decimal places.

User Almar
by
7.6k points

1 Answer

3 votes

Answer:

The given Python program simulates a simple Blackjack game where the player can play against a computer dealer, following the rules and specifications provided.

Explanation:

Here's a Python program for playing Blackjack against a computer dealer:

import random

def draw_card():

cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']

suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']

return random.choice(cards) + ' of ' + random.choice(suits)

def calculate_points(cards):

points = 0

num_aces = 0

for card in cards:

rank = card.split()[0]

if rank.isdigit():

points += int(rank)

elif rank in ['Jack', 'Queen', 'King']:

points += 10

elif rank == 'Ace':

num_aces += 1

points += 11

while points > 21 and num_aces:

points -= 10

num_aces -= 1

return points

def blackjack():

money = 100.0

play_again = 'y'

while play_again.lower() == 'y':

print("BLACKJACK! Blackjack payout is 3:2")

print(f"Money: {money}")

bet = float(input("Bet amount: "))

dealer_cards = [draw_card()]

player_cards = [draw_card(), draw_card()]

print(f"DEALER's SHOW CARD: {dealer_cards[0]}")

print(f"YOUR CARDs: {', '.join(player_cards)}")

while True:

action = input("Hit or stand? (hit/stand): ")

if action.lower() == 'hit':

player_cards.append(draw_card())

print(f"YOUR CARDs: {', '.join(player_cards)}")

player_points = calculate_points(player_cards)

if player_points > 21:

print("Bust! You lose.")

money -= bet

break

elif action.lower() == 'stand':

break

dealer_cards.append(draw_card())

print(f"DEALER's CARDS: {', '.join(dealer_cards)}")

dealer_points = calculate_points(dealer_cards)

while dealer_points < 17:

dealer_cards.append(draw_card())

print(f"DEALER's CARDS: {', '.join(dealer_cards)}")

dealer_points = calculate_points(dealer_cards)

print(f"YOUR POINTs: {calculate_points(player_cards)}")

print(f"DEALER's POINTS: {dealer_points}")

if dealer_points > 21 or (player_points <= 21 and player_points > dealer_points):

if len(player_cards) == 2 and player_points == 21:

blackjack_payout = round(bet * 1.5, 2)

print(f"Blackjack! You win {blackjack_payout}!")

money += blackjack_payout

else:

print("You win!")

money += bet

elif player_points == dealer_points:

print("It's a tie.")

else:

print("Sorry. You lose.")

money -= bet

print(f"Money: {money}")

play_again = input("Play again? (y/n): ")

print("Come back soon! Bye!")

blackjack()

This program simulates a simple Blackjack game where the player can play against a computer dealer.

The rules are enforced according to the provided specifications.

The game allows the player to place bets, receive cards, and make decisions to hit or stand.

The dealer also follows the rules for taking cards until reaching at least 17 points.

The program also handles the payout for a blackjack (a hand worth 21 points with only 2 cards).

The player's money is tracked, and the option to play again is provided.

Feel free to run this program in a Python environment to play the game and experience the Blackjack simulation.

Thus, the given Python program is for playing Blackjack against a computer dealer.

User TheFallenOne
by
7.6k points