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. 

Wednesday, August 31, 2011

An Analysis of the FizzBuzz Implementation

FizzBuzz should be a pretty simple implementation for computer science graduates right? A program that outputs number 1 thorugh 100, printing only "Fizz" when the number is multiple of 3, "Buzz" when it is multiple of 5, and "FizzBuzz" when the number is multiple of 15. Simple enough, but I was told by a professor during a class that most employee when they are recruiting, they ask the applicants to implement the FizzBuzz program. Should not be that hard right? Surprinsingly, my professor furiously told us that most of the applicant failed to implement the program. I was surprised. Then he assigned us to write the program.

Below here is the FizzBuzz program I wrote in Java.


I time myself and it took me around 8 minutes to write it. I was being a newbie, because I tried to put the for loop inside the method which will might not return any value if all of the statements were false. That was my mistake and it took me a couple of minutes to figure it out. Other than that, I am pretty much comfortable with eclipse cause I have used it since my very first Java programming class
I guess if a programmer knows how to program, but have not touch or used a program for a long time, then he/she might run into some problems as me when coding again. Learning new languages program takes time and some motivation. Also, it requires for the programmer to be able to keep up with program as new updates and features are developed so fast that it is easy to fall behind.

Monday, August 29, 2011

Open Source Software and the Three Prime Directives

Software: Sweet Home 3D


Overview.
Sweet Home 3D is a java simulation for interior design of houses in which the user can place furniture in a 2D plan with a 3D preview. Intended for new users that wants to learn more about interior design or for architects who wants to model "simulated" houses.
The software is written in Java with a Java Swing interface. Developed by Emmanuel Pyubaret in 2006, it has since then became a widely popular-free open source software that has been updated very often (Latest version 3.3 - July 26, 2011).

Prime Directive # 1: The system successfully accomplishes a useful task.
Users can find as many as other interior design software, paid or free. However, Sweet Home 3D can accomplishes the tasks that a newbie user wants. Having a 2D plan on the top right, a 3D preview plan on the bottom right, and a catalog of furniture on the top left side, makes the use of the software pain-free (but I would say not that very straightforward) for the users. Users can create walls, rooms, take a picture or make video of the 3D preview plan, etc.
Sweet Home 3D does not have the luxuries of tasks that other paid 3D interior design software have, but it is worth a try.


Prime Directive # 2: An external user can successfully install and use the system.
Easy to download, and comes with an .exe file which is pretty straightforward to install the software. Developed for Windows, OS X and Linux;  therefore pretty much any user can run it. Users also can look up for the User's guide online in which detailed instructions for the installation and the use of the software are provided. 
The use of the software can take some time for new users to be comfortable with it. After that, users will just leave it to their imagination to create new simulated houses. 


Prime Directive # 3: An external developer can successfully understand and enhance the system.
Looking the Developer's guide and the documentation of the software, it is extremely organized and kind of easy to read. It has a nicely drawn picture of the UML model which describes all the 20 classes used to create the Sweet Home 3D software.
Developers can create plug-ins using the packages available which allows them to use any of the Home Sweet 3D features without modifying the source files of the most current version. A developer who is comfortable using Java, reading and going through the documentation should not be hard, but eventually will consume a lot of time.  
The documentation of Home Sweet 3D can be found here: http://www.sweethome3d.com/javadoc/index.html.



Links: