Tuesday, September 20, 2011

Java Fueled Robots

  So you know how to program, your familiar with object oriented programming and you can write some fairly complex code, now where can you put your awesome skills to the test and have fun doing so?  How about designing the behaviour of a robot?
That sounds fun...
Better yet how about designing a complex, cannon wielding, slaying battle robot !
Now thats better.
   Robocode is an open source educational game started by Matt Nelson of IBM, you can get a good idea of what the game is all about here: ROBOCODE
Robocode allows you to design the look and behaviour of a battle robot, you can create anything from a simple robot that shoots its gun in the direction of an enemy, to one that utilises complex statistical analysis, your skill level is the only limit with these battle bots.  Well then, who do you get to battle and prove your superiority over given that you put your time and effort into making an effective killing machine? Robocode has a good sized online community where you can test your robots might against other programmers robots here: sourceforge 
  Okay so you are convinced and want to take on the quest of creating a robot capable of some mayhem, you need a starting point.  I would suggest starting the way I did, by completing the 13 robocode Katas.

Robocode Code Katas

  1. The minimal robot. Does absolutely nothing at all. 
  2.  Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
  3. Each turn, move forward a total of N pixels per turn, then turn right. N is initialized to 15, and increases by 15 per turn.
  4. Move to the center of the playing field, spin around in a circle, and stop.
  5. Move to the upper right corner. Then move to the lower left corner. Then move to the upper left corner. Then move to the lower right corner.
  6.  Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
  7. Pick one enemy and follow them.
  8. Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
  9. Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  10. Sit still. Rotate gun. When it is pointing at an enemy, fire.
  11. Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  12.  Sit still. Rotate gun. When it is pointing at an enemy, use bullet power proportional to the distance of the enemy from you. The farther away the enemy, the less power your bullet should use (since far targets increase the odds that the bullet will miss). 
  13. Sit still. Pick one enemy and attempt to track it with your gun. In other words, try to have your gun always pointing at that enemy. Don't fire (you don't want to kill it).

  These katas are a great starting place to really get familiar with the robocode system (in my opinion katas are a great starting place for any new concept).  Once completed you will have a firm grasp of how to use all of the robots functions and have a good idea about how to start coding a battle ready robot.  I found these katas to be fairly basic only spend significant time on a few of them, why? because I had to remember my trigonometry !  You know... that class you took years ago and never found a use for... well, its back.  I was actually very excited to be able to use things like the distance formula and tangent lines for something practical.  The most difficult aspect of these katas for me was reviewing and figuring out applicable trig. formulas.

So now that you know what Robocode is I am sure you want to see it in action. 
Here are katas 7 and 9 together, no destructiveness but a funny demonstration of their respective behaviours: 

 Go get started with Robocode, I guarantee you will not be disappointed.

No comments:

Post a Comment