mardi 21 avril 2020

Blackjack game creation in Python, how to pick two random cards?

I am trying to create blackjack card, but I am having trouble with picking random cards in while loop.

import random

card_dict = {'A': 11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}

cardcolor = ['S', 'H', 'C', 'D']



player_score = 0
dealer_score = 0


# In here, because I have no idea how to create card number randomly, I created random.choice a lot for dealer to pick up another cards if their card sum is not over 17.
# Is there anyway to write one sentence or few sentences code to pick up random number in while loop?

#For example, if I am using player_num = random.choice(list(card_dict)), and put this in every line where the random number needed, it gave me all same numbers in while-loop

# I am not sure that I explained well, I hope that you get it. Thanks for your reading and help! If you want to know more, please give me a comment.

while True:

    player_num01 = random.choice(list(card_dict))
    player_num02 = random.choice(list(card_dict))
    player_num03 = random.choice(list(card_dict))
    player_num04 = random.choice(list(card_dict))
    player_color = random.choice(cardcolor)

    dealer_num01 = random.choice(list(card_dict))
    dealer_num02 = random.choice(list(card_dict))
    dealer_num03 = random.choice(list(card_dict))
    dealer_num04 = random.choice(list(card_dict))
    dealer_color = random.choice(cardcolor)

    player_card01 = player_num01 + player_color
    player_card02 = player_num02 + player_color
    player_card03 = player_num02 + player_color
    player_card04 = player_num02 + player_color
    dealer_card01 = dealer_num01 + dealer_color
    dealer_card02 = dealer_num02 + dealer_color
    dealer_card03 = dealer_num02 + dealer_color
    dealer_card04 = dealer_num02 + dealer_color

    print('your cards: ', player_card01, player_card02)
    player_total = card_dict[player_num01] + card_dict[player_num02]
    stand_hit = input('(S)tand or (H)it?...: ')


    if stand_hit == 's':
        print(dealer_card01, dealer_card02)
        dealer_total = card_dict[dealer_num01] + card_dict[dealer_num02]
        if dealer_total < 17:
            print('Dealer drew: ', dealer_card03)
            dealer_total += card_dict[dealer_num03]
        elif dealer_total > 21:
            player_score += 1
            print('Player wins')
            print('Dealer:', dealer_score, 'Player:', player_score)

            if dealer_total <17:
                print('Dealer drew: ', dealer_card04)
                dealer_total += card_dict[dealer_card04]
            elif dealer_total > 21:
                player_score += 1
                print('Player wins')
                print('Dealer:', dealer_score, 'Player:', player_score)

            elif dealer_total >= 17:

                if dealer_total > player_total:
                    print('Dealer wins')
                    dealer_score += 1
                    print(dealer_total)
                    print('Dealer:', dealer_score, 'Player:', player_score)
                elif dealer_total < player_total:
                    print('You win')
                    player_score += 1
                    print(dealer_total)
                    print('Dealer: ', dealer_score, 'Player: ', player_score)


        elif dealer_total >= 17:

            if dealer_total > player_total:
                print('Dealer wins')
                dealer_score += 1
                print(dealer_total)
                print('Dealer:', dealer_score, 'Player:', player_score)
            elif dealer_total < player_total:
                print('You win')
                player_score += 1
                print(dealer_total)
                print('Dealer: ', dealer_score, 'Player: ', player_score)
            else:
                print('same score')



Aucun commentaire:

Enregistrer un commentaire