Wednesday, September 9, 2009

Robocode: The Battle Bot Arena!


Robocode is an excellent way to learn intermediate Java in a fun interactive way. It lets you see what your doing instead of guessing with a traditional debugger.

So what is Robocode?

Robocode is a game where you program a robot that fights other robots in an on screen arena. You extend the Robocode Class which lets you inherit a wide variety of methods to use to attack your enemy. The API specifies many classes, but the most interesting is the ScannedRobotEvent Class. While using this Class, if the radar on your robot scans an enemy, it triggers the ScannedRobotEvent event. Most of the code you put into your robot will typically go in this method.

My Experience

To start my Robocode adventure I engaged in creating some simple robots for basic movement, tracking and firing after setting up Eclipse IDE for OSX. I didn't encounter any problems but that may be because I am familiar with Eclipse. Robocode also gives you a refresher on your trigonometry skills if its been a while and forces you to get really familiar with the Robocode API and the Java Math Class. With the skills I've learned after completing these robots, I feel I am about ready to start thinking about more advanced strategies for competition. Here is a list of the robots I've implemented so far successively:

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


What's Next?

My next task is to create a competitive robot. I have been practicing and reading about techniques for a one on one competition against other classmates. Even though I still have a bit to go before I can produce a really competitive robot, I have a few ideas. One of them consists of creating a robot that zig-zags side ways, perpendicular to the enemy to avoid being hit. My robot will remain at a striking distance but not get too close. If the other robot attacks me, I will flank it and hit it from behind. A circular zig-zag pattern seems to be a great way to play defensively and offensively at the same time. When I check the health of the other robot and notice that it is getting closer to death, my robot will play a bit more aggressive and get closer to the enemy while increasing fire power.

For More Information and Sample Code

If you want to see what this Robocode fuss is all about a great link to get you started is here:
http://robocode.sourceforge.net/

If you want to take a look at the robots I've implemented you can do so by downloading the package here:
Download My Robocode Samples

-Remy

0 comments:

Post a Comment