Created
August 2, 2015 00:46
-
-
Save gnagel/ba2b987792b48ae45323 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # CodeSkuptor URL: | |
| # http://www.codeskulptor.org/#user40_qps4KtZAHn_0.py | |
| # | |
| # guess_the_number.py | |
| # github.com/gnagel | |
| # 2015/08/01 | |
| # | |
| # | |
| # We have provided a basic template for this mini-project here. | |
| # Our suggested development strategy for the basic version of “Guess the number” is below. | |
| # Remember to run your program after each step to ensure that you implemented that step correctly. | |
| # | |
| # 1. Add code to the program template that creates a frame with an input field whose handler has the name input_guess. | |
| # You will use this input field to enter guesses. | |
| # | |
| # 2. Add code to the event handler input_guess(guess) that takes the input string guess, converts it to an integer, and prints out a message of the form "Guess was 37" (or whatever the guess actually was). | |
| # Hint: We have shown you how to convert strings to numbers in the lectures. | |
| # | |
| # 3. Add code to the function new_game that initializes a global variable secret_number to be a random number in the range [0, 100). | |
| # (Note that this range should not include 100 as a possible secret number.) Remember to include a global statement. | |
| # Hint: Look at the functions in the random module to figure out how to easily select such a random number. | |
| # Observe that the call to new_game() at the bottom of the template ensures that secret_number is always initialized when the program starts running. | |
| # | |
| # 4. Add code to input_guess that compares the entered number to secret_number and prints out an appropriate message such as "Higher", "Lower", or "Correct". | |
| # | |
| # 5. Test your code by playing multiple games of “Guess the number” with a fixed range. | |
| # At this point, you will need to re-run your program between each game (using the CodeSkulptor “Run” button). | |
| # You are also welcome to use this testing template http://www.codeskulptor.org/#examples-gtn_testing_template.py. | |
| # The bottom of the template contains a sequence of calls to input_guess() for three games of "Guess the number". | |
| # Uncomment each sequence of calls and check whether the output in the console matches that provided in the comments below. | |
| # Note that your output doesn't have to be identical, just of a similar form. | |
| # | |
| # From this minimal working version of “Guess the number”, the rest of this project consists of adding extra functionality to your project. | |
| # There are two improvements that you will need to make to get full credit: | |
| # | |
| # 1. Using function(s) in the simplegui module, add buttons to restart the game so that you don't need to repeatedly click “Run” in CodeSkulptor to play multiple games. | |
| # You should add two buttons with the labels "Range is [0,100)" and "Range is [0,1000)" that allow the player to choose different ranges for the secret number. | |
| # Using either of these buttons should restart the game and print out an appropriate message. | |
| # They should work at any time during the game. | |
| # In our implementation, the event handler for each button set the desired range for the secret number (as a global variable) and then call new_game to reset the secret number in the desired range. | |
| # | |
| # As you play “Guess the number”, you might notice that a good strategy is to maintain an interval that consists of the highest guess that is “Lower” than the secret number and the lowest guess that is “Higher” than the secret number. | |
| # A good choice for the next guess is the number that is the average of these two numbers. | |
| # The answer for this new guess then allows you to figure a new interval that contains the secret number and that is half as large. | |
| # For example, if the secret number is in the range [0, 100), it is a good idea to guess 50. | |
| # If the answer is "Higher", the secret number must be in the range [51, 100). | |
| # It is then a good idea to guess 75 and so on. | |
| # This technique of successively narrowing the range corresponds to a well-known computer algorithm known as binary search. | |
| # | |
| # 2. Your final addition to “Guess the number” will be to restrict the player to a limited number of guesses. | |
| # After each guess, your program should include in its output the number of remaining guesses. | |
| # Once the player has used up those guesses, they lose, the game prints out an appropriate message, and a new game immediately starts. | |
| # | |
| # Since the strategy above for playing “Guess the number” approximately halves the range of possible secret numbers after each guess, any secret number in the range [low, high) can always be found in at most n guesses where n is the smallest integer such that 2 ** n >= high - low + 1. | |
| # For the range [0, 100), n is seven. | |
| # For the range [0, 1000), n is ten. | |
| # In our implementation, the function new_game() set the number of allowed guess to seven when the range is [0, 100) or to ten when the range is [0, 1000). | |
| # For more of a challenge, you may compute n from low and high using the functions math.log and math.ceil in the math module. | |
| # | |
| # When your program starts, the game should immediately begin in range [0, 100). | |
| # When the game ends (because the player either wins or runs out of guesses), a new game with the same range as the last one should immediately begin by calling new_game(). | |
| # Whenever the player clicks one of the range buttons, the current game should stop and a new game with the selected range should begin. | |
| # | |
| import math | |
| import random | |
| import simplegui | |
| # Max number value | |
| number_max = 100 | |
| number_counter = 0 | |
| secret_number = 0 | |
| # helper function to start and restart the game | |
| def new_game(): | |
| # Globals | |
| global number_max | |
| global number_counter | |
| global secret_number | |
| # Select a new number | |
| # Set the number of guesses to log2(number_max) | |
| secret_number = random.randrange(0, number_max) | |
| number_counter = int(math.ceil(math.log(number_max, 2))) | |
| print "New game. Range is from 0 to: ", number_max | |
| print "Number of remaining guesses is: ", number_counter, "\n" | |
| # Start the game | |
| new_game() | |
| # | |
| # Set the range to [0, 100), then start a new game | |
| # | |
| def range100(): | |
| global number_max | |
| number_max = 100 | |
| new_game() | |
| # | |
| # Set the range to [0, 1000), then start a new game | |
| # | |
| def range1000(): | |
| global number_max | |
| number_max = 1000 | |
| new_game() | |
| # | |
| # Handle a guess by the user | |
| # | |
| def input_guess(guess): | |
| # main game logic goes here | |
| global number_counter | |
| global secret_number | |
| global number_max | |
| guess = 0 | |
| try: | |
| guess = int(guess) | |
| except ValueError: | |
| print "You entered invalid characters:", guess, "\n", "The number your entered should just be number.\n" | |
| return | |
| if guess < 0 or guess >= number_max: | |
| print "You entered number is out of range!\n" | |
| return | |
| number_counter -= 1 | |
| print "Guess was", guess | |
| print "Number of remaining guesses is", number_counter | |
| # Was the guess correct? | |
| if secret_number == guess: | |
| print "Correct!\n" | |
| new_game() | |
| return | |
| # Are we out of turns? | |
| if number_counter <= 0: | |
| print "You ran out of guesses. The number was", secret_number, "\n" | |
| new_game() | |
| return | |
| # Print the high/low indicator | |
| if secret_number > guess: | |
| print "Higher!\n" | |
| else: | |
| print "Lower!\n" | |
| # | |
| # Setup the GUI | |
| # | |
| # | |
| frame = simplegui.create_frame("Guess the number", 200, 200) | |
| # | |
| frame.add_button("Range is [0, 100)", range100, 200) | |
| frame.add_button("Range is [0, 1000)", range1000, 200) | |
| # | |
| frame.add_input( "Enter a guess", input_guess, 200) | |
| # | |
| frame.start() | |
| # always remember to check your completed program against the grading rubric |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment