Module: TournamentSystem::Algorithm::RoundRobin

Extended by:
RoundRobin
Included in:
RoundRobin
Defined in:
lib/tournament_system/algorithm/round_robin.rb

Overview

This module provides algorithms for dealing with round robin tournament systems.

Instance Method Summary collapse

Instance Method Details

#guess_round(teams_count, matches_count) ⇒ Integer

Guess the next round (starting at 0) for round robin.

Parameters:

  • teams_count (Integer)

    the number of teams

  • matches_count (Integer)

    the number of existing matches

Returns:

  • (Integer)

    next round number



25
26
27
# File 'lib/tournament_system/algorithm/round_robin.rb', line 25

def guess_round(teams_count, matches_count)
  matches_count / (Util.padded_teams_even_count(teams_count) / 2)
end

#round_robin(array, round) ⇒ Object

Rotate array using round robin.

Parameters:

  • array (Array<>)

    array to rotate

  • round (Integer)

    the round number, ie. amount to rotate by



33
34
35
36
37
# File 'lib/tournament_system/algorithm/round_robin.rb', line 33

def round_robin(array, round)
  rotateable = array[1..-1]

  [array[0]] + rotateable.rotate(-round)
end

#round_robin_enum(array) ⇒ Object

Enumerate all round robin rotations.



40
41
42
43
44
# File 'lib/tournament_system/algorithm/round_robin.rb', line 40

def round_robin_enum(array)
  Array.new(total_rounds(array.length)) do |index|
    round_robin(array, index)
  end
end

#round_robin_pairing(teams, round) ⇒ Array<Array(team, team)>

Rotates teams and pairs them for a round of round robin.

Uses GroupPairing#fold for pairing after rotating.

Parameters:

  • teams (Array<team>)

    teams playing

  • round (Integer)

    the round number

Returns:

  • (Array<Array(team, team)>)

    the paired teams



53
54
55
56
57
# File 'lib/tournament_system/algorithm/round_robin.rb', line 53

def round_robin_pairing(teams, round)
  rotated = round_robin(teams, round)

  GroupPairing.fold(rotated)
end

#total_rounds(teams_count) ⇒ Integer

Calculates the total number of rounds needed for round robin with a certain amount of teams.

Parameters:

  • teams_count (Integer)

    the number of teams

Returns:

  • (Integer)

    number of rounds needed for round robin



16
17
18
# File 'lib/tournament_system/algorithm/round_robin.rb', line 16

def total_rounds(teams_count)
  Util.padded_teams_even_count(teams_count) - 1
end