Skip to content

Instantly share code, notes, and snippets.

@listrophy
Created February 21, 2015 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save listrophy/93bfe261f5300f40d681 to your computer and use it in GitHub Desktop.
Save listrophy/93bfe261f5300f40d681 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors, Lizard, Spock
# Exactly 140 characters! :)
# We rescue StandardError and re-raise a RuntimeError
# because that's nicer than getting NoMethodError or TypeError
def play a, b
t = %w(scissors paper rock lizard spock)
['tie', b, a, b, a][(t.index(a)-t.index(b)) % 5]
rescue StandardError
raise 'invalid'
end
require 'minitest/autorun'
describe 'play' do
%w(
scissors paper
paper rock
rock lizard
lizard spock
spock scissors
scissors lizard
lizard paper
paper spock
spock rock
rock scissors
).each_slice(2) do |winner, loser|
specify "#{winner} beats #{loser}" do
play(winner, loser).must_equal(winner)
play(loser, winner).must_equal(winner)
end
end
%w(scissors paper rock lizard spock).each do |tie|
specify "#{tie} ties #{tie}" do
play(tie, tie).must_equal('tie')
end
end
specify "fails if first arg is invalid" do
-> { play('foo', 'spock') }.must_raise RuntimeError
end
specify "fails if second arg is invalid" do
-> { play('spock', 'bar') }.must_raise RuntimeError
end
specify "fails if both args are invalid" do
-> { play('baz', 'qux') }.must_raise RuntimeError
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment