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.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在进行分组,大家如果用的话可以改造一下哦

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值