-
-
Save edgarsandi/7810512 to your computer and use it in GitHub Desktop.
(New FIFA rule added)
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 | |
/* | |
* World Cup Draw Example. | |
* | |
* Using Array functions & SPL Iterators to simulate (simplified) World Cup draws. | |
* | |
* @author Oscar Merida <[email protected]> | |
* @author Edgar Sandi <[email protected]> (New FIFA rule added) | |
*/ | |
// Teams organized by pots, 8 in each, but the latter group contains 9 teams and the second contains 7 teams, | |
// a team will be drawn from the last group and added in the second group | |
$pots = [ | |
['Brazil', 'Argentina', 'Colombia', 'Uruguay', 'Spain', 'Germany', 'Belgium', 'Switzerland'], | |
['Chile', 'Ecuador', 'Cote d\'Ivoire', 'Ghana', 'Algeria', 'Nigeria', 'Cameroon'], | |
['Japan', 'Iran', 'Korea Republic', 'Australia', 'USA', 'Mexico', 'Cost Rica', 'Honduras'], | |
['Netherlands', 'Italy', 'England', 'Portugal', 'Greece', 'Bosnia-Herzegovina', 'Croatia', 'Russia', 'France'], | |
]; | |
shuffle($pots[3]); | |
array_push($pots[1], array_pop($pots[3])); | |
// shuffle teams within each pot and setup the multipleIterator to generate groups | |
$multiple = new MultipleIterator(); | |
foreach ($pots as $pot) { | |
shuffle($pot); | |
$multiple->attachIterator(new ArrayIterator($pot)); | |
} | |
// $multiple->current() returns an array of 4 teams. | |
// The first call to current() gives us the first element in each of the 4 pots, and so on. | |
// Calling next() advances the internal pointer to the next 4 teams in each of the 4 pots. | |
foreach (range('A', 'H') as $name) { | |
echo $name . ': ' . implode(', ', $multiple->current()) . PHP_EOL; | |
$multiple->next(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment