Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# TODO: Student writes their name and the date they started the assignment at the top of the file in the comment block below. #

# TODO: Student writes their name and the date they started the assignment at the top of the file in the comment block below. # (delete this TODO line when completed) (1 pt.) # # # # TODO: Student writes the assignment number, and a brief description of what the assignment is inside the comment block above. # This description should document what the code is and what it does, such that a new user can read it and understand what # the script does. # (delete this TODO line when completed) (1 pt.) # TODO: Student turns in their assignment before the 11:59 p.m. Friday deadline and the file is named spinnerWinner.py # (delete this TODO line when completed) (1 pt.) import random # NOTE: Your functions should go here! def printTitleMaterial(): """ This function prints the 'title material' that prints out when the program starts. """ print("Spinner Winner!") print() # TODO: Have student print their name/ section when the script runs # (delete this TODO line when completed) (1 pt.) print("By: ") print("[COM S 127

]") print() def initialChoice(): """ This function allows the player to make various choices when starting the game. This is an example of the 'do-while' pattern. :return String: The choice the player has made when starting the program to [p]lay the game, view the [i]nstructions, or [q]uit the program.. """ choice = input("Choice? [p]lay, [i]nstructions, [q]uit: ") while choice != "p" and choice != "i" and choice != "q": print("ERROR: Please enter 'p', 'i', or 'q'...") choice = input("Choice? [p]lay, [i]nstructions, [q]uit: ") return choice def chooseNumPlayers(): """ This function allows the player to choose whether they will play against another human, or play against the computer. :return Integer: The number of players in the game. """ numPlayers = 0 # TODO: Use the 'do-while' pattern to take in input for how many players will play the game # NOTE: The only valid inputs should be '1' and '2' # If the user chooses '1,' then they play against the computer # If the user chooses '2,' then they play against another human return numPlayers def wait(): """ This function has the computer 'wait' until the [Enter] key is pressed. This allows for better 'readability' in the final output. """ input("Press [Enter] To Continue...") print() def printBanner(): """ Prints the 'banner' between each round of the game so that the output text does not get too 'messy.' """ print("#######################################################################") print() print("~~ Starting New Round ~~") print() def printPoints(playerNum, points): """ This function prints the number of points a certain player currently has. :param Integer playerNum: The player whose points are being displayed. :param Integer points: The number of points to be displayed """ print("* Player {0} Has {1} Points!".format(playerNum, points)) print() def wagerPointsHuman(playerNum, points): """ This function uses the 'do-while' pattern to take in input for the number of points to be wagered. It checks to make sure that the player has entered a valid amount of points. Meaning, the player cannot wager more points than they have, nor zero points, nor a negative number of points. The function then returns the wager. As there could potentially be two human players, this function requires the playerNum to know which player to include in any printouts. :param Integer playerNum: The player to include in any printouts. Can be 1 or 2. :param Integer points: The number of points the specified player currently has. :return Integer: The number of points to be wagered. """ wager = 0 # TODO: Use the 'do-while' pattern to take in input for how many points a human would like to wager # NOTE: The player cannot wager more points than they have, nor can they wager zero points, nor can they wager a negative number of points return wager def wagerPointsAI(playerNum, points): """ This function should choose a random number between 1 and the number of points the AI has (inclusive). For example, if the computer has 5 points, it can wager either 1, 2, 3, 4, or 5. This function should include a printout similar to what is printed when the human wagers points. :param Integer playerNum: The player to include in any printouts. While this will usually be 2, including this value allows for a future version of the game with 2 computer players. :param Integer points: The number of points the specified player currently has. :return Integer: The number of points to be wagered. """ wager = 0 # TODO: Generate a random integer from 1 to (points + 1), non-inclusive, which will be the number of points for the computer to wager # NOTE: Try to have the AI version match the look of the output of the human version above return wager def generateTargetValue(numSpinners, spinnerLow, spinnerHigh): """ This function generates the 'target value' that the players try to match. This number is generated by summing together random values between 'spinnerLow' and 'spinnerHigh' (inclusive), produced by a random number of spinners, between 1 and numSpinners (inclusive). For example, if there are 3 spinners, and a spinner can have values between 1 - 3, then the target value would be the summation of 1-3 random values between 1 and 3 (inclusive). :param Integer numSpinners: The number of spinners used in the game. :param Integer spinnerLow: The smallest value a spinner can generate. :param Integer spinnerHigh: The highest value a spinner can generate. :return Integer: The target value generated by summing together 'numSpinners' number of random numbers between 'spinnerLow' and 'spinnerHigh' (inclusive). """ target = 0 # TODO: Randomly generate a 'target value' for the players to try to match # This should be done by first randomly choosing a number of spinners from 1 to (numSpinners + 1), non-inclusive # This number of spinners should each generate a random value from spinnerLow to (spinnerHigh + 1), non-inclusive # These values should be summed together to be the 'target value' return target def getSpinnerChoiceHuman(playerNum, target, numSpinners, spinnerLow, spinnerHigh): """ This function gets the number of spinners that the human wants to spin. It should print out the 'target value' that the player is trying to match, as well as the values that a spinner can produce (ex: 1 - 3), and the number of spinners that can be spun. The player cannot pick more spinners than are in the game, nor can the pick zero spinners, nor can they pick a negative number of spinners. :param Integer playerNum: The player to include in any printouts. Can be 1 or 2. :param Integer target: The 'target value' the player is trying to match. :param Integer numSpinners: The total number of spinners in the game. :param Integer spinnerLow: The smallest value a spinner can generate. :param Integer spinnerHigh: The highest value a spinner can generate. :return Integer: The number of spinners the player chooses to spin. """ spinnerChoice = 0 # TODO: Use the 'do-while' pattern to take in input from the user for how many spinners they would like to spin # Ex: if numSpinners is 3, then the user can pick between 1-3 # NOTE: Use the spinnerLow and spinnerHigh values to output data about what the player could spin return spinnerChoice def getSpinnerChoiceAI(playerNum, target, numSpinners, spinnerLow, spinnerHigh): """ This function gets the number of spinners that the computer wants to spin. This number should be a randomly generated value between 1 and numSpinners (inclusive). It should print out text similar to what the 'getSpinnerChoiceHuman()' function produces. :param Integer playerNum: The player to include in any printouts. While this will usually be 2, including this value allows for a future version of the game with 2 computer players. :param Integer target: The 'target value' the computer is trying to match. The computer does not take this value into account when choosing the number of spinners - it should be used for printouts, however. :param Integer numSpinners: The total number of spinners in the game. :param Integer spinnerLow: The smallest value a spinner can generate. :param Integer spinnerHigh: The highest value a spinner can generate. :return Integer: The number of spinners the computer chooses to spin. """ spinnerChoice = 0 # TODO: Generate a random integer from 1 to (numSpinners + 1), non-inclusive, for the computer to know how many spinners to spin # NOTE: Try to have the AI version match the look of the output of the human version above return spinnerChoice def spinSpinners(playerNum, spinnerChoice, target, spinnerLow, spinnerHigh): """ This function can be used for either human or computer players, and it calculates the summed values of the number of spinner spins. For example, if the player chooses to spin 3 spinners, and these spinners can have values between 1 and 3, the player could spin values of 2, 3, and 1 for a total of 6. This is the value the function would return. This function should print out the results of each spin as each spin is spun. The function should then print the sum of all the spins and the target value once all the spins are complete. Please note - the winner of the round is *not* calculated here - only the spinner totals. :param Integer playerNum: The player to include in any printouts. Can be 1 or 2. :param Integer spinnerChoice: The number of spinners the player wishes to spin. :param Integer target: Use this value in the printout so the user can compare what they spun compared to the target. :param Integer spinnerLow: The smallest value a spinner can generate. :param Integer spinnerHigh: The highest value a spinner can generate. :return Integer: The sum of all the spinner spins. """ spinVal = 0 # Assign the output of a random number between spinnerLow and spinnerHigh (inclusive) to this variable. spinTotal = 0 # Sum the spinVal values together with this variable. # TODO: Calculate the total generated by a given number of spinners (spinnerChoice) # Ex: if spinnerChoice is 3, and you spin 2, 3, and 1, then spinTotal would be 6 return spinTotal def main(): """ This is the main function that executes when the game is started from the terminal. It contains all of the logic/ states necessary to play the game. """ # main script running control variable running = True # gameplay variables SPINNER_LOW = 1 SPINNER_HIGH = 3 NUM_SPINNERS = 3 INITIAL_POINTS = 10 player1Points = INITIAL_POINTS player2Points = INITIAL_POINTS # print the title/ author information printTitleMaterial() # play the game while running: choice = initialChoice() if choice == "p": numPlayers = chooseNumPlayers() # main game loop while True: # round setup printBanner() # TODO: Complete the logic of the game (4 pts.) # NOTE: You can do this any way you like - in fact, you can even discard this file entirely and make your own version! # The comments below provide a general outline of the overall 'algorithm' of the game # Each comment could have multiple lines of code associated with it # Your job is to iteratively build up a game that works - it doesn't really matter how you code it # Find a target value. # Player 1 wager. (Player 1 will always be human.) # Player 2 wager. (Player 2 can be either human or AI - you must account for both.) # Player 1 spin - get the total of all the spinners. (Player 1 will always be human.) # Player 2 spin - get the total of all the spinners. (Player 2 can be either human or AI - you must account for both.) # Calculate Winner of the round. (If Player1 is closer, Player 1 wins. Else if Player 2 is closer Player 2 wins. If they are equal it is a draw.) # Print the points for both players. # Check of the game is over - if it is, print a 'game over' message, reset the points to default values, # and break out of the gameplay loop. Otherwise, print that it is the end of the round. elif choice == "i": # TODO: Print out the instructions for the game (see the rubric for details) (1 pt.) pass # Remove this 'pass' statement when other code has been added. elif choice == "q": # TODO: Set 'running' to False, and print out a 'goodbye' message to the player (1 pt.) pass # Remove this 'pass' statement when other code has been added. else: print("ERROR: Variable 'choice' should have been 'p', 'i', or 'q', but instead was:", choice) quit() if __name__ == "__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

What are the essential differences between victim and annex caches?

Answered: 1 week ago