Created
July 11, 2018 16:29
-
-
Save fractalatcarf/9f959fb9caad3b42bff829fb7b7a02df to your computer and use it in GitHub Desktop.
this evening's Live code
This file contains 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
# def acronimize(sentence) | |
# return "" unless sentence.is_a? String | |
# acronyme = "" | |
# # split sentence into array | |
# words = sentence.split(" ") | |
# # for each item in my array, push 1st letter into result | |
# words.each do |word| | |
# acronyme = acronyme + word[0] | |
# end | |
# # upcase the stuff | |
# return acronyme.upcase | |
# end | |
# p acronimize("save our soul") == "SOS" | |
# p acronimize("Olympique Marseille") == "OM" | |
# p acronimize("Société nationale Chemins fer") == "SNCF" | |
# p acronimize("") == "" | |
# p acronimize(" ") == "" | |
# p acronimize(12) == "" | |
# p acronimize(12.948483) == "" | |
# Rock Paper Scissors | |
# games starts | |
# ask user for choice (pa, ro, sc) | |
# until user choice is empty | |
# computer choose randomly | |
# evaluate game result | |
# if computer wins => +1 computer score | |
# if user wins => + user score | |
# display scores | |
def game_result(computer_choice, user_choice) | |
return "tie" if computer_choice == user_choice | |
wins = ["rosc", "paro", "scpa"] | |
return "computer" if wins.include? computer_choice+user_choice | |
return "user" | |
end | |
p game_result("ro","sc") == "computer" | |
p game_result("pa","ro") == "computer" | |
p game_result("sc","pa") == "computer" | |
p game_result("sc","ro") == "user" | |
p game_result("ro","pa") == "user" | |
p game_result("pa","sc") == "user" | |
p game_result("sc","sc") == "tie" | |
p game_result("ro","ro") == "tie" | |
This file contains 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
# games starts | |
# ask user for choice (pa, ro, sc) | |
# until user choice is empty | |
# computer choose randomly | |
# evaluate game result | |
# if computer wins => +1 computer score | |
# if user wins => + user score | |
# display scores | |
require_relative "acronimizer" | |
computer_score = 0 | |
user_score = 0 | |
loop do | |
user_choice = "" | |
loop do | |
puts "Choose pa, ro or sc" | |
user_choice = gets.chomp | |
break if ["sc", "pa", "ro", ""].include? user_choice | |
end | |
break if user_choice == "" | |
computer_choice = %w(sc pa ro).sample | |
result = game_result(computer_choice, user_choice) | |
computer_score += 1 if result == "computer" | |
user_score += 1 if result == "user" | |
puts "computer: #{computer_choice}, you: #{user_choice} #{result == 'tie' ? 'TIE !' : result+ ' wins !'}" | |
end | |
puts "computer : #{computer_score}" | |
puts "you : #{user_score}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment