Thursday, September 29, 2011

Ant Code Katas

Building upon my last blog entry on the application of Katas on improving my programming, I applied this concept to the Apache Ant build system for Java. Although not my first time messing around with a build system, this is by far the most complex I've encountered. Similar to the Make utility found on most Unix-based operating systems, the Apache Ant build system automatically builds executable programs from source code and libraries.

For this particular build system, I worked on the following katas:

1. Ant Hello World
2. Ant Immutable Properties
3. Ant Dependencies
4. Hello Ant Compilation
5. Hello Ant Execution
6. Hello Ant Documentation
7. Cleaning Hello Ant
8. Packaging Hello Ant

Sparing the details for each kata (you can read about them here), each kata gets more difficult along the way. Funny enough, learning how to print words to the screen in Ant took a bit longer than expected. Unlike a programming language, it seems like the first thing the authors of a build system would like you to do is to actually use their program to build a system. I knew I had to enclose the "echo" task around what I wanted to print to the screen but to actually set up the script to do this took longer than expected. A quick Google searched revealed resources on how to do exactly this.

Next, completing the immutability and dependency scripts were pretty painless since the Wikibooks website also provided a good tutorial on those two concepts, however, I would soon hit a brick wall with the next three scripts. Out of the three, compilation wasn't too bad since the Ant manual provides a good basis on how to do this. On the execution script I would later find out the pain in trying to run a Java program using Ant. During this time I've probably went back-and-forth a few times deciding whether to follow the Apache model and JAR my HelloWorld program and execute the the JAR, or to simply find a way to execute the program without having to do this step. Finally, I found the documentation script to be a little simpler since I only had to be concerned with the location of the source files and the directory where I want to store the Javadoc reference files. Luckily, I had no problems with the clean and distribution scripts since I found the sample scripts from another build system distribution straightforward.

With these code katas I was able to dive a little deeper with just how complex build systems can get pass the compilation and execution stages. With the headaches I've encountered when trying to run other peoples Java implementations on my computer, it is clear how useful and in my case necessary to have a robust build system to accompany any software system.

Tuesday, September 20, 2011

Robocode Code Katas

Having no prior knowledge of what a "Kata" is, I took the time to read through the following articles to get myself acquainted with the idea of a "code kata".

http://en.wikipedia.org/wiki/Kata_(martial_arts)
http://www.codinghorror.com/blog/2008/06/the-ultimate-code-kata.html

The idea is sound. The only way I can get better as a programmer is to constantly challenge myself with harder sets of programming problems. Although I often find myself in an impossible situation when trying something new, I've always come out getting something out of the experience.

Applying this concept to Robocode, an open source programming game, revealed my limitations as a programmer and how the code kata concept can work seamlessly with any programming exercise. In this case, I challenged myself to thirteen exercises devoted to teaching me the basic fundamentals of a robot in the Robocode universe.

Position01: The minimal robot. Does absolutely nothing at all.
Position02: Move forward a total of 100 pixels per turn. When you hit a wall, reverse direction.
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.
Position04: Move to the center of the playing field, spin around in a circle, and stop.
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.
Position06: Move to the center, then move in a circle with a radius of approximately 100 pixels, ending up where you started.
Follow01: Pick one enemy and follow them.
Follow02: Pick one enemy and follow them, but stop if your robot gets within 50 pixels of them.
Follow03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
Boom01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
Boom02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
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).
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).

My approach for this particular exercise was simple, and that was to use as little trigonometry as possible. For the Position robots, everything was fine until I got to the Position04 robot. Knowing that the robot will spawn at a random location on the map immediately caused me to retract my previous "no trigonometry" approach. But thanks to the power of Google, I found an IBM solution in the form of a sample robot called DwRotater found in the article detailing the Robocode basics.

http://www.ibm.com/developerworks/java/library/j-robocode/

DwRotater does exactly as what the Position04 specifications call for, that is, to move to the center of the playing field. I would also use the basic outline of the code and modify to send the robot off into the four corners of the map in Position05. But when it came to the Position06, the "no trigonometry" motto proved to be fruitless as I was not able to find a solution to move the robot in a circle.

The Follow robots proved simple enough by using the built-in functions to give a distance that allows the robot to close in on a particular enemy.

Finally, with the Boom robots things got interesting by experimenting with the firing mechanics of the game, Boom03 in particular because it shows how manipulating the values issued to the fire command will have on enemy tanks. Boom04 proved to also be a hard one since I was only able to track an enemy sitting still by simply rotating the gun in that direction.

With that said, I was able to complete most of the exercises by using no trigonometry whatsoever. Obviously not a smart decision since I'm sure most if not all of the elite robots are using trigonometry to do the more advanced stuff like evasion techniques or firing at the enemy on the move. To build a competitive robot I simply need to learn how to not get hit while still being able to fire effectively at targets. In general, the code kata approach helped by starting on an easy problem and gradually increasing the difficulty to get better at it.