Module: TournamentSystem::Algorithm::Swiss

Extended by:
Swiss
Included in:
Swiss
Defined in:
lib/tournament_system/algorithm/swiss.rb

Overview

This module provides algorithms for dealing with swiss tournament systems. Specifically it provides algorithms for grouping teams.

Instance Method Summary collapse

Instance Method Details

#group_teams_by_score(teams, scores) ⇒ Array<Array<team>>

Groups teams by their score.

Parameters:

  • teams (Array<team>)

    the teams to group

  • scores (Hash{team => Number})

    the scores of each team

Returns:

  • (Array<Array<team>>)

    groups of teams sorted with the highest score at the front



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.

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)


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.

Parameters:

  • groups (Array<Array<team>>)

    groups of teams

Returns:

  • (nil)


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