当前位置:首页 > JavaScript

js实现赛程

2026-04-05 08:21:39JavaScript

实现赛程的基本思路

使用JavaScript实现赛程功能通常涉及生成比赛对阵表、处理轮次安排以及确保每支队伍公平比赛。常见的赛程类型包括单循环赛、双循环赛和淘汰赛。

单循环赛实现

单循环赛指每支队伍与其他队伍比赛一次。可以使用数组的轮转算法生成对阵表。

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

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

        // 轮转数组(固定第一支队伍)
        teams.splice(1, 0, teams.pop());
    }
    return schedule;
}

const teams = ['A', 'B', 'C', 'D'];
console.log(generateRoundRobin(teams));

双循环赛实现

双循环赛每支队伍与其他队伍比赛两次(主场和客场)。可以在单循环赛基础上扩展,交换主客场。

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

console.log(generateDoubleRoundRobin(teams));

淘汰赛实现

淘汰赛每轮淘汰一半队伍,直到决出冠军。可以使用递归或迭代方式生成对阵表。

function generateKnockout(teams) {
    const schedule = [];
    let currentTeams = [...teams];
    let round = 1;

    while (currentTeams.length > 1) {
        const matches = [];
        for (let i = 0; i < currentTeams.length; i += 2) {
            if (currentTeams[i + 1]) {
                matches.push({
                    home: currentTeams[i],
                    away: currentTeams[i + 1]
                });
            } else {
                // 轮空处理
                matches.push({
                    home: currentTeams[i],
                    away: null
                });
            }
        }
        schedule.push({ round, matches });
        currentTeams = matches.map(match => 
            Math.random() > 0.5 ? match.home : match.away
        );
        round++;
    }
    return schedule;
}

console.log(generateKnockout(teams));

赛程可视化

可以使用HTML和CSS将生成的赛程渲染为可视化的对阵表。

js实现赛程

<div id="schedule"></div>

<script>
function renderSchedule(schedule) {
    const container = document.getElementById('schedule');
    schedule.forEach(round => {
        const roundDiv = document.createElement('div');
        roundDiv.innerHTML = `<h3>Round ${round.round}</h3>`;
        round.matches.forEach(match => {
            roundDiv.innerHTML += `
                <div class="match">
                    ${match.home} vs ${match.away || 'BYE'}
                </div>
            `;
        });
        container.appendChild(roundDiv);
    });
}

const schedule = generateRoundRobin(['A', 'B', 'C', 'D']);
renderSchedule(schedule);
</script>

<style>
.match {
    padding: 8px;
    margin: 4px;
    background: #f0f0f0;
}
</style>

注意事项

  • 队伍数量为奇数时需要处理轮空(BYE)。
  • 实际应用中可能需要考虑主客场平衡、休息时间等复杂因素。
  • 大规模赛程建议使用专业算法库或后端服务生成。

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

相关文章

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与响应式…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

jquery.js

jquery.js

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

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…