jeudi 21 octobre 2021

Main loop running too many times and giving too many cards in Blackjack game

I want my loop to work fine, hope someone can help me and try to run the code.

I'm making a blackjack game to find the probability of certain hands winning or not. I have a while loop to cycle through one full deck of 52 cards that have been shuffled. Within that, I have a while loop having the dealer draw until his count is 17+ just like real blackjack.

When I run the code, it will run ask me how many chips I will bet and I type the number of chips. But after it checks with the if statement. It will print "GAME OVER" if I lose and ask me if I want to play again. If I say yes I want to play again. The code gives me more than two cards and loops many times.

Screenshot

Code:

import random

full_deck = {"Two of clubs": 2, "Three of clubs": 3, "Four of clubs": 4, "Five of clubs": 5, "Six of clubs": 6,
             "Seven of clubs": 7, "Eight of clubs": 8, "Nine of clubs": 9, "Ten of clubs": 10,
             "Jack of clubs": 10, "Queen of clubs": 10, "King of clubs": 10, "Ace of clubs": 11,
             "Two of diamonds": 2, "Three of diamonds": 3, "Four of diamonds": 4, "Five of diamonds": 5,
             "Six of diamonds": 6, "Seven of diamonds": 7, "Eight of diamonds": 8, "Nine of diamonds": 9,
             "Ten of diamonds": 10, "Jack of diamonds": 10, "Queen of diamonds": 10, "King of diamonds": 10,
             "Ace of diamonds": 11,
             "Two of hearts": 2, "Three of hearts": 3, "Four of hearts": 4, "Five of hearts": 5, "Six of hearts": 6,
             "Seven of hearts": 7, "Eight of hearts": 8, "Nine of hearts": 9, "Ten of hearts": 10,
             "Jack of hearts": 10, "Queen of hearts": 10, "King of hearts": 10, "Ace of hearts": 11,
             "Two of spades": 2, "Three of spades": 3, "Four of spades": 4, "Five of spades": 5, "Six of spades": 6,
             "Seven of spades": 7, "Eight of spades": 8, "Nine of spades": 9, "Ten of spades": 10,
             "Jack of spades": 10, "Queen of spades": 10, "King of spades": 10, "Ace of spades": 11,
             }

player_hand = []

dealer_hand = []

chips = 7

counter_win = 0

player_bet = 0

def get_new_shuffled_deck():
    deck = list(full_deck.items())
    random.shuffle(deck)
    return deck

def get_player_cards(player, cards, amount):
    for i in range(amount):
        last_element = cards[-1]
        player.append(last_element)
        cards.pop()
    return player

get_player_cards(player_hand, get_new_shuffled_deck(), 2)
get_player_cards(dealer_hand, get_new_shuffled_deck(), 2)


def calculate_hand_value(hand):
    hand_value = 0
    for x, value in enumerate(hand):
        hand_value += hand[x][1]

    return hand_value




def calculate_dealer_first_card(hand):
    dealer_hand_value = 0
    dealer_hand_value += hand[0][1]

    return dealer_hand_value



lists = [list(k) for k in player_hand]
lists2 = [list(m) for m in dealer_hand]

