Tuesday, September 20, 2011

Robocode

Recently I began messing around with Robocode, a programming game for Java and a couple other languages.  It involves programming a little robot that then tries to kill other little robots in an arena.  While the premise is simple, it quickly became obvious that making a good robot takes a ton of time, effort, and sometimes complex algorithms.  Without regard to robots that can actually compete, I started with some simple katas, described below.
  1.  Position01: The minimal robot. Does absolutely nothing at all. 
  2. Position02: Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
  3. Position03: 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. Position04: Move to the center of the playing field, spin around in a circle, and stop.
  5. Position05: 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. Position06: Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
  7. Follow01: Pick one enemy and follow them.
  8. Follow02: Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
  9. Follow03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
  10. Boom01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
  11. Boom02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
  12. Boom03: 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. Boom04: 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).
Most of these were pretty easy to implement, although I decided to use some trigonometry for some and realized I forgot most of it already. The movement ones were all really straightforward because they didn't involve enemy robots who may not play nicely.  From the follow on, however, I ran in to a bit more trouble.  When following I found the enemy robot's location and moved to it, but by the time I got there, the enemy had almost always moved on already, leaving my robot looking a bit stupid.  I think to make a more advanced follow, I'd have to locate, move forward a bit, then relocate, reorient, and move again.  The same problem applies for the second follow. Because I can't move and scan at the same time, I couldn't tell if I was within 50 pixels or not and had to just guess.

The final kata, with gun tracking, was definitely the most interesting of the bunch, although my implementation was extremely naive.  I just found the enemy's center, orientated towards it and scanned again, hoping it hadn't moved out of scanner range by then.  I think if I were to work on it longer, I would keep track of how many degrees I had to move my gun to orient within the last couple scans and maybe move a little ahead if I keep having to rotate a lot to keep up.

I feel that these katas were really useful.  When I first started, I couldn't even make my robot move (as embarassing as that is) and it got me thinking in terms of what the robots can do and how they're limited.  Turning the radar and body independently, predicting movement, and moving in different ways.  I wasn't able to fully incorporate each concept, but I got a bit closer to figuring out how to do so.  With the basics, I can finally start working on a robot that will move and.. try to kill the enemy!

No comments:

Post a Comment