Created
March 3, 2012 06:36
-
-
Save feiskyer/1964724 to your computer and use it in GitHub Desktop.
Berkeley SaaS class HW1 Rock-Paper-Scissors
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 | |
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" ] ] ) |
Author
feiskyer
commented
Mar 4, 2012
via email
Line 40
…On Sun, Mar 4, 2012 at 10:24 PM, oppih < ***@***.*** > wrote:
How about the raise NoSuchStrategyError ?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1964724
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
Indeed, thanks.
…On Mon, Mar 5, 2012 at 12:35 AM, Anton < ***@***.*** > wrote:
# 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
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1964724
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment