Sure! Below is a Python implementation of a simple blackjack game where the user plays against the computer (dealer). Each card has an additional description that is displayed when the card is dealt. The descriptions add flavor, such as personality or background to the cards.
The game flow is:
- The user and computer are each dealt two cards initially.
- The user can choose to "hit" (draw a card) or "stand" (end their turn).
- After the user stands, the dealer draws according to simple rules (hit until 17 or more).
- The winner is decided by who has the highest score without busting.
Here is the complete code:
import random
# Define card ranks and suits
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
# Card values for blackjack; Ace initially valued at 11 but can count as 1 later
values = {
'2': 2, '3': 3, '4': 4, '5': 5, '6': 6,
'7': 7, '8': 8, '9': 9, '10': 10,
'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11
}
# Descriptions for ranks (extra descriptions that add flavor)
rank_descriptions = {
'2': "a humble servant card",
'3': "a slightly mischievous card",
'4': "a solid and dependable card",
'5': "a fiery, passionate card",
'6': "a calm and balanced card",
'7': "a lucky card, favored by many",
'8': "a strong and sturdy card",
'9': "a high-reaching ambitious card",
'10': "a powerful and confident card",
'Jack': "a cunning and youthful rogue",
'Queen': "a graceful and commanding lady",
'King': "a wise and authoritative ruler",
'Ace': "a mysterious and unpredictable ace"
}
# Descriptions for suits (adds character)
suit_descriptions = {
'Hearts': "filled with warmth and passion",
'Diamonds': "sparkling with wealth and power",
'Clubs': "rooted in growth and fortune",
'Spades': "sharp and cutting, symbolizing wisdom"
}
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.value = values[rank]
def description(self):
return f"{self.rank} of {self.suit} - {rank_descriptions[self.rank]} {suit_descriptions[self.suit]}."
def __str__(self):
return f"{self.rank} of {self.suit}"
class Deck:
def __init__(self):
self.cards = [Card(rank, suit) for suit in suits for rank in ranks]
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop()
def calculate_hand_value(hand):
value = sum(card.value for card in hand)
# Count how many aces we have
num_aces = sum(card.rank == 'Ace' for card in hand)
# Adjust for aces if value is over 21
while value > 21 and num_aces:
value -= 10 # counting ace as 1 instead of 11
num_aces -= 1
return value
def print_hand(hand, owner="Player"):
print(f"\n{owner}'s Hand:")
for card in hand:
print(f"- {card} ({card.description()})")
value = calculate_hand_value(hand)
print(f"Total value: {value}")
def blackjack_game():
deck = Deck()
# Initial hands
player_hand = [deck.deal_card(), deck.deal_card()]
dealer_hand = [deck.deal_card(), deck.deal_card()]
# Show initial hands (dealer only shows one card)
print_hand(player_hand, "Player")
print(f"\nDealer's Hand:")
print(f"- {dealer_hand[0]} ({dealer_hand[0].description()})")
print("- Hidden card")
# Player's turn
while True:
player_value = calculate_hand_value(player_hand)
if player_value > 21:
print("\nPlayer busts! You lose.")
return
action = input("\nDo you want to [h]it or [s]tand? ").lower()
if action == 'h':
new_card = deck.deal_card()
player_hand.append(new_card)
print(f"\nYou drew {new_card} ({new_card.description()})")
print_hand(player_hand, "Player")
elif action == 's':
break
else:
print("Invalid input. Please type 'h' to hit or 's' to stand.")
# Dealer's turn
print_hand(dealer_hand, "Dealer")
dealer_value = calculate_hand_value(dealer_hand)
while dealer_value < 17:
new_card = deck.deal_card()
dealer_hand.append(new_card)
print(f"\nDealer draws {new_card} ({new_card.description()})")
dealer_value = calculate_hand_value(dealer_hand)
print_hand(dealer_hand, "Dealer")
# Determine outcome
player_value = calculate_hand_value(player_hand)
dealer_value = calculate_hand_value(dealer_hand)
if dealer_value > 21:
print("\nDealer busts! You win!")
elif dealer_value == player_value:
print("\nIt's a tie!")
elif dealer_value > player_value:
print("\nDealer wins!")
else:
print("\nYou win!")
if __name__ == "__main__":
print("Welcome to Blackjack with Descriptions!")
while True:
blackjack_game()
again = input("\nPlay again? (y/n): ").lower()
if again != 'y':
print("Thanks for playing!")
break
How it works
- When cards are dealt, it prints their rank, suit, and a little descriptive phrase.
- Player chooses to hit or stand.
- Dealer hits until reaching at least 17.
- Scores are calculated with aces dynamically counted as 1 or 11.
- Winner is announced.
- Option to replay.
Sample output excerpt:
Welcome to Blackjack with Descriptions!
Player's Hand:
- King of Clubs (a wise and authoritative ruler rooted in growth and fortune)
- 5 of Diamonds (a fiery, passionate card sparkling with wealth and power)
Total value: 15
Dealer's Hand:
- 9 of Spades (a high-reaching ambitious card sharp and cutting, symbolizing wisdom)
- Hidden card
Do you want to [h]it or [s]tand?
Feel free to run the code and let me know if you want enhancements or GUI/graphics versions!