Tuesday, September 8, 2009

Chronicles of Robocode

Robocode is game that battles virtual robots people have created in an arena. The code for each robot is written in Java. Programming a robot is a fun way to improve your programming skills. It’s easy to get started and you can program a simple robot within minutes, however, creating more advanced robots can take a lot more time and research. There is also a certain degree of math involved in getting your robot to behave the way you want it to, so it's a good idea to brush up on your trigonometry.

There are several aspects to a robot including movement, tracking enemy robots, and targeting/firing at enemy robots. We started slowly with simple robots in each category to familiarize ourselves with basic commands and successively built upon the previous robot to build more complicated robots. A list of the robots I successfully created are listed below.

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

I ran into some problems while creating the Movement04 robot. At first, I couldn't get my robot to face the correct direction to move to the center. I eventually found that the angles I was working with were in different coordinate systems, one in degrees and one in radians. After correcting this error, I was able to get the robot working. This problem helped my debugging skills because at first glance the code looked correct. Upon further investigation, I was able to figure out the problem.

I learned a lot about how Robocode robots behave through creating the different robots. There are some details that I'm not quite clear on yet, but I feel that I have a good grasp on most of the basic robot behaviors. Using the Robocode API was very helpful in figuring out how things work. I'm looking forward to creating more advanced robots. I think that a strategy that I may use is the random movement strategy because it would make targeting the robot difficult. I'm not sure what strategy I'll use yet, but I'm looking forward to seeing how my robot stacks up against others.

The project containing the source code for the robots I created can be found here.

No comments:

Post a Comment