Created
October 5, 2012 21:48
-
-
Save phildionne/3842620 to your computer and use it in GitHub Desktop.
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 WrongNumberOfPlayersError < StandardError ; end | |
class NoSuchStrategyError < StandardError ; end | |
public | |
def rps_result(m1, m2) | |
# YOUR CODE HERE | |
end | |
def invalid?(turn) | |
!['r', 'p', 's'].include?(turn[1]) | |
end | |
def rps_game_winner(game) | |
game = game.map {|player, strategy| [player, strategy.downcase]} | |
raise WrongNumberOfPlayersError unless game.length == 2 | |
raise NoSuchStrategyError if game.any? { |turn| invalid?(turn) } | |
winning_strategies = [['r', 's'], ['s', 'p'], ['p', 'r']] | |
strategies = game.map { |turn| turn[1] } | |
(winning_strategies.include?(strategies) || | |
strategies.first == strategies.last) ? game.first : game.last | |
end | |
def is_game?(game) | |
game[0][0].is_a?(String) | |
end | |
def rps_tournament_winner(tournament) | |
if is_game?(tournament) | |
rps_game_winner(tournament) | |
else | |
left, right = tournament | |
wleft = rps_tournament_winner(left) | |
wright = rps_tournament_winner(right) | |
rps_game_winner([wleft, wright]) | |
end | |
end | |
game = [["Philippe", "R"], ["Louis", "S"]] | |
tournament = [ | |
[ | |
[ ["Armando", "P"], ["Dave", "S"] ], | |
[ ["Richard", "R"], ["Michael", "S"] ] | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
], | |
[ | |
[ ["Allen", "S"], ["Omer", "P"] ], | |
[ ["David E.", "R"], ["Richard X.", "P"] ] | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment