#------------------------- # Name: Take Away Game # Standard: TE PUNGA - Programming Assessment # School: Tauranga Boys' College # Author: Kayden Franz-Kwan # Date: # Python: 3.4.2 #------------------------- # This program plays the Take Away Game. A game where you have 21 chips and need to remove all of them to win. # You specify the amount of rounds you want to play and you play those rounds. # To take away a chip, you input a number between 1 and 3 and that many chips will be removed. # There is a second player however so once you take away some chips, the other player can take away chips as well. # The first person to finish removing all of the chips wins. # This program has a logical structure and well named variable names so it is easier to understand # The top paragrapgh explains the base game but I added additions to the game such as multiple players and a final scoreboard at the end. #Seting up constants MAXCHIPS = 21 #Set max chips to 21 to set chips to when round resets MAXPLAYERS = 10 #Set max players to 10 so more players can play meaning more variation null = "" #Null variable for blank lines INV = "Invalid input. Please try again" #Invalid Response for invalid inputs #Set up name array for a maximum of 20 players names = [] #Function that ensures the value inputted is numeric def isNumeric(inputStatement): inputLoop = True while inputLoop == True: isNumericInput = input(inputStatement) if isNumericInput.isnumeric() == True: inputLoop = False isNumericInput = int(isNumericInput) else: print(INV) return(isNumericInput) #Add player names to array nameLoop = True playerCount = 0 while nameLoop == True: playerCount += 1 #Player count increases by 1 countLoop = True #Secondary loop to prevent Player Count for increasing with invalid inputs while countLoop == True: invalidName = False playerName = input("Player " + str(playerCount) + " name(type DONE when all players are in): ") if playerCount > 1: #Ensures nobody has the same name for clarity for x in range(len(names)): if playerName == names[x]: invalidName = True #If 2 names are the same, force to beggining if invalidName == True: #If the player name has already been inputed, redo the loop print(INV) elif playerName == "DONE" and playerCount > 2: #If the players have all been inputed and there is 2 players minimum, stop loop nameLoop = False countLoop = False elif playerName == "DONE": #If the players have all been inputed but there is less than 2 players, redo the loop print(INV) elif playerName == null: #If the players have inputed a null name, redo the loop print(INV) elif playerCount >= MAXPLAYERS: #If the amount of players exceeds the maximum allowed players, stop the loop nameLoop = False countLoop = False else: #If none of the above is true, add a player and stop the loop names.append(playerName) countLoop = False #Sets up score board array scores = [] for x in range(len(names)): scores.append(0) #Asks for amount of rounds and checks if the round value inputted is valid rounds = isNumeric("How many rounds: ") #Loop which runs the game roundNumber = 1 while roundNumber <= rounds: chipsRemaining = MAXCHIPS #Loop for every round roundLoop = True while roundLoop == True: playerNumber = 0 #Keeps track of which players turn it is #Loop so players rotate turns instead of the game ending with chips greater than 0 while playerNumber < len(names) and roundLoop == True: playerLoop = True #Tertiary Loop to ensure playerNumber doesn't increase with invalid inputs while playerLoop == True: #Prints whose turn it is and how many chips are to be removed print("--------------------") print("Round", roundNumber) print(names[playerNumber] + "'s Turn") print("Chips:", chipsRemaining) takeAway = isNumeric("How many chips would you like to remove: ") print("--------------------") #If staement to ensure no invalid inputs if 1 <= takeAway <= 3: chipsRemaining -= takeAway playerLoop = False else: print(INV) #Checks if someone has won if chipsRemaining < 1: print(names[playerNumber], "wins round", roundNumber) roundLoop = False scores[playerNumber] += 1 else: playerNumber += 1 #Increases round roundNumber += 1 #When all rounds are done, print out names and scores print(null) print("Scores:") print("-" * len(names) * 4) #Prints a spacer line for x in range(len(names)): print(names[x] + ": " + str(scores[x])) #Finds who the overall winner topPoints = 0 winner = null for x in range(len(scores)): if scores[x] > topPoints: topPoints = scores[x] winner = names[x] #Print who is winner is and their points print("And the winner is", winner, "with", str(topPoints), "points")