tourney = [
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
],
]
那是一场锦标赛,需要通过以下代码来解决:
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
现在的基本情况当然是,
def self.tournament_winner(tournament)
if tournament.size == 2
self.winner(tournament[0], tournament[1])
else
#WORK HERE
end
end
但是在其他方法中,我如何才能获得第一个数组集是包含“Armando”和“Dave”的数组,检查谁获胜然后继续,它需要用于任意大小的数组。有没有办法遍历元素并过滤胜利?即,在递归的第一个实例中,它应该返回:
tourney = [
[
["Dave", "S"],
[ ["Richard", "R"], ["Michael", "S"] ]
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
],
]
如果我的措辞不好,我很抱歉。我一直在尝试解决这个问题,但它已经晚了,我缺乏理智。