Rock-paper-scissors-lizard-Spock in Python

So I recently enrolled for a course from Coursera.org on Introduction to Interactive Python, After I received a certificate on Introduction to Python programming instructed by Prof. Charles Severance. https://www.coursera.org/course/pythonlearn
My first mini-project is a simple Rock-paper-scissors-lizard-Spock game.


# Mini-project #1 - "Rock-paper-scissors-lizard-Spock game"
# Created by Mpho "!nerd" Mphego - Marion Island SANSA Engineer 2014-2015
#
# 'Introduction to Interactive Programming in Python' Course
# RICE University - https://class.coursera.org/interactivepython-005
# by Joe Warren, John Greiner, Stephen Wong, Scott Rixner
#
# Rock-paper-scissors is a surprisingly popular game that many people play seriously 
# Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows -
# five choices. Each choice wins against two other choices, loses against two other choices -
# and ties against itself. Much of RPSLS's popularity is that it has been featured in 3 episodes - 
# of the TV series "The Big Bang Theory". 
# The Wikipedia entry for RPSLS gives the complete description of the details of the game.
# http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock

# http://www.codeskulptor.org/#user38_e9yDvuII0ZGsvuc.py
#!/bin/python2
import random
#import math
   
# Function converting string to number
def name_to_number(name):
    if name == "rock":
       name = 0
    elif name == "Spock":
        name = 1
    elif name == "paper":
        name = 2
    elif name == "lizard":
        name = 3
#    elif name == "scissors":
#        name = 4
    else:
        name = 4
        #print "Error"
    return name  
 
# Function converting number to string
def number_to_name(number):
    if number == 0:
       number = "rock"
    elif number == 1:
        number = "Spock"
    elif number == 2:
        number = "paper"
    elif number == 3:
        number = "lizard"
#    elif number == 4:
#        number = "scissors"
    else:
        number = "scissors"
#        print "Error"
    return number 

# function to determine the winner
def rpsls(player_choice): 
    nothing = None 
    
    print "\n"
    # print out the message for the player's choice
    print "Player chooses", player_choice

    # convert the player's choice to player_number using the function name_to_number()
    player_number = name_to_number(player_choice)
    #print player_number #Debugging
    
    # compute random guess for comp_number using random.randrange()
    comp_number = random.randrange(0, 5)
    #print comp_number #Debugging
    
    # convert comp_number to comp_choice using the function number_to_name()
    comp_choice = number_to_name(comp_number)
    #print comp_choice #Debugging
    
    # print out the message for computer's choice
    print "Computer chooses", comp_choice
    
    # compute difference of comp_number and player_number modulo five
    
    difference = ( comp_number - player_number) % 5
    #print difference, comp_number, player_number #Debugging
    
    # use if/elif/else to determine winner, print winner message
    if ( difference == 1 or difference == 2 ):
        print "Computer Wins!"
    elif ( difference == 3 or difference == 4 ):
        print "Player Wins!"
    else :
        print "Computer and Player Ties"
    return nothing

# Play five games with random guesses from computer.
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")




Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s