Last active
August 29, 2015 14:21
-
-
Save jdmorlan/594d07b3469c4e608ce3 to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors Winner Algorithm
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
class RPSAlgorithm | |
# Let Paper = 1, Rock = 2, Scissors = 3 | |
def initialize(values={}) | |
@values = {paper: 1, rock: 2, scissors: 3} | |
end | |
def set_values(values) | |
@values = values | |
end | |
# get_winner(:rock, :paper) => :paper | |
# Takes the sum of the two choices, based on the values hash above. | |
# If the sum is even, the greater value of the two choices is the winner. | |
# If the sum is odd, the lesser value of the two choices is the winner. | |
def get_winner(choice_one, choice_two) | |
return "tie" if choice_one == choice_two | |
choice_values = [@values[choice_one], @values[choice_two]] | |
choice_sum = choice_values.inject(0, :+) | |
winning_value = choice_sum.even? ? choice_values.max : choice_values.min | |
return @values.key(winning_value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment