Skip to content

Instantly share code, notes, and snippets.

@littleidea
Created August 18, 2009 01:04
Show Gist options
  • Save littleidea/169476 to your computer and use it in GitHub Desktop.
Save littleidea/169476 to your computer and use it in GitHub Desktop.
class RPSLS
TYPES = ["Rock", "Paper", "Scissors", "Lizard", "Spock"]
RESOLVER = { "Rock" => ["Scissors", "Lizard"],
"Paper" => ["Rock", "Spock"],
"Scissors" => ["Paper", "Lizard"],
"Lizard" => ["Paper", "Spock"],
"Spock" => ["Scissors", "Rock"] }
def self.get_type(type)
type = TYPES[type] if type.is_a? Fixnum
type
end
def self.resolve(first, second)
first = get_type(first)
second = get_type(second)
if first == second
return "tie"
end
if RESOLVER[first].include?(second)
puts(first + " beats " + second)
return "first"
else
puts(first + " loses to " + second)
return "second"
end
end
end
>> score = Hash.new(0)
=> {}
>> 10.times { |i| score[RPSLS.resolve(rand(5), rand(5))] += 1 }
Rock beats Lizard
Paper loses to Scissors
Lizard loses to Scissors
Spock loses to Lizard
Lizard loses to Rock
Scissors beats Lizard
Spock loses to Lizard
Rock beats Lizard
=> 10
>> RPSLS.resolve("Rock", "Spock")
Rock loses to Spock
=> "second"
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment