Update: Drat! I suspect Limbic~Region is right about 2**n. This solution fails for groups of 6 and 10. But for groups numbering in powers of two read on.
Unencumbered as I am by any proper background in such things, let me offer this...
For each round, for each player in succession who does not yet have an opponent in that round, we pick the first available unmatched person who this player has not played in a previous round. For convenience in screening the possibilities, we keep track of the matchings both ways ( A vs B *and* B vs A). and then weed out the dupes later.
You may or may not find this a bit verbose. Season to taste:
#!/usr/bin/perl
use warnings;
use strict;
{
my @round_aoh;
my @players = qw/John Jane Bill Suzi Bret Erin Eric Teri/;
my $rounds = @players - 1;
for my $round ( 1..$rounds ) {
for my $player ( @players ) {
next if $round_aoh[$round]{$player};
$round_aoh[$round]{$player} = 'pending';
my ($opponent) = grep {unmatched( $player, $_, \@round_aoh)}
grep {not $round_aoh[$round]{$_}} @players;
$round_aoh[$round]{$player } = $opponent;
$round_aoh[$round]{$opponent} = $player;
}
}
# Now, show what we did.
for my $round ( 1..$rounds ) {
print "Round $round\n";
for my $player ( keys %{$round_aoh[$round]} ) {
# crude hack to skip dupes: A vs B : B vs A
next unless $player gt $round_aoh[$round]{$player};
print " $player vs $round_aoh[$round]{$player}\n";
}
}
}
sub unmatched {
my ($player, $opponent, $round_arefoh) = @_;
for my $round ( 1..@$round_arefoh) {
if (defined $round_arefoh->[$round]{$player}
and $round_arefoh->[$round]{$player} eq $opponent) {
return 0;
}
}
return 1;
}
The rationalle seems right, the code runs, and the results look like they meet the spec. Good luck.
HTH,
David
------------------------------------------------------------
"Perl is a mess
and that's good because the
problem space is also a mess." - Larry Wall
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|