2015-06-27 62 views
2

对于最少8支球队和最多18支球队的比赛,我必须确定比赛日历。锦标赛有17轮或比赛日。所以每个团队每个比赛日必须遇到另一个团队。如果只有不到18支球队的遭遇可以重复,那么一支球队可以不止一次对阵另一个球队。我应该如何用Javascript解决这个组合场景?

This is an example for 18 teams tournament.And this would be a case for less than 18 teams fixture, here in particular 9 teams

所以,我得做排列,然后安排他们在不同的回合。我已经试过:

组合:

function k_combinations(set, k) { 
    var i, j, combs, head, tailcombs; 

    if (k > set.length || k <= 0) { 
     return []; 
    } 

    if (k == set.length) { 
     return [set]; 
    } 

    if (k == 1) { 
     combs = []; 
     for (i = 0; i < set.length; i++) { 
      combs.push([set[i]]); 
     } 
     return combs; 
    } 

    combs = []; 
    for (i = 0; i < set.length - k + 1; i++) { 
     head = set.slice(i, i+1); 
     tailcombs = k_combinations(set.slice(i + 1), k - 1); 
     for (j = 0; j < tailcombs.length; j++) { 
      combs.push(head.concat(tailcombs[j])); 
     } 
    } 
    return combs; 
} 

var teams = [ {name: 'Real Madrid'}, 
       {name: 'Las Palmas'}, 
       {name: 'Alavés'}, 
       {name: 'Valencia'}, 
       {name: 'Sevilla'}, 
       {name: 'Betis'}, 
       {name: 'Córdoba'}, 
       {name: 'Deportivo'}, 
       {name: 'Atlético de Madrid'}, 
       {name: 'Levante'}, 
       {name: 'Rayo Vallecano'}, 
       {name: 'Athletic Bilbao'}, 
       {name: 'Osasuna'}, 
       {name: 'Zaragoza'}, 
       {name: 'Villareal'}, 
       {name: 'Racing de Santander'}, 
       {name: 'Espanyol'}, 
       {name: 'Cádiz'}, 
       ]; 
// Compute whole encounters combinations. 
var seasonMatches = k_combinations(teams,2); 

大红大紫的组合安排:

var calendar = {}; 
for (var i = 0; i<17; i++) { 
    calendar[i+1] = []; 
} 
var encounters = seasonMatches; 

for (var i = 0; i<Object.keys(calendar).length; i++) { 

    encounters.map(function (match,index) { 

     if (! _.any(calendar, function (m) { 

      return m[0].name === match[0].name || m[1].name === match[1].name || m[0].name === match[1].name || m[1].name === match[0].name; 
     })) { 
      calendar[i+1].push(match); 
     } 
    }); 
} 

我使用lodash简化任何遭遇是否存在等检查在前面回合。

我得到的问题是,这种方式我得到了相同的日历中的每一个相遇。而且,如果我在seasonMatch中添加拼接,则每轮最终都会有不同的匹配。

I've got a fiddle with this example shown above. 我应该如何解决这个问题?

回答

1

好像你喜欢努力工作:)还有一个更简单的方法(jsbin link):

var teamsCount = 9; 
var matchDays = 17; 
var matches = []; 
var teams = _.shuffle(_.range(teamsCount)); 
while(matches.length < matchDays){ 
    var newMatches = _(teams).chunk(2).partition(function(match){ 
    return match.length === 2; 
    }).value(); 
    matches = matches.concat(newMatches[0]); 
    if(newMatches[1].length) { // one team was left out, let's make sure it is playing 
    // we put it first, and add the other teams, shuffled, without that one team 
    teams = newMatches[1][0].concat(_.shuffle(_.without(_.range(teamsCount), newMatches[1][0][0]))); 
    } else { 
    teams = _.shuffle(_.range(teamsCount)); 
    } 
} 

// we might get more then we need 
matches = _.take(matches, matchDays); 

_.each(matches, function(match, index){ 
    console.log('round ' + index + ': ' + match); 
}); 

说明: 既然你没有施加其他限制(例如,每队必须发挥各其他),足够带领球队,将他们洗牌并一次将他们分成两块(=一场比赛)。然后,我们将这些块分成真正的比赛(2队阵列)和左边界(1队阵列)。

我们把真实的匹配并将它们添加到现有的匹配。如果我们留下了过剩,我们保留它,并且将它们连接到洗牌团队(没有剩下的团队)并再次分块。我们继续,直到我们有足够的比赛。因为我们可能会得到更多的比赛,所以我们只需要前17名。

JSbin更精细,并将匹配转换为团队名称。

我想看看你的代码,看看为什么你得到你所表现出的模式,但它太复杂,我明白了,我喜欢做的事情简单的方法;-)

+1

感谢的人!晚会很晚,但我很感激 – diegoaguilar