Skip to content

Instantly share code, notes, and snippets.

@KBeltz
Last active August 29, 2015 14:22
Show Gist options
  • Save KBeltz/1216aca3fc3784f3cd40 to your computer and use it in GitHub Desktop.
Save KBeltz/1216aca3fc3784f3cd40 to your computer and use it in GitHub Desktop.
Rock-Paper-Scissors
# rps = array of possible plays
rps = [:rock, :paper, :scissors]
puts "Player 1, choose your weapon: "
player1 = gets.chomp.downcase.to_sym
# accepts only valid input from player 1
if rps.include?(player1) == false
puts "ERROR! INVALID INPUT!"
else
puts "Player 2, choose your weapon: "
player2 = gets.chomp.downcase.to_sym
#accepts only valid input from player 2
if rps.include?(player2) == false
puts "ERROR! INVALID INPUT!"
else
# rules hash contains valid player 1 moves as the key
# the values of the rules hash are a second hash
# the value hashes contain valid player 2 moves as the key
# corresponding hash values are the game results
rules = {
:rock => {:rock => :draw, :paper => :paper, :scissors => :rock},
:paper => {:rock => :paper, :paper => :draw, :scissors => :scissors},
:scissors => {:rock => :rock, :paper => :scissors, :scissors => :draw}
}
puts "Player 1 chose #{player1}."
puts "Player 2 chose #{player2}."
# determines the winner from the rules hash
if rules[player1][player2] == :draw
puts "Draw. You are equals...for now."
elsif rules[player1][player2] == player1
puts "Player 1 wins!"
elsif rules[player1][player2] == player2
puts "Player 2 wins!"
else
puts "This shouldn't have happened. How embarrassing!"
end
end
end
@sumeetjain
Copy link

Nice! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment