Module: Tournament::Algorithm::SingleBracket

Extended by:
SingleBracket
Included in:
SingleBracket
Defined in:
lib/tournament/algorithm/single_bracket.rb

Overview

This module provides algorithms for dealing with single bracket elimination tournament systems.

Instance Method Summary collapse

Instance Method Details

#guess_round(teams_count, matches_count) ⇒ Integer

Guess the next round (starting at 0) for a single bracket tournament.

Parameters:

  • teams_count (Integer)

    the number of teams

  • matches_count (Integer)

    the number of existing matches

Returns:

  • (Integer)

    next round number

Raises:

  • (ArgumentError)

    when the number of matches does not add up



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tournament/algorithm/single_bracket.rb', line 34

def guess_round(teams_count, matches_count)
  rounds = total_rounds(teams_count)
  total_teams = max_teams(rounds)

  # Make sure we don't have too many matches
  unless total_teams >= matches_count
    raise ArgumentError, 'Too many matches'
  end

  round = rounds - Math.log2(total_teams - matches_count)
  unless (round % 1).zero?
    # Make sure we don't have some weird number of matches
    raise ArgumentError, 'Invalid number of matches'
  end

  round.to_i
end

#max_teams(rounds) ⇒ Integer

Calculates the maximum number of teams that can play in a single bracket tournament with a given number of rounds.

Parameters:

  • rounds (Integer)

    the number of rounds

Returns:

  • (Integer)

    number of teams that could play



24
25
26
# File 'lib/tournament/algorithm/single_bracket.rb', line 24

def max_teams(rounds)
  2**rounds
end

#padd_teams(teams) ⇒ Array<team, nil>

Padd an array of teams to the next highest power of 2.

Parameters:

  • teams (Array<team>)

Returns:

  • (Array<team, nil>)


56
57
58
59
60
61
# File 'lib/tournament/algorithm/single_bracket.rb', line 56

def padd_teams(teams)
  required = max_teams(total_rounds(teams.length))

  # Insert the padding at the bottom to give top teams byes first
  Array.new(required) { |index| teams[index] }
end

#seed(teams) ⇒ Array<team>

Seed teams for a single bracket tournament.

Seed in a way that teams earlier in teams always win against later ones, the first team plays the second in the finals, the 3rd and 4th get nocked out in the semi-finals, etc.

Designed to be used with GroupPairing#adjacent.

Parameters:

  • teams (Array<Team>)

Returns:

  • (Array<team>)

Raises:

  • (ArgumentError)

    when the number of teams is not a power of 2



74
75
76
77
78
79
80
81
82
83
# File 'lib/tournament/algorithm/single_bracket.rb', line 74

def seed(teams)
  unless (Math.log2(teams.length) % 1).zero?
    raise ArgumentError, 'Need power-of-2 teams'
  end

  teams = teams.map.with_index do |team, index|
    OpenStruct.new(team: team, index: index)
  end
  seed_bracket(teams).map(&:team)
end

#total_rounds(teams_count) ⇒ Integer

Calculates the total number of rounds needed for a single bracket tournament with a certain number of teams.

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)

    number of rounds needed for round robin



15
16
17
# File 'lib/tournament/algorithm/single_bracket.rb', line 15

def total_rounds(teams_count)
  Math.log2(teams_count).ceil
end