我需要实现一个石头剪刀布锦标赛模拟器,它将玩所有回合并返回锦标赛。
这是我的锦标赛数组:
tournament = [
[
[
["Armando", "P"], ["Dave", "S"]
],
[
["Richard", "R"], ["Michael", "S"]
],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
]
例如 ["Richard", "R"], ["Michael", "S"] 代表理查德玩剪刀石头布的游戏。理查德应该是获胜者并晋级下一轮。
我正在尝试为此实现递归函数,但我无法正确设置数组
这是我的方法:
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
我已经为排位赛的获胜者发明了很多阵列,但无法找到解决方案。希望您能够帮助我。谢谢你。
编辑:
这是我在方法的每次迭代中应该得到的
第一轮过后
tournament = [
[
[
["Dave", "S"], ["Richard", "R"]
],
[
["Allen", "S"], ["Richard X.", "P"]
]
]
]
第二轮过后:
tournament = [
[
[
["Richard", "R"] , ["Allen", "S"]
]
]
]
优胜者
["Richard", "R"]
编辑:2
这是完整的代码: http: //pastebin.com/gjKfiWLD