Skip to content

Instantly share code, notes, and snippets.

@carlosagp
Created August 21, 2014 07:04
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 carlosagp/ed4f65fa39e6d85977e8 to your computer and use it in GitHub Desktop.
Save carlosagp/ed4f65fa39e6d85977e8 to your computer and use it in GitHub Desktop.
Simpler Rock Paper Scissors
#!/usr/bin/ruby
choices = {1 => 'Rock', 2 => 'Paper', 3 => 'Scissors'}
puts "These are your choices"
puts "1) Rock"
puts "2) Paper"
puts "3) Scissors"
print "Please enter the number of your choice: "
player_choice = gets
player_choice = player_choice.to_i
puts "You chose #{player_choice} - #{choices[player_choice]}"
# Ruby 2.0
computer_choice = rand(1..3) # rand(2) + 1
puts "Computer choses #{player_choice} - #{choices[player_choice]}"
# Rock beats Scissors
# Paper beats Rock
# Scissors beats Paper
# In case of a tie
if player_choice == computer_choice
puts "You have tied!"
exit
end
if player_choice == 1 && computer_choice == 3 || player_choice == 2 && computer_choice == 1 || player_choice == 3 && computer_choice == 2
puts "Congratulations! #{choices[player_choice]} beats #{choices[computer_choice]}"
else
puts "You lose! #{choices[computer_choice]} beats #{choices[player_choice]}"
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment