Created
October 17, 2012 01:30
-
-
Save sumitngupta/3903230 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 throw_result(m1, m2) | |
| m1.upcase! | |
| m2.upcase! | |
| raise NoSuchStrategyError unless (m1 == "R" or m1 == "P" or m1 == "S") and (m2 == "R" or m2 == "P" or m2 == "S") | |
| if m1 == "R" and m2 == "R" | |
| return 1 | |
| elsif m1 == "R" and m2 =="P" | |
| return 2 | |
| elsif m1 == "R" and m2 =="S" | |
| return 1 | |
| elsif m1 == "P" and m2 == "R" | |
| return 1 | |
| elsif m1 == "P" and m2 =="P" | |
| return 1 | |
| elsif m1 == "P" and m2 =="S" | |
| return 2 | |
| elsif m1 == "S" and m2 == "R" | |
| return 2 | |
| elsif m1 == "S" and m2 =="P" | |
| return 1 | |
| elsif m1 == "S" and m2 =="S" | |
| return 1 | |
| end | |
| end | |
| def rps_game_winner(game) | |
| p " Game " | |
| p game | |
| raise WrongNumberOfPlayersError unless game.length % 2 == 0 | |
| if throw_result(game[0][1], game[1][1]) == 1 | |
| return game[0] | |
| else | |
| return game[1] | |
| end | |
| end | |
| def process_region(region) | |
| is_throw = region[0][0].is_a?(String) | |
| if is_throw | |
| winner = rps_game_winner(region) | |
| p "winner!" | |
| p winner | |
| return winner | |
| else | |
| region.map! do | subregion | | |
| p "Sub region thats being generated" | |
| p subregion | |
| subregion = process_region(subregion) | |
| end | |
| end | |
| end | |
| def rps_tournament_winner(tournament) | |
| return process_region(tournament) | |
| end | |
| game = [ | |
| [ | |
| [ | |
| ["Armando", "P"], ["Dave", "S"] | |
| ], | |
| [ | |
| ["Richard", "R"], ["Michael", "S"] | |
| ] | |
| ], | |
| [ | |
| [ | |
| ["Allen", "S"], ["Omer", "P"] | |
| ], | |
| [ | |
| ["Asshole", "R"], ["NotRetarded", "P"] | |
| ] | |
| ] | |
| ] | |
| p "START GAME" | |
| p rps_tournament_winner(game) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment