Round-robin tournament schedule: Difference between revisions

Added solution for Pascal
(Added solution for Pascal)
Line 960:
Round 4: ( 1 vs 3 ) ( 4 vs 2 ) ( 5 vs 0 )
Round 5: ( 1 vs 2 ) ( 3 vs 0 ) ( 4 vs 5 )
</pre>
 
=={{header|Pascal}}==
A console program in Free Pascal. Uses the circle method, but not exactly as in Wikipedia: the fixed player is the dummy, if present, otherwise the last; and the rotation is anticlockwise.
<syntaxhighlight lang="pascal">
program RoundRobin;
(*
Rosetta Code: write list of matches in a round robin tournament.
Command line:
RoundRobin number_of_players
*)
{$mode objfpc}{$H+}
 
uses SysUtils;
 
var
nrPlayers, round : integer;
n, m, c, j, k : integer;
a : array of integer;
 
// Write the matches in a round, formatting nicely
procedure WriteRound();
var
t, u : integer;
begin
Write( 'Round', round:3, ': ');
u := 0;
for t := 0 to m - 2 do begin
Write( '(', a[u]:2); inc(u);
Write( ' v', a[u]:3, ') '); inc(u);
end;
Write( '(', a[u]:2); // u = n - 2
if c > 0 then
WriteLn( ' v', c:3, ')')
else
WriteLn( ' bye)');
end;
 
begin
if ParamCount < 1 then begin
WriteLn( 'Number of players is required');
exit;
end;
nrPlayers := SysUtils.StrToIntDef( ParamStr(1), -1);
// if string can't be converted, nrPlayers := -1
if (nrPlayers < 2) then begin
WriteLn( 'Invalid number of players');
exit;
end;
WriteLn( 'Round robin with ', nrPlayers, ' players');
m := (nrPlayers + 1) div 2;
n := 2*m;
if Odd( nrPlayers) then c := 0 // dummy player, opponent gets a bye
else c := n; // genuine player
SetLength( a, n);
k := 0;
for j := 0 to m - 2 do begin
a[k] := m - j; inc(k);
a[k] := m + 1 + j; inc(k);
end;
a[k] := 1;
a[n - 1] := c; // a[n - 1] stays = c throughout
round := 1;
WriteRound();
for round := 2 to n - 1 do begin
for j := 0 to n - 2 do begin // increment all entries except a[n - 1]
inc(a[j]);
if a[j] = n then a[j] := 1; // wrap round if necessary
end;
WriteRound();
end;
end.
</syntaxhighlight>
{{out}}
<pre>
Round robin with 12 players
Round 1: ( 6 v 7) ( 5 v 8) ( 4 v 9) ( 3 v 10) ( 2 v 11) ( 1 v 12)
Round 2: ( 7 v 8) ( 6 v 9) ( 5 v 10) ( 4 v 11) ( 3 v 1) ( 2 v 12)
Round 3: ( 8 v 9) ( 7 v 10) ( 6 v 11) ( 5 v 1) ( 4 v 2) ( 3 v 12)
Round 4: ( 9 v 10) ( 8 v 11) ( 7 v 1) ( 6 v 2) ( 5 v 3) ( 4 v 12)
Round 5: (10 v 11) ( 9 v 1) ( 8 v 2) ( 7 v 3) ( 6 v 4) ( 5 v 12)
Round 6: (11 v 1) (10 v 2) ( 9 v 3) ( 8 v 4) ( 7 v 5) ( 6 v 12)
Round 7: ( 1 v 2) (11 v 3) (10 v 4) ( 9 v 5) ( 8 v 6) ( 7 v 12)
Round 8: ( 2 v 3) ( 1 v 4) (11 v 5) (10 v 6) ( 9 v 7) ( 8 v 12)
Round 9: ( 3 v 4) ( 2 v 5) ( 1 v 6) (11 v 7) (10 v 8) ( 9 v 12)
Round 10: ( 4 v 5) ( 3 v 6) ( 2 v 7) ( 1 v 8) (11 v 9) (10 v 12)
Round 11: ( 5 v 6) ( 4 v 7) ( 3 v 8) ( 2 v 9) ( 1 v 10) (11 v 12)
 
Round robin with 9 players
Round 1: ( 5 v 6) ( 4 v 7) ( 3 v 8) ( 2 v 9) ( 1 bye)
Round 2: ( 6 v 7) ( 5 v 8) ( 4 v 9) ( 3 v 1) ( 2 bye)
Round 3: ( 7 v 8) ( 6 v 9) ( 5 v 1) ( 4 v 2) ( 3 bye)
Round 4: ( 8 v 9) ( 7 v 1) ( 6 v 2) ( 5 v 3) ( 4 bye)
Round 5: ( 9 v 1) ( 8 v 2) ( 7 v 3) ( 6 v 4) ( 5 bye)
Round 6: ( 1 v 2) ( 9 v 3) ( 8 v 4) ( 7 v 5) ( 6 bye)
Round 7: ( 2 v 3) ( 1 v 4) ( 9 v 5) ( 8 v 6) ( 7 bye)
Round 8: ( 3 v 4) ( 2 v 5) ( 1 v 6) ( 9 v 7) ( 8 bye)
Round 9: ( 4 v 5) ( 3 v 6) ( 2 v 7) ( 1 v 8) ( 9 bye)
</pre>
 
113

edits