#-------------------------------------------------------------------------- # Name : 21 game # Purpose : 11DGT 10% Assessment 2024 # Author: Bailey WItheford # Created : 19.3.2024 # Finished : 22.3.2024 # Copyright : © Bailey Witheford 2024 # Python: 3.9.13 #-------------------------------------------------------------------------- # This program allows two users to play the card game 21. First they need to input # their names for them to see whos turn it is. You will then chose how many rounds # you wish to play and the code will repeat that amount of times (variable: play_rounds) # you then chose between 1 and the specifyed amount of chips/cards/tokens and the # first to the number 21 wins that round. At the end the program announces which # player won based on their wins compared to the other or announces a tie. # _________________________________ # Imports import time # Finds any characters apart from letters (True/False outputs) isvalidname = False def is_valid_name(s): return all(char.isalpha() or char.isspace() for char in s) def br(): # br = Break in lines (easier to code) print("") def br2(): # br2 = Break in lines 2 times (easier to code) print("") print("") def br3(): # br3 = Break in lines 3 times (easier to code) print("") print("") print("") def br56(): # br56 = Break in lines 56 times (easier to code) lines = 56 while lines > 0 : print("") lines -= 1 time.sleep(0.01) # Variables player_one_name = "" player_two_name = "" player_one_chips = [] # What player 1 chose save data player_two_chips = [] # What player 2 chose save data max_chips = 21 # Change if you want more chips in your game chips_left = max_chips max_number_player_can_take = 3 # If you want to chose more than 3 chips per round change this play_rounds = 0 # Amount of rounds players chose to play played_rounds = 0 # Amount of rounds currently played player_choice = 0 # Don't mess with this, it does a variety of things p1_rounds_won = 0 p2_rounds_won = 0 # Greeting br2() print("Hello there new players!") br() # Player one print("Now who might player number 1 be? ") while isvalidname == False : # Checking and getting player 1 name player_one_name = input("Player 1 name: ") isvalidname == is_valid_name(player_one_name) if is_valid_name(player_one_name) : break print("not a valid name") br2() # Player two print("Now who might player number 2 be? ") while isvalidname == False : # Checking and getting player 2 name player_two_name = input("Player 2 name: ") isvalidname == is_valid_name(player_two_name) if is_valid_name(player_two_name) : break print("not a valid name") time.sleep(1) br3() # Asks how many rounds the player wishes to play while play_rounds == 0 : play_rounds = int(input("How many rounds would you like to play? ")) if play_rounds == 0 : print("hmm, that doesn't seem to be a valid number. Please try again") time.sleep(1.5) br56() while not play_rounds == played_rounds : played_rounds += 1 br3() br3() print("ROUND {}".format(played_rounds)) br3 time.sleep(1.3) while chips_left > 0 : # Player one chooses chips 1, 2, or 3 player_choice = 0 br2() print(" ' Player {}: '".format(player_one_name)) print("Chips: ", max_chips - chips_left) player_choice = int(input("Take 1 to 3 chips: ")) while player_choice not in range(1, max_number_player_can_take + 1) : # If not print(" - INVALID: CHOOSE BETWEEN 1 AND 3 -") br() player_choice = int(input("Take 1 to 3 chips: ")) chips_left -=player_choice player_one_chips.append(player_choice) if not chips_left > 0 : br() # If reached number 21 player 2 has won (below) print("CONGRATS {}! You have won round {}!".format(player_one_name, played_rounds)) p1_rounds_won += 1 break # Player two chooses chips 1, 2, or 3 player_choice = 0 br2() print(" ' Player {}: '".format(player_two_name)) print("Chips: ", max_chips - chips_left) player_choice = int(input("Take 1 to 3 chips: ")) while player_choice not in range(1, max_number_player_can_take + 1) : print(" - INVALID CHOOSE BETWEEN 1 AND 3 -") br() player_choice = int(input("Take 1 to 3 chips: ")) chips_left -=player_choice player_two_chips.append(player_choice) if not chips_left > 0 : br() # If reached number 21 player 2 has won (below) print("CONGRATS {}! You have won round {}!".format(player_two_name, played_rounds)) p2_rounds_won += 1 break time.sleep(2) # End of round stats (displays their current wins and what chips they choose during the game) br3() print("Round {} stats:".format(played_rounds)) print("{} played chips: {} this round and won {} rounds".format(player_one_name, player_one_chips, p1_rounds_won)) time.sleep(0.3) print("and") time.sleep(0.3) print("{} played chips: {} this round and won {} rounds".format(player_two_name, player_two_chips, p2_rounds_won)) player_one_chips.clear() player_two_chips.clear() chips_left = max_chips br() input("press any key and enter to continue: ") # When player is ready they type an input and see who has won br56() if p1_rounds_won > p2_rounds_won : # If player 1 has more wins they win print("Congats {}! You won {} rounds out of {}!".format(player_one_name, p1_rounds_won, play_rounds)) if p2_rounds_won > p1_rounds_won : # If player 2 has more wins they win print("Congats {}! You won {} rounds out of {}!".format(player_two_name, p2_rounds_won, play_rounds)) if p1_rounds_won == p2_rounds_won : # If both players tie on wins print("Huh, it seems you both won {} times in your {} rounds, its a tie!".format(p1_rounds_won, play_rounds)) else : print("Huh, for some reason I can't display who won the game :c it seems you've found a bug. Congrats!") br3()