2015-06-27 65 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