Created
July 4, 2014 19:18
-
-
Save AndrewRadev/96e502042ec75810b824 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' | |
get '/play/:choice' do | |
player_choice = params[:choice] | |
choices = ["rock", "paper", "scissors"] | |
if player_choice == "random" | |
player_choice = choices.sample | |
end | |
if not choices.include? player_choice | |
next "Your choice, '#{player_choice}', is invalid" | |
end | |
computer_choice = choices.sample | |
game_rules = { | |
"rock" => "scissors", | |
"scissors" => "paper", | |
"paper" => "rock", | |
} | |
if player_choice == computer_choice | |
"Your choice was #{player_choice}, Computer choice is #{computer_choice}. It's a tie" | |
elsif player_choice == game_rules[computer_choice] | |
"Your choice was #{player_choice}, Computer choice is #{computer_choice}. You lose" | |
else | |
"Your choice was #{player_choice}, Computer choice is #{computer_choice}. You win" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment