jpn@genrad.com (John P. Nelson) (10/24/90)
>Given a list of the names of the players in a tournament (such >as golf or chess) I'd like sources to a program which calculates >the number of rounds required and the pairs of players scheduled >to play during each round. > >CONDITIONS: >- Every player must play every other player exactly once. >- Every player must have an opponent during every round. I took a shot at this, and implemented it using "perl". Depending on how you invoke perl on your system, you may need to modify the first few lines of this script. #! /usr/local/perl # roundrobin - take a list of player names, and create a roundrobin # tournament where each player plays every other player # exactly once, and each player is active each round. # collect the names, one per line. $maxlen = 0; $phantom = "__phantom__"; while (<>) { chop; push(players, $_); if (length > $maxlen) { $maxlen = length; } } die "Note enough players" if ($#players < 2); $format = sprintf("%%-%ds plays %%%ds\n", $maxlen, $maxlen); # make sure we have an even number of players. if (($#players & 1) == 0) { unshift(players, $phantom); } # make rotator with two arrays and a pivot $pivot = $players[0]; @list1 = @players[1..(($#players-1)/2)]; @list2 = @players[(($#players+1)/2)..$#players]; # perform the round robin for ($i = 1; $i <= $#players; ++$i) { print "\n Round $i:\n"; if ($pivot eq $phantom) { print "$list2[0] is idle\n"; } else { printf $format, $pivot, $list2[0]; } for ($j = 0; $j <= $#list1; ++$j) { printf $format, $list1[$j], $list2[$j+1]; } # rotate the players $temp = shift(list2); unshift(list1, $temp); $temp = pop(list1); push(list2, $temp); } --- john nelson uucp: {decvax,mit-eddie}!genrad!jpn domain: jpn@genrad.com