Wednesday, September 9, 2009

Battle with Robocode

Robocode is an interesting piece of code that you can work on to brush up your Java programming skills. I find Robocode challenging, as it requires numerous programming, math, and of course, debugging skills.

We were intructed to download and install the source code to work with Eclipse IDE. Although I barely had an experience working with Eclipse in the past, the installation process went quite smoothly. I find it rewarding to finally see my Robot tank on my screen with my name on it after following series of complex instructions.

I have created various robot tanks that can do certain movements, such as moving in a circle, move to all corners, etc. The following is a list of movements I have produced

Movement01: The minimal robot. Does absolutely nothing at all.
Movement02: Move forward a total of 50 pixels per turn. If you hit a wall, reverse direction.
Movement03: Each turn, move forward a total of N pixels per turn, then turn left. N is initialized to 10, and increases by 10 per turn.
Movement04: Move to the center of the playing field and stop.
Movement05: Move to the upper left corner. Then move to the lower right corner. Then move to the upper right corner. Then move to the lower left corner.
Movement06: Move to the center, then move in a circle, ending up where you started.
Tracking01: Pick one enemy and follow them.
Tracking02: Pick one enemy and follow them, but stop if your robot gets within 20 pixels of them.
Tracking03: Each turn, Find the closest enemy, and move in the opposite direction by 100 pixels, then stop.
Firing01: Sit still. Rotate gun. When it is pointing at an enemy, fire.
Firing02: Sit still. Pick one enemy. Only fire your gun when it is pointing at the chosen enemy.
Firing03: 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).
Firing04: 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).

All of the movements through 01 to 04 I was able to implement successfully. Movement05 involved many trigonometric computations and I was not able to get the Math functions in Java to work properly, so I ended up dropping the diagonal movements all together. The robot in Movement05 now interestingly reaches all four of the corners without any diagonal movements.

I also had a couple of problems with tracking02. It seems that when I try to code the robot to stop 20 pixels before reaching the target, it does not take the size of the robot itself into account. With the setting Distance - 20(pixels), my robot ended up hitting the target everytime. I solved this problem by adding about 40 pixels to on top of the 20 pixels to compensate for the size of the tank. After applying the change, my robot now seems to follow its target about 20 pixels behind.

Another problem that I had occured while developing tracking03. I programmed the tank so that it would store the name and the distance to the robot who is the closest. I could not fully test if everything was working correctly as Robocode battlefield tend to get pretty chaotic when it has more then 3 robots running around. The robot also does not fully stop after traveling 100 pixels. It only briefly stops after each move and attempts to search again for the nearest robot.

If you would like to download my Robocode, feel free to do so.

I will also also provide a link to the source forge site here:
http://robocode.sourceforge.net/

No comments:

Post a Comment