Wednesday, September 30, 2009

It's time to package!

To deploy a software, it is always nice to have all the files packaged together. We don't want to look around for some third party libraries in order to run the code successfully. Organizing the dependency in a software project is quite troublesome unless using Apache Ant build system. Developer can define different tasks in an Ant build system. All those tasks are coding in xml files, which is kind of neat and structured. In addition, it can integrate the Apache Ivy so that it will check and download the missing third party libraries automatically during a build.

Another issue I was working on recently is quality assurance. Quality assurance is a critical part in a software development. However, mostly people can get really bored by working on some tasks like that. Is there any tool that can somewhat automate this procedure? The answer is yes! There are plenty third party tools designed for automated quality assurance. Here are three popular tools: CheckStyle, PMD, FindBugs.

CheckStyle -- will check the coding standard for you. It can be really helpful if you want to make the code clean and readable.
PMD --------- will check the syntax problem of the code. For example an empty block for a catch statement.
FindBugs ---- will check how the code works. It will try to find the part that make no sense and consider it as a potential bug.

All these tools are very user friendly. Every time after checking, they will generate reports in html so that developer can check the result quickly. The first time I run those tools on my robocode project, I got 132 errors for CheckStyle which means my coding standard is poor; and I got 3 warnings for PMD which is about reassigning a value to itself (I think this one is acceptable); and I got no bug for FindBugs. It take me about 10 to 20 minutes to fix those problems. I think it is much faster than checking the code manually.

I adapted Ant to my robocode project. The new robocode build system is available at:
http://www2.hawaii.edu/~yichi/robocode-xyc-yichispecialbot-1.1.930.zip

Monday, September 21, 2009

Let's fight smartly

