Last active
December 20, 2015 05:39
-
-
Save vjdhama/6079665 to your computer and use it in GitHub Desktop.
Edx Class 169.1x HW 1-2
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/env ruby | |
class WrongNumberOfPlayersError < StandardError ; end | |
class NoSuchStrategyError < StandardError ; end | |
class Object | |
def depth #Check depth of array passed | |
self.class == Array ? 1 + self[0].depth : 0 | |
end | |
end | |
def compare_game?(game) | |
return (game[0][1] + game[1][1]) =~ /rs|sp|pr|rr|ss|pp/i | |
end | |
def rps_game_winner(game) | |
strategy=["r","p","s"] | |
raise WrongNumberOfPlayersError unless game.length == 2 | |
if strategy.include?(game[0][1].downcase) && strategy.include?(game[1][1].downcase) | |
compare_game?(game) ? game[0]:game[1] | |
else | |
raise NoSuchStrategyError | |
end | |
end | |
def rps_tournament_winner(tournament) | |
d = tournament.depth | |
if d == 2 #Passing rps_game_winner method a array of depth 2 | |
rps_game_winner(tournament) | |
else | |
a1 = rps_tournament_winner(tournament[0]) #Recursive Calls | |
a2 = rps_tournament_winner(tournament[1]) #To the array halves | |
rps_tournament_winner([a1,a2]) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment