务实地管理淘汰赛

时间:2015-01-01 10:13:00

标签: ruby

我正在Ruby中创建一个库,用Rails管理一组锦标赛风格(主要用于我为另一组建立的项目)。我希望图书馆能够处理多种锦标赛风格(即单次淘汰赛,双淘汰赛,循环赛),但不幸的是,我无法真正理解整个赛事。

首先,我们假设所有类TournamentMatchTeam都已定义。我需要为可变数量创建单个淘汰赛(我们假设这里有17个)。在创建锦标赛时,我需要创建锦标赛中的所有Match es并将它们存储在实例变量@matches中,并使用相应的Match es和{{1}作为来源。当我拨打Team时,我会给它一个Tournament.create的数组,这些数据将在锦标赛中出现。如果Team的内容如此,我需要在Tournament#create_single_elimination中做什么:

Tournament.create
可以使用module Tournament def self.create(teams:, type: :single) tournament = Tournament.new(type) case type when :single tournament.create_single_elimination(teams) # ... else # ... end end def create_single_elimination(teams) # ??? end end 方法创建

Match es,并且为了引用#new的来源,您只需添加到名为Match的数组中Match

#sources

匹配来源可以是match.sources << team Match,但除了这两个外,其他任何内容都不能。

1 个答案:

答案 0 :(得分:1)

这样的事情对我有用

Team = Struct.new(:name)
Match = Struct.new(:sources)

def matches(sources)
  this_round = sources.each_slice(2).collect {|pair| Match.new(pair)}
  if this_round.length > 1
    this_round + matches(this_round)
  else
    this_round
  end
end

这一次建立一轮比赛..

在执行此操作之前,您可能需要考虑改组团队,以及如果团队数量不是2的幂,您还想要做什么(此时您可以将此代码的内容解释为自动执行赢得任何没有对手的人,但这可能不是你想要做的事情,而且比其他事情更偶然)