# frozen_string_literal: true
ABBREVIATIONS = {
'r' => 'rock',
'p' => 'paper',
's' => 'scissors',
'l' => 'lizard',
'v' => 'spock'
}.freeze
WINNING_COMBOS = {
'rock' => %w(scissors lizard),
'paper' => %w(rock spock),
'scissors' => %w(paper lizard),
'lizard' => %w(paper spock),
'spock' => %w(rock scissors)
}.freeze
def prompt(message)
puts("=> #{message}")
end
def win?(first_player, second_player)
WINNING_COMBOS[ABBREVIATIONS[first_player]].include?(ABBREVIATIONS[second_player])
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
def play_again?
loop do
answer = gets.chomp
if answer.casecmp('y') == 0
break
elsif answer.casecmp('n') == 0
exit
else
prompt("We didn't recognize that. Please enter 'y' or 'n'.")
end
end
end
loop do
player_points = 0
computer_points = 0
prompt("Remember, the first to get 5 points, wins!")
loop do
player_choice = ''
loop do
prompt <<~CHOOSE
Please enter
r for rock
p for paper
s for scissors
l for lizard
v for spock.
CHOOSE
prompt(">>> ")
player_choice = gets.chomp
if ABBREVIATIONS.keys.include?(player_choice)
break
else
prompt("That's not a valid choice.")
end
end
computer_choice = ABBREVIATIONS.keys.sample
prompt <<~CHOSE
"You chose: #{ABBREVIATIONS[player_choice]} and
the computer chose: #{ABBREVIATIONS[computer_choice]}")
CHOSE
display_results(player_choice, computer_choice)
if win?(player_choice, computer_choice)
player_points += 1
elsif win?(computer_choice, player_choice)
computer_points += 1
else
player_points += 1
computer_points += 1
end
prompt("+++++++++++")
prompt("Player has #{player_points}. Computer has #{computer_points}.")
prompt("The first to get 5 points wins!")
prompt("+++++++++++")
break if player_points == 5 || computer_points == 5
end
prompt("Do you want to play again?")
play_again?
end