Thursday, September 29, 2011

Ant katas

So I continue learning new skills about programming, and this time I did some katas using Ant. Ant is Java tool used for building files, allowing to compile, assemble, test, and run Java code. It is similar to the make tool, but Ant offers you more than that. For example, projects builds in a Ant script can be transported across any system. Also, Ant supports any OS environments.

So below are the Ant katas I did:
  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
Ant was totally a new tool for me, and it is written in XML which I haven't touch it for some time. I then had to open up the Ant manual, which was somehow a bit confusing to understand. Then, I attempted to understand my professors' Ant codes, which took me around 1-2 hours to go over a couple of them. It was pretty straightforward, even without comments. However, it is still hard to remember all tags and their attributes.



So I start doing my katas, and like the Robocodes' one, the first 4 were pretty straightforward. I had a hard time doing the "Hello Ant Execution" which it had to run the Hello Ant java code. After doing some research about the <java> Ant tag, I realized that the classname attribute is just the name and not the path to the .class file. After that, the documentation and packaging katas were pretty straightforward also.

I still have to play around with this awesome tool to kata it to the perfection. I like also how it works well Ivy, which allows you to download third party libraries. It makes life so much easier so you don't have to download and import all the libraries required to run a Java application all by yourself. 


Tuesday, September 20, 2011

Kata it to the Perfection

The word "Kata" comes from the Japanese martial arts which describes a series of choreographed movements to develop the "perfect" form. Similarly, "Code Kata" is a series of small programming projects that allow programmers to kata their programming skills. Well, I tried to kata my knowledge in programming by creating thirteen robots using Robocode.
Robocode is an open-source engine game written in Java that allows us to create our own robots and battle with other creators. It is quite popular, since it was first started by IBM. However, IBM dropped the project,but some die-hard fans of Robocode decided to resume and improve the game.


This is how my thirteen robots behave:

  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). 

Honestly, I was super-hyper excited to start building my robots. I like games and this was my first time doing some game programming. But Hey!, I didn't expect to use trigonometry to create my robots so they can behave as mentioned above. I used a good amount of my muscular brain and I think I had enough of Math related topics for the rest of the semester. 


The first 3 robots were pretty simple, since it is just to test that our Robocode environment is set up correctly.  The next 3 which involves a lot of trigonometry knowledge (for me it is a lot!) were the hardest. I had to stop here for a couple of days and start refreshing all trigonometry I had learnt from high school. With some help from some classmates I was able to model these 3 robots. A problem that I still can't figure out is in the Position05 robot. I had calculated all the angles of all the corners, and my robot was behaving as expected until it tried to move from the bottom left to the upper left. It moved only 1/3 and hit wall, then moved to the lower right corner. I had changed my code with the help of a classmate and now it works fine, but my robot won't be facing exactly to the upper right corner, so I had to do some simple tricks to at least looks like my robot hits the corner. Position04 is just finding the distance between my robot's coordinates and the center coordinates, and the angle using the Math.atan method. Position06 builds off from Position04.


The next 3, which kata our knowledge about scanning in Robocode, does not require any kind of trigonometry so it was much easier than the previous 3 (I was so glad, really!). One thing is that in Follow02, my robot will only stop when it hits the enemy robot it is tracking. I am not sure if it behaves correctly, but I will just leave there to debug in the future if time is allowed. In Follow03, I had to rotate 360 degrees to scan all the enemy robots and compare each other to find the closest one to my robot. I am not sure also if my robot behaves as expected, but at the end my robot will eventually hits wall and stop rotating there forever. 


The last 4 robots allows us to kata our knowledge in scanning enemies and firing at them. Again, it didn't involve any trigonometry. However, it was as hard as the last 3 Position robots. We have to scan and fire at the enemy now. Boom02 and Boom03 builds from Boom01, differing in one thing each other. Boom02 has to  keep track of the enemy chosen, and Boom03 fires using a power proportional to the distance between my robot and the enemies. Boom04 was hardest in this group. One odd thing in Boom04 is that my robot will sometimes rotate 360 degrees if the heading of my robot is directly opposite to the heading of its gun. 


I had some headaches doing this "Code kata", but overall I had fun also. Doing this set of kata I could kata a little bit of my programming skills, especially in Java since I haven't touched it for a while. Now I need to kata a robot for the upcoming tournament and doing these katas I think I got an idea of how my robot would be.