""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: maxwell lee Date: 21/3/2025 Python: 3.5 ------------------------------------------------- """ import random import time # Playing variables MAX_CHIPS = 21 round_count = 1 # Card values and deck card_values = { '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11 } # Function to draw a card def draw_card(): return random.choice(list(card_values.keys())) # Checking for name validation. Will only accept names with no numbers or spaces while True: name1 = str(input("What is your name player 1?: ")) if name1.isalpha() == True: break else: print("That's not an acceptable name, try again") # Checking for name validation. Will only accept names with no numbers or spaces while True: name2 = str(input("What is your name player 2?: ")) if name2.isalpha() == True: break else: print("That's not an acceptable name, try again") # Checking for integer input validation while True: try: rounds = int(input("How many rounds do you want to play? ")) if rounds < 1 or rounds > 5: print("Please choose a number between 1 and 5") else: break except: print("You didn't even put in a number, how am I supposed to process that?") # Loss counters player1_losses = 0 player2_losses = 0 # Main game loop for rounds while round_count <= rounds: time.sleep(1) print("Round " + str(round_count) + " begins!") # Reset the scores for both players for each round PlayerOneScore = 0 PlayerTwoScore = 0 # Player 1's turn while True: PlayerOneScore = PlayerOneScore + card_values[draw_card()] print(name1 + "'s score is now " + str(PlayerOneScore)) if PlayerOneScore == MAX_CHIPS: print(name1 + " has reached a score of 21! Can " + name2 + ",tie things up!") break if PlayerOneScore > MAX_CHIPS: print(name1 + "'s score is over 21! You lost!") player1_losses = player1_losses + 1 break decision1 = input("Does " + name1 + " want to [H]it or [S]tand? ").lower() if decision1 == 's': print(name1 + "'s turn is up. Let's see how they did with a score of " + str(PlayerOneScore) + ".") break elif decision1 == 'h': print("Here comes another card for " + name1 + "...") continue else: print("Invalid input, please type 'H' to hit or 'S' to stand.") # Player 2's turn while True: PlayerTwoScore = PlayerTwoScore + card_values[draw_card()] print(name2 + "'s score is now " + str(PlayerTwoScore)) if PlayerTwoScore == MAX_CHIPS: print(name2 + " has reached a score of 21! You win!") break if PlayerTwoScore > MAX_CHIPS: print(name2 + "'s score is over 21! Busted!") player2_losses = player2_losses + 1 break decision2 = input("Does " + name2 + " want to [H]it or [S]tand? ").lower() if decision2 == 's': print(name2 + "'s turn is up. Let's see how they did with a score of " + str(PlayerTwoScore) + ".") break elif decision2 == 'h': print("Here comes another card for " + name2 + "...") continue else: print("Invalid input, please type 'H' to hit or 'S' to stand.") if PlayerOneScore > MAX_CHIPS: print(name1 + " lost this round!") elif PlayerTwoScore > MAX_CHIPS: print(name2 + " lost this round!") elif PlayerOneScore > PlayerTwoScore: print(name1 + " wins this round with " + str(PlayerOneScore) + " points!") elif PlayerTwoScore > PlayerOneScore: print(name2 + " wins this round with " + str(PlayerTwoScore) + " points!") else: print("It's a tie this round!") round_count = round_count + 1 time.sleep(1) # Final winner if player1_losses < player2_losses: time.sleep(1) print(name1 + " is the overall winner!!!") elif player2_losses < player1_losses: time.sleep(1) print(name2 + " is the overall winner!!!") else: time.sleep(1) print("It's a tie overall based on wins and losses!")