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.