Rock-paper-scissors-lizard-Spock

Rock-paper-scissors-lizard-Spock is a five-gesture expansion of the classic selection method game rock-paper-scissors. It operates on the same basic principle, but includes two additional weapons: the lizard (formed by the hand as a sock-puppet-like mouth) and Spock (formed by the Star TrekVulcan salute). This reduces the chances of a round ending in a tie. The game was invented by Sam Kass and Karen Bryla.


The game was mentioned in three episodes of The Big Bang Theory. According to an interview with Kass, the series producers did not ask for permission to use the game, but he was later referenced in an episode in the fifth season.
Rules

The rules of Rock-paper-scissors-lizard-Spock are:

  • Scissors cuts paper
  • Paper covers rock
  • Rock crushes lizard
  • Lizard poisons Spock
  • Spock smashes scissors
  • Scissors decapitates lizard
  • Lizard eats paper
  • Paper disproves Spock
  • Spock vaporizes rock
  • Rock crushes scissors

There are ten possible pairings of the five gestures; each gesture beats two of the other gestures and is beaten by the remaining two.
My implementation of this program in different languages.


Python

# Rock Paper Scissors Lizard Spock

# to import random function for using random.randrange
import random

# function to convert number to name
def number_to_name(number):
    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"
    else:
        return "Error"

# function to convert name to number
def name_to_number(name):
    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
    else:
        print name + "is not a character in RPSLS"


# function which selects the winner
def rpsls(guess): 
    
    # convert name to player_number using name_to_number
    player_number = name_to_number(guess)
    
    # compute random guess for comp_number using random.randrange()
    comp_number = random.randrange(0,5)

    # compute difference of player_number and comp_number modulo five
    winner = (comp_number - player_number) % 5

    # use if/elif/else to determine winner
    if winner < 3:
        player_win = False
    else:
        player_win = True
    
    # convert comp_number to name using number_to_name
    comp_name = number_to_name(comp_number)
    
    # print results
    print "Player chooses " + guess
    print "Computer chooses " + comp_name
    if player_win:
        print "Player wins!\n"
    elif comp_number == player_number:
        print "Player and computer tie!\n"
    else:
        print "Computer wins!\n"

    
player_guess = raw_input('Enter your choice.. Options: rock, Spock, paper, lizard, scissors')

rpsls(str(player_guess))

Java

package rpsls;

import java.util.Random;

/**
 *
 * @author vinoth
 */
public class Rpsls {

    static String number_to_name(int n) {
        
        switch(n) {
            case 0:
                return "rock";
            case 1:
                return "Spock";
            case 2:
                return "paper";
            case 3:
                return "lizard";
            case 4:
                return "scissors";
            default:
                return "Not defined";
        }
    }
    
    static int name_to_number(String name) {
        
        switch(name) {
            case "rock":
                return 0;
            case "Spock":
                return 1;
            case "paper":
                return 2;
            case "lizard":
                return 3;
            case "scissors":
                return 4;
            default:
                return 5;
        }
    }
    
    static void winner(String player_guess){
        
        Random rand = new Random();
        
        int player_number = name_to_number(player_guess);

        int comp_number = rand.nextInt(5);

        String comp_name = number_to_name(comp_number);

        System.out.println("Player chooses " + player_guess);
        System.out.println("Computer chooses " + comp_name);

        if (comp_number == player_number) {
            System.out.println("Player and computer tie!\n");
        } else if ((comp_number - player_number) % 5 < 3) {
            System.out.println("Computer wins!\n");
        } else {
            System.out.println("Player wins!\n");
        }
    }
    
    public static void main(String[] args) {
        
        // Test samples
        winner("rock");
        winner("Spock");
        winner("paper");
        winner("lizard");
        winner("scissors");
    }
}

C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char * number_to_name(int number)
{
  switch(number)
  {
    case 0: return "rock";

    case 1: return "Spock";

    case 2: return "paper";

    case 3: return "lizard";

    case 4: return "scissors";

    default: return "Error";

  }
}

int name_to_number(char *name)
{
  if(strcmp(name,"rock") == 0)
  return 0;
  if(strcmp(name,"Spock") == 0)
	return 1;
  if(strcmp(name,"paper") == 0)
	return 2;
  if(strcmp(name,"lizard") == 0)
	return 3;
  if(strcmp(name,"scissors") == 0)
	return 4;
  else
  {
	printf("Error");
	return 5;
  }

}

void rpsls(char *guess)
{
 int player_number = name_to_number(guess);

 int comp_number = rand() % 5;

 printf("\nPlayer chooses : %s\n",number_to_name(player_number));
 printf("Computer chooses : %s\n",number_to_name(comp_number));

 if(player_number == 5)
	printf("Your choice is wrong.. Try again");
 else if(comp_number == player_number)
	printf("Computer and player ties\n");
 else if ((comp_number - player_number) % 5 < 3)
	printf("Computer wins\n");
 else
	printf("Player wins\n");

}

void main()
{
 char *player_choice;
 clrscr();

 printf("\nEnter your Choice : rock,Spock,paper,lizard,scissors CASE SENSITIVE\n\n");
 scanf("%s",player_choice);
 rpsls(player_choice);

 /* //test
 rpsls("rock");
 rpsls("Spock");
 rpsls("paper");
 rpsls("lizard");
 rpsls("scissors");
  */
 getch();
}

Ruby

def number_to_name(number)
  case number
    when 0
      return "rock"
    when 1
      return "Spock"
    when 2
      return "paper"
    when 3
      return "lizard"
    when 4
      return "scissors"
    else
      return "Not defined"
  end
end

def name_to_number(name)
  case name
    when "rock"
      return 0
    when "Spock"
      return 1
    when "paper"
      return 2
    when "lizard"
      return 3
    when "scissors"
      return 4
    else 
      return "Not defined"
  end
end

def rpsls(guess)
  
  player_number = name_to_number(guess)
  computer_number = rand(5)
  
  puts "Player chooses #{number_to_name(player_number)}"
  puts "Computer chooses #{number_to_name(computer_number)}"
  
  computer_number == player_number ? ( puts "Player and computer tie!" ) : (((computer_number - player_number) % 5) < 3? (puts "Computer wins") : (puts "Player wins" ))
  puts "\n"
  
end

# test your code
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
Show Comments