Perl: the Markov chain saw PerlMonks

### Re: Round robin tournament problem

by dvergin (Monsignor)
 on Oct 21, 2003 at 23:48 UTC Need Help??

in reply to Round robin tournament problem

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

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://301112]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
As of 2024-02-23 03:53 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (22 votes). Check out past polls.