# Begining RRobots bot

**URL:** <https://www.ruby-forum.com/t/begining-rrobots-bot/58288>\
**Category:** Ruby\
**Created:** [April 20, 2006, 9:37am UTC](https://www.ruby-forum.com/t/begining-rrobots-bot/58288 "2006-04-20T09:37:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![snow-templar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@snow-templar](https://www.ruby-forum.com/u/snow-templar)\
**Post date:** [April 20, 2006, 9:37am UTC](https://www.ruby-forum.com/t/begining-rrobots-bot/58288/1 "2006-04-20T09:37:05Z")

</div>

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
