http://www.perlmonks.org?node_id=301110

in reply to Round robin tournament problem

CiceroLove,
I only see an easy solution for for 2n players. I have provided working brute-force code for those cases.
```#!/usr/bin/perl -w
use strict;

my @name = qw(a b c d e f g h);
die "You must have an even number of players" if @name % 2;

my %tournament;
\$tournament{\$_} = [] for @name;

for my \$round (1 .. \$#name) {
\$round--;
for my \$player (@name) {
next if \$tournament{\$player}->[\$round];
for my \$opponent (@name) {
next if \$opponent eq \$player;
next if \$tournament{\$opponent}->[\$round];
next if @{\$tournament{\$player}} && grep /^\$opponent\$/ , @{
+\$tournament{\$player}};
\$tournament{\$player}->[\$round] = \$opponent;
\$tournament{\$opponent}->[\$round] = \$player;
last;
}
}
}

for my \$round (1 .. \$#name) {
\$round--;
print "\n\nRound: \$round\n";
my %seen;
for my \$player (@name) {
next if \$seen{\$player};
print "\$player plays \$tournament{\$player}->[\$round]\n";
\$seen{\$tournament{\$player}->[\$round]} = 1;
}
}
Update: Originally, I stated that this was all that's possible, but as blokhead points out non 2n solutions with backtracking is possible.

Hope this gets you started - L~R

Replies are listed 'Best First'.
Re: Re: Round robin tournament problem
by waswas-fng (Curate) on Oct 21, 2003 at 23:56 UTC
It is possible just not a ballanced table, you will have a table like the following:
```Round 1
A-B
C-D
E Sits Out
Round 2
B-C
D-E
A Sits Out
...