Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint #This allows a random integer to be generated
- menuChoice = 1 #This sets the default value for 'menuChoice' to one
- amountOfColumns = 8 #This sets the default amount of columns in the grid to 8
- amountOfRows = 8 #This sets the default amount of rows in the grid to 8
- amountOfBandits = 5 #This sets the default amount of bandits to 5
- amountOfTreasureChests = 10 #This sets the default amount of treasure chests to 10
- score = 0 #This sets the default value of the score to 0
- #These values create a space where the user can input the values for most of the variables
- #Task 1: Main Menu (User Need 1)
- def menu(): #This declares a new function
- global amountOfColumns #This allows this variable to be called outside of this function
- global amountOfRows #This allows this variable to be called outside of this function
- global amountOfBandits #This allows this variable to be called outside of this function
- global amountOfTreasureChests #This allows this variable to be called outside of this function
- print("\nWelcome to the AQA Treasure hunt game.\nShown below are the 3 options that you can select from:\nOption 1: Play the treasure hunt game\nOption 2: Custom rules\nOption 3: Quit")
- #This outputs a welcome message and menu instructions for the user; the linebreaker \n outputs a new line for the user
- #Allowing the user to choose an option (User Need 2)
- while True: #This creates a while loop
- try:
- menuChoice = int(input("\nPlease type in the assigned number of the option of your choice: "))
- #This creates a user-inputted variable which must have an integer data type
- except ValueError: #If an integer is not inputted...
- print("Your option was not a whole number. Please type in your option as a whole number")
- #This outputs an error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- if menuChoice < 1: #Condition set by selection for the variable value
- print("Sorry, your typed in option cannot be below '1'.")
- #If the option is less than 1, then this error message is displayed
- continue #This continues the while loop so the user has to re-input the value
- elif menuChoice > 3: #This sets another condition where the inputted option is greater than 4
- print("Sorry, your typed in option cannot be above '3'.")
- #If the option is greater than 4, then this error message is outputted
- continue #This continues the while loop so the user has to re-input the value
- #And the loop is continued so that the option has to be re-entered
- else: #If the conditions are not met...
- break #...Then the loop is broken and the option value is stored
- if menuChoice == 1: #Selection and the comparison operator == creates a condition for this variable
- banditsInTheGrid(programmerGrid) #This calls and runs the function declared here
- treasureChestsInTheGrid(programmerGrid) #This calls and runs the function declared here
- movingUpOrDown(programmerGrid,userGrid) #This calls and runs the function declared here
- elif menuChoice == 2: #Condition set so that when the variable equals 2...
- #Allowing the user to set custom dimensions and numbers of bandits + chests (User Need 10)
- while True: #This sets up an iterative while loop
- try:
- amountOfColumns = int(input("\nPlease type in how many columns you want in your grid: "))
- #This creates a user-inputted variable for the number of columns in the grid which must have an integer data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer is not a whole number.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- if amountOfColumns < 1: #Condition set by selection and comparison operators for the variable value
- print("You cannot have less than 1 column in the grid.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- else: #If the variable value is appropriate to be stored...
- while True: #This sets up an iterative while loop
- try:
- amountOfRows = int(input("\nPlease type in how many rows you want in your grid: "))
- #This creates a user-inputted variable for the number of rows in the grid which must have an integer data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer is not a whole number.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- if amountOfRows < 1:#Condition set by selection and comparison operators for the variable value
- print("You cannot have less than 1 row in the grid.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- else: #If the variable value is appropriate to be stored...
- while True: #This sets up an iterative while loop
- try:
- amountOfBandits = int(input("\nPlease type in how many bandits you want in your treasure hunt game: "))
- #This creates a user-inputted variable for the number of bandits in the game which must have an integer data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer is not a whole number.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- if amountOfBandits < 0: #Condition set by selection and comparison operators for the variable value
- print("You cannot have a negative amount of bandits.\nPlease re-enter the value.")
- #This outputs a personalised error message to the user which indicates them what they need to input
- continue #This continues the while loop so the user has to re-input the value
- else: #If the variable value is appropriate to be stored...
- while True: #This sets up an iterative while loop
- try:
- amountOfTreasureChests = int(input("\nPlease type in how many treasure chests you want in your treasure hunt game: "))
- #This creates a user-inputted variable for the number of treasure chests in the game which must have an integer data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer is not a whole number.\nPlease re-enter the value.")
- continue #This continues the while loop so the user has to re-input the value
- if amountOfTreasureChests < 4: #Condition set by selection and comparison operators for the variable value
- print("You cannot have less than 4 treasure chests.\nPlease re-enter the value.")
- continue #This continues the while loop so the user has to re-input the value
- else: #If the variable value is appropriate to be stored...
- menu() #This calls and runs the 'menu' function, returning the user back to the main menu
- elif menuChoice == 3: #Selection and comparison operators used to set a condition
- while True: #This sets up a while loop
- try:
- exitProgramChoice = str(input("Are you sure that you want to quit the program? Type in 'y' for yes or 'n' for no: "))
- #This variable is the choice that the user inputs for if they want to close the program or not
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Sorry, your typed in option is not a letter. Please re-enter your option.")
- #If the user doesn't input a string, then this error message is outputted
- continue #And the loop is continued so that the choice has to be re-entered
- if exitProgramChoice == 'y': #This sets a condition where the inputted choice is equal to 'y' (yes)
- quit() #This built-in function means that the program is terminated
- elif exitProgramChoice == 'n': #This sets a condition where the inputted choice is equal to 'n' (no)
- menu() #The 'menu' function is called where the user is returned back to the main menu
- else: #If the conditions have not been met...
- print("Your option was neither 'y' or 'n'. Please re-enter your option with either the letter 'y' for yes or 'n' for no.")
- #...Then this error message is displayed
- continue #And the loop continues so that the choice has to be re-entered
- #Creating a grid (User Need 3)
- programmerGrid = [[0 for x in range(amountOfColumns)] for y in range(amountOfRows)] #This 2D array sets the dimensions of the grid for the programmer
- userGrid = [[0 for x in range(amountOfColumns)] for y in range(amountOfRows)] #This 2D array sets the dimensions of the grid for the user
- #Setting up bandits (User Need 3)
- def banditsInTheGrid(programmerGrid): #This starts a new function with passing parameters
- banditsCounter = 0 #This defines a new local variable which the initial value of zero
- while banditsCounter < amountOfBandits: #This creates an iterative while loop for the local variable/counter
- YCoordinate = randint(0,(amountOfRows)-1) #This randomnly selects and indexes a column to assign a bandit
- XCoordinate = randint(0,(amountOfColumns)-1) #This randomnly selects and indexes a row to assign a bandit
- if programmerGrid[YCoordinate][XCoordinate] == 0: #Selection used to set a condition for the random index generated previously
- programmerGrid[YCoordinate][XCoordinate] = "B" #This assigns the random index in the array the character 'B' (for bandit)
- banditsCounter = banditsCounter + 1 #This increases the value of the counter by one
- #Setting up treasure chests (User Need 3)
- def treasureChestsInTheGrid(programmerGrid): #This creates a new function with passing parameters
- treasureChestsCounter = 0 #This creates a new local variable/counter
- while treasureChestsCounter < amountOfTreasureChests: #This sets up an iterative while loop for the local variable/counter
- YCoordinate = randint(0,(amountOfRows)-1) #This randomnly selects and indexes a column to assign a bandit
- XCoordinate = randint(0,(amountOfColumns)-1) #This randomnly selects and indexes a row to assign a bandit
- if programmerGrid[YCoordinate][XCoordinate] == 0: #Selection used to set a condition for the random index generated previously
- programmerGrid[YCoordinate][XCoordinate] = "TC" #This assigns the random index in the array the character 'TC' (for treasure chest)
- treasureChestsCounter = treasureChestsCounter + 1 #This increases the value of the counter by one
- userGrid[(amountOfRows)-1][0] = "P" #This creates and indexes a starting position for the player in the bottom left corner of the grid
- for row in userGrid: #For each row in the user grid...
- print(row) #The rows are outputted to the user; this outputs the user version of the grid
- #Letting the user move up and down (User Need 4)
- def movingUpOrDown(programmerGrid,userGrid): #This defines a new function
- global score #This allows this variable to be called outside of this function
- r = (amountOfColumns)-1 #This variable locates which column the user is in after each move
- c = 0 #This variable locates which column the user is in after each move
- while True: #This sets up an iterative while loop
- #This replaces the previous player position with a blank space (0) after the player has moved
- try:
- upOrDown = str(input("Do you want to move up or down?\nType in 'u' for up or 'd' for down: "))
- #This variable indicates if the user wants to move up or down, and it must have a string data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer was not a letter. Please re-enter your answer as either 'u' or 'd'")
- #This outputs an error message which is personalised
- continue #This continues the while loop so the user has to re-input the value
- if upOrDown.lower() =='u' or upOrDown.lower() =='d': #Condition set by selection and boolean operators for the variable value
- while True:#This sets up an iterative while loop
- try:
- movingUpOrDown = int(input("How many squares do you want to move up/down: "))
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer was not a whole number. Please re-enter your answer as a whole number")
- #This outputs an error message which is personalised
- continue #This continues the while loop so the user has to re-input the value
- if movingUpOrDown >= 0 and movingUpOrDown < amountOfRows and upOrDown.lower() =='u':
- if r-(movingUpOrDown) >= 0:
- userGrid[r][c] = 0
- #Condition set by selection and boolean operators for the variable value
- userGrid[r-(movingUpOrDown)][c] = "P" #Array indexing used to mark the new player position on the grid
- r = r - movingUpOrDown #This changes the value of the variable 'c'
- for row in userGrid: #For each row in the user grid...
- print(row) #The rows are outputted to the user with the new player position displayed
- #Calculating the player's score (User Need 5)
- trackPlayerPosition = programmerGrid[r][c] #This variable tracks each move the player makes by indexing the array of the grid
- if trackPlayerPosition =="TC": #Condition set by selection for the variable value
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a treasure chest
- programmerGrid[r][c] = "tc" #This declares that this spot in the grid is now the 2nd visit of the treasure chest
- elif trackPlayerPosition =="tc": #If this position in the grid is the 2nd treasure chest visit then...
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a treasure chest
- programmerGrid[r][c] = "B" #This declares that this spot in the grid is now the 3rd treasure chest visit
- elif trackPlayerPosition =="B": #If the player visits the treasure chest a 3rd time then ...
- score = 0 #The player's score resets to 0 as the treasure chest is now a bandit
- print("You have landed on a bandit. Your score is now", score)
- ##Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a bandit
- programmerGrid[r][c] = 0 #This sets the space visited by the user by a neutral/empty block (0)
- rightOrLeftMovement(userGrid,programmerGrid,r,c)
- else:
- print("Your answer is out of bounds. Please re-enter the value")
- elif movingUpOrDown >= 0 and movingUpOrDown < amountOfRows and upOrDown =='d':
- if r+(movingUpOrDown) <= (amountOfRows)-1:
- userGrid[r][c] = 0
- #Condition set by selection and boolean operators for the variable value
- userGrid[r+(movingUpOrDown)][c] = "P" #Array indexing used to mark the new player position on the grid
- r = r + movingUpOrDown #This changes the value of the variable 'c'
- for row in userGrid: #For each row in the user grid...
- print(row) #The rows are outputted to the user with the new player position displayed
- trackPlayerPosition = programmerGrid[r][c] #This variable tracks each move the player makes by indexing the array of the grid
- if trackPlayerPosition =="TC": #Condition set by selection for the variable value
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a treasure chest
- programmerGrid[r][c] = "tc" #This declares that this spot in the grid is now the 2nd visit of the treasure chest
- elif trackPlayerPosition =="tc": #If this position in the grid is the 2nd treasure chest visit then...
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a treasure chest
- programmerGrid[r][c] = "B" #This declares that this spot in the grid is now the 3rd treasure chest visit
- elif trackPlayerPosition =="B": #If the player visits the treasure chest a 3rd time then ...
- score = 0 #The player's score resets to 0 as the treasure chest is now a bandit
- print("You have landed on a bandit. Your score is now", score)
- #Concatenation and the command 'print' is used here to output the player's new score to the user as well as the fact they landed on a bandit
- programmerGrid[r][c] = 0 #This sets the space visited by the user by a neutral/empty block (0) after they have visited a bandit
- rightOrLeftMovement(userGrid,programmerGrid,r,c) #This calls and executes this function, allowing the user to move right or left in the grid
- else:
- print("Your answer is out of bounds. Please re-enter the value")
- else: #If none of the conditions have been met...
- print("Your answer was not a number between 0 and",(amountOfRows)-1,". Please re-enter your answer as a number between 1 and",(amountOfRows)-1)
- #Concatenation is used here to output an error message which is personalised
- continue #This continues the while loop so the user has to re-input the value
- elif upOrDown !='u' or upOrDown !='d': #Selection, boolean operators and comparison operators used to set a unique condition
- print("Your answer was neither the lowercase 'u' or the lowercase 'd'. Please re-enter your answer")
- #This outputs an error message which is personalised
- continue #This continues the while loop so the user has to re-input the value
- #Letting the user move left and right (User Need 4)
- def rightOrLeftMovement(userGrid,programmerGrid,r,c): #This defines a new function
- userGrid[r][c] = 0
- global score #This allows this variable to be called outside of this function
- while True: #This sets up an iterative while loop
- try:
- rightOrLeft = str(input("Do you want to move right or left?\nType in 'r' for right or 'l' for left: "))
- #This creates a user-inputted variable which must have a string data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer was not a letter. Please re-enter your answer as either 'r' or 'l'")
- #This outputs an error message which is personalised
- continue #This continues the loop so that the user has to re-input the value of the variable
- if rightOrLeft =='r' or rightOrLeft =='l': #Boolean operators and selection used to set a condition
- try:
- movingRightOrLeft = int(input("How many squares do you want to move right/left: "))
- #This creates a user-inputted variable which must have an integer data type
- except ValueError: #The error and exception checking sets a condition where the input is not the desired data type
- print("Your answer was not a whole number. Please re-enter your answer as a whole number")
- #This outputs an error message which is personalised
- continue #This continues the loop so that the user has to re-input the value of the variable
- if movingRightOrLeft >= 0 and movingRightOrLeft < amountOfColumns and rightOrLeft =='r':
- if c-(movingRightOrLeft) <= amountOfColumns:
- userGrid[r][c] = 0
- #Selection and boolean operators used to set a condition here
- userGrid[r][c+(movingRightOrLeft)] = "P" #This assigns a new player position according to the movement
- c = c+(movingRightOrLeft) #This variable has been assigned a new value here
- for row in userGrid: #Iterative for loop used here to access each row in the user version of the grid
- print(row) #Each row in the user grid is outputted to the user by the command 'print'
- #Calculating the player's score (User Need 5)
- trackPlayerPosition = programmerGrid[r][c] #This variable tracks the player position in the grid
- if trackPlayerPosition =="TC": #Selection used to see what the user lands on in the grid
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- #Outputting the new score to the user (User Need 7)
- print("You have landed on a treasure chest. Your score is now", score)
- #This outputs the new score to the user whenever they land on a treasure chest
- programmerGrid[r][c] = "tc" #This declares that this spot in the grid is now the 2nd visit of the treasure chest
- elif trackPlayerPosition =="tc": #If this position in the grid is the 2nd treasure chest visit then...
- score = score + 10 #The score is increased by the constant 10
- #Outputting the new score to the user (User Need 7)
- print("You have landed on a treasure chest. Your score is now", score)
- #This outputs the new score to the user whenever they land on a treasure chest
- programmerGrid[r][c] = "B" #This declares that this spot in the grid is now the 3rd treasure chest visit
- #Replacing a treasure chest with a bandit after the 3rd visit (User Need 6)
- elif trackPlayerPosition =="B": #If the player visits the treasure chest a 3rd time then ...
- score = 0 #The player's score resets to 0 as the treasure chest is now a bandit
- #Outputting the new score to the user (User Need 7)
- print("You have landed on a bandit. Your score is now", score)
- #This outputs to the user that they have now landed on a bandit, and then outputs their new score
- programmerGrid[r][c] = 0 #This sets the space visited by the user by a neutral/empty block (0)
- movingUpOrDown(programmerGrid,userGrid) #This calls the function, allowing the user to move up or down again in the grid
- else:
- print("Your answer is out of bounds. Please re-enter the value")
- elif movingRightOrLeft >= 0 and movingRightOrLeft < amountOfColumns and rightOrLeft =='l':
- if (movingRightOrLeft)-c >= amountOfColumns
- userGrid[r][c] = 0
- #Selection, boolean operators and comparison operators used to set a unique condition
- userGrid[r][c-(movingRightOrLeft)] = "P" #This assigns the new player position in the grid where the player has chosen to move
- c = c-(movingRightOrLeft) #This updates what column the user is in the grid
- for row in userGrid: #For loop used to access each row in the user version of the grid
- print(row) #Each row of the grid is printed to the user with the new player position shown
- trackPlayerPosition = programmerGrid[r][c] #This variable tracks the player's position in the grid
- if trackPlayerPosition =="TC": #If the player lands on a treasure chest then...
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #This outputs the new score to the user whenever they land on a treasure chest
- programmerGrid[r][c] = "tc" #This declares that this spot in the grid is now the 2nd visit of the treasure chest
- elif trackPlayerPosition =="tc": #Selection used to set a condition for this position in the grid
- score = score + 10 #This increases the players score to 10 by adding the constant 10 to it
- print("You have landed on a treasure chest. Your score is now", score)
- #This outputs the new score to the user whenever they land on a treasure chest
- programmerGrid[r][c] = "B" #This declares that this spot in the grid is now the 3rd treasure chest visit
- elif trackPlayerPosition =="B": #If the player visits the treasure chest a 3rd time then ...
- score = 0 #The player's score resets to 0 as the treasure chest is now a bandit
- print("You have landed on a bandit. Your score is now", score)
- #This outputs to the user that they have now landed on a bandit, and then outputs their new score
- programmerGrid[r][c] = 0 #This sets the space visited by the user by a neutral/empty block (0)
- movingUpOrDown(programmerGrid,userGrid) #This calls the function, allowing the user to move up or down again in the grid
- else:
- print("Your answer is out of bounds. Please re-enter the value")
- else: #If none of the other conditions have been met then...
- print("Your answer was not a number between 0 and",(amountOfColumns)-1,". Please re-enter your answer as a number between 1 and",(amountOfColumns)-1)
- #If the value is not between 0 and the amount of columns, then this error message is outputted to the user...
- continue #...And the user has to re-input the value of the variable again because of the loop
- else: #If none of the other conditions have been met then...
- print("Your answer was neither the lowercase 'r' or the lowercase 'l'. Please re-enter your answer")
- #If the input is not a lowercase 'r' or 'l', then this error message is displayed to the user
- continue #...And the user has to re-input the value of the variable again because of the loop
- #Notifying if the user has won and lost and their score and total moves made (User Need 8)
- if score >= 100: #Selection and comparison operators used to set a condition
- print("Congratulations, you have gained 100 gold coins!\nYou have won the treasure hunt game.")
- #This outputs a congratulations message to the user indicating that they have won the game
- #Returning the user back to the main menu (User Need 9)
- menu() #This calls and runs the 'menu' function (returns the user back to the menu)
- elif score < 100 and programmerGrid[:]!= "TC" and programmerGrid[:]!= "tc":
- #Selection, comparison operators and boolean operators used to set a condition here
- print("Sorry, you have visited all the treasure chests 3 times and not gained 100 gold coins.\nYou have lost the treasure hunt game as your score was only", score,".")
- #This outputs a consolation message to the user indicating that they have lost the game
- #Returning the user back to the main menu (User Need 9)
- menu() #This calls and runs the 'menu' function (returns the user back to the menu)
- menu() #This calls and runs the 'menu' function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement