Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created April 14, 2022 01:28
Show Gist options
  • Save caioertai/25c763237aa7015412bf715ca9807f64 to your computer and use it in GitHub Desktop.
Save caioertai/25c763237aa7015412bf715ca9807f64 to your computer and use it in GitHub Desktop.
def acronymize(sentence)
acronym = ""
# .capitalize
# split
words = sentence.upcase.split
# iterate over words
words.each do |word|
# get first letter
acronym += word[0]
end
# add letter to variable
# return the variable
return acronym
end
p acronymize("what the hell")
puts acronymize("what the hell").class == String
puts acronymize("what the hell") == "WTH"
puts acronymize("oh my god") == "OMG"
puts "Welcome to the amazing RPS game!!!"
# Set the valid hands beforehand
valid_hands = ["rock", "paper", "scissors"]
# We're starting the game loop
# Everytime the "end" of the this loop is reached we come back here.
loop do
# Get and store the computer hand via sample
pc_hand = valid_hands.sample
# We interpolate the valid hands in the message to the user.
puts "Pick a hand: #{valid_hands.join(", ")} (q to quit)"
# Get and store the user hand
# FEATURE HINT: we could validate the hand by checking if it's included in
# the valid hands array.
user_hand = gets.chomp
# Break "jumps" to the end of the loop if triggered. We're only running it
# if the user says "q" to quit.
break if user_hand == "q"
# Some user feedback about the pc hand choice
puts "PC picked #{pc_hand}"
# Game state checks
if user_hand == pc_hand
puts "It's a draw!"
elsif (user_hand == "rock" && pc_hand == "scissors") ||
(user_hand == "scissors" && pc_hand == "paper") ||
(user_hand == "paper" && pc_hand == "rock")
puts "It's a win!"
puts "Congratulations."
else
puts "It's a loss!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment