当前位置:首页 > 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 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

js 实现分页

js 实现分页

实现分页的基本逻辑 分页功能通常需要后端返回数据总量或总页数,前端根据当前页码和每页条数截取对应数据。以下是一个基于JavaScript的简单分页实现方案: 前端分页实现 假设已有从后端获取的完整数…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的J…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

vue实现js休眠

vue实现js休眠

实现 JavaScript 休眠的方法 在 Vue 中实现 JavaScript 休眠(延迟执行)可以通过以下方式实现。由于 JavaScript 本身没有内置的 sleep 函数,通常使用 Prom…