我希望能够为淘汰赛生成空白比赛。这里有一个例子说明了我的意思。
假设我们有一个有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支球队的列表,将生成以下matches数组:
[
[ [], [], [], [] ],
[ [], [] ],
[ [] ]
]
有没有人知道该怎么做?到目前为止,我只有以下几点:
for(var i = 0; i < numRounds; i++) {
matches.push([]);
}
这会生成锦标赛的每一轮比赛,所以对于8支球队,它会生成一个长度为3的数组,但我不知道如何在每轮比赛中生成必要的匹配数。
发布于 2013-03-14 02:00:42
这将为给定数量的团队生成一个空的匹配数组:
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的幂的团队计数。它确实会为再见回合生成一个空位(当团队数量为奇数时)。
发布于 2019-08-20 19:16:41
假设任何一支没有配对的球队已经获得了下一轮的参赛资格
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;
}
发布于 2019-11-24 00:45:26
这将为每个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))
https://stackoverflow.com/questions/15392681
复制相似问题