Skip to content

Instantly share code, notes, and snippets.

@Papillard
Last active December 19, 2015 06:19
Show Gist options
  • Save Papillard/5910918 to your computer and use it in GitHub Desktop.
Save Papillard/5910918 to your computer and use it in GitHub Desktop.
rock! scissors! you loose fat buddy.
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_result( first_strategy, second_strategy )
worst_than = { "r"=>"s", "s"=>"p", "p"=>"r" } # Hash of rock/paper/scissors rules
worst_than[second_strategy[1].downcase] == first_strategy[1].downcase ? second_strategy : first_strategy
end
def rps_game_winner( game )
raise WrongNumberOfPlayersError unless game.length == 2
raise NoSuchStrategyError unless game[0].length == 2 && game[1].length == 2
raise NoSuchStrategyError unless game[0][1].downcase.match(/^[rps]$/) && game[1][1].downcase.match(/^[rps]$/)
rps_result( game[0], game[1] )
end
def rps_tournament_winner( tournament )
if tournament[0][0].is_a? String
rps_game_winner tournament
else
rps_game_winner( [ rps_tournament_winner( tournament[0] ), rps_tournament_winner( tournament[1] ) ] )
end
end
def main
# Testing winner of a single game
single_game = [ [ "Armando", "P" ], [ "Dave", "S" ] ]
puts rps_game_winner single_game
# Testing winner of the all tournament !
tournament = [
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
]
puts rps_tournament_winner tournament
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment