><>, Retina, Python 2: 144 127 123 bytes
1 byte saved thanks to @Loovjo by removing a space
4 bytes saved thanks to @mbomb007 by using input
instead of raw_input
#v"PAPER"v?%4-2{"SCISSORS"v?%2:i
#>ooooo; >oooooooo<"ROCK"~<
a="KRS".index(input()[-1])
print["SCISSORS","ROCK","PAPER"][a]
Posted in TNB as a challenge, I decided to try out this combination of languages.
><>
Try it online!
The IP starts moving right.
# Reflect the IP so that it now moves left and it wraps around the grid
i: Take one character as input and duplicate it
The possible characters that will be taken into the input are PRS
(since the program only takes the first character). Their ASCII-values are 80
, 81
and 82
.
2% Take the modulo 2 of the character. Yields 0, 1, 0 for P, R, S respectively
?v If this value is non-zero (ie the input was ROCK), go down, otherwise skip this instruction
If the input was rock, then this is what would happen:
< Start moving to the left
~ Pop the top most value on the stack (which is the original value of R and not the duplicate)
"KCOR" Push these characters onto the stack
< Move left
oooo Output "ROCK" as characters (in turn these characters are popped)
o Pop the top value on the stack and output it; but since the stack is empty, the program errors out and exits promptly.
Otherwise, if the input was SCISSORS
or PAPER
, this is what the IP would encounter:
"SROSSICS" Push these characters onto the stack
{ Shift the stack, so the the original value of the first char of the input would come to the top
2-4% Subtract 2 and take modulo 4 of the ASCII-value (yields 2, 0 for P, S respectively)
?v If it is non-zero, go down, otherwise skip this instruction
If the input was PAPER
, then:
>ooooooooo Output all characters on the stack (ie "SCISSORS")
< Start moving left
o Pop a value on the stack and output it; since the stack is empty, this gives an error and the program exits.
Otherwise (if the input was SCISSORS
):
"REPAP" Push these characters onto the stack
v>ooooo; Output them and exit the program (without any errors).
Retina
Try it online!
In this case, Retina considers each pair of two lines as a pair of a match and substitution. For example, it tries to replace anything matching the first line with the second line, but since the first line is never matched, it never substitutes it with anything, thus preserving the input.
Python 2
Try it online!
The Python program requires input to be put in between "
s.
The first two lines are comments in Python.
a="KRS".index(input()[-1]) # Get the index of the last character of the input in "KRS"
print["SCISSORS","ROCK","PAPER"][a] # Print the ath index of that array
Can a language error out to exit? – user41805 – 2017-04-12T17:39:10.593
2
@KritixiLithos Go with the meta concensus. "I think terminating with an error or an uncaught exception is fine here, as long as it doesn't produce stray output to STDOUT."
– Calvin's Hobbies – 2017-04-12T17:42:21.2472Never quite sure with polyglots, can the different languages take input in different ways? – Jonathan Allan – 2017-04-12T21:28:24.667
3@JonathanAllan That's ok. For some language sets that only have certain forms of input it would be necessary. – Calvin's Hobbies – 2017-04-13T06:31:27.473
What happened to Lizard, Spock? http://bigbangtheory.wikia.com/wiki/Rock_Paper_Scissors_Lizard_Spock
– Ole Tange – 2017-04-14T22:16:05.180