Class: SimpleTournament
Overview
SimpleTournament
Instance Attribute Summary collapse
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#tournament ⇒ Object
readonly
Returns the value of attribute tournament.
Instance Method Summary collapse
-
#apply_challengers(challengers) ⇒ Object
apply challengers to tournament.
-
#initialize(number) ⇒ SimpleTournament
constructor
initialize tournament by number.
-
#start_match(proc) ⇒ Object
start tournament match.
Constructor Details
#initialize(number) ⇒ SimpleTournament
initialize tournament by number
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/simple_tournament.rb', line 8 def initialize(number) @number = number @matches = [] cnt = 0 @tournament = [] while number >= (2**cnt) tournament << Array.new(2**cnt) cnt += 1 end diff = number - (2**(cnt - 1)) @tournament << Array.new(diff * 2) unless (diff == 0) end |
Instance Attribute Details
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
5 6 7 |
# File 'lib/simple_tournament.rb', line 5 def matches @matches end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
5 6 7 |
# File 'lib/simple_tournament.rb', line 5 def number @number end |
#tournament ⇒ Object (readonly)
Returns the value of attribute tournament.
5 6 7 |
# File 'lib/simple_tournament.rb', line 5 def tournament @tournament end |
Instance Method Details
#apply_challengers(challengers) ⇒ Object
apply challengers to tournament
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/simple_tournament.rb', line 22 def apply_challengers(challengers) fail 'incorrect challengers size. challengers must equals tournament.size' unless challengers.size == @number @tournament.reverse_each do |outer| outer.each_with_index do |v, i| poped = challengers.pop return @tournament if poped.nil? outer[i] = poped end end end |
#start_match(proc) ⇒ Object
start tournament match. set result to tournament, matches
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/simple_tournament.rb', line 34 def start_match(proc) @tournament.reverse_each.with_index do |outer, outer_index| outer.reverse_each.with_index do |inner, inner_index| next if inner_index.odd? next if inner_index == outer.size - 1 rets = proc.call outer[-(inner_index + 1)], outer[-(inner_index + 2)], outer_index winner = rets.first @matches << rets.last set_winner(winner, outer_index) end end end |