mod@masscomp.ccur.com (2915) (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. A "phantom" player may be created in case there is an odd number of players, and the phantom is scheduled during every round just like any other player. Of course, the "real" player paired with the phantom during any particular round is actually idle during that round. The "real" player would probably win that round... So, if the program were called "roundRobin" and you had the names of the players in the file "playerList" you'd say something like: # cat playerList Moe Larry Curly Agnes Velma Gertrude # roundRobin <playerList ROUND 01: Curly plays Agnes Larry plays Velma Moe plays Gertrude ROUND 02: Larry plays Agnes Moe plays Velma Curly plays Gertrude ROUND 03: Larry plays Curly Moe plays Agnes Velma plays Gertrude ROUND 04: Moe plays Curly Agnes plays Velma Larry plays Gertrude ROUND 05: Moe plays Larry Curly plays Velma Agnes plays Gertrude If there is enough interest, I'll post results. Thanks, ---------------------------------------------------------------------------- Michael O'Donnell (508)392-2915 home(508)251-7576 fax(508)692-8307 ___________ / ________/__ ...!{harvard,uunet,petsd}!masscomp!mod /__/_______/ / mod@westford.ccur.com Concurrent /__________/ Computer Corporation 1 Technology Way Westford, MA 01886 DISCLAIMER: My opinions only coincidentally resemble those of my employer. ----------------------------------------------------------------------------
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