Class: SportsManager::Matches::Algorithms::SingleEliminationAlgorithm
- Defined in:
- lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb
Overview
TODO: implement Public: Algorithm for building the rounds and matches in a Single Elimination Tournament. This format is also known as Knockout
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#opening_round_matches ⇒ Object
readonly
Returns the value of attribute opening_round_matches.
-
#opening_round_size ⇒ Object
readonly
Returns the value of attribute opening_round_size.
Instance Method Summary collapse
-
#initialize(category:, matches:) ⇒ SingleEliminationAlgorithm
constructor
A new instance of SingleEliminationAlgorithm.
-
#needs_bye? ⇒ Boolean
TODO: make use of it later.
-
#next_matches ⇒ Object
TODO: BYE, odd matches.
- #round_for_match(match_number) ⇒ Object
-
#total_matches ⇒ Object
Internal: The number of matches required to find a winner, without a third place match, is the number of players/teams minus one.
-
#total_rounds ⇒ Object
Internal: The number of rounds is the closest Log2N for N players.
Constructor Details
#initialize(category:, matches:) ⇒ SingleEliminationAlgorithm
Returns a new instance of SingleEliminationAlgorithm.
12 13 14 15 16 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 12 def initialize(category:, matches:) @category = category @opening_round_matches = matches @opening_round_size = matches.size end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
10 11 12 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 10 def category @category end |
#opening_round_matches ⇒ Object (readonly)
Returns the value of attribute opening_round_matches.
10 11 12 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 10 def opening_round_matches @opening_round_matches end |
#opening_round_size ⇒ Object (readonly)
Returns the value of attribute opening_round_size.
10 11 12 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 10 def opening_round_size @opening_round_size end |
Instance Method Details
#needs_bye? ⇒ Boolean
TODO: make use of it later
37 38 39 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 37 def needs_bye? !power_of_two? end |
#next_matches ⇒ Object
TODO: BYE, odd matches
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 19 def next_matches return @next_matches if defined? @next_matches matches = opening_round_matches.dup remaining_matches.times.reduce(0) do |count, _| team1 = count team2 = team1 + 1 matches << build_next_match(matches[team1], matches[team2], matches.size + 1) team2 + 1 end @next_matches = matches - opening_round_matches end |
#round_for_match(match_number) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 56 def round_for_match(match_number) return 0 if match_number.zero? || opening_round_size.zero? rounds = total_rounds (1..rounds).each do |round| matches_in_round = 2**(rounds - round) return round if match_number <= matches_in_round match_number -= matches_in_round end end |
#total_matches ⇒ Object
Internal: The number of matches required to find a winner, without a third place match, is the number of players/teams minus one.
43 44 45 46 47 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 43 def total_matches return 0 if teams_size.zero? teams_size - 1 end |
#total_rounds ⇒ Object
Internal: The number of rounds is the closest Log2N for N players.
50 51 52 53 54 |
# File 'lib/sports_manager/matches/algorithms/single_elimination_algorithm.rb', line 50 def total_rounds return 0 if teams_size.zero? Math.log2(teams_size).ceil end |