Round-robin tournament schedule
Round-robin tournament schedule is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
A round-robin tournament is also known as an all-play-all-tournament; each participant plays every other participant once.
For N participants the number of rounds is N-1 if N is an even number. When there are an odd number of participants then each round one contestor has no opponent (AKA as a "bye"). The number of rounds is N in that case.
- Task
Write a program that prints out a tournament schedule for 12 participants (represented by numbers 1 to 12).
- See also
Ruby
<lang ruby>def round_robin( n )
rotating_players = (2..n).map(&:to_s) #player 1 to be added later rotating_players << "bye" if n.odd? Array.new(rotating_players.size) do |r| all = ["1"] + rotating_players.rotate(-r) [all[0, all.size/2], all[all.size/2..].reverse] end
end
round_robin(12).each.with_index(1) do |round, i|
puts "Round #{i}" round.each do |players| puts players.map{|player| player.ljust(4)}.join end puts
end </lang>
- Output:
Round 1 1 2 3 4 5 6 12 11 10 9 8 7 Round 2 1 12 2 3 4 5 11 10 9 8 7 6 Round 3 1 11 12 2 3 4 10 9 8 7 6 5 Round 4 1 10 11 12 2 3 9 8 7 6 5 4 Round 5 1 9 10 11 12 2 8 7 6 5 4 3 Round 6 1 8 9 10 11 12 7 6 5 4 3 2 Round 7 1 7 8 9 10 11 6 5 4 3 2 12 Round 8 1 6 7 8 9 10 5 4 3 2 12 11 Round 9 1 5 6 7 8 9 4 3 2 12 11 10 Round 10 1 4 5 6 7 8 3 2 12 11 10 9 Round 11 1 3 4 5 6 7 2 12 11 10 9 8