tourney = [
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
],
]
That is a tournament that needs to be solved by the following code:
def self.winner(player1, player2)
p1c = player1.last.downcase
p2c = player2.last.downcase
unless RPS.include?(p1c) && RPS.include?(p2c)
raise NoSuchStrategyError, "Strategy must be one of R,P,S"
end
if p1c!=p2c
case p1c
when "r"
p2c=="s" ? player1 : player2
when "p"
p2c=="r" ? player1 : player2
when "s"
p2c=="p" ? player1 : player2
end
else
player1
end
end
Now the base case is, of course,
def self.tournament_winner(tournament)
if tournament.size == 2
self.winner(tournament[0], tournament[1])
else
#WORK HERE
end
end
But in the else how can I get the first array set being the array containing "Armando" and "Dave" check who wins then continue, it needs to be used on arbitrary sized arrays. is there a way to go through the elements and filter the wins; i.e., on the first instance of recursion it should return:
tourney = [
[
["Dave", "S"],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
],
]
I'm sorry if my wording's bad. I've been trying to solve this for a while and its getting late and I lack sense.