Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Initialize user_input
- user_input = nil
- # Validate user input
- until user_input == "rock" || user_input == "paper" || user_input == "scissors"
- # Ask user input (rock/paper/scissors)
- puts "Choose between rock, paper or scissors."
- user_input = gets.chomp
- end
- # Randomly select rock/paper/scissors (computer)
- choices = ["rock", "paper", "scissors"]
- computer_input = choices.sample
- # Display the user's and computer's picks
- puts "The computer chose #{computer_input}."
- puts "You chose #{user_input}."
- # Define game rules for rock/paper/scissors
- # Compare computer and user selection
- # Display results
- if computer_input == user_input
- puts "It's a draw. :-| "
- elsif computer_input == "rock"
- if user_input == "paper"
- puts "You win"
- else
- puts "You lose!"
- end
- elsif computer_input == "paper"
- if user_input == "scissors"
- puts "You win"
- else
- puts "You lose!"
- end
- else
- if user_input == "rock"
- puts "You win"
- else
- puts "You lose!"
- end
- end
- # End game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement