""" ------------------------------------------------- Project: 21 Pick-up Game Standard: TE PUNGA - Programming Assessment School: Tauranga Boys' College Author: Yashil Singh Date: 21/03/24 Python: 3.9.13 ------------------------------------------------- ------------------------------------------------- This code is for a game where there is 21 chips, and whoever takes the last chip wins. The code below makes it so that you (as a player) cannot simply take 21 chips and win stright away, instead you can only take an option of 1, 2, or 3 chips. If you are to try and take more than 3 chips or less than 1 chip, you will be faced with a message that reads "That's not... :(" this message reads this way to tell the player who tried to take more or less than the allowed chip amount to input a correct number. This code has 2 diffrent functions that looks confusing at first but make sence to seperate, otherwise it would be confusing. the function "Setup" is for the needed inputed infomation the players provide such as names, and rounds. It also includes the constant value of 21 inside of a "const" variable named "STARTINGCHIPS" this sets the chips to 21 at the start of the game. The function "maintanance" includes the regular variables that the game loop NEEDS in order to function, the players cannot change the the variables inside of this function but the players change these variables due to their choices. The "P1_score" and "P2_score" are the scores of players 1 and 2, after the end of each round, the person who took the last chip has a point increase of 1 added to their score. The "currently_playing" variable updates everytime a player makes a move within a round, at the start of the round the random libary will choose if player 1 or player 2 goes first, then after that every time a player plays within the round, the variable currently_playing will switch so that the other player plays within that round. The chips variable starts off as 21 and after a player takes away a certain amount of chips (that is allowed to them) the chips variable will change by that amount. The game loop itself has a "try" function, a try function will check if a player chooses the correct input, if this input is incorrect the player will be prompted to try again until they input correctly, the input intended for them to put in is clearly highlighted. Inside of the game loop, when the round ends, if there are more rounds, the amount of chips reset to 21, a increment of 1 is added to the winner of the rounds score, and a round is counted. If there are no more rounds to be played, it will move on to the final part of the code. The final part of the code will simply state the winner of the overall game according to the score of the player. If player 1 has a higher score they win vise versa, if they have the same score, they both tie. ------------------------------------------------- """ # Import the random libary so that the "currently_playing" variable can choose between player 1 or 2 randomly # (This cannot happen without the use of the random libary) import random def setup(): # All varibles that either cannot be changed or is user dependent global player1, player2, rounds, STARTINGCHIPS # Varibles that can be used anywhere so long as it is initilized (meaning loaded) # Checks if the Player/s input an incorrect name (Not a letter), if they do input an incorrect name, they must input a new name while True: player1 = str(input("What is your name, Player 1? ")) player2 = str(input("What is your name, player 2? ")) if player1.isalpha() == True and player2.isalpha() == True: # The function "Is_alpha" checks for if the input is only letters break # Exits the loop else: print("Not a real name, one or both of you, type in a real name.") # Amount of rounds to be played, chip number set here rounds = int(input("How many rounds do you want to play?")) STARTINGCHIPS = 21 def maintanance(): # All varibles that can be changed, but not directly by the player, only indirectly global P1_score, P2_score, currently_playing, chips # Varibles that can be used anywhere so long as it is initilized (meaning loaded) # Score of players P1_score = 0 P2_score = 0 currently_playing = 0 # If variable "currently_playing" is 0, player 1 playes, otherwise, player 2 plays chips = STARTINGCHIPS setup() # Loads in the variables that the player inputs, or variables that cannot change maintanance() # Loads in all variables that can only be indirectly changed while rounds >= 1: # Game loop currently_playing = random.randint(0, 1) # Who goes first player 1 plays if the number is 1, otherwise, player 2 plays while chips > 0: if currently_playing == 0: print(player1, "Pick 1, 2 or 3 chips") else: print(player2, "Pick 1, 2 or 3 chips") take_chips = int(input("1, 2, or 3? ")) # Intended chips to be taken out pile # Checks if the correct number has been inputed while True: try: # Checks for the correct input if take_chips > 3 or take_chips <= 0: print("That's not... :(") print("There are", chips, "chips left.") take_chips = int(input("1, 2, or 3? ")) else: break # Exits the loop except: print("That's not a number :(") # If the player input isn't an number take_chips = int(input("1, 2, or 3? ")) # Checks if the chips variable goes under 0, if so, makes a user try again until chips are over or equal 0 while True: if chips < take_chips: print("Chips... not... there...") print("There are", chips, "chips left.") take_chips = int(input("Just take what's left, you've won. ")) else: break # Exits the loop # Handles chips taken and turn switch chips -= take_chips # Take away amount of chips the player inputs print("There are", chips, "chips left.") currently_playing = 1 - currently_playing # Switch players # Update scores and reset chips for the next round print("Round end!") if currently_playing == 1: # Score increase for Player 1 P1_score += 1 print(player1, "wins, SCORE:", P1_score, player2, "SCORE:", P2_score) else: # Score increase for Player 2 P2_score += 1 print(player2, "wins, SCORE:", P2_score, player1, "SCORE:", P1_score) chips = STARTINGCHIPS # Reset chips for the next round rounds -= 1 # Updates the round # Tells players who won Player 1 or Player 2 if P1_score > P2_score: # If player 1 wins print(player1, "Wins!") elif P1_score < P2_score: # If player 2 wins print(player2, "Wins!") else: # If a tie happens print("It's a tie!")