Skip to content

Instantly share code, notes, and snippets.

@feiskyer
Created March 3, 2012 06:36
Show Gist options
  • Select an option

  • Save feiskyer/1964724 to your computer and use it in GitHub Desktop.

Select an option

Save feiskyer/1964724 to your computer and use it in GitHub Desktop.
Berkeley SaaS class HW1 Rock-Paper-Scissors
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; 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)
if compare_game?(game)
game[0]
else
game[1]
end
else
raise NoSuchStrategyError
end
end
def rps_tournament_winner(game)
if game[0][1].class==String
rps_game_winner(game)
else # 迭代
a1=rps_tournament_winner(game[0])
a2=rps_tournament_winner(game[1])
rps_tournament_winner([a1,a2])
end
end
p rps_tournament_winner([[[["Armando", "P"], ["Dave", "S"]], [["Richard", "R"], ["Michael", "S"]]], [[["Allen", "S"], ["Omer", "P"]], [["David E.", "R"], ["Richard X.", "P"]]]])
p rps_game_winner([ [ "Armando", "P" ], [ "Dave", "S" ] ] )
@feiskyer
Copy link
Copy Markdown
Author

feiskyer commented Mar 4, 2012 via email

@viller239
Copy link
Copy Markdown

accidentally faced your code

just want you to notice, compare_game? could be written as
(game[0][1] + game[1][1]) =~ /rs|sp|pr|rr|ss|pp/i

@feiskyer
Copy link
Copy Markdown
Author

feiskyer commented Mar 5, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment