# ------------------------------------------------- # Project: 21 Number Game # School: Tauranga Boys' College # Author: Mason Angel Bunce # Date: 25/03/2024 # Python: 3.5 # ------------------------------------------------- # Constant variable MAX_CHIPS = 21 # Regular Variables # How much score this specific player has gamer1_score = 0 gamer2_score = 0 # The names the players have chosen (input variable) gamer1_name = " " gamer2_name = " " # The amount of rounds the players choose to play (input variable) rounds_play = 0 # Allows system to check if the amount of chips taken is a number between 1 and 3 loop = 0 # How many chips are remaining in the game chips_left = 0 # Allows players to choose how many chips they want to take (input variable) gamer_1 = 0 gamer_2 = 0 # Defines the names that both players want to choose def game_names(): global gamer1_name global gamer2_name gamer1_name = input("Please Type Player One's Name: ") gamer2_name = input("Please Type Player Two's Name: ") if gamer1_name.isalpha() and gamer2_name.isalpha(): print("Gamer one's name is {}! \nGamer two's name is {}!".format(gamer1_name, gamer2_name)) rounds() else: if not gamer1_name.isalpha(): print("{} isn't a valid name, please try again.".format(gamer1_name)) if not gamer2_name.isalpha(): print("{} isn't a valid name, please try again".format(gamer2_name)) game_names() #Defines how many rounds the players want to play def rounds(): global rounds_play try: rounds_play = int(input("How many rounds do you want to play between 1 and 5: ")) if 1 <= rounds_play <= 5: print("You will be playing {} rounds!".format(rounds_play)) game_play() else: print("You need to write a number between 1 and 5") rounds() except ValueError: print("That is not a number, please input a number") rounds() #Plays the 21 game def game_play(): global rounds_play, gamer_1, gamer1_score, gamer2_score while rounds_play > 0: chips_left = MAX_CHIPS while chips_left > 0: if chips_left > 0: try: gamer_1 = int(input("How many chips do you want {} between 1 and 3: ".format(gamer1_name))) if 1 <= gamer_1 <= 3: chips_left -= gamer_1 print("Chips left:", chips_left) if chips_left <= 0: gamer1_score += 1 rounds_play -= 1 print("{} won this round!".format(gamer1_name)) print("{} has {} points".format(gamer1_name, gamer1_score)) break else: print("Please input a number between 1 and 3") except ValueError: print("That is not a number") if chips_left > 0: try: gamer_2 = int(input("How many chips do you want {} between 1 and 3: ".format(gamer2_name))) if 1 <= gamer_2 <= 3: chips_left -= gamer_2 print("Chips left:", chips_left) if chips_left <= 0: gamer2_score += 1 rounds_play -= 1 print("{} won this round!".format(gamer2_name)) print("{} has {} points".format(gamer2_name, gamer2_score)) break else: print("Please input a number between 1 and 3") except ValueError: print("That is not a number") if rounds_play == 0: if gamer1_score < gamer2_score: print("{} WINS".format(gamer2_name)) print("{} had {} points and {} had {} points".format(gamer2_name, gamer2_score, gamer1_name, gamer1_score)) else: print("{} WINS".format(gamer1_name)) print("{} had {} point(s) and {} had {} point(s)".format(gamer1_name, gamer1_score, gamer2_name, gamer2_score)) # Checks if the code is being run from the main program if __name__ == "__main__": game_names()