Skip to content

Instantly share code, notes, and snippets.

@omerida
Last active December 30, 2015 03:59
Show Gist options
  • Save omerida/7773008 to your computer and use it in GitHub Desktop.
Save omerida/7773008 to your computer and use it in GitHub Desktop.
Simplified World Cup draw simulator
<?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();
}
@edgarsandi
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment