Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class WrongNumberOfPlayersError < StandardError ; end
- class NoSuchStrategyError < StandardError ; end
- def rps_result(m1, m2)
- p1 = m1[1].upcase
- p2 = m2[1].upcase
- case p1
- when 'R'
- if p2 == 'S'
- m1
- else if p2 == 'R'
- m1
- else
- m2
- end
- end
- when 'S'
- if p2 == 'S'
- m1
- else if p2 == 'R'
- m2
- else
- m1
- end
- end
- when 'P'
- if p2 == 'S'
- m2
- else if p2 == 'R'
- m1
- else
- m1
- end
- end
- end
- end
- def rps_game_winner(game)
- valid_strategies = ['R','P','S']
- raise WrongNumberOfPlayersError unless game.length == 2
- raise NoSuchStrategyError unless valid_strategies.include? game[0][1].upcase
- raise NoSuchStrategyError unless valid_strategies.include? game[1][1].upcase
- rps_result(game[0],game[1])
- end
- def rps_tournament_winner(tournament)
- qualifying_round_winners = []
- round_winners = []
- # last round
- if tournament.length < 1
- return
- end
- tournament.each_with_index do |round,i|
- puts round.inspect
- qualifying_round_winners[i] = []
- round_winners = []
- round.each_with_index do |game,j|
- winner = rps_game_winner(game)
- round_winners.push winner
- end
- qualifying_round_winners[i] = round_winners
- end
- rps_tournament_winner(qualifying_round_winners)
- end
- game = [ ["Armando", "r"], ["Dave", "r"]]
- tournament = [
- [
- [
- ["Armando", "P"], ["Dave", "S"]
- ],
- [
- ["Richard", "R"], ["Michael", "S"]
- ],
- ],
- [
- [ ["Allen", "S"], ["Omer", "P"] ],
- [ ["David E.", "R"], ["Richard X.", "P"] ]
- ]
- ]
- rps_tournament_winner tournament
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement