Sunday 4 May 2014

Guess the number game in PYTHON!!

Well, we love to guess and we feel happy if our guess is right.
This game is based on your guess. Computer randomly selects a number and you have to guess the number.
Simple!! Isn't it?? But you are give limited chances based on your range. So test your intuition with this game!!!
This game is written in python and output of the program will be as shown in figure:

The program is as follows:

import simplegui
import random
secret_number=0
secret_number1=0
secret_number2=0
remaining_100=0
remaining_1000=0
remaining=0
def new_game():
    global secret_number1,secret_number2,remaining_100,remaining_1000
    secret_number1=random.randrange(0,100)
    secret_number2=random.randrange(0,1000)
    remaining_100=7
    remaining_1000=10

def range100():
    
    global secret_number1,secret_number2,remaining_100,remaining,secret_number 
    new_game()
    secret_number=secret_number1
    remaining=remaining_100
    
def range1000():
   
    global secret_number1,secret_number2,remaining,remaining_1000,secret_number 
    new_game()
    secret_number=secret_number2
    remaining=remaining_1000
    
    
def input_guess(guess):
    global remaining,secret_number
    
    remaining=remaining - 1
    if(secret_number>int(guess)):
        print "Guess was: ",int(guess)
        print "No.of remaining choices: ",remaining
        print "Higher!!"
        
    elif(secret_number<int(guess)):
        print "Guess was: ",int(guess)
        print "No.of remaining choices: ",remaining
        print "Lower!!"
    else:
        print "Your guess was: ",int(guess)
        print "You win!!!"
    if(remaining==0):
        print "You lose!!"
        
        
frame=simplegui.create_frame("Guess the number!!!",300,300)
frame.add_input("Enter the guess",input_guess,150)
frame.add_button("Range of 0-100",range100,150)
frame.add_button("Range of 0-1000",range1000,150)
frame.start()

Saturday 3 May 2014

Rock-Paper-Scissor-Lizard-Spock game in PYTHON

The key idea of this game is as follows:
It's very simple:

  1. Scissors cuts paper
  2. Paper covers rock
  3. Rock crushes lizard
  4. Lizard poisons Spock
  5. Spock smashes scissors
  6. Scissors decapitates lizard
  7. Lizard eats paper
  8. Paper disproves Spock
  9. Spock vaporizes rock
  10. Rock crushes scissors
The program is as follows:


# Rock-paper-scissors-lizard-Spock template


# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors


import random
import math
def name_to_number(name):
    
    if(name=="rock"):
        num=0
    elif(name=="spock"):
        num=1
    elif(name=="paper"):
        num=2
    elif(name=="lizard"):
        num=3
    elif(name=="scissors"):
        num=4
    else:
        num=None
    return num



def number_to_name(number):
   
    if(number==0):
        name="rock"
    elif(number==1):
        name="spock"
    elif(number==2):
        name="paper"
    elif(number==3):
        name="lizard"
    elif(number==4):
        name="scissors"
    else:
        name=None
    return name

   
    

def rpsls(player_choice): 
    
    
    # print a blank line to separate consecutive games
    print
    # print out the message for the player's choice
    print "Player's choice: ", player_choice
    # convert the player's choice to player_number using the function name_to_number()
    player_number=name_to_number(player_choice)
    # compute random guess for comp_number using random.randrange()
    comp_number=random.randrange(0,5)
    # convert comp_number to comp_choice using the function number_to_name()
    comp_choice=number_to_name(comp_number)
    # print out the message for computer's choice
    print "Computer's choice: " , comp_choice
    # compute difference of comp_number and player_number modulo five
    
    result=(comp_number-player_number)%5
    if(result>=0):
        if(result>=3):
            print "Player wins"
        elif(result==0):
            print "There is a tie"
        else:
            print "Computer wins"
    else:
        if(result>-3):
            print "Computer wins"
        else:
            print "Player wins"
        
   
    
# test your code 
rpsls("rock")
rpsls("spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")