more useful options PerlMonks

### Re: Round robin tournament problem

by Limbic~Region (Chancellor)
 on Oct 21, 2003 at 23:40 UTC ( #301110=note: print w/replies, xml ) Need Help??

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
...

-Waswas

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2019-10-16 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (42 votes). Check out past polls.

Notices?