Round-robin tournament schedule: Difference between revisions
Content added Content deleted
imported>Thebeez |
(Added BASIC256 and Yabasic. Grouping BASIC dialects) |
||
Line 233: | Line 233: | ||
arr[2] = tmp |
arr[2] = tmp |
||
} |
} |
||
}</syntaxhighlight> |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre>1 is too few participants |
|||
<pre> |
|||
1 is too few participants |
|||
2 players |
2 players |
||
Line 294: | Line 292: | ||
round 11: 1 3 4 5 6 7 |
round 11: 1 3 4 5 6 7 |
||
2 12 11 10 9 8 |
2 12 11 10 9 8</pre> |
||
</pre> |
|||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="c++"> |
|||
{{trans|FreeBASIC}} |
|||
#include <algorithm> |
|||
<syntaxhighlight lang="qbasic">arraybase 1 |
|||
#include <cstdint> |
|||
print "Twelve teams" |
|||
#include <iomanip> |
|||
call roundrob(12) |
|||
#include <iostream> |
|||
print "Nine teams with byes" |
|||
#include <numeric> |
|||
call roundrob(9) |
|||
#include <stdexcept> |
|||
end |
|||
#include <vector> |
|||
function nob(n,i,byes) |
|||
void round_robin(uint32_t team_count) { |
|||
#helper function to allow byess to be printed intelligently |
|||
if ( team_count < 2 ) { |
|||
throw std::invalid_argument("Number of teams must be greater than 2: " + team_count); |
|||
} |
|||
if n > 9 then pad = " " else pad = "" |
|||
std::vector<uint32_t> rotating_list(team_count); |
|||
if n = i and byes then |
|||
std::iota(rotating_list.begin(), rotating_list.end(), 2); |
|||
return pad + "B" |
|||
if ( team_count % 2 == 1 ) { |
|||
else |
|||
rotating_list.emplace_back(0); |
|||
if i < 10 then return pad + string(i) else return string(i) |
|||
team_count++; |
|||
end if |
|||
} |
|||
end function |
|||
subroutine roundrob(n) |
|||
for ( uint32_t round = 1; round < team_count; ++round ) { |
|||
byes = 0 |
|||
std::cout << "Round " << std::setw(2) << round << ":"; |
|||
if n mod 2 = 1 then #if there is an odd number of competitors |
|||
std::vector<uint32_t> fixed_list(1, 1); |
|||
byes = 1 #make note of this fact |
|||
fixed_list.insert(fixed_list.end(), rotating_list.begin(), rotating_list.end()); |
|||
n += 1 #and treat the tournament as having one more competitor |
|||
for ( uint32_t i = 0; i < team_count / 2; ++i ) { |
|||
end if |
|||
std::cout << " ( " << std::setw(2) << fixed_list[i] |
|||
dim schd(n) |
|||
<< " vs " << std::setw(2) << fixed_list[team_count - 1 - i] << " )"; |
|||
for i = 1 to n |
|||
} |
|||
schd[i] = i #initial population of the array with numbers 1-n |
|||
std::cout << std::endl; |
|||
next i |
|||
std::rotate(rotating_list.rbegin(), rotating_list.rbegin() + 1, rotating_list.rend()); |
|||
for r = 1 to n-1 |
|||
} |
|||
print "Round "; rjust(string(r), 2); ": "; |
|||
} |
|||
for i = 1 to n/2 #print the pairings according to the scheme |
|||
#1 2 3 4 |
|||
int main() { |
|||
#5 6 7 8 |
|||
std::cout << "Round robin for 12 players:" << std::endl; |
|||
print "("; nob(n,schd[i],byes); " -"; nob(n,schd[i+n/2],byes); " ) "; |
|||
round_robin(12); |
|||
next i |
|||
std::cout << std::endl << std::endl; |
|||
print |
|||
std::cout << "Round robin for 5 players, 0 denotes a bye:" << std::endl; |
|||
#now move positions 2-n around clockwise |
|||
round_robin(5); |
|||
temp1 = schd[n/2] #need to track two temporary variables |
|||
} |
|||
temp2 = schd[n/2+1] |
|||
</syntaxhighlight> |
|||
for i = n/2 to 3 step -1 #top row |
|||
{{ out }} |
|||
schd[i] = schd[i-1] |
|||
<pre> |
|||
next i |
|||
Round robin for 12 players: |
|||
for i = n/2+1 to n-1 #bottom row |
|||
Round 1: ( 1 vs 12 ) ( 2 vs 11 ) ( 3 vs 10 ) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 ) |
|||
schd[i] = schd[i+1] |
|||
Round 2: ( 1 vs 11 ) ( 13 vs 10 ) ( 2 vs 9 ) ( 3 vs 8 ) ( 4 vs 7 ) ( 5 vs 6 ) |
|||
next i |
|||
Round 3: ( 1 vs 10 ) ( 12 vs 9 ) ( 13 vs 8 ) ( 2 vs 7 ) ( 3 vs 6 ) ( 4 vs 5 ) |
|||
schd[n] = temp1 #end ifll in the ones that "jumped" between rows |
|||
Round 4: ( 1 vs 9 ) ( 11 vs 8 ) ( 12 vs 7 ) ( 13 vs 6 ) ( 2 vs 5 ) ( 3 vs 4 ) |
|||
schd[2] = temp2 |
|||
Round 5: ( 1 vs 8 ) ( 10 vs 7 ) ( 11 vs 6 ) ( 12 vs 5 ) ( 13 vs 4 ) ( 2 vs 3 ) |
|||
next r |
|||
Round 6: ( 1 vs 7 ) ( 9 vs 6 ) ( 10 vs 5 ) ( 11 vs 4 ) ( 12 vs 3 ) ( 13 vs 2 ) |
|||
end subroutine</syntaxhighlight> |
|||
Round 7: ( 1 vs 6 ) ( 8 vs 5 ) ( 9 vs 4 ) ( 10 vs 3 ) ( 11 vs 2 ) ( 12 vs 13 ) |
|||
Round 8: ( 1 vs 5 ) ( 7 vs 4 ) ( 8 vs 3 ) ( 9 vs 2 ) ( 10 vs 13 ) ( 11 vs 12 ) |
|||
Round 9: ( 1 vs 4 ) ( 6 vs 3 ) ( 7 vs 2 ) ( 8 vs 13 ) ( 9 vs 12 ) ( 10 vs 11 ) |
|||
Round 10: ( 1 vs 3 ) ( 5 vs 2 ) ( 6 vs 13 ) ( 7 vs 12 ) ( 8 vs 11 ) ( 9 vs 10 ) |
|||
Round 11: ( 1 vs 2 ) ( 4 vs 13 ) ( 5 vs 12 ) ( 6 vs 11 ) ( 7 vs 10 ) ( 8 vs 9 ) |
|||
Round robin for 5 players, 0 denotes a bye: |
|||
Round 1: ( 1 vs 6 ) ( 2 vs 5 ) ( 3 vs 4 ) |
|||
Round 2: ( 1 vs 5 ) ( 0 vs 4 ) ( 2 vs 3 ) |
|||
Round 3: ( 1 vs 4 ) ( 6 vs 3 ) ( 0 vs 2 ) |
|||
Round 4: ( 1 vs 3 ) ( 5 vs 2 ) ( 6 vs 0 ) |
|||
Round 5: ( 1 vs 2 ) ( 4 vs 0 ) ( 5 vs 6 ) |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
==={{header|FreeBASIC}}=== |
||
<syntaxhighlight lang="freebasic">function nob( n as uinteger, i as uinteger, bye as boolean ) as string |
<syntaxhighlight lang="freebasic">function nob( n as uinteger, i as uinteger, bye as boolean ) as string |
||
'helper function to allow byes to be printed intelligently |
'helper function to allow byes to be printed intelligently |
||
Line 436: | Line 419: | ||
Round 8: ( 1 - 3) ( 4 - 2) ( 5 - 6) ( B - 7) ( 9 - 8) |
Round 8: ( 1 - 3) ( 4 - 2) ( 5 - 6) ( B - 7) ( 9 - 8) |
||
Round 9: ( 1 - 2) ( 3 - 6) ( 4 - 7) ( 5 - 8) ( B - 9) |
Round 9: ( 1 - 2) ( 3 - 6) ( 4 - 7) ( 5 - 8) ( B - 9) |
||
</pre> |
|||
==={{header|uBasic/4tH}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="ubasic-4th">Print "Twelve teams" |
|||
Proc _Roundrob(12) |
|||
Print |
|||
Print "Nine teams with byes" |
|||
Proc _Roundrob(9) |
|||
End |
|||
' helper function to allow byes to be printed intelligently |
|||
_Nob Param (3) : Return (Iif (a@ = b@ * c@, Dup(" B"), Str("_#", b@))) |
|||
_Roundrob |
|||
Param (1) |
|||
Local (5) |
|||
b@ = 0 |
|||
' if there is an odd number of competitors |
|||
If a@ % 2 = 1 Then b@ = 1 : a@ = a@ + 1 |
|||
' make note of this fact and treat the tournament |
|||
For d@ = 1 To a@ ' as having one more competitor |
|||
@(d@) = d@ ' initial population of the array with numbers 1-n |
|||
Next |
|||
For c@ = 1 To a@-1 ' print the pairings according to the scheme |
|||
Print Using "Round __: ";c@; |
|||
' 1 2 3 4 |
|||
For d@ = 1 To a@/2 ' 5 6 7 8 |
|||
Print "("; Show(FUNC(_Nob (a@, @(d@), b@)));" - "; |
|||
Print Show(FUNC(_Nob (a@, @(d@+a@/2), b@)));") "; |
|||
Next |
|||
Print ' now move positions 2-n around clockwise |
|||
e@ = @(a@/2) ' need to track two temporary variables |
|||
f@ = @(a@/2+1) |
|||
' top row |
|||
For d@ = a@/2 To 3 Step -1 |
|||
@(d@) = @(d@-1) |
|||
Next |
|||
' bottom row |
|||
For d@ = a@/2+1 To a@-1 |
|||
@(d@) = @(d@+1) |
|||
Next |
|||
@(a@) = e@ ' fill in the ones that "jumped" between rows |
|||
@(2) = f@ |
|||
Next |
|||
Return</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="vb">print "Twelve teams" |
|||
roundrob(12) |
|||
print "Nine teams with byes" |
|||
roundrob(9) |
|||
end |
|||
sub nob$(n,i,byes) |
|||
//helper sub to allow byess to be printed intelligently |
|||
//dim as string pad |
|||
if n > 9 then pad$ = " " else pad$ = "" : fi |
|||
if n = i and byes then |
|||
return pad$+"B" |
|||
else |
|||
if i < 10 then return pad$+str$(i) else return str$(i) : fi |
|||
fi |
|||
end sub |
|||
sub roundrob(n) |
|||
byes = 0 |
|||
if mod(n, 2) = 1 then //if there is an odd number of competitors |
|||
byes = 1 //make note of this fact |
|||
n = n+1 //and treat the tournament as having one more competitor |
|||
fi |
|||
dim schd(n) |
|||
//, r, i, temp1, temp2 |
|||
for i = 1 to n |
|||
schd(i) = i //initial population of the array with numbers 1-n |
|||
next i |
|||
for r = 1 to n-1 |
|||
print "Round ", r using "##", ": "; |
|||
for i = 1 to n/2 //print the pairings according to the scheme |
|||
//1 2 3 4 |
|||
//5 6 7 8 |
|||
print "(", nob$(n,schd(i),byes), " -", nob$(n,schd(i+n/2),byes), " ) "; |
|||
next i |
|||
print |
|||
//now move positions 2-n around clockwise |
|||
temp1 = schd(n/2)//need to track two temporary variables |
|||
temp2 = schd(n/2+1) |
|||
for i = n/2 to 3 step -1 //top row |
|||
schd(i) = schd(i-1) |
|||
next i |
|||
for i = n/2+1 to n-1 //bottom row |
|||
schd(i) = schd(i+1) |
|||
next i |
|||
schd(n) = temp1 //fill in the ones that "jumped" between rows |
|||
schd(2) = temp2 |
|||
next r |
|||
end sub</syntaxhighlight> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="c++"> |
|||
#include <algorithm> |
|||
#include <cstdint> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
#include <numeric> |
|||
#include <stdexcept> |
|||
#include <vector> |
|||
void round_robin(uint32_t team_count) { |
|||
if ( team_count < 2 ) { |
|||
throw std::invalid_argument("Number of teams must be greater than 2: " + team_count); |
|||
} |
|||
std::vector<uint32_t> rotating_list(team_count); |
|||
std::iota(rotating_list.begin(), rotating_list.end(), 2); |
|||
if ( team_count % 2 == 1 ) { |
|||
rotating_list.emplace_back(0); |
|||
team_count++; |
|||
} |
|||
for ( uint32_t round = 1; round < team_count; ++round ) { |
|||
std::cout << "Round " << std::setw(2) << round << ":"; |
|||
std::vector<uint32_t> fixed_list(1, 1); |
|||
fixed_list.insert(fixed_list.end(), rotating_list.begin(), rotating_list.end()); |
|||
for ( uint32_t i = 0; i < team_count / 2; ++i ) { |
|||
std::cout << " ( " << std::setw(2) << fixed_list[i] |
|||
<< " vs " << std::setw(2) << fixed_list[team_count - 1 - i] << " )"; |
|||
} |
|||
std::cout << std::endl; |
|||
std::rotate(rotating_list.rbegin(), rotating_list.rbegin() + 1, rotating_list.rend()); |
|||
} |
|||
} |
|||
int main() { |
|||
std::cout << "Round robin for 12 players:" << std::endl; |
|||
round_robin(12); |
|||
std::cout << std::endl << std::endl; |
|||
std::cout << "Round robin for 5 players, 0 denotes a bye:" << std::endl; |
|||
round_robin(5); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Round robin for 12 players: |
|||
Round 1: ( 1 vs 12 ) ( 2 vs 11 ) ( 3 vs 10 ) ( 4 vs 9 ) ( 5 vs 8 ) ( 6 vs 7 ) |
|||
Round 2: ( 1 vs 11 ) ( 13 vs 10 ) ( 2 vs 9 ) ( 3 vs 8 ) ( 4 vs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> |
||
Line 1,399: | Line 1,551: | ||
2 12 11 10 9 8 |
2 12 11 10 9 8 |
||
</pre> |
</pre> |
||
=={{header|uBasic/4tH}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="ubasic-4th"> |
|||
Print "Twelve teams" |
|||
Proc _Roundrob(12) |
|||
Print |
|||
Print "Nine teams with byes" |
|||
Proc _Roundrob(9) |
|||
End |
|||
' helper function to allow byes to be printed intelligently |
|||
_Nob Param (3) : Return (Iif (a@ = b@ * c@, Dup(" B"), Str("_#", b@))) |
|||
_Roundrob |
|||
Param (1) |
|||
Local (5) |
|||
b@ = 0 |
|||
' if there is an odd number of competitors |
|||
If a@ % 2 = 1 Then b@ = 1 : a@ = a@ + 1 |
|||
' make note of this fact and treat the tournament |
|||
For d@ = 1 To a@ ' as having one more competitor |
|||
@(d@) = d@ ' initial population of the array with numbers 1-n |
|||
Next |
|||
For c@ = 1 To a@-1 ' print the pairings according to the scheme |
|||
Print Using "Round __: ";c@; |
|||
' 1 2 3 4 |
|||
For d@ = 1 To a@/2 ' 5 6 7 8 |
|||
Print "("; Show(FUNC(_Nob (a@, @(d@), b@)));" - "; |
|||
Print Show(FUNC(_Nob (a@, @(d@+a@/2), b@)));") "; |
|||
Next |
|||
Print ' now move positions 2-n around clockwise |
|||
e@ = @(a@/2) ' need to track two temporary variables |
|||
f@ = @(a@/2+1) |
|||
' top row |
|||
For d@ = a@/2 To 3 Step -1 |
|||
@(d@) = @(d@-1) |
|||
Next |
|||
' bottom row |
|||
For d@ = a@/2+1 To a@-1 |
|||
@(d@) = @(d@+1) |
|||
Next |
|||
@(a@) = e@ ' fill in the ones that "jumped" between rows |
|||
@(2) = f@ |
|||
Next |
|||
Return |
|||
</syntaxhighlight> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |