Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created January 27, 2022 00:12
Show Gist options
  • Save caioertai/a3888a1d42f2636e120e52f9e02195ea to your computer and use it in GitHub Desktop.
Save caioertai/a3888a1d42f2636e120e52f9e02195ea to your computer and use it in GitHub Desktop.
# 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