Here's a Python program that implements the requirements you've outlined for a simplified version of Blackjack. The program consists of two main parts: determining the winner and simulating the dealer's play. Each part is encapsulated in its own set of functions, and the program is structured to provide clear output and user prompts.
import random
def main():
print_header()
dealer_result = dealer_play()
player_result = get_player_result()
print(determine_winner(dealer_result, player_result))
print("Program finished.")
def print_header():
print("###" * 10)
print("### Welcome to the Blackjack Game ###")
print("###" * 10)
print("The objective in Blackjack is to have a hand with a total value higher than the dealer’s without going over 21.")
print("Aces are worth 11, Kings, Queens, Jacks and Tens are worth a value of 10, and the remaining cards are counted at face value.")
print("###" * 10)
def dealer_play():
initial_card = get_dealer_initial_card()
dealer_hand = [initial_card]
dealer_value = initial_card
print(f"Dealer is dealt a {initial_card} giving a hand value of {dealer_value} and status is hit.")
while dealer_value < 17:
new_card = deal_card()
dealer_hand.append(new_card)
dealer_value += new_card
if dealer_value > 21:
print(f"Dealer is dealt a {new_card} giving a hand value of {dealer_value} and status is bust.")
return ["bust", dealer_value]
else:
print(f"Dealer is dealt a {new_card} giving a hand value of {dealer_value} and status is hit.")
print(f"Dealer stands with a hand value of {dealer_value}.")
return ["stand", dealer_value]
def get_dealer_initial_card():
card = int(input("Enter the dealer’s initial face card (2-10, 10 for J/Q/K): "))
if card > 10 or card < 2:
print("Invalid card value. Please enter a value between 2 and 10 (10 for J/Q/K).")
return get_dealer_initial_card()
return card
def deal_card():
return random.randint(2, 10) # Simulating card values from 2 to 10
def get_player_result():
player_blackjack = input("Did the player hit Blackjack? (yes/no): ").strip().lower() == 'yes'
player_bust = input("Did the player go bust? (yes/no): ").strip().lower() == 'yes'
player_hand_value = int(input("Enter the player's hand value: "))
return [player_blackjack, player_bust, player_hand_value]
def determine_winner(dealer_result, player_result):
dealer_status, dealer_value = dealer_result
player_blackjack, player_bust, player_value = player_result
if player_blackjack:
return "Player wins with Blackjack!"
if player_bust:
return f"Dealer wins! Player status is bust with a hand value of {player_value}."
if dealer_status == "bust":
return f"Player wins! Dealer status is bust with a hand value of {dealer_value}."
if player_value > dealer_value:
return f"Player wins! Player status is stand with a hand value of {player_value}.\nDealer status is stand with a hand value of {dealer_value}."
elif player_value < dealer_value:
return f"Dealer wins! Player status is stand with a hand value of {player_value}.\nDealer status is stand with a hand value of {dealer_value}."
else:
return f"It's a tie! Both have a hand value of {player_value}."
if __name__ == "__main__":
main()
Explanation of the Code:
- Main Function: This is the entry point of the program. It calls the header function, dealer play function, player result function, and determines the winner.
- Print Header: This function prints the program's header and the objective of Blackjack.
- Dealer Play: This function simulates the dealer's play, starting with an initial card and continuing to draw cards until the dealer stands or busts.
- Get Dealer Initial Card: This function prompts the user for the dealer's initial card and validates the input.
- Deal Card: This function randomly generates a card value between 2 and 10.
- Get Player Result: This function collects the player's final status and hand value.
- Determine Winner: This function evaluates the results of the dealer and player to declare the winner.
Output Formatting:
The program includes clear prompts and outputs, with appropriate spacing and headers to enhance readability. The results are presented in a professional manner, making it easy for users to understand the outcome of the game.