Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (38 sloc) 1.08 KB
VALID_CHOICES = ['rock', 'paper', 'scissors']
def prompt(message)
Kernel.puts("=> #{message}")
end
def win?(first, second)
(first == 'rock' && second == 'scissors') ||
(first == 'scissors' && second == 'paper') ||
(first == 'paper' && second == 'rock')
end
def display_results(player, computer)
if win?(player, computer)
prompt('You won!')
elsif win?(computer, player)
prompt('Computer won!')
else
prompt("It's a tie!")
end
end
loop do # play again? loop
choice = ''
loop do
prompt("Choose one: #{VALID_CHOICES.join(', ')}")
choice = Kernel.gets().chomp().downcase
if VALID_CHOICES.include?(choice)
break
else
prompt('That is not a valid choice.')
end
end
computer_choice = VALID_CHOICES.sample()
Kernel.puts("You chose: #{choice}; Computer chose: #{computer_choice}.")
display_results(choice, computer_choice)
prompt('Do you want to play again?
Press Y to continue, or any other key to exit.')
play_again = Kernel.gets().chomp().downcase
break unless play_again == 'y'
end
prompt('Thank you for playing! Goodbye!')
You can’t perform that action at this time.