Tuesday, September 20, 2011

Robokatas, it's martial arts for programmers

I know what it sounds like, but this is some serious stuff. Katas are a training regimen that martial artists use to memorize and practice the forms that are used in their martial arts. There are many styles of martial arts and just as many katas that are in each school, if not more. Katas are good practice because they're both repetitive and have a style all their own. If you have been to any of the martial arts exibitions or demonstrations, the forms and routines are usually katas in general, although I'm sure there are more advanced katas and styles just to show how intense that form of martial arts is and not to be taken lightly. As for one that has practiced martial arts, my experience has taught me that learning and practicing katas is not only hard work, but extremely rewarding and great exercise.

I'm sure that most of you have had some programming experience and even if you don't, it is never too late to try. with that in mind, the only real way to get better at programming is to keep programming. The problem with programming is that it gets repetitive and sometimes downright boring. Fear not, for there is some relief to boring shopping lists and phonebook arrays that often so grace programming books: Robocode. Robocode is a fun way to program and get into programming more. Robocode is freeware and is easily downloadable off Sourceforge. There is more information as well as a ton of resource and reference material on their Robocode product page. Go ahead and click on the link, I'll still be waiting here.

The Robocode project page is here.

Now that you're, hopefully, back, I bet you're wondering why I was talking about katas in the first place. Katas are a good way to train and you can apply that same type of regimen to programming. Take for instance this simple training kata that I recently learned about for programming simple robots in Robocode:

  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).
That's a big list, but from my experience katas are about this long, each step is a single move or form, so for this beginner's robokata, there are 13 code forms and/or steps that are to be taken. To challenge myself, and see how badly I might screw this up, I took a deep breath and started up my programming noggin for some programming katas. I started to modify the generic Robot class, just so that there is less of a hassle instead of working with the AdvancedRobot class.

As expected, the first one was easy; just set up a robot that exists. The second and third ones were easy as well, not even breaking a sweat. The fourth one was a bit more challenging, but there are ways to figure out the size of the room and to calculate the midpoint from that. All I had to do was to figure out how to get there from getting the midpoint, which was easy using some coordinate manipulation. Number five was difficult in the sense that the robot has it owe size, which interferes with the sides of the battlefield. So I had to take into account the width of the robot when navigating to the corners.

The sixth one was really challenging. The problem seemed simple enough, get to the center and travel in a circle. Getting to the center was no problem, I already figured that out for the fourth robot in the robokata. The moving in a circle was the problem. I decided to break the circle into degrees and formed polygons of numerous sizes so that I was working with ever decreasing triangles. Using the Law of Cosines, I figured out how to move in a polygon circle first, then as I made the triangle slices smaller, the polygon looked more like a circle.

Now that the hardest one was over, I started on the tracking ones. One by one, the tracking robots were finished and now I got to move onto the fun ones, ones that can actually shoot! I was in for a real workout now, Not only the tracking on the robots is difficult, the response and timing is difficult. Fortunately, the Robot class has some help in tracking other robots to help with shooting, which is lacking in the AdvancedRobot class. Good thing I started with the robot class.

After first getting done with the first shooting robot, I wanted to see how it did within Robocode. Boy, was I disappointed! Not only did it fail miserably, but it was shooting every which way. I took a deep breath and went back into the code to see what needed to be fixed. I tightened the turning radius for the gun and now it seemed to be working better. I tackled the second shooter and it was much easier, because I only had to concentrate on one and fire if the robot could see the target. The third one was just like the first one, but now I had to just adjust how much power the robot used when firing the gun. The fourth one was more like the second one, but it only tracked the robot, so I just removed the part where it would fire from the second shooting robot.

With all that done, I went through my first robokata. It was fun and I must admit, it was challenging and after I finshed, I want to go back and make them better, faster, and more concise on their movement.

Just like riding a bike for the first time when you fall on your rear, you pick yourself up and try again. Or you could just forget the while bike thing and walk away, but you're fighter aren't you? That's what these katas are for, so you can get good at fighting your way through programming problems like they're nobody's buisness.

No comments:

Post a Comment