All pastes #1439339 Raw Edit

Number Guessing Game

public text v1 · immutable
#1439339 ·published 2009-05-29 10:42 UTC
rendered paste body


print "\nWelcome! I see you came to play the Number Guessing Game. There are a\n\
couple of things I must ask you before we get started."

name = raw_input("\nFirst off, what's your name? I like to know my opponents. ")
print "\nNice to meet you, %s. Now my next important question..." % (name)

instructions = \
"\nNo problem. It's very simple, really. I am going to ask you to choose two\n\
integers, which will be the lower bound and upper bound of possible values. \n\
You will then think of an integer in the range you defined, including the   \n\
boundary points. My job is to see how quickly I can guess your number. Each \n\
time I guess a number, you tell me if your number is equal to, greater than,\n\
or less than my guess. \n\nAll right, then. Let's get started.\n"

while True: # Offer instructions for those who haven't played before
    response = raw_input("\nDo you know how to play? (yes, no) ")
    if response == 'yes':
        print "\nAll right, then. Let's get started.\n"
        break
    elif response == 'no':
        print instructions
        break
    else:
        print "\nPlease answer the question properly, %s." % (name)
        # Continue to prompt the user until a proper answer is provided

while True: # Ensure the boundary numbers meet the required conditions
    errors = 0
    [lower_bound, upper_bound] = input("Enter the lower and upper bounds, \
separated by a comma: ")
    if lower_bound != int(lower_bound) or upper_bound != int(upper_bound):
        print "Both numbers must be integers."
        errors += 1
    if (lower_bound or upper_bound) < -2147483648 \
       or (lower_bound or upper_bound) > 2147483648:
        print "The values can't be less than -2147483648 or exceed 2147483647."
        errors += 1
    if lower_bound > upper_bound:
        print "The lower bound cannot be greater than the upper bound."
        errors += 1
    if errors != 0: # Force the user to enter new values until no errors exist
        print name + "\n... Please select two appropriate integers.\n"
    else:
        break # End the infinite loop when all conditions are met


lower = lower_bound  # lower_bound and upper_bound represent the orginal bounds
upper = upper_bound  # whereas lower and upper will change as the function runs

import math
max_guesses = int(math.ceil(math.log((upper-lower+1), 2)))
# Maximum required guesses is proportional to the log of amounf of numbers

ok = raw_input("\nPick a number in that range. Tell me when you're ready. ")
# Input any text to continue

print "\nAll right, %s I bet I can guess your number in no more\n\
than %d tries. Here we go.\n" % (name, max_guesses)
# Awfully smug for a guy who has no one on his side but mathematics

guesses = []  # create a list to store the guessed values for later

while True:  # another infinite loop...
    middle = (lower + upper) / 2
    # The most efficient value to guess, as it eliminates half of the values

    if middle in guesses:
    # If the next number to be guessed has already been guessed, it means the
    # user lied to the computer somewhere along the way.  This explains how
    # angry he gets.
        if answer == 'higher':
            print "\n%s, are you seriously trying to tell me that your number \n\
is higher than %d after you just told me it was lower than %d? Well if you  \n\
are going to lie to me like that, I'm never playing with you again. I have  \n\
plenty of people who love to play this game with me, and they don't feel the\n\
need to cheat, so forget you.\n" % (name, middle, middle+1)
            what = raw_input("And you know what else, %s? " % (name))
            print '\n'
            while True:
                print "I HATE YOU!"
        if answer == 'lower':
            print "\n%s, are you seriously trying to tell me that your number \n\
is lower than %d after you just told me it was higher than %d? Well if you  \n\
are going to lie to me like that, I'm never playing with you again. I have  \n\
plenty of people who love to play this game with me, and they don't feel the\n\
need to cheat, so forget you.\n" % (name, middle, middle-1)
            what = raw_input("And you know what else, %s? " % (name))
            print '\n'
            while True:
                print "I HATE YOU!"

    answer = raw_input("\nIs your number %d? (yes, higher, lower) " % (middle))
    guesses.append(middle)
    # Reduce the possible values by half each time
    if answer == 'higher':
        lower = middle + 1
    elif answer == 'lower':
        upper = middle - 1
    elif answer == 'yes':
        break  # End the infinite loop once the number is guessed.
    else:
        guesses = guesses[:len(guesses)-1]
        print "\nI need you to give me a real answer here, %s." % (name)
        # The computer won't continue until it receives a proper answer.

print "\nWell, %s, I correctly guessed your number was %d, and I did it in\n\//
only %d tries. Good game. Come back any time." % (name, middle, len(guesses))
number = middle
t = raw_input('\nType "table" to view the data table for this game. ')
# Produce a table cataloguing the details of the game just finished
if t == 'table':
    print '\n'
    print ' '*4 + '='*46 + ' '*4
    print ' '*5 + 'Number Guessing Game' + ' '*4 + 'Player:' + \
          ' '*(13-len(str(name))) + str(name) + ' '*5
    print ' '*4 + '-'*46 + ' '*4
    print ' '*5 + 'Lower:' + ' '*(14-len(str(lower_bound))) + str(lower_bound)\
          + ' '*4 + 'Upper:' +' '*(14-len(str(upper_bound))) + \
          str(upper_bound) + ' '*5
    print ' '*4 + '-'*46 + ' '*4
    print ' '*5 + 'Number:' + ' '*(13-len(str(number))) + str(number) + ' '*4 \
          + 'Guesses:' + ' '*(12-len(str(len(guesses)))) + str(len(guesses)) +\
          ' '*5
    print ' '*4 + '='*46 + ' '*4
    print ' '*4 + '+' + '-'*6 + '+' + '-'*13 + '+' + '-'*12 + '+' + '-'*10 + \
          '+' + ' '*4
    print ' '*4 + '|' + ' ' + 'TURN' + ' ' + '|' + ' '*4 + 'GUESS' + ' '*4 + \
          '|' + ' ' + 'DIFFERENCE' + ' ' + '|' + ' '*2 + 'RESULT' + ' '*2 +  \
          '|' + ' '*4
    print ' '*4 + '+' + '-'*6 + '+' + '-'*13 + '+' + '-'*12 + '+' + '-'*10 + \
          '+' + ' '*4

    differences = [None]*len(guesses)
    results = [None]*len(guesses)
    for i in range(len(guesses)):
        differences[i] = abs(guesses[i]-number)
        if number > guesses[i]:
            results[i] = 'higher'
        elif number < guesses[i]:
            results[i] = 'lower'
        else:
            results[i] = 'yes'
            
    for i in range(len(guesses)):
        print ' '*4 + '|' + ' '*(4-len(str(i+1))) + str(i+1) + ' '*2 + '|' + \
              ' '*(12-len(str(guesses[i]))) + str(guesses[i]) + ' ' + '|' + \
              ' '*(11-len(str(differences[i]))) + str(differences[i]) + ' ' +\
              '|' + ' '*2 + results[i] + ' '*(8-len(results[i])) + '|' + ' '*4

    print ' '*4 + '+' + '-'*6 + '+' + '-'*13 + '+' + '-'*12 + '+' + '-'*10 +\
          '+' + ' '*4
    print '\n'