After working on RoboCode for a week, it is the time to start implementing my own strategy. This time, I will try to create a robot (YichiSpecialBot) that can beat the sample robots (Crazy, Corner, Fire, Sitting Duck, Spin Bot, Ram Fire, Tracker, and Wall) that come with the robocode package. Following is my strategy.
1.Movement
To win a battle, you have to survive first. Movement is a very important part in the battle strategy. After playing around with the sample robots, I figure that "Wall" is the one that can survive most of the time. Why it can survive? I think it is because Wall is moving constantly so that it can dodge the bullet effectively. Therefore, I am going to use the same strategy as Wall. However, if I do the exactly same as Wall, I probably cannot beat it. Instead of moving on the edge of the battlefield, I decide to make my robot follow the diagonal of the battlefield if it is moving horizontally and follow the edge if it is moving vertically. In this case, when my robot played against the Wall, it can reach the corner earlier than Wall so that it can set an ambush.
2.Targeting
Since my robot keeps moving during the battle, it will not try to track any enemy. It will fire at any one it scans. However, At the beginning of each turn, it will turn the gun a little bit so that it can scan a different area. Also if it hit by another robot or bullets, it will turn its gun to the direction that the event happened.
3.Firing
It is not smart to fire all the time. I set my robot only fire if the enemy is within 500 pixels and fire hard if the enemy is within 100 pixels. I also tried to implement a perdicting function in my robot so that it can perdict the enemy movement according to the distance of the enemy, heading of the enemy, and the velocity of the enemy, but it does not work properly so far:(

Testing Result for YichiSpecialBot (10 battles):
vs Sitting Duck: 10 wins
vs Crazy: 10 wins
vs Fire: 10 wins
vs Corner: 8 wins
vs Ram Fire: 4 wins
vs Tracker: 7 wins
vs Wall: 7 wins
vs Spin Bots: 7 wins

The result is reasonable. It can reliablly beat Sitting Duck, Crazy and Fire. It has some advantages when fighting with Corner, Wall, Tracker and Spin Bots. It does not work well when fighting against Ram Fire. I think the problem is my robot does not run a way when hit by a robot. Therefore, Ram Fire can take advantage because it is design for short distance battle. I should probably find a solution to that later on.

Here is my code for YichiSpecialBots:
http://www2.hawaii.edu/~yichi/xyc.YichiSpecialBot.1.0.jar

Wednesday, September 16, 2009

Dig into the sample Robo

In the robocode package, there are plenty sample robots. Before implementing the battle strategy for a robocode robot, it is a good thing to take a look at those sample robot. Here is some analysis done by me. I will focus on three aspects: movement, targeting, and firing.
1. Corners
-movement: Corner bot will move to a corner at the beginning of the battle and stay there until battle is over. If there are more than 75 percent robot still alive when it dies, it will switch to another corner.
-targeting: It will not target any robot while moving. When it sits down, it will turn the gun to the left first. If the gun has been turned for 90 degrees, it will change the direction of turning the gun. No target locking involved.
-firing: It will fire different bullet according to the distance and its energy. If it is weak fire or the enemy is too far away, fire a small bullet. If the enemy is within 200 pixels, it will fire a middle bullet. If the enemy is within 50 pixels, fire a big bullet.
2. Crazy
-movement: Crazy bot will move and turn at the same time. The first time, it will turn 90 degrees then reverse the heading. However, the rest time, it will turn 180 degrees then reverse.
-targeting: It does not move radar or gun. It is always watching the front side of itself. No target locking involved.
-firing: It always fire the same size of bullet.
3.Fire
-movement: Fire bot will only move when it is hit by a bullet. It will move back and forth within a certain distance.
-targeting: It will turn the gun around slowly. If it sees another robot, it will fire. No target locking involved.
-firing: It adjusts the power of bullet according to the distance and its energy. If the enemy is within 50 pixels and the fire bot's energy is more than 50, it will fire hard. Also if it is hit by another robot, it will fire at that robot at once.
4.Walls
-movement: Wall bot will move to the wall at the beginning of the bettle. Then it will keep moving from corner to corner.
-targeting: It does not move radar and it keeps the fun pointing to the front. It will fire whenever it finds another robot. No target locking involved.
-firing: It fires the same bullet all the time.
5.Tracker
-movement: Tracker bot will not move until it finds it target. Then it will move towards its target. If it is too closed to the target, it will move back a little bit.
-targeting: The direction that Tracker bot will turn its gun to depends on how many turns it has tracked for its target. If it does not see its target for 10 turns, it will start looking for a new target.
-firing: It will only fire when an enemy is really close or it is hit by another robot. It never fire at some robot that is more than 150 pixels away.
6.SpinBot
-movement: Spin bot will move and turn at the same time. It basically keep moving on a circle route. Only turn a little bit if it hits another robot.
-targeting: It does not move radar or gun. It is always watching the front side of itself. No target locking involved.
-firing: It fires the same bullet all the time.
7.RamFire
-movement: RamFire bot will keep turning until it detects an enemy. Then it will move towards to that robot and try to run into that robot.
-targeting: It does not move radar or gun. It is always watching the front side of itself. No target locking involved.
-firing: It only fires when it hit a robot. The bullet power that it fires depends on the target's energy. It will try to fire a bullet that contains a weaker energy than the target. Then move ahead and try to crash the target by using its body.
8.Sitting Duck
-movement: Sit...
-targeting: Never mind...
-firing: I am just a peaceful little duck sitting in the battlefield...

As a summary, I think all of the bots I listed above are sample bots. They basically adapt a simple strategy. However, I think there is one principle for this game that will help all kinds of strategy: "Fire hard and fire carefully".

Monday, September 14, 2009

Is Coding Standard Necessary?

Last week, I worked on refactoring my code. One major task was adapting the coding standard. After gone through the coding standard, I realized that most code I wrote before did not follow the standard. It is because most of the time I just code by myself, and I don't think other people will ever try to take a look of my code. In that case, no matter how I write the code, it should be easy for my to catch on later. However, it does not work that way. Sometimes when I work on a complicate program, it took me quite a long time to understand the code, even though it is written by me. I think keep the code in standard is seriously important. It will make the code more readable and more organized. The most important thing is that it will bring you a good mood while coding. I used to work on debugging some old code. When I saw some messy code, I just felt so exhausted and desperated. Usually, that is a pretty bad thing:(

I agree with most the standard, but there are several rules that I am not so sure. The top thing bothers me is puting the open brace at the end of the line. I really see no differece between putting the open brace in a new line. To me, putting the open brace in a new line seems more organized and easy to read.

Anyway, I think it is a good time to start following the coding standard. Although you might need some extra efforts to made the documentation, it will definitely pay you back in the future.

Here are the links that put my new RoboCode source code: http://www2.hawaii.edu/~yichi/RoboCode-YichiXu.jar
http://www2.hawaii.edu/~yichi/RoboCodeXYC.zip

Wednesday, September 9, 2009

Go Robo~

In the past week, I was working on some basic functionality in RoboCode. The RoboCode system is very cool. I can debug without re-run my code. All you need is simply pause the current battle. Then when you press the "restart" button, all the change in the code will be applied. That's very amazing to me.

I worked on moving, tracing and aiming for a robot in Robocode. The Robocode Documentation really helps when you trying to do some warm-up program. The Robocode API is quite powerful. However, I think it should contain some methods that will get the direction to a point in the battlefield. Right now, I have to use Java.Math.atan method to find the angle that robot should turn. Also it is very accurate since too many number processing going on. Therefore, I still working on how to make a robot move circularly properly. Right now, I make the bot move in a square route:( I think it is so hard to move in a round circle because if you want your bot move at 1 pixel per turn (as small as possible) and you turn 1 degree per turn (as small as possible again), it is just too slow.

Another issue in Robocode is scanRobotEvent. This class contains the method that will return the distance between two robots. However, in my understanding, it looks like this scanRobotEvent does not happen continuously to one robot. For example, bot A saw bot C. As long as bot C is within bot A's sight, scanRobotEvent does not occur. If that so, how does it update the new distance etc.?

Over all, Robocode is a very nice project to work on. It can be simple or complicate. I enjoy the most from Robocode is the satisfaction after I see my robots working correctly:)

My RoboCode source code is available at http://www2.hawaii.edu/~yichi/RoboCodeXYC.zip