close = False
while not close:

    player_bet_input = int(input("How many chips do you want to bet?"))

    if player_bet_input < 1:
        print("You can not bet wit 0 chips")


    elif player_bet_input > chips:
        print("You dont have enough chips to bet, you have to recude the amount.")

    elif player_bet_input == chips:
        print(f"You have bet down all your chips.")

    else:
        print(f"You have bet down {player_bet_input} of {chips} chips")

    print(f"The cards have been dealt. You have a {lists[0][0]} and a {lists[1][0]} , with a total value of {calculate_hand_value(lists)}. \n")

    del player_hand[:]
    print(lists)

    if calculate_hand_value(lists) < 21:
        print("Do you wish to hit or stay?")
        print("1 - Hit")
        print("2 - Stand \n")
    answer = int(input("Answer(1/2):"))



    if answer == 1:
        print("\nYou chose to Hit.\n")

        while calculate_hand_value(player_hand) < 21:

            #her skal det komme while
            get_player_cards(player_hand, get_new_shuffled_deck(), 1)
            lists = [list(k) for k in player_hand]

            print(f"You have been dealt one card ({lists[-1][1]}). Your hand now consists of the following cards:")
            for index, name in enumerate(lists):
                print(f" - {lists[index][0]} \n ")

            print(f"The total value of your hand is now {calculate_hand_value(lists)}, and the dealers is {calculate_dealer_first_card(lists)}.\n")

            if calculate_hand_value(lists) == 21:
                print(f"Gratulerer, du vant. Det ble Blackjack.")

                chips += (player_bet_input*2)
                print(f"You have 2X your chips ({player_bet_input*2}) and your total chips is now {chips}\n")

                print("Do you want to play again?\n")
                question = input("Y/N")
                if question != "Y":
                    close = True







            if calculate_hand_value(lists) > 21:
                chips -= player_bet_input
                print("GAME OVER\n")
                print(f"You have lost {player_bet_input} of your chips and your total chips is now {chips}\n")
                print("Do you want to play again?\n")
                question = input("Y/N")
                if question != "Y":
                    close = True











    if answer == 2:
        print("\nYou chose to Stay.\n")
        print("The dealer draws cards until their hand value is greater than 17. The dealers cards are the following:")

        target = 17

        while calculate_hand_value(dealer_hand) < target:

            get_player_cards(dealer_hand, get_new_shuffled_deck(), 1)

            if calculate_hand_value(dealer_hand) > 21:
                lists2 = [list(m) for m in dealer_hand]
                for index, name in enumerate(lists2):
                    print(f" - {lists2[index][0]} \n ")

                print(f"The total value of the dealers hand is {calculate_hand_value(lists2)} compared to your hand's value; {calculate_hand_value(lists)}\n")
                print("BUST FOR THE DEALER")
                print("You won!")


            elif calculate_hand_value(dealer_hand) < calculate_hand_value(player_hand):
                lists2 = [list(m) for m in dealer_hand]
                for index, name in enumerate(lists2):
                    print(f" - {lists2[index][0]} \n ")

                print(f"The total value of the dealers hand is {calculate_hand_value(lists2)} compared to your hand's value; {calculate_hand_value(lists)}\n")
                print("You won!")


            elif calculate_hand_value(dealer_hand) > calculate_hand_value(player_hand) and calculate_hand_value(dealer_hand) != 21 and calculate_hand_value(dealer_hand) > 17 :
                lists2 = [list(m) for m in dealer_hand]
                for index, name in enumerate(lists2):
                    print(f" - {lists2[index][0]} \n ")

                print(f"The total value of the dealers hand is {calculate_hand_value(lists2)} compared to your hand's value; {calculate_hand_value(lists)}\n")
                print("Dealer vinner over player")


            elif calculate_hand_value(dealer_hand) > calculate_hand_value(player_hand) and calculate_hand_value(dealer_hand) == 21 :
                lists2 = [list(m) for m in dealer_hand]
                for index, name in enumerate(lists2):
                    print(f" - {lists2[index][0]} \n ")

                print(f"The total value of the dealers hand is {calculate_hand_value(lists2)} compared to your hand's value; {calculate_hand_value(lists)}\n")
                print(f"Det ble Blackjack for dealer.")


            else:
                lists2 = [list(m) for m in dealer_hand]
                for index, name in enumerate(lists2):
                    print(f" - {lists2[index][0]} \n ")

                print(f"The total value of the dealers hand is {calculate_hand_value(lists2)} compared to your hand's value; {calculate_hand_value(lists)}\n")
                print("Ingen vinner og det blir uavgjort")

Aucun commentaire:

Enregistrer un commentaire