当前位置:首页 > JavaScript

js实现赛程

2026-03-02 07:17:15JavaScript

赛程实现的基本思路

使用JavaScript实现赛程安排需要考虑比赛轮次、对阵组合以及时间分配。常见的赛程类型包括单循环赛、双循环赛和淘汰赛,具体实现需根据实际需求调整。

单循环赛实现

单循环赛指每支队伍与其他队伍各比赛一次。以下是一个简单的实现示例:

function generateRoundRobin(teams) {
  const schedule = [];
  const numTeams = teams.length;
  const rounds = numTeams - 1;

  for (let round = 0; round < rounds; round++) {
    const roundMatches = [];
    for (let i = 0; i < numTeams / 2; i++) {
      const home = teams[i];
      const away = teams[numTeams - 1 - i];
      if (home && away) {
        roundMatches.push({ home, away, round });
      }
    }
    schedule.push(roundMatches);
    teams.splice(1, 0, teams.pop());
  }
  return schedule.flat();
}

双循环赛实现

双循环赛是在单循环基础上增加主客场互换:

function generateDoubleRoundRobin(teams) {
  const firstHalf = generateRoundRobin(teams);
  const secondHalf = firstHalf.map(match => ({
    home: match.away,
    away: match.home,
    round: match.round + teams.length - 1
  }));
  return [...firstHalf, ...secondHalf];
}

淘汰赛实现

淘汰赛适合需要快速决出冠军的场景:

function generateKnockout(teams) {
  const matches = [];
  let currentRound = [...teams];
  let roundNumber = 0;

  while (currentRound.length > 1) {
    const nextRound = [];
    for (let i = 0; i < currentRound.length; i += 2) {
      const match = {
        home: currentRound[i],
        away: currentRound[i + 1],
        round: roundNumber
      };
      matches.push(match);
      nextRound.push(`Winner of ${i}-${i+1}`);
    }
    currentRound = nextRound;
    roundNumber++;
  }
  return matches;
}

时间安排优化

为赛程添加时间属性可提高实用性:

function addScheduleDates(matches, startDate, daysBetweenRounds) {
  const date = new Date(startDate);
  let currentRound = -1;

  return matches.map(match => {
    if (match.round !== currentRound) {
      date.setDate(date.getDate() + daysBetweenRounds);
      currentRound = match.round;
    }
    return {
      ...match,
      date: new Date(date)
    };
  });
}

可视化输出

将生成的赛程渲染为HTML表格:

js实现赛程

function renderSchedule(schedule) {
  const table = document.createElement('table');
  const header = table.createTHead();
  const headerRow = header.insertRow();
  ['Round', 'Date', 'Home', 'Away'].forEach(text => {
    const th = document.createElement('th');
    th.textContent = text;
    headerRow.appendChild(th);
  });

  const body = table.createTBody();
  schedule.forEach(match => {
    const row = body.insertRow();
    [match.round, match.date.toDateString(), match.home, match.away].forEach(text => {
      const cell = row.insertCell();
      cell.textContent = text;
    });
  });
  document.body.appendChild(table);
}

实际应用建议

  1. 对于大型赛事应考虑使用专业算法库如round-robin-tournament
  2. 添加异常处理确保队伍数量为偶数时自动处理轮空情况
  3. 考虑实现持久化存储将赛程保存至数据库
  4. 前端框架用户可结合React/Vue实现动态交互式赛程表

以上实现可根据具体需求进行调整扩展,如添加比分记录、积分计算等功能。

标签: 赛程js
分享给朋友:

相关文章

js实现验证码

js实现验证码

实现验证码的JavaScript方法 生成随机验证码 使用Math.random()生成随机字符串,结合数字和字母: function generateCaptcha() { const cha…

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callbac…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现图片

js实现图片

图片加载与显示 在JavaScript中,可以通过Image对象动态加载图片。创建实例后设置src属性触发加载,通过onload回调处理加载完成后的操作: const img = new Ima…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…