mnito / round-robin
PHP 中的循环排列调度生成实现
v2.2.0
2021-09-06 22:46 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: 9.5.*
README
PHP 7 的循环排列调度生成参考实现,许可协议为 MIT 协议
特性
- 通过高效的循环排列轮换函数实现高效的调度生成
- 能够生成任意数量的轮次
- 通过为奇数个队伍添加轮空来支持任意数量的队伍
- 简单、简洁的实现,便于分析算法
- 单元测试
- 文档化
- 现代 PHP 7 代码
- 面向对象和过程式 API
基本用法
生成常用调度
生成一个随机调度,其中每个玩家与其他每个玩家只相遇一次
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $scheduleBuilder = new ScheduleBuilder($teams); $schedule = $scheduleBuilder->build();
或
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $schedule = schedule($teams);
使用 $rounds 整数参数生成一个随机的主场-客场调度,其中每个玩家与其他每个玩家相遇两次,一次主场一次客场
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $rounds = (($count = count($teams)) % 2 === 0 ? $count - 1 : $count) * 2; $scheduleBuilder = new ScheduleBuilder($teams, $rounds); $schedule = $scheduleBuilder->build();
或
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $rounds = (($count = count($teams)) % 2 === 0 ? $count - 1 : $count) * 2; $schedule = schedule($teams, $rounds);
使用 $shuffle 布尔参数生成一个不随机洗牌的调度
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $scheduleBuilder = new ScheduleBuilder($teams); $scheduleBuilder->doNotShuffle(); $schedule = $scheduleBuilder->build();
或
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $schedule = schedule($teams, null, false);
使用 $seed 整数参数使用自己的种子进行预定洗牌
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $scheduleBuilder = new ScheduleBuilder($teams); $scheduleBuilder->shuffle(89); $schedule = $scheduleBuilder->build();
或
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $schedule = schedule($teams, null, true, 89);
遍历一个调度
遍历完整调度
设置
$teams = ['The 1st', '2 Good', 'We 3', '4ward']; $schedule = schedule($teams, null, true, 89);
或
$scheduleBuilder = new ScheduleBuilder(); $scheduleBuilder->setTeams($teams); $scheduleBuilder->setRounds(10); $scheduleBuilder->doNotShuffle(); $schedule = $scheduleBuilder->build();
遍历
<?php foreach($schedule as $round => $matchups){ ?> <h3>Round <?=$round?></h3> <ul> <?php foreach($matchups as $matchup) { ?> <li><?=$matchup[0] ?? '*BYE*'?> vs. <?=$matchup[1] ?? '*BYE*'?></li> <?php } ?> </ul> <?php } ?>
遍历队伍调度
<?php $scheduleBuilder = new ScheduleBuilder($teams, 10); $scheduleBuilder->shuffle(18); $schedule = $scheduleBuilder->build(); ?> <?php foreach($teams as $team) { ?> <h3><?=$team?></h3> <ol> <?php foreach($schedule($team) as $contest) { ?> <li><?=(($contest['home'] ? '' : '@').($contest['team'] ?? '*BYE*'))?></li> <?php } ?> </ol> <?php } ?>
许可协议
MIT 许可协议
作者
Michael P. Nitowski <mike@nitow.ski>