Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
30 lines (25 sloc) 759 Bytes
class WrongNumberOfPlayersError < StandardError;
end
class NoSuchStrategyError < StandardError;
end
def winner_for(strategy1, strategy2)
if (strategy1 == strategy2) || # equal strategies
(strategy1 + strategy2 =~ /pr|rs|sp/i) # first has winner combination
0 #=> first wins
else
1 #=> second wins
end
end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
raise NoSuchStrategyError if game.any? { |player| player[1] !~ /\As|r|p\z/i }
game[winner_for(game[0][1], game[1][1])]
end
def rps_tournament_winner(tournament)
if tournament[0][0].is_a?(Array)
tournament.map! { |games| rps_tournament_winner(games) }
rps_tournament_winner(tournament)
else
rps_game_winner(tournament)
end
end