Rock Paper Scissors

LOLCODE, 1397

Note: I submitted this before I noticed the winning requirement was changed from popularity with golf tie-break to golf with popularity tie-break.

There's not really any strict syntax, but I'm sure this is acceptable.

HAI
    I HAS A CRAZY, LUCKY, CHALLENGE, TREAT
    I HAS YUMMY ITZ "LOL U LOZED"
    I HAS MEH ITZ "NOWAI TIED"
    I HAS GROSS ITZ "OMG U WONNED"
    I HAS BURNT ITZ "LAME"
    GIMMEH CHALLENGE
    BTW I HOPE I R TEH WINZ
    LOL CRAZY IZ BETWEEN 1 AN 3
    I HAS A SUPA ITZ A BUKKIT
    LOL SUPA'Z 1 R "ROCK"
    LOL SUPA'Z 2 R "PAPER"
    LOL SUPA'Z 3 R "SCIZZORS"
    LOL LUCKY R SUPA'Z CRAZY
    GOT CHALLENGE, WTF?
        OMG "Rock"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R MEH, GTFO
                OMG PAPER, LOL TREAT R YUMMY, GTFO
                OMG SCIZZORS, LOL TREAT R GROSS, GTFO
            OIC
        OMG "Paper"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R GROSS, GTFO
                OMG PAPER, LOL TREAT R MEH, GTFO
                OMG SCIZZORS, LOL TREAT R YUMMY, GTFO
            OIC
        OMG "Scissors"
            GOT LUCKY, WTF?
                OMG ROCK, LOL TREAT R YUMMY, GTFO
                OMG PAPER, LOL TREAT R GROSS, GTFO
                OMG SCIZZORS, LOL TREAT R MEH, GTFO
            OIC
        OMGWTF
            VISIBLE "WHAT U SAYZ?", LOL TREAT R BURNT
            GTFO
    OIC
        BOTH SAEM TREAT AN BURNT, O RLY?
            YARLY
                VISIBLE "YOU BURNTED MAH TREAT!"
            NOWAI
                VISIBLE SMOOSH "I GUESSED " AN LUCKY
                VISIBLE TREAT
        KTHX
KTHXBAI

If this were to be successfully executed as RockPaperScissors.LOL, here's what some possible random outcomes could be:

  • Input: Rock - Output: I GUESSED SCIZZORS U WONNED
  • Input: Paper - Output: I GUESSED PAPER NOWAI TIED
  • Input: Scissors - Output: I GUESSED ROCK LOL U LOZED
  • Input: Tuna - Output: WHAT U SAYZ? YOU BURNTED MAH TREAT!

GolfScript

n"Draw"

Above code implements the required functionality. Additionally, it ensures that the player will never be left angry because of a (perceived) unfairness of computer's strategy.

Ungolfed version

n"Draw"

How to invoke the program

The input (a single character of 'r', 'p', 's') has to be provided on STDIN, possibly terminated with newline.

A sample run

> echo r | ruby golfscript.rb rockpaperscissors.gsc
r
Draw

Explanation of the code

For all those not familiar with GolfScript I'll add an detailed explanation of how this code works. The code essentially exists of three parts.

### Computer's strategy ###
# The strategy used to play r/p/s. 
# The computer is so fast, it can really guess in an instance 
# what the player has played. Since the computer should 
# not play unfair, the best strategy is to always go for a 
# draw and choose the same move.
        # on the stack is player's move
        # choose to play the same -> now the computer's move is on the stack

### Fiddle with input ###
# The input may of may not be delimited by newline.
# In order to make the output readable, we'll give
# a newline here.
n       # Push a newline onto the stack

### Give the result ###
# We can skip a complicated calculation of the result
# since we chose to play draw anyways.
"Draw"  # Push the result onto the stack

# Output is printed automatically when GolfScript code terminates.

Notes

Since this is not code-golf but popularity contest I didn't choose the shortest version. Maybe in case of a tie a shorter code will knock out my solution. Nevertheless, for those interested in golfing, the following possibilities are given:

  • Deal only with proper input and force user to provide a newline. This will save one character.
  • The rules have a small insufficiency which allows to save another character by bending the rules. The result can always be printed as "Win" - it was not specified that the correct result has to be printed. But note that players will soon get angry if you choose to implement a cheating program.
  • The output format is not well specified. We can choose 0 as output for draw. Thus, the shortest valid program is the single-character code 0.

Ruby: 61 54 characters

o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]

Somehow explained:

The entire problem is reduced to calculating the following results:

  ╲ machine
h  ╲| 0 1 2
u ──┼──────
m 0 │ 0 1 2 
a 1 │ 2 0 1
n 2 │ 1 2 0

Where the numbers mean:

  • choice: 0 rock, 1 paper, 2 scissor
  • result: 0 draw, 1 win, 2 lose

For this I used the formula: machine_choice - human_choice. This occasionally results negative value, but as it is only used as index and negative index is counted backward, will pick the correct array element.

# ┌── choosable object type
# │           ┌── machine's choice numeric code
# │           │                  ┌── result type
# │           │                  │                   ┌── human's choice
# │           │          ┌───────┴───────┐           │
  o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]
#           └─────┬────┘                   └─────┬────┘  
#                 └── machine's choice letter    │
#                                                └── result numeric code

Used methods (others then Fixnum's obvious ones):

  • Kernel.p(obj) → obj – “For each object, directly writes obj.inspect followed by a newline to the program’s standard output.”
  • Kernel.rand(max=0) → number – “If called without an argument, or if max.to_i.abs == 0, rand returns a pseudo-random floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.”
  • String.index(substring [, offset]) → fixnum or nil – “Returns the index of the first occurrence of the given substring or pattern (regexp) in str.”

Ungolfed:

object_type = "rps";
result_type = %w{Draw Win Lose}

machine_choice = rand(3)
human_choice = $_

p object_type[machine_choice]

result_code = machine_choice - object_type.index(human_choice)
p result_type[result_code]

Sample run:

bash-4.2$ ruby -nle 'o="rps";p o[c=rand(3)],%w{Draw Win Lose}[c.-o.index$_]'
r
"p"
"Win"
p
"p"
"Draw"
s
"p"
"Lose"

Tags:

Code Golf

Game