import random # displays instructions def instructions(): """prints instructions""" print(""" ⋆⁺。˚⋆˙‧₊☽ Instructions ☾₊‧˙⋆˚。⁺⋆ first choose the round you want to play, best of three or best of 5 then you can choose to hit or stand on the current cards you have if hit, draw another card, if stand, let the cpu draw their card first get as close to 21 as you can without going over to win! oh, and by the way, in this version of blackjack, kings, queens, & jacks are all equal to 10, and aces are equal to 1. Have Fun!! """) # asks if user wants instructions and then displays if user says yes def string_checker(question, valid_ans): while True: error = f'enter a valid response from {valid_ans}' try: user_response = input(question).lower() if user_response == "yes" or user_response == "y": return "yes" elif user_response == "no" or user_response == "n": return "no" else: print(error) except ValueError: print(error) # asks the user how many rounds they want and records their answer def int_check(int_question, low, high): """checks if users want a best of three or a best of five""" while True: error = f"Invalid input. Enter a number between {low} and {high}" try: response = int(input(int_question)) if low <= response <= high: return response else: print(error) except ValueError: print(error) # creates player hand and cpu hand and shows the player the hands def create_hands_user(player): for i in range(2): random_card_number = random.randint(0, len(card_list) - 1) user_card_value.append(card_values[random_card_number]) player_card_selection = card_list[random_card_number] player.append(player_card_selection) def create_hands_cpu(cpu): for i in range(2): random_card_number = random.randint(0, len(card_list) - 1) cpu_card_value.append(card_values[random_card_number]) cpu_card_selection = card_list[random_card_number] cpu.append(cpu_card_selection) # second string checker for the hitting and standing section def player_choice(question, hs_valid_ans): while True: error = f'enter a valid response from {hs_valid_ans}' try: response = input(question).lower() if response in hs_valid_ans: return response else: print(error) except ValueError: print(error) # asks the user if they want to hit or stand and changes hand or leave depending on their answer def player_turn(user_response): #user_response2 = user_response2.lower() # if its hit add a card to player hand and leave cpu hand if user_response == "hit" or user_response == "h": print("") print("--- your hand ---") random_card_number = random.randint(0, len(card_list) - 1) user_card_value.append(card_values[random_card_number]) player_card_selection = card_list[random_card_number] player_hand.append(player_card_selection) for i in range(len(player_hand)): print(player_hand[i]) print("") print("--- Opponents hand ---") for i in range(len(cpu_hand)): print(cpu_hand[i]) # if its stand don't change player hand or cpu hand if user_response == "stand" or user_response == "s": print("") print("--- your hand ---") for i in range(len(player_hand)): print(player_hand[i]) print("") print("--- Opponents hand ---") for i in range(len(cpu_hand)): print(cpu_hand[i]) # the cpu decision AI which makes it hit or stand def cpu_turn(): choice = random.randint(0, 1) if choice == 0: cpu_hit() else: cpu_stand() def cpu_hit(): print("|Opponents turn|") random_card_number = random.randint(0, len(card_list) - 1) cpu_card_value.append(card_values[random_card_number]) cpu_card_selection = card_list[random_card_number] cpu_hand.append(cpu_card_selection) print(f"the opponent chose to hit. their hand is now: ") for i in range(len(cpu_hand)): print(cpu_hand[i - 1]) def cpu_stand(): print("|Opponents turn|") print("the opponent chose to stand. their hand is now ") for i in range(len(cpu_hand)): print(cpu_hand[i]) def win_loss_checker(user_card_value_total, cpu_card_value_total, game_stats): if user_card_value_total == 21: game_stats['user_wins'] += 1 return "You win! Nice job!" elif user_card_value_total > 21: game_stats['cpu_wins'] += 1 return "You lose this game. nice try!" elif cpu_card_value_total > 21: game_stats['user_wins'] += 1 return "You win! Nice job!" elif cpu_card_value_total == 21: game_stats['cpu_wins'] += 1 return "You lose this game. nice try!" else: return "no winner yet. keep going." # initialise game variables player_hand = [] cpu_hand = [] points = 0 card_list = ["A of hearts", "A of diamonds", "A of spades", "A of clubs", "K of hearts", "K of diamonds", "K of spades", "K of clubs", "Q of hearts", "Q of diamonds", "Q of spades", "Q of clubs", "J of hearts", "J of diamonds", "J of spades", "J of clubs", "10 of hearts", "10 of diamonds", "10 of spades", "10 of clubs", "9 of hearts", "9 of diamonds", "9 of spades", "9 of clubs", "8 of hearts", "8 of diamonds", "8 of spades", "8 of clubs", "7 of hearts", "7 of diamonds", "7 of spades", "7 of clubs", "6 of hearts", "6 of diamonds", "6 of spades", "6 of clubs", "5 of hearts", "5 of diamonds", "5 of spades", "5 of clubs", "4 of hearts", "4 of diamonds", "4 of spades", "4 of clubs", "3 of hearts", "3 of diamonds", "3 of spades", "3 of clubs", "2 of hearts", "2 of diamonds", "2 of spades", "2 of clubs"] card_values = [1, 1, 1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2] hit_stand = ["hit", "stand", "h", "s"] yes_no = ["yes", "no", "y", "n"] cpu_card_value = [] user_card_value = [] game_stats = {'user_wins': 0, 'cpu_wins': 0} # main routine # ask user instructions question tf = string_checker("you gon lose a load a cash if ya dun know what ur doin, would you like to read the instructions? ", yes_no) if tf == "yes": instructions() print() # checks if the user wants the best of three rounds or the best of five num_rounds = int_check("How many rounds do you want to play? choose between three and five. ", 3, 5) print() # game loop rounds = 1 for i in range(num_rounds): print(f"Round {rounds} of {num_rounds}") # run hand creator for player create_hands_user(player_hand) print("") print("--- Your Hand ---") for i in range(2): print(player_hand[i - 1]) # run hand creator for cpu create_hands_cpu(cpu_hand) print("") print("--- Opponents hand ---") for i in range(2): print(cpu_hand[i - 1]) print() while True: ans2 = player_choice("Are ye gon hit? or stand? ", hit_stand) player_turn(ans2) user_card_value_total = sum(user_card_value) print() cpu_turn() cpu_card_value_total = sum(cpu_card_value) print() result = win_loss_checker(user_card_value_total, cpu_card_value_total, game_stats) print(result) if result == 'You win! Nice job!' or result == 'You lose this game. nice try!': break else: continue print() print() rounds += 1 player_hand.clear() cpu_hand.clear() cpu_card_value.clear() user_card_value.clear() print("You did well! Here are your game statistics: ") print(f"|Rounds won: {game_stats['user_wins']}|") print(f"|Rounds lost: {game_stats['cpu_wins']}|") print() print("Thanks for playing! i hope you enjoyed my blackjack game. to play again, please press the green play button in the top right.")