Skip to content

Instantly share code, notes, and snippets.

@mhriess
Created October 5, 2012 05:47
Show Gist options
  • Save mhriess/3838304 to your computer and use it in GitHub Desktop.
Save mhriess/3838304 to your computer and use it in GitHub Desktop.
Rock-Paper-Scissors
CHOICES = ["Rock", "Paper", "Scissors"]
def player_choice
print "Enter your choice below:
>"
player_input = gets.chomp.capitalize
CHOICES.include?(player_input) ? player_input : player_choice
end
def computer_choice
CHOICES[rand(3)]
end
def outcome
player_decision = player_choice
computer_decision = computer_choice
combination_hash = { "Scissors" => "Paper", "Paper" => "Rock", "Rock" => "Scissors" }
decision = combination_hash[player_decision] == computer_decision ? "win" : (combination_hash[computer_decision] == player_decision ? "lose" : "draw")
puts "You chose #{player_decision} and the computer chose #{computer_decision}. You #{decision}!"
end
def new_game
puts "Let's play some Rock-Paper-Scissors!"
outcome
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment