Skip to content

Instantly share code, notes, and snippets.

@galbrecht18
Last active September 3, 2015 00:45
Show Gist options
  • Save galbrecht18/168b28e4a5097ed7195c to your computer and use it in GitHub Desktop.
Save galbrecht18/168b28e4a5097ed7195c to your computer and use it in GitHub Desktop.
Gist for Lesson One Rock, Paper, Scissors application.
#Lesson One Rock Paper Scissors App
#Created by George Albrecht
#9-1-2015
#Updated Version
require 'pry'
CHOICES = {'r' => 'Rock', 'p' => 'Paper', 's' => 'Scissors'}
puts "***** Welcome to Rock, Paper, Scissors! Can you beat the computer? *****"
#method to compare choices and evaluate a winner
def compare_choices player_choice, computer_choice
if player_choice == computer_choice
puts "Tie! Try again!"
else
win = "You win!"
case [player_choice, computer_choice]
when ['r','s']
puts "Rock beats scissors! #{win}"
when ['p','r']
puts "Paper beats rock! #{win}"
when ['s','p']
puts "Scissors beats paper! #{win}"
else
puts "Computer kicked your behind! "+ CHOICES[computer_choice]+ " beats "+ CHOICES[player_choice]+"!"
end
end
end
#start the loop/game
loop do
#get the player choice. Loop to make sure that the player picks a valid choice
begin
puts "***** Enter (r, p, or s) for rock, paper, or scissors. *****"
player_choice = gets.chomp.downcase.to_s
puts "You chose #{CHOICES[player_choice]}"
end until CHOICES.keys.include?(player_choice)
#get computer choice
computer_choice = CHOICES.keys.sample.to_s
puts "Computer chose #{CHOICES[computer_choice]}"
#compare the choices
compare_choices(player_choice, computer_choice)
puts '***** Want to play again? (y/n)? *****'
break if gets.chomp.downcase != 'y'
end
puts "Later!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment