Advertisement
jctt

rps_tournament_winner

Feb 21st, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.50 KB | None | 0 0
  1. class WrongNumberOfPlayersError < StandardError ; end
  2. class NoSuchStrategyError < StandardError ; end
  3.  
  4. def rps_game_winner(game)
  5.     raise WrongNumberOfPlayersError unless game.length == 2
  6.    
  7.     ng = []
  8.     game.each do |x|
  9.         if x.flatten.length == 16
  10.             x = rps_tournament_winner(x)
  11.         elsif x.flatten.length == 8
  12.             x = rps_result(x[0],x[1])
  13.         end
  14.         raise NoSuchStrategyError unless x[1].match(/[RPS]/i)
  15.         x[1] = x[1].upcase
  16.         ng << x
  17.     end
  18.     game = ng
  19.  
  20.    
  21.     result = game[1]   
  22.     if game[0][1] == game[1][1]
  23.         result = game[0]
  24.     elsif game[0][1] == "R"
  25.         result = game[0] unless game[1][1] == "P"
  26.     elsif game[0][1] == "P"
  27.         result = game[0] unless game[1][1] == "S"
  28.     else
  29.         result = game[0] unless game[1][1] == "R"
  30.     end
  31.    
  32.     return result
  33. end
  34.  
  35. def rps_result(m1, m2)
  36.     return rps_game_winner([rps_game_winner(m1),rps_game_winner(m2)])
  37. end
  38.  
  39. def rps_tournament_winner(tournament)
  40.     return nil unless tournament.kind_of?(Array)
  41.    
  42.     if tournament.flatten.length == 4      
  43.         rps_game_winner(tournament)
  44.     elsif tournament.flatten.length == 8
  45.         rps_result(tournament[0],tournament[1])
  46.     else
  47.         rps_game_winner(
  48.             [
  49.             rps_result(tournament[0][0],tournament[0][1]),
  50.             rps_result(tournament[1][0],tournament[1][1])
  51.             ]
  52.         )
  53.     end
  54. end
  55.  
  56. # x = [
  57.    # [
  58.        # [ ["Armando", "P"], ["Dave", "S"] ],
  59.        # [ ["Richard", "R"],  ["Michael", "S"] ],
  60.    # ],
  61.    # [
  62.        # [ ["Allen", "S"], ["Omer", "P"] ],
  63.        # [ ["David E.", "R"], ["Richard X.", "P"] ]
  64.    # ]
  65. # ]
  66.  
  67. # print rps_tournament_winner(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement