Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created May 26, 2018 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pachacamac/cc91470eba79ffce8739ebe35b2c215a to your computer and use it in GitHub Desktop.
Save pachacamac/cc91470eba79ffce8739ebe35b2c215a to your computer and use it in GitHub Desktop.
rock paper scissors lizard spock
VALID_CHOICES = {
'rock' => %w(scissors lizard),
'paper' => %w(rock spock),
'scissors' => %w(paper lizard),
'lizard' => %w(paper spock),
'spock' => %w(rock scissors)
}
last_player_choice = nil
strategy_modifier = rand(0.2..0.8)
player_score = 0
computer_score = 0
player_choice = ''
loop do
loop do
puts "Choose one: #{VALID_CHOICES.keys.join(', ')}"
player_choice = gets.chomp
break if VALID_CHOICES.keys.include?(player_choice)
puts "That's not a valid choice."
end
computer_choice = last_player_choice && rand > strategy_modifier ? last_player_choice : VALID_CHOICES.keys.sample
last_player_choice = player_choice
puts "You chose: #{player_choice}; Computer chose: #{computer_choice}"
if VALID_CHOICES[player_choice].include?(computer_choice)
player_score += 1
puts "You win!"
elsif VALID_CHOICES[computer_choice].include?(player_choice)
computer_score += 1
puts "You lose!"
else
puts "It's a tie!"
end
break if player_score == 5 || computer_score == 5
end
puts "You #{player_score > computer_score ? 'WON' : 'LOST'} the match!"
puts "Your score was #{player_score} - #{computer_score}. Thank you for playing. Have a nice day!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment