""" ------------------------------------------------- Project: Take-Away Game Standard: 91883 (AS1.7) v.1 School: Tauranga Boys' College Author: Luke Jones Date: Python: 3.5 ------------------------------------------------- This program is to play the game Take-Away. There are 2 players in the game and they input their name alongside the amount of rounds they would like to play. The game then asks player one to pick the amount of chips they would like to take from the amount of chips remaining, then cycles to player two after that. The winner is whoever is the person to get the amount of chips left to 0 chips. There are also statistics displayed after each round and after the game is over. This program uses variables that are clearly named to make the code easier to understand The game can potentially be extended by changing the value for the maximum amount of chips, adding more players, or more. ------------------------------------------------- """ # Imports the sleep function from the time library so we can make certain code wait before running from time import sleep # Sets the starting values for the variables that will be used in the game currentRound = 1 chipsLeft = 0 chipsTaken = 0 playerOneScore = 0 playerTwoScore = 0 roundEnd = False playerTurn = "" # Sets the constant variable for the maximum chips to set for each round maxChips = 21 # Introduces the game to the players, tells them the game name and basic info about the game print("Welcome to Take-Away!") sleep(1) print("In this game you will be competing against another player to be the one who gets the chips to 0.") sleep(1) # User Inputs to ask for player names, has input validation while True: playerOne = str(input("Please input the name of player one: ")) if playerOne.isalpha() == True: break else: print("Input a real name!") while True: playerTwo = str(input("Please input the name of player two: ")) if playerTwo.isalpha() == True: break else: print("Input a real name!") # Asks for the amount of rounds the players want to play, has input validation while True: rounds = int(input("How many rounds would you like to play? Pick between 1-5: ")) if rounds > 0 and rounds < 6: break else: print("Please input a number between 1-5") # A function for players taking an amount of chips during their turn, has input validation def takingChips(player): global chipsLeft while True: print("Current Chips: {}".format(chipsLeft)) print(" ") chipsTaken = int(input("How many chips would you like to take, {}? ".format(player))) if chipsTaken > 3 or chipsTaken < 1: print("You can only take 1-3 chips each turn!") else: chipsLeft -= chipsTaken break # Function to check after the chip taking function whether the round should end def roundEndCheck(): if chipsLeft < 1: global currentRound, rounds, roundEnd, playerOneScore, playerTwoScore roundEnd = True currentRound += 1 rounds -= 1 # Checks who won the round and adds 1 to their score if playerTurn == "playerOne": print("{} wins the round!".format(playerOne)) playerOneScore += 1 else: print("{} wins the round!".format(playerTwo)) playerTwoScore += 1 # Function to display the final statistics when the game ends, displays final scores and who won. def gameScores(): print(" ") print("The game has ended.") sleep(1) print(" ") print("Final Scores:") print("{}'s final score was {}".format(playerOne, playerOneScore)) print("{}'s final score was {}".format(playerTwo, playerTwoScore)) sleep(1) if playerOneScore > playerTwoScore: print("{} won!".format(playerOne)) elif playerTwoScore > playerOneScore: print("{} won!".format(playerTwo)) else: print("The game was a tie!") # The while loop for the game, resets variables and prints statistics while rounds > 0: roundEnd = False chipsLeft = maxChips sleep(1) print(" ") print("Current round: {}".format(currentRound)) print("{}'s score: {}".format(playerOne, playerOneScore)) print("{}'s score: {}".format(playerTwo, playerTwoScore)) # The while loop for rounds, cycles through each player for the turns while roundEnd == False: playerTurn = "playerOne" takingChips(playerOne) roundEndCheck() if roundEnd == True: break playerTurn = "playerTwo" takingChips(playerTwo) roundEndCheck() # The game scores function which is run after the while loop for the game ends gameScores()