It’s not likely that there will be any Star Wars prequels featuring a young Scott McNealy battling the forces of an evil empire with the good, “mocha” side of Java. But you can still get your fix of Java-based, gun-blasting sci-fi action, complete with cute robotic droids, and learn about the language in the process, with IBM AlphaWorks’ free programming game, Robocode.

Robocode is a new wrinkle on classic geek games like Robot Battle, where players program virtual robot tanks that move around an arena and attempt to destroy all other robots by shooting or ramming them. In Robot Battle, this is done using a proprietary language, while in Robocode, the programming is done using Java.

Building the perfect killing machine
You create your robots by extending one of two robot base classes that the game provides and by writing code to control your robot’s movement and its reaction to certain events, like another robot moving across its field of vision, being hit by fire from another robot’s gun, or the completion of a movement order. You can then watch your creation duke it out with other robots in a battle-royale (Figure A) that would make the hosts of Comedy Central’s Robot Wars blush.

Figure A
Go lca.MontsBot!

The game’s basic Robot class provides you with a simple, easy-to-work-with, but not extremely capable robot. The AdvancedRobot base class, on the other hand, gives you the foundation for building a much more capable but more complicated robot by giving you greater control over its actions. For example, Robot-based robots can move only in straight lines forward and backward; they cannot turn while moving. However, robots extending AdvancedRobot can turn while they move in either direction.

To guide you in your quest to create the ultimate death-dealing cybertank, the game includes the source for a few robot designs. These sample robots each represent a different basic strategy, from the wall-hugging Walls robot to the try-to-run-you-down RamFire robot. Any of these samples can serve as a template for you to use when you build your own robot.

Of course, there’s a point to all this mindless carnage. While you are programming your violent robotic creations, you are learning about Java’s syntax, event-handling system, and object-oriented features. Robocode is definitely an entertaining way to learn about Java.

Not as easy as it sounds
Even if the force is strong with you, and you’re already a Java-knight, building a killer robot is still no walk in the park. When pitted against the stock robots that come with Robocode, most of my early creations died quick embarrassing deaths. I lost count of the number of times my robot got stupidly stuck in a corner or blasted repeatedly from behind while blithely staring in the other direction. In my case, the problem was usually some forgotten bit of the basic high-school trigonometry that’s needed to maneuver a robot around the arena. Wow, Ms. Post was right, I really did need to learn that stuff.

Regardless of my initial difficulties, I had a lot of fun and wasted quite a few hours pecking away at my robots’ code, trying to increase their survivability. Whatever your skill level with the Java language, you’re sure to find Robocode to be a worthwhile diversion.

What do you suggest for learning Java?

Do you know of some interesting and innovative resource for learning Java? How about suggestions for those of us without a lot of time or money who still want to share in the Java revolution? Send us an e-mail with your thoughts and suggestions or post a comment below.