Skip to content

Instantly share code, notes, and snippets.

@jonny-gates
Created July 10, 2019 17:11
Show Gist options
  • Save jonny-gates/8e9f92d6cf6d35c27c59c513bde25434 to your computer and use it in GitHub Desktop.
Save jonny-gates/8e9f92d6cf6d35c27c59c513bde25434 to your computer and use it in GitHub Desktop.
Flow, Conditionals & Arrays - Live Code Solutions
def acronomyze(string)
# split our sentence on space
string_split = string.split(" ")
string_split.map do |word|
word[0] # first letter of each string
end
# same as:
# letters = []
# string_split.each do |word|
# letters << word[0]
# end
# join our array into our string
string_together = string_split.join
# make the string uppercase
string_together.upcase
end
puts acronomyze("Away From Keyboard") == "AFK"
puts acronomyze("Out of Offce") == "OOO"
puts acronomyze("") == ""
puts acronomyze("be right back") == "BRB"
# ask for input
# generate random input
# compare the two
# game logic
# announce result
# generate our options
array = ["rock", "paper", "scissors"]
# initialize answer variable
answer = nil
# keep looping until the user picks a valid choice
until array.include?(answer)
# ask the user to make a choice
puts "Rock Paper Scissors"
answer = gets.chomp.downcase
# tell the user invalid if their answer is not in the array
puts "Invalid choice" if !array.include?(answer)
end
# generate a hand for the computer
computer_choice = array.sample
def game_logic(answer, computer_choice)
# checking the possible scenarios and returning the result
if answer == computer_choice
return "Draw"
elsif answer == "rock" && computer_choice == "paper"
return "Computer wins"
elsif answer == "rock" && computer_choice == "scissors"
return "You win!"
elsif answer == "paper" && computer_choice == "rock"
return "You win!"
elsif answer == "paper" && computer_choice == "scissors"
return "Computer wins!"
elsif answer == "scissors" && computer_choice == "paper"
return "You win!!!!"
elsif answer == "scissors" && computer_choice == "rock"
return "Computer wins!"
end
end
# implementing the game logic and printing the result
puts game_logic(answer, computer_choice)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment