vendredi 27 mars 2020

Looking for critique on my first Python Code and a way to keep variables

After just learning a bit of Python I tried coding something myself. I attempted to do a game of Blackjack, which is far from perfect, and still doesn't have some rules a real game has (in my game an ace is always 11 points, whereas in reality it can also be counted as 1 point).

There is however an issue that I'm having, simply because I don't understand it: I would like pack some of my code in methods and call them, but I'm having an issue with some variables. I use j as a variable for the card that would be on top of the deck. By default it is set to 4, because after the starting hands have been dealt the top card of the deck is the fourth one. Each time the player is dealt an additional card j is increased by 1. When the user finishes playing and it's the dealers turn I want to keep the current value of j and not revert to j being 4. How do I need to restructure my code in order to keep the changes that have been made to j in the while loop, if I put that loop into its own method? Is there a way I can "extract" a value of a variable from another method?

Additionally I want to develop good habits right from the start and tried using the best practices I know of, like using formatted Strings or using j += 1instead of j = j + 1. Are there any bad practices in my code, where can I improve? Here's the game in its current state, it seems to be working correctly, as I haven't found a way to break it. (A minor flaw is that you have to press "Y" or "y" for another card, but instead of having to press "n" or "N" for no more cards you can press anything)

import random

# the player plays first
# dealer goes second
# dealer hits on 16 and stands on 17

deck_of_cards = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
cards = {
    "Two": 2,
    "Three": 3,
    "Four": 4,
    "Five": 5,
    "Six": 6,
    "Seven": 7,
    "Eight": 8,
    "Nine": 9,
    "Ten": 10,
    "Jack": 10,
    "Queen": 10,
    "King": 10,
    "Ace": 11,
}


def drawing_a_card(card_num):
    i = card_num - 1
    drawn_card = deck_of_cards[i]
    return drawn_card


random.shuffle(deck_of_cards)
players_first_card = drawing_a_card(card_num=1)
players_second_card = drawing_a_card(card_num=3)

dealers_first_card = drawing_a_card(card_num=2)
players_total = cards[players_first_card] + cards[players_second_card]

print(f"Your first card: {players_first_card}")
print(f"Your second card: {players_second_card}")
print(f"Dealer's first card: {dealers_first_card}")
decision = input(f"You are standing at {players_total}, would you like another card? (Y/N) ")
if players_total == 21 :
    print(f"You hit 21! That's Blackjack, you win!")

j = 4
while decision.lower() == "y":
    if decision.lower() == "y":
        players_next_card = drawing_a_card(card_num=j)
        print(f"Your card: {players_next_card}")
        players_total += cards[players_next_card]
        j += 1
        if players_total > 21:
            print(f"That's a Bust! Your total was {players_total}")
            break
        elif players_total == 21:
            print(f"You hit 21! That's Blackjack, you win!")
            break
        else:
            decision = input(f"You are now standing at {players_total}, would you like another card? (Y/N) ")

k = j+1

if players_total < 21:
    print("The Dealer will now take his turn...")
    dealers_second_card = drawing_a_card(card_num=k)
    k += 1
    print(f"Dealer's cards are {dealers_first_card} and {dealers_second_card}")
    dealers_total = cards[dealers_first_card] + cards[dealers_second_card]
    if dealers_total == 21:
        print(f"The Dealer hit 21! That's Blackjack, the Dealer wins!")
    while dealers_total <= 16:
        print(f"Dealer's total is {dealers_total}, he'll take another card.")
        dealers_next_card = drawing_a_card(card_num=k)
        print(f"The Dealer's card: {dealers_next_card}")
        dealers_total += cards[dealers_next_card]
        k += 1
    if dealers_total == 21:
        print(f"The Dealer hit 21! That's Blackjack, the Dealer wins!")
    elif dealers_total > 21:
        print(f"The Dealers total is {dealers_total}, he has bust and you win!")
    if dealers_total >= 17:
        print(f"The Dealer stands at {dealers_total}")
        if dealers_total < players_total:
            print(f"Your total is {players_total}, which is higher than the Dealer's {dealers_total}, you win!")
        elif dealers_total == players_total:
            print(f"You and the Dealer are both standing at {dealers_total}, it's a draw!")
        elif dealers_total > players_total:
            print(f"The Dealer beats your {players_total} with his {dealers_total}, you lose!")




Aucun commentaire:

Enregistrer un commentaire