Created
January 27, 2022 00:12
-
-
Save caioertai/a3888a1d42f2636e120e52f9e02195ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Create an array of valid hands | |
# 0 1 2 | |
# -3 -2 -1 | |
valid_hands = %w[rock scissors paper] | |
user_hand = nil | |
# Get user input for a hand | |
loop do | |
puts "Choose your hand: #{valid_hands.join(' | ')}" | |
puts "<To exit just press enter.>" | |
user_hand = gets.chomp | |
break if user_hand.empty? | |
until valid_hands.include?(user_hand) | |
puts "That's not a valid hand!" | |
user_hand = gets.chomp | |
end | |
# Get random pc hand from the valid | |
pc_hand = valid_hands.sample | |
puts "PC chose #{pc_hand}" | |
# What determines a winning hand? | |
if (user_hand == "rock" && pc_hand == "scissors") || | |
(user_hand == "scissors" && pc_hand == "paper") || | |
(user_hand == "paper" && pc_hand == "rock") | |
puts "You win" | |
elsif pc_hand == user_hand | |
puts"Draw" | |
else | |
puts"You lose" | |
end | |
end | |
puts "Goodbye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment