Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # Initialize user_input
  2. user_input = nil
  3.  
  4. # Validate user input
  5. until user_input == "rock" || user_input == "paper" || user_input == "scissors"
  6. # Ask user input (rock/paper/scissors)
  7. puts "Choose between rock, paper or scissors."
  8. user_input = gets.chomp
  9. end
  10.  
  11. # Randomly select rock/paper/scissors (computer)
  12. choices = ["rock", "paper", "scissors"]
  13. computer_input = choices.sample
  14.  
  15. # Display the user's and computer's picks
  16. puts "The computer chose #{computer_input}."
  17. puts "You chose #{user_input}."
  18.  
  19. # Define game rules for rock/paper/scissors
  20. # Compare computer and user selection
  21. # Display results
  22. if computer_input == user_input
  23. puts "It's a draw. :-| "
  24. elsif computer_input == "rock"
  25. if user_input == "paper"
  26. puts "You win"
  27. else
  28. puts "You lose!"
  29. end
  30. elsif computer_input == "paper"
  31. if user_input == "scissors"
  32. puts "You win"
  33. else
  34. puts "You lose!"
  35. end
  36. else
  37. if user_input == "rock"
  38. puts "You win"
  39. else
  40. puts "You lose!"
  41. end
  42. end
  43.  
  44. # End game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement