Advertisement
Guest User

Untitled

a guest
Jan 17th, 2013
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.75 KB | None | 0 0
  1. class WrongNumberOfPlayersError < StandardError ; end
  2. class NoSuchStrategyError < StandardError ; end
  3.  
  4. def rps_result(m1, m2)
  5.   p1 = m1[1].upcase
  6.   p2 = m2[1].upcase
  7.   case p1
  8.   when 'R'
  9.     if p2 == 'S'
  10.         m1
  11.     else if p2 == 'R'
  12.         m1
  13.     else
  14.       m2   
  15.     end  
  16.     end
  17.   when 'S'
  18.     if p2 == 'S'
  19.         m1
  20.     else if p2 == 'R'
  21.         m2
  22.     else
  23.       m1
  24.     end  
  25.     end
  26.    when 'P'
  27.      if p2 == 'S'
  28.         m2
  29.     else if p2 == 'R'
  30.         m1
  31.     else
  32.       m1
  33.       end
  34.     end  
  35.    end  
  36.        
  37.            
  38. end
  39.  
  40. def rps_game_winner(game)
  41.  valid_strategies = ['R','P','S']
  42.  raise WrongNumberOfPlayersError unless game.length == 2
  43.  
  44.  raise NoSuchStrategyError unless valid_strategies.include? game[0][1].upcase
  45.  raise NoSuchStrategyError unless valid_strategies.include? game[1][1].upcase
  46.  rps_result(game[0],game[1])
  47. end
  48.  
  49. def rps_tournament_winner(tournament)
  50.  
  51.   qualifying_round_winners = []
  52.    round_winners = []
  53.   # last round
  54.  
  55.   if tournament.length < 1
  56.     return
  57.   end
  58.  
  59.   tournament.each_with_index do |round,i|  
  60.  puts round.inspect
  61.      qualifying_round_winners[i] = []
  62.      round_winners = []
  63.      round.each_with_index do |game,j|
  64.  
  65.          winner = rps_game_winner(game)
  66.          round_winners.push winner
  67.      end
  68.      qualifying_round_winners[i] = round_winners
  69.  
  70.   end
  71.  
  72.  
  73.   rps_tournament_winner(qualifying_round_winners)
  74. end
  75.  
  76.  
  77.  
  78. game = [ ["Armando", "r"], ["Dave", "r"]]
  79. tournament = [
  80.     [
  81.         [
  82.             ["Armando", "P"], ["Dave", "S"]
  83.         ],
  84.         [
  85.             ["Richard", "R"],  ["Michael", "S"]
  86.        
  87.         ],
  88.     ],
  89.     [
  90.         [ ["Allen", "S"], ["Omer", "P"] ],
  91.         [ ["David E.", "R"], ["Richard X.", "P"] ]
  92.     ]
  93. ]
  94.  
  95.  rps_tournament_winner tournament
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement