# frozen_string_literal: true
CHOICE_VALUES = {
rock: 1,
paper: 2,
scissors: 3,
spock: 4,
lizard: 5
}
only_values = CHOICE_VALUES.values
def win?(first, second)
(((first.odd? && second.odd?) ||
(first.even? && second.even?)) &&
first < second) ||
(((first.odd? && second.even?) ||
(first.even? && second.odd?)) &&
first > second)
end
def display_results(player_choice, computer)
if win?(player_choice, computer)
puts "You won!"
elsif win?(computer, player_choice)
puts "The computer won."
else
puts "It was a tie."
end
end
def play_again?
loop do
puts "Do you want to play again? (y for yes or n for no)"
response = gets.chomp.downcase
break if response.downcase.start_with?('y')
if response.downcase.start_with?('n')
puts "Bye!"
exit
else
puts "Sorry. We didn't recognize your response."
end
end
end
player_points = 0
computer_points = 0
player_choice = ''
p "The score is 0 to 0. It's anyone's game!"
loop do
loop do
puts("Please choose one: #{CHOICE_VALUES.keys.join(', ')}")
puts ">>> "
player_input = gets.chomp.downcase
if CHOICE_VALUES.keys.join(', ').include?(player_input)
player_choice = CHOICE_VALUES[player_input.to_sym]
break
else
puts "We didn't recognize your input."
end
end
computer = only_values[rand(only_values.size)]
puts "You chose: #{CHOICE_VALUES.key(player_choice)}"
puts "The computer chose: #{CHOICE_VALUES.key(computer)}"
display_results(player_choice, computer)
if win?(player_choice, computer)
player_points += 1
elsif win?(computer, player_choice)
computer_points += 1
else
player_points += 1
computer_points += 1
end
loop do
puts "What's the score?"
puts "You have #{player_points}."
puts "The computer has #{computer_points}."
if player_points == 5 && computer_points == 5
put "It's a tie! Game over."
play_again?
elsif computer_points >= 5
puts "The computer wins! Game over."
play_again?
elsif player_points >= 5
puts "You win. Game over."
play_again?
else
puts "Let's keep playing."
break
end
end
end