Created
July 29, 2012 02:57
-
-
Save lfborjas/3195867 to your computer and use it in GitHub Desktop.
This file contains 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 'sinatra' | |
before do | |
content_type :txt | |
@defeat = { rock: :scissors, paper: :rock, scissors: :paper } | |
@throws = @defeat.keys | |
end | |
get '/throw/:type' do | |
player_throw = params[:type].to_sym | |
# delivers error if user does not pick a valid throw | |
if [email protected]?(player_throw) | |
halt 403, "You must throw one of the following: #{throws}" | |
end | |
# selects a random throw for the computer | |
computer_throw = @throws.sample | |
# compares throws and selects winner | |
if player_throw == computer_throw | |
"Whoa, jinx. You tied with the computer. Try again." | |
elsif computer_throw == @defeat[player_throw] | |
"Awesome. As we know, #{player_throw} defeats #{computer_throw}." | |
else | |
"Hmm... #{computer_throw} unfortunately defeats #{player_throw}." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment