From cd9d28d3e1b821b0c78a7d49fbe8ed82b2b6a026 Mon Sep 17 00:00:00 2001 From: Bubbles The Dev <152947339+KernFerm@users.noreply.github.com> Date: Sun, 6 Oct 2024 06:22:56 -0400 Subject: [PATCH] Update main.py Signed-off-by: Bubbles The Dev <152947339+KernFerm@users.noreply.github.com> --- main.py | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index db0b824..b9c6e4e 100644 --- a/main.py +++ b/main.py @@ -15,13 +15,34 @@ def clear_screen(): clear_screen() print(Fore.GREEN + "šŸŽ‰ Welcome to Blackjack! šŸŽ‰") -# šŸŽ“ Cards available to be selected (simplified to only face values) -cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +# šŸŽ“ Cards available to be selected (simplified to only face values including Ace) +cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] # 10 repeated to simulate face cards # šŸŽ² Function to draw a card def draw_card(): return random.choice(cards) +# šŸŽ“ Function to calculate the total of a hand with Ace handling +def calculate_total(cards): + total = sum(cards) + # If there's an Ace and it can count as 11 without busting, count it as 11 + if 1 in cards and total + 10 <= 21: + return total + 10 + return total + +# šŸ’” Function to check if the player has busted +def check_bust(cards): + return calculate_total(cards) > 21 + +# šŸ¤µ Dealer's turn logic: Dealer hits until they reach 17 or more +def dealer_turn(dealer_cards): + while calculate_total(dealer_cards) < 17: + new_card = draw_card() + dealer_cards.append(new_card) + print(Fore.YELLOW + f"šŸ¤µ Dealer draws a {new_card}. Dealer's cards: {', '.join(map(str, dealer_cards))}") + time.sleep(1) + return dealer_cards + # šŸŽ“ Drawing the initial cards for the player and the dealer player_cards = [draw_card(), draw_card()] dealer_cards = [draw_card(), draw_card()] @@ -32,19 +53,15 @@ def draw_card(): print(f"{Fore.CYAN}šŸƒ Your cards: {player_cards[0]} and {player_cards[1]}") time.sleep(1) -# šŸ”„ Function to calculate the total of a hand -def calculate_total(cards): - return sum(cards) - -# šŸ’” Function to check if the player has busted -def check_bust(cards): - return calculate_total(cards) > 21 - # šŸ“Š Game loop for player decisions while True: # Clear the screen for better readability clear_screen() + # Display current hands + print(f"{Fore.YELLOW}šŸ¤µ Dealer's cards: {dealer_cards[0]} šŸ‚ ") + print(f"{Fore.CYAN}šŸƒ Your cards: {', '.join(map(str, player_cards))} (Total: {calculate_total(player_cards)})") + # šŸ“ Asking the player for their decision print("\n" + Fore.MAGENTA + "What would you like to do? šŸ¤”") print(Fore.BLUE + "šŸ‘‰ (1) Hit šŸƒ") @@ -62,22 +79,25 @@ def check_bust(cards): print(Fore.RED + "šŸ’„ You've gone above 21! You busted! šŸ’„") sys.exit() elif player_input == '2': - # Player stands - print(Fore.YELLOW + f"\nšŸ¤µ Dealer's cards: {dealer_cards[0]} and {dealer_cards[1]}") + # Player stands, it's the dealer's turn + dealer_cards = dealer_turn(dealer_cards) + print(Fore.YELLOW + f"\nšŸ¤µ Dealer's final cards: {', '.join(map(str, dealer_cards))} (Total: {calculate_total(dealer_cards)})") time.sleep(1) - print(Fore.CYAN + f"šŸƒ Your cards: {', '.join(map(str, player_cards))}") + print(Fore.CYAN + f"šŸƒ Your final cards: {', '.join(map(str, player_cards))} (Total: {calculate_total(player_cards)})") time.sleep(1) - if calculate_total(player_cards) >= calculate_total(dealer_cards): + + # Compare hands to determine the winner + if check_bust(dealer_cards): + print(Fore.GREEN + "šŸŽ‰ Dealer busted! You win! šŸŽ‰") + elif calculate_total(player_cards) > calculate_total(dealer_cards): print(Fore.GREEN + "šŸŽ‰ You have won! Congratulations! šŸŽ‰") + elif calculate_total(player_cards) == calculate_total(dealer_cards): + print(Fore.YELLOW + "šŸ¤ It's a tie!") else: print(Fore.RED + "šŸ˜¢ You have lost. Better luck next time! šŸ˜¢") sys.exit() elif player_input == '3': # Player forfeits - print(Fore.YELLOW + f"šŸ¤µ Dealer's cards: {dealer_cards[0]} and {dealer_cards[1]}") - time.sleep(1) - print(Fore.CYAN + f"šŸƒ Your cards: {', '.join(map(str, player_cards))}") - time.sleep(1) print(Fore.RED + "šŸš« You forfeited. The dealer wins automatically.") sys.exit() else: