Created
June 18, 2012 23:18
-
-
Save bcamarda/2951389 to your computer and use it in GitHub Desktop.
RPS Winner
This file contains hidden or 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
def game(pair) | |
raise WrongNumberOfPlayersError unless pair.length == 2 | |
strategy = pair[0][1].downcase + pair[1][1].downcase | |
if ["rs", "sp", "pr"].include?(strategy) | |
#return "#{pair[0]} wins since #{pair[0][1]} > #{pair[1][1]}}" | |
return pair[0] | |
elsif ["sr", "ps", "rp"].include?(strategy) | |
#return "#{pair[1]} wins since #{pair[1][1]} > #{pair[0][1]}}" | |
return pair[1] | |
elsif ["rr", "ss", "pp"].include?(strategy) | |
#return "It's a tie since #{pair[0][1]} = #{pair[1][1]}" | |
return "It's a Tie!" | |
else | |
raise NoSuchStrategyError | |
end | |
end | |
def find_winner(bracket) | |
if bracket.first.first.is_a?(String) | |
game([bracket.first, bracket.last]) | |
else | |
game([find_winner(bracket.first), find_winner(bracket.last)]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment