- 2nd Jan 2024
- 21:19 pm
Here's an example of a basic Blackjack game in Python that uses an API to get a deck of cards from the deckofcardsapi.com:
import requests
# Function to get a new deck of cards from the API
def get_new_deck():
response = requests.get('https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1')
if response.status_code == 200:
deck_id = response.json()['deck_id']
return deck_id
else:
print("Error: Failed to fetch new deck")
return None
# Function to draw cards from the deck
def draw_cards(deck_id, count=1):
response = requests.get(f'https://deckofcardsapi.com/api/deck/{deck_id}/draw/?count={count}')
if response.status_code == 200:
cards = response.json()['cards']
return cards
else:
print("Error: Failed to draw cards")
return []
# Function to calculate the value of a hand
def calculate_hand_value(cards):
values = {'KING': 10, 'QUEEN': 10, 'JACK': 10, 'ACE': 11}
hand_value = 0
ace_count = 0
for card in cards:
if card['value'] in values:
hand_value += values[card['value']]
elif card['value'].isdigit():
hand_value += int(card['value'])
else:
hand_value += 10 # For cards like 10
if card['value'] == 'ACE':
ace_count += 1
while ace_count > 0 and hand_value > 21:
hand_value -= 10
ace_count -= 1
return hand_value
# Function to play the Blackjack game
def play_blackjack():
deck_id = get_new_deck()
player_hand = draw_cards(deck_id, 2)
dealer_hand = draw_cards(deck_id, 2)
print("Player's Hand:")
for card in player_hand:
print(f"{card['value']} of {card['suit']}")
player_hand_value = calculate_hand_value(player_hand)
print(f"Total value of player's hand: {player_hand_value}\n")
print("Dealer's Hand:")
print(f"{dealer_hand[0]['value']} of {dealer_hand[0]['suit']}")
print("Hidden Card\n")
# Player's turn
while player_hand_value < 21:
decision = input("Do you want to hit or stand? (h/s): ").lower()
if decision == 'h':
new_card = draw_cards(deck_id, 1)[0]
player_hand.append(new_card)
print(f"Player draws: {new_card['value']} of {new_card['suit']}")
player_hand_value = calculate_hand_value(player_hand)
print(f"Total value of player's hand: {player_hand_value}\n")
elif decision == 's':
break
# Dealer's turn
print("Dealer's Hand:")
for card in dealer_hand:
print(f"{card['value']} of {card['suit']}")
dealer_hand_value = calculate_hand_value(dealer_hand)
print(f"Total value of dealer's hand: {dealer_hand_value}\n")
while dealer_hand_value < 17:
new_card = draw_cards(deck_id, 1)[0]
dealer_hand.append(new_card)
print(f"Dealer draws: {new_card['value']} of {new_card['suit']}")
dealer_hand_value = calculate_hand_value(dealer_hand)
print(f"Total value of dealer's hand: {dealer_hand_value}\n")
# Determine winner
if player_hand_value > 21:
print("Player busts! Dealer wins.")
elif dealer_hand_value > 21:
print("Dealer busts! Player wins.")
elif player_hand_value > dealer_hand_value:
print("Player wins!")
elif player_hand_value < dealer_hand_value:
print("Dealer wins!")
else:
print("It's a tie!")
# Start the game
play_blackjack()
This code creates a basic text-based Blackjack game in Python. It uses the requests library to interact with the deckofcardsapi.com to simulate drawing cards for the game. The game allows the player to hit or stand and determines the winner based on the hand values.