#-------------------------------------------------------------------------- # Name : Take-Away game # Purpose : 11DGT Assessment 1 # Author: Joe Hilford # Created : 24/03/2024 # Copyright : © Joe Hilford 24/03/2024 # License : No license needed #-------------------------------------------------------------------------- #Imported libraries import time import sys #Defining variables STARTING_CHIPS = 21 #Number of chips for the start of the game (Constant) current_chips = 0 #Number of chips takeaway_chips = 0 round_number = 0 turn = 1 player1_wins = 0 player2_wins = 0 player1_name = 0 player2_name = 0 #Starting instructions print("Welcome players to the Take-Away game") time.sleep(1) print("\nIn this game two players will start with 21 chips") time.sleep(3) print("\nYou will each take turns to take away 1, 2 or 3 chips") time.sleep(4) print("\nYou will take away until one player lands on 0 chips") time.sleep(4) print("\nNow before we can begin please input your names") #Player1 name input and checking the name is only text while True: player1_name = input("\nWhat is player No.1's name? ") if player1_name.isalpha() == True: break else: print("\nPlease input a logical name") #Player2 name input and checking the name is only text while True: player2_name = input("\nWhat is player No.2's name? ") if player2_name.isalpha() == True: break else: print("\nPLease input a logical name") #Greeting and setting round number print("\nHello {} and {}.".format(player1_name, player2_name)) print("Now that you have selected your names, how many rounds would you like to play? (1-10) ") #Input for number of rounds and making sure rounds is between 1 and 10 while True: total_number_of_rounds = int(input()) if total_number_of_rounds > 0 and total_number_of_rounds < 11: break else: print("Try another number that is between 1 and 10") #Game total rounds loop while round_number < total_number_of_rounds: round_number = round_number + 1 current_chips = STARTING_CHIPS print("\nRound {}".format(round_number)) #Loop for each individual round checking that there is more than 0 chips while current_chips > 0: #Player 1's turn if turn == 1: print("\nThe current chips is {}".format(current_chips)) print("\n", player1_name , "'s turn") #Input for number of chips to take whilst checking it is between 1 and 3 while True: takeaway_chips = int(input("\nHow many chips would you like to take? ")) if 1 <= takeaway_chips <= 3 and takeaway_chips <= current_chips and takeaway_chips <= current_chips: break else: print("That is not a number between 1 and 3") #Calculating chips left current_chips = current_chips - takeaway_chips #Calculating player 1's wins if current_chips < 1: player1_wins = player1_wins + 1 #Breaking loop if there are zero counters left after player 1's turn if current_chips == 0: break else: #Switching to player 2's turn turn += 1 #Player 2's turn if turn == 2: print("\nThe current chips is {}".format(current_chips)) print("\n{}'s turn".format(player2_name)) #Input for number of chips to take whilst checking it is between 1 and 3 while True: takeaway_chips = int(input("\n How many chips would you like to take? ")) if 1 <= takeaway_chips <= 3 and takeaway_chips <= current_chips and takeaway_chips <= current_chips: break else: print("That is not a number between 1 and 3") #Calculating chips left current_chips = current_chips - takeaway_chips #Calculating player 2's wins if current_chips < 1: player2_wins = player2_wins + 1 #Switching back to turn 1 for the next round turn -= 1 #Win loop for when rounds is equal to total rounds while round_number == total_number_of_rounds: #Loop for a tie while player1_wins == player2_wins: print("\nThe game was a tie with {} wins each".format(player1_wins)) print(" ") print(" ") sys.exit() #Loop for player 1 winning while player1_wins == total_number_of_rounds: print("\n{} wins with {} rounds won".format(player1_name, player1_wins)) print(" ") print(" ") sys.exit() #Loop for player 2 winning while player2_wins == total_number_of_rounds: print("\n{} wins with {} rounds won".format(player2_name, player2_wins)) print(" ") print(" ") sys.exit()