很多比赛项目的赛制分为单循环和单淘汰赛,单循环就是小组中的每个成员互相PK,取小组前n名进入下一轮。单淘汰是直接12PK 34PK 如果组内成员是单数则有一人轮空(如果有种子选手 则种子选手轮空) 胜者晋级 败者淘汰。
// 匹配单循环对阵
initGroupSort(index) {
let playerList = this.groupList[index]
for (let i = 0; i < playerList.length; i++) {
playerList[i].roundNum = i + 1
}
let schedule = []; // 存储比赛日程的数组
let n = playerList.length
for (let round = 0; round < n - 1; round++) {
// 当前轮的主场队伍编号
const homeTeam = round % n + 1;
// 为当前主场队伍添加比赛
for (let awayTeam = homeTeam + 1; awayTeam <= n; awayTeam++) {
// 如果awayTeam超出队伍数量,回到1开始编号
if (awayTeam > n) {
awayTeam = 1;
}
// 添加比赛到日程中
schedule.push({
homeTeam: playerList[homeTeam - 1],
awayTeam: playerList[awayTeam - 1]
});
}
}
console.log('schedule', schedule);
this.currentList = schedule
},
// 匹配单淘汰对阵
initExitSort(index) {
let playerList = this.groupList[index]
for (let i = 0; i < playerList.length; i++) {
playerList[i].roundNum = i + 1
}
let seedItem = ''
let currentList = [];
//奇数个选手--种子选手单独挑出来
for (let i = 0; i < playerList.length; i++) {
if (playerList.length % 2 != 0) {
if (playerList[i].isSeedPlayer) {
seedItem = {
homeTeam: playerList[i]
}
playerList.splice(i, 1)
}
}
}
// 遍历playerList并两两分组
for (let i = 0; i < playerList.length; i += 2) {
const group = playerList.slice(i, i + 2);
let groupFormat = {
homeTeam: group[0],
awayTeam: group[1]
}
currentList.push(groupFormat);
}
if (seedItem != '') {
currentList.push(seedItem)
}
console.log('currentList', currentList);
this.currentList = currentList
},
groupList的结构大概是[[],[],[],]这样子,所以函数里设置的还需要获取一下,到一个list在进行分组,大家如果用的话可以改造一下哦