Python Game – “Rock paper scissors lizard Spock”

Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously “throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

Rock smashes scissors Scissors cuts paper Paper covers rock

rockpaperscissorslizardspock_by_iamthemiggy

Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details). Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties less likely.

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.

I have written a Python function rpsls(name) that takes as input the string name, which is one of “rock”, “paper”, “scissors”, “lizard”, or “Spock”. The function then simulates playing a round of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule.

Please feel free to write a simple GUI version of this fun game.

Please note that the code indentation has been lost in the formatting of this blog. For a formatted version of this code please visit my Github page.

</pre>
<code>import random</code>

# 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

# helper functions

def name_to_number(name):
if name == "rock":
number = 0
elif name == "Spock":
number = 1
elif name == "paper":
number = 2
elif name == "lizard":
number = 3
elif name == "scissors":
number = 4
else:
print "you did not make a correct slection"
return number

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:
print "you have not selected the correct number"
return name

def rpsls(player_choice):
print "____________"
print "your choice is ", player_choice
player_number = name_to_number(player_choice)

# computing random guess for comp_number using random.randrange()
comp_number = random.randrange(0,4)

# converting comp_number to comp_choice using the function number_to_name()
comp_choice = number_to_name(comp_number)

# next I am printing out the message for computer's choice
print "The computer choice was ", comp_choice

# computing difference of comp_number and player_number modulo five
result = (comp_number - player_number) % 5

# using if/elif/else to determine winner & printing winning message
if result <= 2: print "The player has won this roud" elif result >2:
print "The computer has won this round"

# testing your code
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")

Leave a comment