Wednesday, September 28, 2011

Another Neat Tool (Ant) Katas


            Apache Ant or more commonly referred to as Ant, is a Java based build tool that is specifically for users to build their Java project into a system. It uses XML files to describe the project’s build process and its dependencies. Ant was originally developed by James Duncan Davidson as another build system too different from Makefile.
           
            Why call it Ant? One would definitely think of the insect when seeing the name, but it has another meaning than one thinks. According to Apache Ant 101 (a pdf file located at this website: https://www6.software.ibm.com/developerworks/education/j-apant/j-apant-ltr.pdf), the name Ant is actually an acronym from the words another neat tool

            For this build system experience I had to write up the eight code katas:
           
1.      Create a script to print out “Hello World”
2.      Immutable properties in Ant
3.      Ant dependencies
4.      Compile a Java file with Ant script
5.      Execute the program with an Ant script
6.      Use Ant script to create the Javadocs
7.      Create a target in the script from code kata 4 to clean up build directory
8.      Pack everything with Ant script

The first three katas were pretty easy to get through since it was mainly to understand
how things work in Ant. For example, in code kata 2, I had to implement two <property> elements with the same name of my.property and assign a value of 1 and 2. An <property> element in Ant can be thought of as a variable that one can assign a value/path location to the <property>. However, once it is assigned the value it will be immutable; in other words it means it can’t be changed. Therefore, when the script was executed I saw the value of 1 was printed; not the value of 2 because of immutability.

            In code kata 5 I was amazed at how a few simple tags written in the XML file could execute a Java file without the use of an IDE or the use of typing the command to compile the file etc. Another code kata that I was amazed at is the final code kata. In that code kata I had to write a script that utilize the compile, execute, and Javadocs code kata before it cleans up the directory where the class and Javadocs are located. After the cleaning it combines everything and zip it into a zip file. In that file I only had to import the previous XML from code katas 4 to 6 and then a script to zip up the entire package.

            To conclude, Apache Ant gave me the experience of how to package a program into a system so I can share it with other users in the future. I also learned that by building a system it can allow other users with different operating systems and IDEs to be able to execute the program and offer suggestions or comments about the program.

           

           

Monday, September 19, 2011

My Robot Experience

            Robocode is an open source game that was originally developed by IBM. It was meant to be an ongoing project, but IBM lost interest in the project and discontinued the project. The project was picked up by Flemming N. Larsen and Parvel Savara and it is available to download on sourceforge.

            This game involves writing code in Java and having the created robot(s) battle each other by moving around, firing at enemies, or just following the enemy around the battlefield. In order to win this game it is not about using strategies but it is to create a robot from algorithms that allows the robot to predict enemy movements, firing of bullets and other actions.

            For my project or kata (a Japanese martial arts term) I had to program robots to perform the following thirteen actions:

  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). 
As you can see, the list of actions can be separated into three categories. The first category is mainly for you to understand how the robot moves around the battlefield, and get familiar with the methods provided. Position 4 and 5 in this first category gave me the hardest time because it involved using trig functions. I was unfortunate in implementing a code that directly uses trig functions in calculating the robot’s angle. Therefore, I have the robot move linearly to the center and corners as specified for those positions. Currently I am still in the process of experimenting with how to incorporate trig functions into calculating the angle and have the robot directly move to center or the corners.
            The second category involves picking an enemy robot and follows it around; which doesn’t involve any trig functions. Generally speaking, for the three exercises in this category I have the robot get the name of the enemy robot and follow it around, and if the one being followed lost contact with my robot I have it rescan for other robots to follow.
            In the final category, it involves exercises to either fire at an enemy or have the gun follow the enemy. For the first three exercises in this category, it was relatively easy to understand how to scan for a robot and fire at it. In the final exercise I had problems of trying to have the gun track the enemy relative to the distance from enemy to my robot. I had to calculate the angle of my robot and its gun relative to the enemy so that the robot knows where to follow the enemy.
            Overall, I think this project is great for people to get familiar with coding standards and learn how to read up API of a project. It is crucial to read up an API of a project because it provides descriptions of all the methods used in the class. This project also helps one to be familiar in using an IDE such as Eclipse to code their projects because it provides many features to help one implement their code.