LJ Archive

Programming Life!

Marcel Gagné

Issue #98, June 2002

Here are a few games that are both addictive and educational for those starting to learn programming.

Programmers are like minor deities, François. They begin with nothing but an idea and access to a few random bits and bytes, then voilà! Out of the chaos, they create a new kind of life that takes form on our Linux systems. It is truly amazing, mon ami.

Yes, of course, François. You are quite right. It is a little intimidating as well. Ah, the power. The responsibility. The sheer pleasure of creating something from nothing. Not just intimidating, mon ami, but intoxicating. Speaking of intoxication, our guests will be here shortly. Tuscany sounds good tonight. Why don't you run down to the cellar and bring up the 1997 Vino Nobile di Montepulciano? Vite, François! They will be here any moment.

But they are here already. Bon soir, mes amis! It is an exceptional pleasure to see you here again at Chez Marcel, where fine Linux fare is always on the menu. Please sit. François is even now in the cellar bringing up today's wine. Before you arrived, we were talking about the joys of digital creation, programming, which you may have noticed is this issue's theme. The Linux systems you use here in the restaurant and at home are the product of numerous talented and passionate programmers, all of whom set out to contribute something to this community. Sometimes what they create is a kind of cybernetic life, creatures and robots of various incarnations. If you have ever sat on the sidelines thinking to yourself that you would also like to join this group of creators, then tonight's menu is for you. Ah, François, merci. Please pour for our guests. Be generous!

One way to learn something new is through games, and so it is with Java. IBM offers something called Robocode, a programming environment where you can create Java robots that battle it out in an arena on your screen. Each battle goes through several rounds, and stats are gathered so that you can see how your favorite robot is doing. From this information, you can tweak various parameters and make your robot more efficient. Will stopping and turning every 30 steps instead of every 100 increase your chances of success? The way to find out is to make the changes, compile, run and watch the results. This is a great starter for the new Java programmer; it can also be addictive.

To get started with Robocode, you will need the Java developer's kit. You can get a free copy from java.sun.com/j2se; simply download the package you need. I used the RPM install on my system, but you also can get a GNU tar shell script for other Linux distributions. I should probably warn you about that installation—the binaries did not get installed in the standard bin directories. To continue, I had to change my PATH to reflect this:

export PATH=$PATH:/usr/java/jdk.1.3.1_02/jre/bin

Once you have Java installed, head on over to IBM alphaWorks for your copy of the Robocode package, located at robocode.alphaworks.ibm.com/home/home.html. You'll find the installation process quite painless, as there is really nothing to compile. Simply run the installation program with Java using the following command:

java -jar robocode-setup.jar
The first thing you'll encounter is a window that asks you to accept the license agreement. This will be followed by a suggestion for an installation directory. I accepted the default, a directory called robocode in my home directory. When the installation completes, you can start the program with this:
cd $HOME/robocode
./robocode.sh
When you start the program, you'll see the Robocode arena where robot tanks do battle. Click Battle on the menu and select New. A card will appear asking you to select robots for the coming battle. All these are prefixed with “sample.”, and each has a certain set of characteristics preprogrammed. For example, start with sample.MyFirstRobot and sample.Spinbot. Before you go ahead and click the “Start battle” button, choose the battlefield tab. For my 1024 × 768 desktop, I chose a nice, unassuming 800 × 600 battlefield. Finally, there are some rules to follow, defined under the rules tab, including the gun cooling rate and inactivity time. Be aware of these, but for now accept the defaults. Ready? Allons, mes amis. Press Start Battle.

You get robots like Sitting Duck, which just sits there and waits to be blown up. You can also work with Crazy, a robot whose motion and firing pattern appears mostly random. Each can be loaded and modified to behave in ways different from the original programming definitions.

If, like me, you were not entirely satisfied with the outcome of this battle, it is time to get coding and modify your robot. The first time you decide to modify your robot, Robocode will build Jikes, the compiler (it even tells you to go get a cup of coffee). Oui, mes amis, I was asking the same question myself. How old should a programmer be before he or she starts on the real java? For a look at the robot editor and a game in progress, have a look at Figure 1.

Figure 1. Editing Robots with Robocode

