Begining RRobots bot


#1

Hi all,

I’m new to ruby and OO programming (been writing procedural perl for a
few years). I’m hoping to use the rrobots modules to practice ruby
and OO (just missed the rubyquiz competition, eh?). Here’s what I
have so far. This is not the smartest bot by any means (only 1 speed
and not the greatest tracking…), but at this point I’m mostly
interested in making sure my code is good clean OO and generally
follows the “ruby way” of doing things. Any/all suggestions welcome!

Thanks!

Greg

require ‘robot’

#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:

got_hit

robot_scanned

broadcasts

class GoofeyDuck

include Robot

@@radar_scan = 60 #make a class with methods??? ie scan.increase,
scan.decrease
@@found = 0

def tick events

turn_radar(radar_dir)
turn_gun(gun_dir)
turn(tank_dir)
accelerate(tank_acc)
fire(gun_str)
say("Dang!!!!") if (energy <= 1)

end

def radar_dir #return between -60 and 60

if (!events['robot_scanned'].empty?) #found_em
  @@radar_scan = decrease(@@radar_scan, 1, 0)
  @@radar_scan = reverse_dir(@@radar_scan)
  found = 1
elsif (found == 1) #had em last tick, adjusting....
  @@radar_scan = increase(@@radar_scan, 2, 60)
  @@radar_scan = reverse_dir(@@radar_scan)
  found = 0
else
  @@radar_scan = increase(@@radar_scan, 2, 60)
end
@@radar_scan

end

def gun_dir #return between -30 and 30
radar_heading - gun_heading
end

def tank_dir #return betweem -10 and 10
if (!events[‘got_hit’].empty?) #got hit - run away!
increase(heading, 10, 360)
elsif ((x <= size) ||
(y <= size) ||
(x >= battlefield_width - size) ||
(y >= battlefield_height- size)) #in a wall - right turn
clyde!
decrease(heading, 10, 0)
else
gun_heading - heading
end
end

def tank_acc #return between -1 and 1
1
end

def gun_str #return between 0.1 and 3
0.5 #make based on size of radar_scan or event[‘robot_scanned’](as
either gets smaller, power gets larger)
end

#helper methods…

def increase(current, amt, max)
return current if (current.abs >= max)
z = current <=> 0
current + (z * amt)
end

def decrease(current, amt, min)
return current if (current.abs <= min)
z = current <=> 0
current - (z * amt)
end

def reverse_dir(current)
current * -1
end
end