Created
May 21, 2014 16:46
-
-
Save sowasred2012/5ac53f2bec2a005dada6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rock Paper Scissors Lizard Spock is a more convoluted and much geekier version of Rock Paper Scissors. | |
The rules are: | |
Rock beats Scissors | |
Rock crushes Lizard | |
Paper covers Rock | |
Paper disproves Spock | |
Scissors cut Paper | |
Scissors decapitate Lizard | |
Lizard poisons Spock | |
Lizard eats Paper | |
Spock melts Scissors | |
Spock vaporizes Rock | |
Like for Like is a draw | |
Create the game, which accepts one of the five items as an argument and has the computer pick an item at random, then gives feedback on who won and who lost. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game | |
GESTURE_COMPARISONS = { :paper => [:rock, :spock], | |
:rock => [:scissors, :lizard], | |
:scissors => [:paper, :lizard], | |
:lizard => [:spock, :paper], | |
:spock => [:rock, :scissors] } | |
attr_accessor :opponent_gesture | |
def initialize | |
end | |
def play(gesture) | |
raise Exception unless valid_gestures.include?(gesture) | |
@gesture = gesture | |
calculate_winner | |
end | |
private | |
def opponent_gesture | |
@opponent_gesture ||= valid_gestures.choice | |
end | |
def valid_gestures | |
GESTURE_COMPARISONS.keys.map { |k| k.to_s.capitalize } | |
end | |
def calculate_winner | |
return "Draw" if @gesture == opponent_gesture | |
if user_wins | |
"Winner: #{@gesture} beats #{opponent_gesture}" | |
else | |
"Loser: #{opponent_gesture} beats #{@gesture}" | |
end | |
end | |
def user_wins | |
GESTURE_COMPARISONS[@gesture.downcase.to_sym].include? opponent_gesture.downcase.to_sym | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'rpsls.rb' | |
describe Game do | |
let(:rpsls) { described_class.new } | |
it 'only accepts a valid gesture' do | |
expect{ rpsls.play('Blah') }.to raise_error | |
end | |
it 'returns "Loser" if the opponent wins' do | |
rpsls.opponent_gesture = "Scissors" | |
result = rpsls.play('Paper') | |
result.should eq('Loser: Scissors beats Paper') | |
end | |
it 'returns "Winner"' do | |
rpsls.opponent_gesture = "Spock" | |
result = rpsls.play('Paper') | |
result.should eq('Winner: Paper beats Spock') | |
end | |
it 'returns a "Draw" if both gestures are the same' do | |
rpsls.opponent_gesture = "Spock" | |
rpsls.play('Spock').should eq('Draw') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment