Created
August 21, 2014 07:04
-
-
Save carlosagp/ed4f65fa39e6d85977e8 to your computer and use it in GitHub Desktop.
Simpler Rock Paper Scissors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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