Skip to main content

Secrets from the Robocode masters: Dodge bullets

Jae Marsh, Developer, Independent Consultant
Jae Marsh is a developer. He enjoys rock climbing, improvisational piano playing, and now, Robocode!

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:  May 2002
Level:  Introductory
Activity:  3898 views

To make a winning Robocode robot, you must be able to hit your opponent more than your opponent hits you. Making your robot target your opponent is a fine art, but what if your opponent couldn't target you? Using a simple trick and some guesswork, DodgeBot, listed here, shows you how to dodge bullets.

Before I show you the trick, you must know some Robocode basics. First, Robocode uses a single energy store for both offense and defense. This leads to interesting game play decisions: you have to decide when to use your energy to fire at the opponent and when to keep it in reserve to withstand potential damage.

Second, your robot has very limited information about the world around it. It can see the distance, bearing, heading, velocity, and energy level of other robots. No, it cannot see bullets, but maybe from these clues, you can guess how to detect when they are fired.

DodgeBot stands still and continuously tracks the previous energy level of the opponent. When it drops by a certain range, DodgeBot assumes the opponent fired and it moves left or right. Surprisingly, this will baffle most robots' targeting methods. Either they will fire directly at their target, or they will try to project your position based on your speed and direction. Since your robot is not moving, both algorithms will fire right at it. And your robot, guessing this, will make a small hop to the side and let the bullet continue on its way. Figure 1 shows DodgeBot in action.


Figure 1. DodgeBot versus Tracker (Gotcha!)
DodgeBot versus Tracker

Listing 1 shows the DodgeBot code. The main block of code is executed every time the radar senses an enemy. First, DodgeBot maintains itself at right angles plus a 30-degree incline toward the opponent. Staying at 90 degrees gives a robot the greatest ability to move out of the way of bullets. The extra 30-degree incline causes the robot to be a bit aggressive and move closer to its target over time. Next is the key piece of code: if the robot senses an energy drop between 0.1 and 3.0 (the range of firing power), the robot will immediately switch directions and move to the left or right. Simple. It will switch the direction of its gun and radar sweep, assuming that if it saw a robot in its last sweep, it will find it again if it sweeps over the same area. Then the robot will fire. Because I left the gun and the radar linked and because the scanner will be called at the exact moment it is facing the opponent, the gun will fire directly at the opponent. Finally, I record the opponent's energy for the next time around.


Listing 1. DodgeBot code
                
import robocode.*;

public class DodgeBot extends AdvancedRobot 
  double previousEnergy = 100;
  int movementDirection = 1;
  int gunDirection = 1;
  public void run() {
    setTurnGunRight(99999);
  }
  public void onScannedRobot(
    ScannedRobotEvent e) {
      // Stay at right angles to the opponent
      setTurnRight(e.getBearing()+90-
         30*movementDirection);
         
     // If the bot has small energy drop,
    // assume it fired
    double changeInEnergy =
      previousEnergy-e.getEnergy();
    if (changeInEnergy>0 &&
        changeInEnergy<=3) {
         // Dodge!
         movementDirection =
          -movementDirection;
         setAhead((e.getDistance()/4+25)movementDirection);
     }
    // When a bot is spotted,
    // sweep the gun and radar
    gunDirection = -gunDirection;
    setTurnGunRight(99999*gunDirection);
    
    // Fire directly at target
    fire ( 2 ) ;
    
    // Track the energy level
    previousEnergy = e.getEnergy();
  }
}

The robot that I used this trick on, Wolverine, uses some additional sensor information to make the sensing more accurate. When an opponent hits my robot, the opponent will be recharged. When my robot hits an opponent, the opponent's energy level will drop. A robot can sense these two events, and Wolverine uses the information to cancel out the corresponding energy fluctuations.

There are problems with this trick. The robot moves as soon as a bullet is fired, so that it may end up moving back into the line of fire. It might be better to move when the bullet is expected to arrive.

An even bigger problem is that even though you can confuse common targeting, side-to-side stepwise movement is, in fact, quite predictable. The best way to use this trick is, perhaps, to let the information guide your movements, but not control them.

You may be thinking that this trick is so simple that you could have thought of it yourself. Good. That is how this game is played, and that is exactly why it is so addictive. Robocode is like a game of chess where each new move is a new idea.


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

Jae Marsh is a developer. He enjoys rock climbing, improvisational piano playing, and now, Robocode!

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=242231
ArticleTitle=Secrets from the Robocode masters: Dodge bullets
publish-date=052002
author1-email=jaemarsh@yahoo.com
author1-email-cc=jaloi@us.ibm.com

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