Skip to content

Commit 2b4f89e

Browse files
author
David Rust-Smith
committed
!Fixed friendly fire code in robot.rb
+Added tracking for shots fired, shots hit, and accuracy for tournaments and one-off fights
1 parent 885c81f commit 2b4f89e

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

robot.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class RobotRunner
140140
#keeps track of total damage done by this robot
141141
attr_accessor :damage_given
142142

143+
#keeps track of total shots hit by this robot
144+
attr_accessor :shots_hit
145+
146+
#keeps track of total shots fired by this robot
147+
attr_accessor :shots_fired
148+
143149
#keeps track of the kills
144150
attr_accessor :kills
145151

@@ -169,6 +175,8 @@ def set_initial_state
169175
@speed = 0
170176
@energy = 100
171177
@damage_given = 0
178+
@shots_hit = 0
179+
@shots_fired = 0
172180
@kills = 0
173181
teleport
174182
end
@@ -195,7 +203,7 @@ def set_action_limits
195203

196204
def hit bullet
197205
damage = 0 #default no damage, unless different teams
198-
damage = bullet.energy unless bullet.origin.team != @team
206+
damage = bullet.energy unless bullet.origin.team == @team
199207
@energy -= damage
200208
@events['got_hit'] << [@energy]
201209
damage

rrobots.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def initialize bf, x, y, heading, speed, energy, origin
117117
@x, @y, @heading, @origin = x, y, heading, origin
118118
@speed, @energy = speed, energy
119119
@battlefield, dead = bf, false
120+
origin.shots_fired += 1
120121
end
121122

122123
def state
@@ -137,6 +138,7 @@ def tick
137138
@battlefield << explosion
138139
damage = other.hit(self)
139140
origin.damage_given += damage
141+
origin.shots_hit += 1
140142
origin.kills += 1 if other.dead
141143
@dead = true
142144
end
@@ -215,6 +217,9 @@ def print_outcome(battlefield)
215217
puts "robots :"
216218
battlefield.robots.each do |robot|
217219
puts " #{robot.name}:"
220+
puts " shots_fired: #{'%.1f' % robot.shots_fired}"
221+
puts " shots_hit: #{'%.1f' % robot.shots_hit}"
222+
puts " accuracy: #{'%.1f' % (100*robot.shots_hit/robot.shots_fired)} %"
218223
puts " damage_given: #{'%.1f' % robot.damage_given}"
219224
puts " damage_taken: #{'%.1f' % (100 - robot.energy)}"
220225
puts " kills: #{robot.kills}"

tournament.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ def simul?
4848
def timedout?
4949
@timedout == 1
5050
end
51+
52+
53+
def winner_shots_fired
54+
@bots[winner]['shots_fired'].to_i
55+
end
56+
57+
def loser_shots_fired
58+
@bots[loser]['shots_fired'].to_i
59+
end
60+
61+
def winner_shots_hit
62+
@bots[winner]['shots_hit'].to_i
63+
end
64+
65+
def loser_shots_hit
66+
@bots[loser]['shots_hit'].to_i
67+
end
68+
69+
def winner_accuracy
70+
100* @bots[winner]['shots_hit'].to_i/@bots[winner]['shots_fired'].to_i
71+
end
72+
73+
def loser_accuracy
74+
100* @bots[loser]['shots_hit'].to_i/@bots[loser]['shots_fired'].to_i
75+
end
5176

5277
#between 100 and 0
5378
def winner_health
@@ -61,7 +86,7 @@ def loser_health
6186

6287
def one_line_summary
6388
if !tie?
64-
line = "#{winner} beats #{loser} by #{'%.1f' % margin} energy in #{@ticks} ticks"
89+
line = "#{winner} beats #{loser} by #{'%.1f' % margin} energy in #{@ticks} ticks. #{winner} accuracy #{'%.1f'% winner_accuracy} vs. #{loser} accuracy #{'%.1f'% loser_accuracy}"
6590
if @timedout then line += " (match timed out, so #{winner} gets #{winner_points}, loser gets #{loser_points})" end
6691
else
6792
line = "#{winner} ties #{loser} at #{'%.1f' % winner_health} energy in #{@ticks} ticks"
@@ -85,7 +110,7 @@ def initialize (matches)
85110
@bots = Hash.new {|h,key| h[key] = {}}
86111

87112
both_bots = [@bots[@matches[0].winner], @bots[@matches[0].loser]]
88-
stats_to_init = ['wins', 'points', 'ties', 'margin', 'simul', 'timedout', 'round_wins']
113+
stats_to_init = ['wins', 'points', 'ties', 'margin', 'simul', 'timedout', 'round_wins','accuracy', 'shots_fired', 'shots_hit']
89114
stats_to_init.each {|stat| both_bots.each {|b| b[stat] = 0 }}
90115

91116
@matches.each do |match|
@@ -95,6 +120,15 @@ def initialize (matches)
95120
both_bots.each {|b| b['timedout'] += 1 if match.timedout?}
96121
both_bots.each {|b| b['simul'] += 1 if match.simul?}
97122
@bots[match.winner]['margin'] += match.margin
123+
124+
@bots[match.winner]['shots_fired'] += match.winner_shots_fired
125+
@bots[match.loser]['shots_fired'] += match.loser_shots_fired
126+
@bots[match.winner]['shots_hit'] += match.winner_shots_hit
127+
@bots[match.loser]['shots_hit'] += match.loser_shots_hit
128+
129+
@bots[match.winner]['accuracy'] = 100*@bots[match.winner]['shots_hit'] / @bots[match.winner]['shots_fired']
130+
@bots[match.loser]['accuracy'] = 100*@bots[match.loser]['shots_hit'] / @bots[match.loser]['shots_fired']
131+
98132
if match.tie?
99133
both_bots.each {|b| b['wins'] += 0.5}
100134
else
@@ -269,7 +303,7 @@ def to_html match_data, html
269303
rounds << r
270304
end
271305

272-
stats = ['wins', 'points','round_wins', 'margin', 'simul', 'timedout', 'ties']
306+
stats = ['wins', 'points','round_wins', 'margin', 'simul', 'timedout', 'ties', 'shots_fired','accuracy']
273307
rankings = {}
274308
stats.each{|stat| rankings[stat] = rank_by_round_stat(stat, rounds)}
275309

0 commit comments

Comments
 (0)