Dec 23, 2013

Guess the Number

Executable File/Downloadable 64bit

Source Code

#Author : Mazhar Hussain
#Centre of Excellence in Molecular Biology,
#University of the Punjab Lahore
#Dated : 01 - 11 - 2013
#Game : Guess the Number

import random 
"""Calls Module random which generates a random number in
given range
"""

import simpleguitk as simplegui
"""Calls Module simplegui which produces the input fields
and interactive graphic-user-interface
"""

# initialize global variables used in your code

computer_number = 0 # carries random number from random module
attempts_left = 0 # contains value of remaining number of attempts
default_attempts = 0 # it directly takes value from initial trigger
                    # and preserve value for next playing session
set_value_from = 0 # provides initial value to randrange function in random
set_value_to = 100 # provides final value to randrange function in random

# helper function to start and restart the game

"""can trigger new_game by taking values from the previous game
regarding number of attempts left but generates new value from
random module using random_generator function in game
"""
def new_game():
    global attempts_left
    random_generator(set_value_from , set_value_to)
    attempts_left = default_attempts
"""
random_generator() is function in the heart of game
it directly generates random number in given range
and then preserves the 
"""
    
def random_generator (set_value_from, set_value_to):
    global computer_number
    computer_number = random.randrange (set_value_from, set_value_to)
    return computer_number
"""
attempts_remaining() is helping function and it counts down
the attempts to zero and ultimately using conditionals
assissts in terminating game, if correct number was not guessed
"""
def attempts_remaining():
    global attempts_left
    attempts_left = attempts_left - 1
    return attempts_left
"""
Helper function, Which Prints Current State of Game in fanciful way.
It is called from range100() and range1000() event handlers
"""
def state_of_game(set_value_from, set_value_to, attempts_left):
    print "===================================\n",
    print "Guess the Number Between "+ str(set_value_from) + " to " + str(set_value_to) + "\n",
    print "and you have only "+str(attempts_left) +" attempts. :D"
    print "==================================="   

# define event handlers for control panel
"""
Handle of "Guess Number from 0-100" button. When button is pressed
it sets the variables so that "State of Guess the Number in 0 to 100"
becomes active. Declares global variables which are vital for not only
current game but also for subsequent game on same settings
"""

def range100():
    global set_value_from, set_value_to
    global attempts_left, default_attempts
    set_value_from = 0
    set_value_to = 100
    attempts_left = 7
    default_attempts = 7
    computer_number = random_generator(set_value_from , set_value_to)
    state_of_game(set_value_from, set_value_to, attempts_left)
   
    # button that changes range to range [0,100) and restart
"""
Handle of "Guess Number from 0-1000" button. Other details are same as
discussed for "Guess Number from 0-100) button.
"""

def range1000():
    global set_value_from, set_value_to
    global attempts_left, default_attempts
    set_value_from = 0
    set_value_to = 1000
    attempts_left = 10
    default_attempts = 10
    computer_number = random_generator(set_value_from , set_value_to)
    state_of_game(set_value_from, set_value_to, attempts_left)
    
"""
Core of game, which compares Computer's generated number and player's guess.
It suggests player to GUESS HIGHER or LOWER NUMBER. It counts on the number of attempts
left. It contains rules for declaring win or lose. It immeditely triggers the next
game as soon as current game is ended.
"""
    
    
def input_guess(player_number):
    if (attempts_remaining() == 0) and (computer_number == int(player_number)):
        print "-----------------------------------------------------------------------------"
        print "You Guessed the Number "+str(player_number)+" and Your all attempts are over,"
        print "----------------------But You Guessed the Correct Number---------------------"
        print "--------------------------Congratulations! You Won---------------------------"
        print "----------------Wishing You Awesome Performane in Next Game Too--------------"
        print "-----------------------------------------------------------------------------"
        print "----------------------------Triggered New Game------------------------------"
        new_game()
    elif (attempts_left == 0) and (computer_number != int(player_number)):
        print "------------------------------------------------------"
        print "You Guessed the Number "+str(player_number)+" and Your all attempts are over,"
        print "----------------The correct number was "+str(computer_number)+"-------------"
        print "------------Better Luck in the Next Game--------------"
        print "------------------------------------------------------"
        print "--------------------Triggered New Game---------------"
        new_game()
    else:
        if (computer_number == int(player_number)):
            print "------------------------------------------------------"
            print "You Guessed the Number "+str(player_number)+", which is Correct"
            print ">>>>>>>>>>>Congratualations You Did it!!!<<<<<<<<<<<<<<<"
            print "---------Wishing You Best of Luck For Next Game--------"
            print "------------------------------------------------------"
            print "------------------Triggered New Game------------------"
            new_game()
        elif (computer_number > int(player_number)):
            print"----------------------------------------------"
            print "You guessed the number "+str(player_number)+" and left "+str(attempts_left)+" attempts"
            print ">>>>>>>>>>>>>>>>Guess Higher<<<<<<<<<<<<<<<<"
        else:
            print"----------------------------------------------"
            print "You guessed the number "+str(player_number)+" and left "+str(attempts_left)+" attempts"
            print ">>>>>>>>>>>>>>>>Guess Lower<<<<<<<<<<<<<<<<"

# create frame

frame = simplegui.create_frame("Guess the Number", 200, 200)

# register event handlers for control elements
frame.add_button ("Guess Number from 0 - 100", range100)
frame.add_button ("Guess Number from 0 - 1000", range1000)
frame.add_input ("Put Your Guess Here (Integers Only)", input_guess , 200)

range100()
# call new_game and start frame

frame.start()

No comments:

Post a Comment