Skip to content

Instantly share code, notes, and snippets.

@phildionne
Created October 5, 2012 21:48
Show Gist options
  • Save phildionne/3842620 to your computer and use it in GitHub Desktop.
Save phildionne/3842620 to your computer and use it in GitHub Desktop.
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