Guest User

Untitled

a guest
Mar 28th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. require 'sinatra'
  2. # before we process a route, we'll set the response as
  3. # plain text and set up an array of viable moves that
  4. # a player (and the computer) can perform
  5. before do
  6. content_type :txt
  7. @defeat = {rock: :scissors, paper: :rock, scissors: :paper}
  8. @throws = @defeat.keys
  9. end
  10.  
  11. get '/throw/:type' do
  12. # the params[] hash stores querystring and form data.
  13. player_throw = params[:type].to_sym
  14. # in the case of a player providing a throw that is not valid,
  15. # we halt with a status code of 403 (Forbidden) and let them
  16. # know they need to make a valid throw to play.
  17. if [email protected]?(player_throw)
  18. halt 403, "You must throw one of the following: #{@throws}"
  19. end
  20.  
  21. # now we can select a random throw for the computer
  22. computer_throw = @throws.sample
  23. # compare the player and computer throws to determine a winner
  24. if player_throw == computer_throw
  25. "You tied with the computer. Try again!"
  26. elsif computer_throw == @defeat[player_throw]
  27. "Nicely done; #{player_throw} beats #{computer_throw}!"
  28. else
  29. "Ouch; #{computer_throw} beats #{player_throw}. Better luck next time!"
  30. end
Advertisement
Add Comment
Please, Sign In to add comment