JavaScript-为淘汰赛创建数组 我希望能够为淘汰赛提供空白比赛。这是我的意思的一个例子
假设我们有一场8支球队的淘汰赛: 我使用JavaScript-为淘汰赛创建数组 我希望能够为淘汰赛提供空白比赛。这是我的意思的一个例子,javascript,arrays,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队锦标赛的
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] ]
for(var i = 0; i < numRounds; i++) {
matches.push([]);
}
我正在尝试生成一些代码,这意味着我可以获得一个球队列表,并为锦标赛生成一组空白比赛
因此,如果我将8支球队的列表传递给锦标赛,将生成以下比赛数组:
[
[ [], [], [], [] ],
[ [], [] ],
[ [] ]
]
有人知道怎么做吗?到目前为止,我只有以下几点:
[ [team1,team2], [team3,team4] ]
for(var i = 0; i < numRounds; i++) {
matches.push([]);
}
for(变量i=0;i
这将生成锦标赛的每一轮,因此对于8支球队,它将生成一个长度为3的数组,但我不知道如何在每轮中生成必要数量的比赛。这将为给定数量的球队生成一个空的比赛数组:
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;
}
函数类型匹配(nTeams){
var matchArray=[];
而(nTeams>1){
nTeams=(nTeams+1)>>1;
var匹配=[];
对于(变量i=0;i
它应该正确处理不是2的幂的团队计数。它会为轮次生成一个插槽(当团队数量为奇数时)。这会为给定数量的团队生成一个空的匹配数组:
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;
}
函数类型匹配(nTeams){
var matchArray=[];
而(nTeams>1){
nTeams=(nTeams+1)>>1;
var匹配=[];
对于(变量i=0;i
它应该正确处理不是2的幂的团队计数。它确实会为轮次(当有奇数个队时)生成一个时段。假设没有与任何队配对的任何队已经有资格进入下一轮
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;
}
假设没有与任何球队配对的任何球队已经有资格进入下一轮
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;
}
功能匹配(TotalTeam){
让团队=整体团队;
让匹配项=[];
让extraTeam=0;
while(团队>1){
如果(团队%2==1){
团队=((团队-1)/2);
外部团队=外部团队+1
比赛。推(队);
}
否则{
团队=((团队)/2);
比赛。推(队);
}
如果(团队===1){
常数加=(a,b)=>a+b;
const totalMatches=matches.reduce(add);
返回(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支球队将进行比赛
64支球队将进行比赛
69支球队将进行比赛
82支球队将进行比赛
98支球队将进行比赛
功能匹配(TotalTeam){
让团队=整体团队;
让匹配项=[];
让extraTeam=0;
while(团队>1){
如果(团队%2==1){
团队=((团队-1)/2);
外部团队=外部团队+1
比赛。推(队);
}
否则{
团队=((团队)/2);
比赛。推(队);
}
如果(团队===1){
常数加=(a,b)=>a+b;
const totalMatches=matches.reduce(add);
返回(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支球队将进行比赛
64支球队将进行比赛
69支球队将进行比赛
82支球队将进行比赛
98支球队将进行比赛,这将为你在淘汰赛中的每一次“再见”以及“是”的人数提供一个机会。
如果你想知道第一轮的比赛次数
no.of matches in first round = (teamcount - byecount)/2
const tournamentArray=teamscont=>{
让totalMatches=TeamScont-1;
让byeCount=0;
设matchStructure=[];
让log2=Math.log2(TeamScont);
设floorValue=Math.floor(log2);
如果(log2>楼层价值){
设tempPowerHolder=Math.pow(2,floorValue+1);
让匹配项=[];
byeCount=tempPowerHolder-TeamScont;
TeamScont=tempPowerHolder/2;
for(设i=0;i1){
TeamScont=(TeamScont+1)>>1;
让匹配项=[];
for(设i=0;i console.log(tournamentArray(55))
这将为您在淘汰赛中的每个BYE
以及BYE
数量提供一个插槽。
如果你想知道第一轮的比赛次数
no.of matches in first round = (teamcount - byecount)/2
const tournamentArray=teamsCou