In my latest attempt at writing a bot, my objects are radar, gun and
tank.
methods are things like increase (scan), decrease (scan), reverse,
adjust, etc.
There’s also a bit of procedural code in the main (tick) method to
know what object to call with what method. Is that acceptable in OO?
Code follows - not the smartest bot in the world (he doesn;t even move
yet - trying to get the radar to lock-on consitently), but i’m hoping
it’s a good start that I can add to.
Any/all suggestion and criticism are very welcome!
Greg
require ‘robot’
#in this script, heading is absolute (0-360), dir (direction) is
relative (± and probably no more than 60)
#available options:
#state:
team
battlefield_height
battlefield_width
energy
gun_heading
gun_heat
heading
size
radar_heading
time
game_over
speed
x
y
#actions:
accelerate
stop
fire
turn
turn_gun
turn_radar
broadcast
say
#events:
broadcast
got_hit
robot_scanned
####### Need Unit Tests!!!
class GoofeyDuck
include Robot
def initialize
@got_hit = 0
@found
@seen #found previous round…
@radar = Radar.new
@gun = Gun.new
@tank = Tank.new
end
def tick(events)
@got_hit = events['got_hit']
@found = events['robot_scanned'] if
(!events[‘robot_scanned’].empty?)
#set radar
if (@found)
@radar.decrease(1) unless (@radar.dir.abs <= 1)
@radar.reverse
@seen = 1
say "Locked"
elsif (@seen)
@radar.increase(3)
@radar.reverse
@seen = !@seen
say "Dang, Lost em"
else
@radar.increase(2)
say "Tracking...."
end
@radar.dir = verify(@radar.dir, @radar.max)
#set gun
@gun.adjust(@radar.mid(radar_heading), gun_heading)
@gun.dir = verify(@gun.dir, @gun.max)
#fire or no?
if (@found)
@gun.fire += 0.1
else
@gun.fire -= 0.1 unless (@gun.fire <= 0)
end
@gun.fire = verify(@gun.fire, 3)
#pass results
turn_radar(verify(@radar.adjust(@gun.dir), @radar.max)) #adjust
radar to gun
turn_gun(@gun.dir)
turn(@tank.dir)
accelerate(@tank.vel)
fire(@gun.fire)
say “Uh oh” if (energy <= 10)
puts @gun.fire
#reset
@found = !@found if (@found)
end
def verify(dir, max)
if (dir > max)
dir = max
elsif (dir.abs > max)
dir = (max * -1)
end
dir
end
end
class Radar
include Robot
attr_reader :dir, :max
attr_writer :dir
def initialize
@dir = 60
@max = 60 #max delta
end
def increase(amt)
z = @dir <=> 0
@dir += (amt * z)
end
def decrease(amt)
z = @dir <=> 0
@dir -= (amt * z)
end
def reverse
@dir *= -1
end
def adjust(gun)
@dir - gun
end
def mid(head) #in degrees…
head + (@dir/2)
end
end
class Gun
include Robot
attr_reader :dir, :fire, :max
attr_writer :dir, :fire
def initialize
@dir = 0
@max = 30 #max delta
@fire = 0.1
end
def adjust(target, gun_head)
@dir = target - gun_head
end
end
class Tank
include Robot
attr_reader :dir, :vel
attr_writer :dir, :vel
def initialize
@dir = 0
@max = 10 #max delta
@vel = 0
end
end