Skip to content

Instantly share code, notes, and snippets.

@lfborjas
Created July 29, 2012 02:57
Show Gist options
  • Save lfborjas/3195867 to your computer and use it in GitHub Desktop.
Save lfborjas/3195867 to your computer and use it in GitHub Desktop.
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