Module: TournamentSystem::Algorithm::Swiss
Overview
This module provides algorithms for dealing with swiss tournament systems. Specifically it provides algorithms for grouping teams.
Instance Method Summary collapse
-
#group_teams_by_score(teams, scores) ⇒ Array<Array<team>>
Groups teams by their score.
-
#minimum_rounds(teams_count) ⇒ Integer
Calculates the minimum number of rounds needed to properly order teams using the swiss tournament system.
-
#rollover_groups(groups) ⇒ nil
Rollover the last person in each group if its odd.
Instance Method Details
#group_teams_by_score(teams, scores) ⇒ Array<Array<team>>
Groups teams by their score.
22 23 24 25 26 27 |
# File 'lib/tournament_system/algorithm/swiss.rb', line 22 def group_teams_by_score(teams, scores) groups = teams.group_by { |team| scores[team] || 0 } sorted_keys = groups.keys.sort.reverse sorted_keys.map { |key| groups[key] } end |
#minimum_rounds(teams_count) ⇒ Integer
Calculates the minimum number of rounds needed to properly order teams using the swiss tournament system.
12 13 14 |
# File 'lib/tournament_system/algorithm/swiss.rb', line 12 def minimum_rounds(teams_count) Math.log2(teams_count).ceil end |
#rollover_groups(groups) ⇒ nil
Rollover the last person in each group if its odd. This assumes that the total number of players in all groups is even. Rollover is performed in-place.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tournament_system/algorithm/swiss.rb', line 35 def rollover_groups(groups) groups.each_with_index do |group, index| # Move last from the current group to the front of the next group groups[index + 1].unshift group.pop if group.length.odd? end # Remove any empty groups groups.reject!(&:empty?) nil end |