当前位置:首页 > 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表格:

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
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swipe…

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 j…

js 实现分页

js 实现分页

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

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js实现抽奖

js实现抽奖

实现抽奖功能的基本思路 抽奖功能的核心是随机选择奖项并展示结果。可以通过数组存储奖项,利用随机数生成索引,最后通过动画增强用户体验。 准备奖项数据 定义一个数组存储奖项信息,每个奖项可以包含名称、…