Skip to content

Instantly share code, notes, and snippets.

@CyrusOfEden
Created November 29, 2014 21:57
Show Gist options
  • Save CyrusOfEden/44177cbd5a5498544bc5 to your computer and use it in GitHub Desktop.
Save CyrusOfEden/44177cbd5a5498544bc5 to your computer and use it in GitHub Desktop.
class Hand
WEAPONS = [:rock, :paper, :scissors, :lizard, :spock]
attr_reader :weapon
def initialize(weapon)
@weapon = weapon.to_sym
end
def valid?
WEAPONS.include?(weapon)
end
end
class RPSLS
RESULTS = {
rock: {
lizard: 'crushes',
scissors: 'crushes'
},
paper: {
rock: 'covers',
spock: 'disproves'
},
scissors: {
paper: 'cuts',
lizard: 'decapitate'
},
lizard: {
paper: 'eats',
spock: 'poisons'
},
spock: {
scissors: 'smashes',
vaporizes: 'rock'
}
}
attr_reader :winner, :loser
def initialize(first, second)
first, second = first.to_sym, second.to_sym
if RESULTS[first].keys.include?(second)
@winner, @loser = Hand.new(first), Hand.new(second)
else
@winner, @loser = Hand.new(second), Hand.new(first)
end
raise unless winner.valid? && loser.valid?
end
def result
"#{winner.weapon.capitalize} #{RESULTS[winner.weapon][loser.weapon]} #{loser.weapon.capitalize}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment