enzo
enzo

Reputation: 11496

Python - Simplification of "rock paper scissors" logic conditions

I was solving a question and it said:

In Big Bang Theory, Sheldon and Raj created a new game: "rock-paper-scissors-lizard-Spock".

The rules of the game 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.

In the case of Sheldon's victory, he would've said: "Bazinga!"; if Raj had won, Sheldon would declare: "Raj cheated"; in ties, he would request a new game: "Again!". Given the options chosen by both, make a program that prints Sheldon reaction to the outcome.

The input consists of a series of test cases. The first line contains a positive integer T (T ≤ 100), which represents the number of test cases. Each test case is represented by a line of the input, containing the choices of Sheldon and Raj, respectively, separated by a space.

My code for this question is

T = int(input())

for i in range(T):
    Sheldon, Raj = input().split(' ')

    if(Sheldon == "scissors" and (Raj == "paper" or Raj == "lizard")):
        Win = True
    elif(Sheldon == "lizard" and (Raj == "paper" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "Spock" and (Raj == "rock" or Raj == "scissors")):
        Win = True
    elif(Sheldon == "paper" and (Raj == "rock" or Raj == "Spock")):
        Win = True
    elif(Sheldon == "rock" and (Raj == "scissors" or Raj == "lizard")):
        Win = True
    elif(Raj == "scissors" and (Sheldon == "paper" or Sheldon == "lizard")):
        Lose = True
    elif(Raj == "lizard" and (Sheldon == "paper" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "Spock" and (Sheldon == "rock" or Sheldon == "scissors")):
        Lose = True
    elif(Raj == "paper" and (Sheldon == "rock" or Sheldon == "Spock")):
        Lose = True
    elif(Raj == "rock" and (Sheldon == "scissors" or Sheldon == "lizard")):
        Lose = True
    elif(Sheldon == Raj):
        Tie = True

    if(Win == True):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Lose == True):
        print("Case #{0}: Raj cheated!".format(i+1))
    elif(Tie == True):
        print("Case #{0}: Again!".format(i+1))

    Win = Lose = Tie = False

but I thought it got too long. Is there any way to reduce it?

Upvotes: 1

Views: 1254

Answers (3)

Adriano Segundo
Adriano Segundo

Reputation: 1

Try this, using dictionaries

T = int(input())

for i in range(T):

    rules=  {
        "rock":     {"rock":0, "paper":-1,"scissors":1,"lizard":1,"Spock":-1},
        "paper":    {"rock":1, "paper":0,"scissors":-1,"lizard":-1,"Spock":1},
        "scissors": {"rock":-1, "paper":1,"scissors":0,"lizard":1,"Spock":-1},
        "lizard":   {"rock":1, "paper":-1,"scissors":1,"lizard":0,"Spock":-1},
        "Spock":    {"rock":1, "paper":-1,"scissors":1,"lizard":-1,"Spock":0}
        }
    Sheldon, Raj = input().split(' ') 

    Result = rules[Sheldon][Raj]
    if(Result == 1):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Result == -1):
        print("Case #{0}: Raj cheated!".format(i+1))
    else:
        print("Case #{0}: Again!".format(i+1))

Upvotes: 0

develtype
develtype

Reputation: 76

T = int(input())

for i in range(T):
    Sheldon, Raj = input().split(' ')

    if(Sheldon == Raj):
        Tie = True
    elif((Sheldon == "scissors" and (Raj in ["paper","lizard"])) or
         (Sheldon == "lizard" and (Raj in ["paper","Spock"])) or
         (Sheldon == "Spock" and (Raj in ["rock","scissors"])) or
         (Sheldon == "paper" and (Raj in ["rock","Spock"])) or
         (Sheldon == "rock" and (Raj in ["scissors","lizard"]))
        ):
        Win = True
    else:
        Lose = True

    if(Win == True):
        print("Case #{0}: Bazinga!".format(i+1))
    elif(Lose == True):
        print("Case #{0}: Raj cheated!".format(i+1))
    elif(Tie == True):
        print("Case #{0}: Again!".format(i+1))

    Win = Lose = Tie = False

Upvotes: 0

nosklo
nosklo

Reputation: 222862

First of all, congratulations on attempting to write this! Your logic is pretty good for a first attempt.

The next step is making a data structure you can query in the same way, for the rules. A good fit would be a dictionary:

options = {
 'scissors': ('paper', 'lizard'),
 'paper': ('rock', 'spock'),
 'rock': ('lizard', 'scissors'),
 'lizard': ('spock', 'paper'),
 'spock': ('scissors', 'rock'),
}

Then you can just query it instead of repeating lots of ifs:

if raj == sheldon:
   print("Case #{0}: Again!".format(i+1))
elif raj in options[sheldon]:
   print("Case #{0}: Bazinga!".format(i+1))
else: 
   print("Case #{0}: Raj cheated!".format(i+1))

Upvotes: 7

Related Questions