本文主要是介绍js 生成单循环和单淘汰对阵分组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很多比赛项目的赛制分为单循环和单淘汰赛,单循环就是小组中的每个成员互相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.lengthfor (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在进行分组,大家如果用的话可以改造一下哦
这篇关于js 生成单循环和单淘汰对阵分组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!