JavaScript - 为淘汰赛创建阵列

时间:2013-03-13 17:46:42

标签: javascript arrays

我希望能够为淘汰赛锦标赛生成空白比赛。这是我的意思的一个例子。

假设我们有8支球队参加淘汰赛: 我使用Math.log(teamList.length)/Math.log(2)计算出比赛有3轮。

以下是计算每轮比赛数量的一般规则:

numberOfRounds = n    [ [2^n-1 matches], ...., [2^0 matches] ]

所以我从中知道,8场比赛将有3轮比赛,比赛将如下:

[ [4 matches], [2 matches], [1 match] ]

我应该指出每场比赛都存储为数组,所以例如8场比赛的半决赛可能如下所示:

[ [team1,team2], [team3,team4] ]

我正在尝试生成一些代码,这意味着我可以获取一个团队列表,并为锦标赛生成一组空白匹配。

因此,如果我将8个队伍的列表传递给锦标赛,则会生成以下匹配数组:

[
  [ [], [], [], [] ],
  [ [], [] ],
  [ [] ]
]

有没有人对如何做到这一点有任何想法?到目前为止,我只有以下内容:

for(var i = 0; i < numRounds; i++) {
      matches.push([]);
}

这会产生每轮比赛,因此对于8支球队,它会生成一个长度为3的数组,但我不知道如何在每轮比赛中产生必要数量的比赛。

4 个答案:

答案 0 :(得分:1)

这应该为给定数量的团队生成一个空匹配数组:

function genMatches (nTeams) {
    var matchArray = [];
    while (nTeams > 1) {
        nTeams = (nTeams + 1) >> 1;
        var matches = [];
        for (var i = 0; i < nTeams; ++i) {
            matches.push([]);
        }
        matchArray.push(matches);
    }
    return matchArray;
}

它应该正确处理不是2的权力的团队计数。它确实为再见轮次生成一个插槽(当有奇数个团队时)。

答案 1 :(得分:1)

假设是否有未与任何球队配对的球队有资格参加下一轮比赛

function genMatches(a, match = 0, stage = {}) {
  let isOdd = false
  if (a === 1) {
    return match;
  }
  if (a % 2) {
    isOdd = true;
    a = a - 1;
  }
  match = match + Math.floor(a / 2);

  stage[Object.keys(stage).length] = new Array(Math.floor(a / 2)).fill([[],[]])
  a = Math.floor(a / 2)
  if (isOdd)
    a = a + 1;
 stage = {...stage,... genMatches(a, match, stage)};
 return stage;
}

答案 2 :(得分:1)

这也会给您每个BYE的空位以及KnockOut锦标赛中BYES的数量。 如果您想获得第一轮比赛的比赛次数

no.of matches in first round = (teamcount - byecount)/2
total matches= teamcount -1

const tournamentArray = teamsCount => {
  let totalMatches = teamsCount - 1;
	let byeCount = 0;
	let matchStructure = [];
	let log2 = Math.log2(teamsCount);
	let floorValue = Math.floor(log2);
	if (log2 > floorValue) {
        let tempPowerHolder = Math.pow(2, floorValue + 1);
		let matches = [];
		byeCount = tempPowerHolder - teamsCount;
		teamsCount = tempPowerHolder / 2;
		for (let i = 0; i < teamsCount; ++i) {
			matches.push([]);
		}
		matchStructure.push(matches);
	}

	while (teamsCount > 1) {
		teamsCount = (teamsCount + 1) >> 1;
		let matches = [];
		for (let i = 0; i < teamsCount; ++i) {
			matches.push([]);
		}
		matchStructure.push(matches);
	}

	return {
		byeCount,
    totalMatches,
		matchStructure
	};
};

console.log(tournamentArray(55))

答案 3 :(得分:0)

 function matches(TotalTeams) {
        let teams = TotalTeams;
        let matches = [];
        let extraTeam = 0;

        while(teams > 1 ){
            if(teams % 2 === 1){
                teams = ((teams-1)/2);
                extraTeam  = extraTeam + 1
                matches.push(teams);
            }
            else{
                teams = ((teams)/2);
                matches.push(teams);
            }
            if(teams === 1){
                const add = (a, b) => a + b;
                const totalMatches = matches.reduce(add);
                return (totalMatches + extraTeam)
                
            }
           
        }
    }  
    
document.getElementById("33").innerHTML = matches(33);
document.getElementById("64").innerHTML = matches(64);
document.getElementById("69").innerHTML = matches(69);
document.getElementById("82").innerHTML = matches(82);
document.getElementById("98").innerHTML = matches(98);
   
33 teams will play <span id="33"></span> matches<br/>
64 teams will play <span id="64"></span> matches<br/>
69 teams will play <span id="69"></span> matches<br/>
82 teams will play <span id="82"></span> matches<br/>
98 teams will play <span id="98"></span> matches<br/>