import random
# Define card values and suits
card_values = {
'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
'J': 10, 'Q': 10, 'K': 10, 'A': 11
}
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
# Function to create a deck of cards
def create_deck():
deck = []
for suit in suits:
for card in card_values:
deck.append((card, suit))
random.shuffle(deck)
return deck
# Function to calculate hand value
def calculate_hand_value(hand):
value = sum(card_values[card] for card, suit in hand)
aces = sum(1 for card, suit in hand if card == 'A')
while value > 21 and aces:
value -= 10
aces -= 1
return value
# Function to display hand
def display_hand(hand, player):
print(f"{player}'s hand: ", end='')
for card, suit in hand:
print(f"{card} of {suit}", end=', ')
print(f"Value: {calculate_hand_value(hand)}")
# Function to play a single round of blackjack
def play_round(deck, chips):
# Initial hands
player_hand = [deck.pop(), deck.pop()]
dealer_hand = [deck.pop(), deck.pop()]
# Player's turn
while True:
display_hand(player_hand, 'Player')
if calculate_hand_value(player_hand) > 21:
print("Player busts! Dealer wins.")
return chips - 10 # Lose 10 chips
move = input("Do you want to (h)it or (s)tand? ").lower()
if move == 'h':
player_hand.append(deck.pop())
elif move == 's':
break
else:
print("Invalid move. Please enter 'h' or 's'.")
# Dealer's turn
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deck.pop())
display_hand(dealer_hand, 'Dealer')
# Determine the winner
player_value = calculate_hand_value(player_hand)
dealer_value = calculate_hand_value(dealer_hand)
if dealer_value > 21 or player_value > dealer_value:
print("Player wins!")
return chips + 20 # Win 20 chips
elif player_value < dealer_value:
print("Dealer wins!")
return chips - 10 # Lose 10 chips
else:
print("It's a tie!")
return chips
# Main game function
def play_blackjack():
chips = 100 # Starting chips
while chips > 0:
print(f"\nYou have {chips} chips.")
deck = create_deck()
chips = play_round(deck, chips)
if chips <= 0:
print("You've run out of chips. Game over!")
break
play_again = input("Do you want to play another round? (y/n): ").lower()
if play_again != 'y':
break
print(f"You finished the game with {chips} chips.")
# Start the game
play_blackjack()