Created
July 28, 2012 13:45
-
-
Save prggmr/3193479 to your computer and use it in GitHub Desktop.
Round Robin Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Rotates an array for the round robin algorithm | |
*/ | |
function round_robin_array($array) | |
{ | |
// we always keep index 0 | |
$top = array_shift($array); | |
$last = array_pop($array); | |
$rotate = [$last]; | |
foreach ($array as $_value) { | |
$rotate[] = $_value; | |
} | |
array_unshift($rotate, $top); | |
return $rotate; | |
} | |
/** | |
* Runs a round robin to make a schedule. | |
*/ | |
function round_robin($players, $weeks) | |
{ | |
$schedule = []; | |
$count = count($players); | |
foreach ($players as $_p) { | |
$schedule[$_p] = array_fill(0, $weeks, []); | |
} | |
for ($i=0;$i<$weeks;$i++) { | |
for ($a=0;$a<($count / 2) + 1;$a++) { | |
$vs = $players[$a]; | |
$opp = $players[($count - $a) - 1]; | |
$at = rand(0,1); | |
$pg = [$opp, $at]; | |
$og = [$vs, $at]; | |
$schedule[$vs][$i] = $pg; | |
$schedule[$opp][$i] = $og; | |
} | |
$players = round_robin_array($players); | |
} | |
return $schedule; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment