Skip to main content

Secrets from the Robocode masters: Extensible/reusable robots

Ray Vermette (ray.vermette@ccra-adrc.gc.ca), Programmer, Norwegian University of Science and Technology
Ray Vermette is a mild-mannered mainframe programmer and inline skater by day and avid Robocode and Java enthusiast by night.

Summary:  This tip was published in the "Cloak and turret: Learn secrets from the Robocode masters" article in the May 2002 issue of the IBM developerWorks journal.

Date:  Jun 2002
Level:  Introductory
Activity:  1919 views

My first robot was written as one large class file. Initially, this worked quite well, but as I added more smarts to my robot, it grew to the point where it became difficult to manage. I quickly realized that if I wanted a fighting chance against the top robots, I would have to come up with a better design.

The solution I came up with was to break the robot up into modular, reusable pieces, each managing a different aspect of the robot's behavior. I created one class each to control movement, radar, the gun, and aiming. I created classes to track information on an enemy robot and a class to store information on all enemy robots. Other classes soon followed.

My goal in writing each class was to make them as independent as possible, so I could reuse them in different robots, or change the overall design of the main robot class without having to rewrite scanning, movement, aiming, and other functionality.

Even with this modular design, there was still too much code cluttering the main robot class file. A good portion of the code simply knitted the various modules together, and it obscured what the robot was actually doing.

The solution? I created an Abstract robot class called AbstractBot to hide the knitting code, standard initialization, and other nasty details.

The AbstractBot class has several useful methods that simplify radar operation, movement, target selection, aiming, firing, and more. Now, instead of writing code to evaluate all possible targets, pick the best target, turn the radar, calculate how much energy to use, decide which move to make, and so on, I simply extend the AbstractBot class and code something like:

EnemyBot target = pickTarget();
scan(target);
ram(target);
blast(target);


Or like this:

EnemyBot target = pickTarget();
scan(rammingBot);
evadeRam(rammingBot);
aimAndShoot(target);


It's obvious what the robot does, but not how it does it. The details are conveniently hidden in the AbstractBot and other classes that control the radar, moving, target selection, firing, and so on. The code is much easier to read, and consequently, much easier to change. Mission accomplished. Now I just have to concentrate on beating Predator!


Resources

  • Read all of the Secrets from the Robocode masters . This page will be updated as new tips become available.

  • Robocode's creator, Mathew Nelson, maintains the official Robocode site. This should be the first stop for anyone serious about Robocode.

  • RoboLeague by Christian Schnell is a league and season manager for Robocode. It ensures that all possible groupings indeed play their matches, manages the results, and produces HTML status reports.

  • "Rock 'em, sock 'em Robocode" (developerWorks, January 2002) disarms Robocode and starts you on your way to building your own customized lean, mean, fighting machine.

  • In "Rock 'em, sock 'em Robocode: Round 2" (developerWorks, May 2002), Sing Li looks at advanced robot construction and team play.

  • New to Java? Check out "Introduction to Java programming" (developerWorks, November 2004), a tutorial that steps you through the fundamentals of Java language programming.

  • developerWorks: Hundreds of articles about every aspect of Java programming.

About the author

Ray Vermette is a mild-mannered mainframe programmer and inline skater by day and avid Robocode and Java enthusiast by night.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=243963
ArticleTitle=Secrets from the Robocode masters: Extensible/reusable robots
publish-date=062002
author1-email=ray.vermette@ccra-adrc.gc.ca
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers