Thursday, September 29, 2011

My Experience with Ant Build Systems

I have recently been learning about the XML-based build system known as Ant and how it can improve your Java projects.  Ant is a build system that is primarily designed for Java, which allows you access to a wide range of project management capabilities.  Similar to the Unix Make command, you can set dependencies for targets and Ant will only compile or process certain required tasks that need to be done, rather than doing everything again each time you call it.  You can automate a great deal of XML scripting in order to better manage your project files or even generate javadoc files.

Thus far, I have completed a number of Kata exercises for Ant, which have helped me learn a lot more about Ant's usefulness.  Listed below are 8 very basic katas that each covers a different aspect of Ant build systems:

1. Ant Hello World 
This Kata covers basic structure of an Ant basic XML file.  It simply echos "Hello World".

2. Ant Immutable Properties
This Kata covers basic Ant properties and how they cannot be changed after they have been set.

3. Ant Dependencies
This one dealt with dependencies and how they work.
 
4. Hello Ant Compilation
This one compiled a java file "HelloAnt.java" and placed the class file into another directory.
 
5. Hello Ant Execution
This is the Kata with which I had an issue.  It is supposed to simply run the compiled "HelloAnt" program, but for some reason I couldn't understand how to set up the classpath properly and was met with several errors.  The execution target depends on the file being already compiled, which I didn't have any problems with.

6. Hello Ant Documentation
This generates all of the javadoc files for the project.
 
7.  Cleaning Hello Ant
This exercise simply deletes all of the build directories that the previous Ant scripts generate.

8. Packaging Hello Ant
This exercise creates a zip file for the current directory and all of its sub-directories.  It depends on the compilation, execution, documentation, and then cleans the build directory before creating the zip file.  As I was unable to get my execution script to work properly, I left that dependency out of the equation for now.  I intend to go back and add that once I determine why my execution script did not work.
 
Overall, it was a rewarding experience to delve into XML and learn how to work with targets, dependencies, paths, and even how to manipulate file structures and directories.  Once I figure out how to fix my error with the execution script, I will be able to fully set up a build system for future projects from scratch.  All that is left now is to go over information regarding testing and automated quality assurance.  After I cover those basics, working with large projects should be much easier to deal with.
 

Tuesday, September 20, 2011

Meanwhile, Down at the Robocode Factory...

You may be asking what a "Robocode factory" is, or for that matter just "Robocode".  Well, Robocode is an open source project that was started a while ago by some folks at IBM.  It has since been kept alive by "John Q. Public" (people like you and me).  You can check out their homepage here: http://robocode.sourceforge.net/

When someone trains their skills for tournaments or fighting, they often have to practice certain moves over and over.  Back in the day, martial artists in many different styles learned to comprise basic moves into "sets" or "katas" that they could practice over and over in order to develop muscle memory and the physical development needed to be able to repeat one or more such moves at a later time.  This would enable them to use moves from their kata instinctively and with ease.

In a similar manner, I have been training my skills in developing robots for Robocode by building 13 basic robots, each of which is like a kata that contains particular "moves" that relate to Robocode robot functionality.  This training has been an exercise in basic programming, math, and robot behavior.  The robots themselves are quite simple in their nature as they only have 3 basic aspects: moving, scanning, and shooting.  Here are the 13 basic katas that I completed for my Robocode training:
  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).
I certainly had a lot of fun working on each of these individual robots.  I particularly enjoyed getting to revisit some of my old trigonometry formulas and theories in programming certain behaviors.  I thought it would be a lot more difficult than it actually was, as most of the methods were reusable.  The most useful method was the "goTo" method, which calculates how to move to any spot on the battlefield based on the robot's initial x and y coordinates and starting angle. 

Interestingly, I had the most trouble with getting the robot to move in a circle of a particular radius as you can see in Position06.  I had created this really elaborate calculation that turned the circle into a polygon with adjustable "resolution", or number of sides, and moved accordingly.  It had some very complex calculations, and was a labor of love.  Alas, I eventually threw it away when I later realized how all the inner angles added up to 360 degrees and you can just divide up the circumference by the number of angles you want the 360 degrees divided into.  Then I simply went through a for loop moving forward and turning a little at a time.  All the external angles are actually the same as the inner angles, and I was able to make a method that takes a radius and a "resolution" as parameters.  This way, I can later reduce the number of turns for faster maneuvers, or increase them for a smoother circle.  When being shot at, speed is a key factor in avoidance, and so I would probably opt for an ugly "circle" if it meant that I could avoid bullets!

Tracking was rather fun, but far easier once I delved through the Robocode API for a while.  The API takes care of getting all the information regarding angles and distance that you need.  There's even a really nifty utility method that normalizes angles to between 0 and either 180 or -180 degrees, which is very useful to avoid "overturning" when trying to get the main gun to bear down on your target.  I had done some angle normalization on my own for the "goTo" method, but when I found the utility method in the API, I decided to use that for my Boom04 robot as it is more elegant code.

Altogether, I learned a lot about how the robots behave based on some trial and error I did in organizing my methods.  I've also gained a few inspirations that I would like to try out on a competitive robot that I want to make next.  If you are a casual coder, someone with an interest in games, or perhaps even just bored, I sincerely recommend that you check out Robocode!  I had a lot of fun and spent far too much time playing around with the code.  I am quite positive that you too would really enjoy it if you put in the effort to learn the basics.  They even have a number of Java and Robocode tutorials that you can look at to get started.  I also seriously recommend going through each of the aforementioned katas as they will help you get an understanding of how to get the robots to do what you want, as well as how to interact with other robots.