Skip to content

Instantly share code, notes, and snippets.

@bcamarda
Created June 18, 2012 23:18
Show Gist options
  • Save bcamarda/2951389 to your computer and use it in GitHub Desktop.
Save bcamarda/2951389 to your computer and use it in GitHub Desktop.
RPS Winner
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