## Free Game  ## Friday, May 10, 2013

### Rock Paper Scissors Lizard Spock Algorithm

I believe most of you are familiar with the variation of the classic game Rock Paper Scissors.  The variation of the classic game was invented by Sam Kass with Karen Bryla, as "Rock Paper Scissors Spock Lizard". This variation was made really popular by the TV Show Big Bang Theory. It was mentioned in three different episodes of the series.
In this post I will present to you a simple algorithm to create an application based on that game that requires minimal if and else ifs. In the case of the original game it was pretty simple to create an application because the combinations are very little. Even though programming like that (with many ifs) makes the application slower (the slowest part in source code is a branch) in a game of rock paper scissors it will not be too slow. But in a game of Rock Paper Scissors Lizard Spock it will much slower, and much more complicated AND boring to write. So I will present you with a simpler way to write an application. But first the rules of the game

And now for the code. Since every hand gesture defeats two others and loses to two others (only possible outcome for each combination) for each hand gesture four if checks would be needed summing up the total to 20 if checks to complete the game. A way to simplify this is to put the gestures in a linear position in which every gesture loses to the following two and wins the preceding two.

For example:

0 - rock
1 - Spock
2 - paper
3 - lizard
4 - scissors

To help you understand better in the classic game of Rock Paper Scissors it would be

0 - rock
1 - paper
2 - scissors

Where each gesture loses to the next one and defeats the preceding one.

The numbers are there to simplify source code writing (it's easier to manipulate numbers than manipulating strings)

So I created a python script with which the user gives his choice in a string the script transforms it to a number, creates a random number for the computer, and gives out the winning result.

```import random
def number_to_name(number):
# convert number to a name using if/elif/else
if number==0:
return "rock"
elif number==1:
return "Spock"
elif number==2:
return 'paper'
elif number ==3:
return "lizard"
elif number==4:
return "scissors"

def name_to_number(name):
# convert name to number using if/elif/else
if name=='rock':
return 0
elif name=='Spock':
return 1
elif name=='paper':
return 2
elif name=='lizard':
return 3
elif name=='scissors':
return 4

def rpsls(name):

# convert name to player_number using name_to_number
player_number=name_to_number(name)
# compute random guess for comp_number using random.randrange()
comp_number=random.randrange(5)
# compute difference of player_number and comp_number modulo five
difference= player_number-comp_number
modular_difference=difference%5
# use if/elif/else to determine winner
if modular_difference==1 or modular_difference==2:
result= 'player wins, computer loses'

elif modular_difference==4 or modular_difference==3:
result= 'computer wins, player loses'
else:
result= 'player and user choose the same, the game is tied'
# print results
print "player chooses: ", name
# convert comp_number to name using number_to_name
print "computer chooses: ", number_to_name(comp_number)
print result
print

rpsls("rock")

rpsls("Spock")

rpsls("paper")

rpsls("lizard")

rpsls("scissors")

```
Now a lot smarter suggestion (to simplify things even more)  would be to create a table that contains those strings (or since python is really helpful, create a huge string and cutting slices of that string) and use the number to get the position of the strings table and return that position. But the whole idea was centralized on simplifying the rock paper scissors lizard spock algorithm. Btw if you don't like python view the above code as pseudo-code. That's what python is anyhow.

If you need some explanation on how I am using modular arithmetic to determine the winner let me know in the comments section