Skip to main content
Became Hot Network Question
added 135 characters in body
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221

HowThis is code for a text-based game of Blackjack.

I would also like advice on how can I implement a betting system in this program?. For example starting with an amount of chips and choosing how much of them to bet.

How can I implement a betting system in this program? For example starting with an amount of chips and choosing how much of them to bet.

This is code for a text-based game of Blackjack.

I would also like advice on how can I implement a betting system in this program. For example starting with an amount of chips and choosing how much of them to bet.

edited tags
Link
toolic
  • 16.4k
  • 6
  • 29
  • 221
Source Link
vaun
  • 31
  • 1

Text-based blackjack in python

How can I implement a betting system in this program? For example starting with an amount of chips and choosing how much of them to bet.

import random

card_suit = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
card_value = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
deck = [(value, suit) for suit in card_suit for value in card_value]

def card_points(card):
    if card[0] in ['Jack', 'Queen', 'King']:
        return 10
    elif card[0] == 'Ace':
        return 11
    else:
        return int(card[0])
    
random.shuffle(deck)
player_cards = [deck.pop(), deck.pop()]
dealer_cards = [deck.pop()]
player_points = sum(card_points(card) for card in player_cards)
dealer_points = sum(card_points(card) for card in dealer_cards)

def points(dealer_points, player_points, dealer_cards, player_cards):
    while dealer_points < 17:
        new_card = deck.pop()
        dealer_cards.append(new_card)
        dealer_points += card_points(new_card)

    print("Dealer's Cards:", dealer_cards)
    print("Dealer's Points:", dealer_points)
    print("\n")

while True:
    print("Dealer's Cards:", dealer_cards)
    print("Dealer's Points:", dealer_points)
    print("\n")
    print("Player's Cards:", player_cards)
    print("Player's Points:", player_points)
    print("\n")
    decision = input('What will you do? ["hit" for a new card, "stand" to end the game]: ').lower()
    if decision == "hit":
        new_card = deck.pop()
        player_cards.append(new_card)
        player_points = sum(card_points(card) for card in player_cards)
        if player_points > 21:
            print("Dealer's Cards:", dealer_cards)
            print("Dealer's Points:", dealer_points)
            print("Player's Cards:", player_cards)
            print("Player's Points:", player_points)
            print("Dealer wins (player has more than 21 points)")
            break
    elif decision == "stand":
        while dealer_points < 17:
            new_card = deck.pop()
            dealer_cards.append(new_card)
            dealer_points += card_points(new_card)
        print("Dealer's Cards:", dealer_cards)
        print("Dealer's Points:", dealer_points)
        print("Player's Cards:", player_cards)
        print("Player's Points:", player_points)
        if player_points > 21:
            print("Dealer wins (player has more than 21 points).")
        elif dealer_points > 21:
            print("You win! (dealer has more than 21 points)")
        elif player_points > dealer_points:
            print("You win! (you have more points than the dealer)")
        elif player_points < dealer_points:
            print("Dealer wins (dealer has more points than the player).")
        else:
            print("It's a tie.")
        break
    else:
        print("Unknown input.")
        continue