Since your requirements don't prohibit teams playing against
another team multiple times, you can just shuffle the teams
and pick them two-by-two. Here's one way:
use strict;
use warnings;
my @teams = qw {
Mice
Squirrels
Snails
Fish
Frogs
Bees
Sparrows
Snakes
Camels
Leeches
};
sub shuffle {
foreach (my $i = @_; -- $i;) {
my $r = rand ($i + 1);
@_ [$i => $r] = @_ [$r => $i];
}
@_;
}
my $rounds = shift || 5;
foreach (1 .. $rounds) {
my @pairing = shuffle @teams;
while (@pairing) {
my ($home, $away) = splice @pairing => 0, 2;
printf "%20s - %20s\n" => $home, $away;
}
print "---\n";
}
__END__
Snakes - Snails
Sparrows - Mice
Bees - Frogs
Fish - Leeches
Camels - Squirrels
---
Leeches - Frogs
Mice - Snails
Fish - Camels
Squirrels - Snakes
Sparrows - Bees
---
Bees - Squirrels
Mice - Leeches
Frogs - Sparrows
Snails - Snakes
Camels - Fish
---
Frogs - Sparrows
Camels - Squirrels
Snails - Bees
Fish - Mice
Snakes - Leeches
---
Mice - Camels
Fish - Snails
Leeches - Frogs
Squirrels - Bees
Sparrows - Snakes
---
Abigail |