Skip to content

Instantly share code, notes, and snippets.

@mitio
Created May 27, 2014 22:04
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 mitio/9021c0fefdf6ccec22aa to your computer and use it in GitHub Desktop.
Save mitio/9021c0fefdf6ccec22aa to your computer and use it in GitHub Desktop.
Rock-paper-scissors implemented in Sinatra
require 'sinatra'
before do
content_type :txt
@moves = {rock: :scissors, paper: :rock, scissors: :paper}
end
get '/play/:move' do
# Be wary when calling #to_sym on random strings!
player_move = params[:move].to_sym
unless @moves.key?(player_move)
valid_moves = @moves.keys.map(&:to_s).join(', ')
halt 403, "You must throw one of the following: #{valid_moves}"
end
computer_move = @moves.keys.sample
if player_move == computer_move
"Oh, my!\nIt's a tie!"
elsif computer_move == @moves[player_move]
"Nicely done, #{player_move} beats #{computer_move}!"
else
"Ouch... You got trashed, #{computer_move} beats #{player_move}. Better luck next time!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment