Last active
December 30, 2015 03:59
-
-
Save omerida/7773008 to your computer and use it in GitHub Desktop.
Simplified World Cup draw simulator
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]> | |
*/ | |
// teams organized by pot, 8 in each. | |
$pots = [ | |
['Brazil', 'Argentina', 'Colombia', 'Uruguay', 'Spain', 'Germany', 'Belgium', 'Switzerland'], | |
['Chile', 'Ecuador', 'Cote d\'Ivoire', 'Ghana', 'Algeria', 'Nigeria', 'Cameroon', 'France'], | |
['Japan', 'Iran', 'Korea Republic', 'Australia', 'USA', 'Mexico', 'Cost Rica', 'Honduras'], | |
['Netherlands', 'Italy', 'England', 'Portugal', 'Greece', 'Bosnia-Herzegovina', 'Croatia', 'Russia'], | |
]; | |
// 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
Hi Omerida,
I added a new FIFA rule in https://gist.github.com/edgarsandi/7810512
reference: http://www.fifa.com/worldcup/final-draw/news/newsid=2238933/index.html