The easiest way to learn is to work with the sample robots, and then venture out on your own when you feel more comfortable. When the compiler is ready, you'll be presented with Robocode's development environment. Click File, Open and then choose “sample”. You'll get a new list with all the sample robots (such as Ramfire). The code for each of these robots is well documented, so a new programmer can quickly grasp the concepts of what is happening. For instance, what should your robot do when it hits a wall:

/**
      * onHitWall:  Handle collision with wall.
      */
     public void onHitWall(HitWallEvent e)
     {
            // Bounce off!
            reverseDirection();
     }

The various methods for the robot class are a snap to catch on to, ahead(), back(), fire(), fireBullet(), donothing() and so on. You'll find documentation for the API in your installation directory under javadoc/index.html.

François, if you would be so kind as to refill our guests' glasses, I have another similar programming environment that I would like to share with them.

Andreas Agorander's DroidBattles works on a similar principle as does Robocode. Instead of using Java as its language, however, this program works in an assembler-like language. At this low level, it's as close as you can get to the heart of your software creation. The program itself is built using the 2.X Qt libraries, so those of you running KDE 2.X should have no trouble building the program. But, mes amis, before you build it, it must first come to you, or rather, you must come to it. The latest source is available at www.bluefire.nu/droidbattles/index.html.

The strange thing with this installation was that all the files in the archive were datestamped with a future date, which gave me a little grief when I got to the ./configure step. To make things right, I used the touch command to stamp everything with the current date.

tar -xzvf droidbattles-1.0.4.tar.gz
cd droidbattles-1.0.4
./configure
make
make install

What makes DroidBattles fascinating is that you build the robot in code as well as in hardware, adding CPUs, memory, engines, plasma guns and scanners. When you start the program by typing droidbattles &, you'll get a control panel like the one in Figure 2. To do battle, you need to create and program at least one robot. Pressing the Bot-creator button at the top of the panel is the first step toward unleashing your mechanical warrior.

Figure 2. DroidBattle's Control Panel

You'll be presented with a kind of shopping list for your robot's physical attributes, as well as a window for code. For those who have never looked at assembler, the code will seem a little strange but not for long. These instructions are for the CPU itself—not the one in your computer but the one onboard your robot. The instructions aren't complicated, and a complete manual comes with DroidBattles; just click Documentation on the panel.

Incidentally, if you go ahead and add tons of hardware to your robot, you may be able to assemble it, but the system won't let you run it. “Why?” you ask. The config editor defines the parameters we must adhere to when building our robots. For instance, you define a maximum number of devices or memory, as well as the cost of these items. Furthermore, a robot has a maximum cost and each of those devices cost money. (Fear not, mes amis, this is virtual money.) That cost is determined by how good a particular device is. Plasma guns cost anywhere from $50 to $3,500. These parameters allow you to mix and match the various characteristics within a limited framework. Will you stand a better chance in battle with two plasma guns but only half the processing power? Make the changes, save your robot and find out.

Finally, when you are through, click Tests in the Bot-creator and choose “Quick battle”. You need to select at least two robots to have a battle, but they can be the same ones. The battlefield appears (see Figure 3) and the game is on. Click Play and watch the fun. As the battle progresses, you can click Pause and then switch to single-step mode. When running in test mode, note the debug window that pops up and displays memory locations, registers and instructions as they are being executed.

Figure 3. The DroidBattle Battlefield

This is all very geeky and a great deal of fun. You might also want to watch Andreas' page, as he is in the process of setting up a DroidBattle server.

Well, mes amis, I fear that the time has gone by very quickly today. One of the most difficult aspects to starting down a road like programming is deciding what it is you would like to create. Programs like Robocode and DroidBattles set the stage for you and draw you into a tournament where the code seems more like play than work.

Thank you once again, mes amis. Your visit to Chez Marcel is always a pleasure. Relax. Finish your wine. Enjoy. Until next month. A votre santé! Bon appétit!

Resources

Marcel Gagné (mggagne@salmar.com) is president of Salmar Consulting Inc., a systems integration and network consulting firm and the author of Linux System Administration: A User's Guide, published by Addison-Wesley.

LJ Archive