Class: Round

Inherits:
Object
  • Object
show all
Defined in:
lib/rrobots/tournament/round.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matches) ⇒ Round

matches should be an array of Matches



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rrobots/tournament/round.rb', line 8

def initialize (matches)
  @matches = matches
  @bots = Hash.new {|h,key| h[key] = {}}
  
  both_bots =  [@bots[@matches[0].winner], @bots[@matches[0].loser]]
  stats_to_init = ['wins', 'points', 'ties', 'margin', 'simul', 'timedout', 'round_wins']
  stats_to_init.each {|stat| both_bots.each {|b| b[stat] = 0 }}
  
  @matches.each do |match|
    @bots[match.winner]['points'] += match.winner_points
    @bots[match.loser]['points'] += match.loser_points
    both_bots.each {|b| b['ties'] += 1 if match.tie?}
    both_bots.each {|b| b['timedout'] += 1 if match.timedout?}
    both_bots.each {|b| b['simul'] += 1 if match.simul?}
    @bots[match.winner]['margin'] += match.margin
    if match.tie?
      both_bots.each {|b| b['wins'] += 0.5}
    else
      @bots[match.winner]['wins'] += 1
    end
    if both_bots[0]['wins'] > both_bots[1]['wins'] then both_bots[0]['round_wins'] = 1 end
    if both_bots[1]['wins'] > both_bots[0]['wins'] then both_bots[1]['round_wins'] = 1 end
    if both_bots[1]['wins'] == both_bots[0]['wins'] then both_bots[0]['round_wins'] = 0.5 ;both_bots[1]['round_wins'] = 0.5 end
  end
end

Instance Attribute Details

#botsObject (readonly)

attr_accessor :total_margin



5
6
7
# File 'lib/rrobots/tournament/round.rb', line 5

def bots
  @bots
end

#matchesObject

Returns the value of attribute matches.



2
3
4
# File 'lib/rrobots/tournament/round.rb', line 2

def matches
  @matches
end

#winnerObject

Returns the value of attribute winner.



3
4
5
# File 'lib/rrobots/tournament/round.rb', line 3

def winner
  @winner
end

Instance Method Details

#loserObject



39
40
41
42
# File 'lib/rrobots/tournament/round.rb', line 39

def loser
  sorted = @bots.sort{|a,b| a[1]['wins'] <=> b[1]['wins']}
  return sorted[0][0]
end

#one_line_summaryObject

calc how many points for losing bot



49
50
51
52
53
54
55
# File 'lib/rrobots/tournament/round.rb', line 49

def one_line_summary
  if !tie?
    line = "#{winner} conquers #{loser} (#{@bots[winner]['wins']} to #{@bots[loser]['wins']} )"
  else
    line = "#{winner} ties #{loser} (#{@bots[winner]['wins']} to #{@bots[loser]['wins']} )"
  end
end

#tie?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rrobots/tournament/round.rb', line 44

def tie?
  @bots[winner]['wins'] == @bots[loser]['wins']
end