User:AaronR/Utility Bots
Sometimes, you write a bot whose only purpose in life is to test some tiny little detail of another bot's implementation. Some of the sample bots that come with Robocode fit into this category; for example, Walls is well known as a sanity check for movement (tests avoidance of GuessFactor 0.0) and targeting (tests hitting GF 1.0).
Here are some other bots like this.
WallsReverser
WallsReverser is a bot that can be used for finding bugs in lateral velocity calculations in targeting systems. For the first five rounds, it moves like the sample bot Walls; on the sixth round, it switches to moving in the opposite direction. This bot helped to find a bug in Horizon's gun.
import java.awt.Color;
import robocode.*;
public class WallsReverser extends Robot {
public void run() {
if (getRoundNum() < 5) {
// Set colors
setBodyColor(Color.black);
setGunColor(Color.black);
setRadarColor(Color.orange);
setBulletColor(Color.cyan);
setScanColor(Color.cyan);
} else {
// These are the opposite colors. Clever, huh?
setBodyColor(Color.white);
setGunColor(Color.white);
setRadarColor(Color.blue);
setBulletColor(Color.magenta);
setScanColor(Color.magenta);
}
// turnLeft to face a wall.
// getHeading() % 90 means the remainder of
// getHeading() divided by 90.
turnLeft(getHeading() % 90);
while (true) {
// Move up the wall
ahead(Double.POSITIVE_INFINITY);
// Turn to the next wall
if (getRoundNum() < 5) {
turnRight(90);
} else {
turnLeft(90);
}
}
}
}
SelfDisablingBot
Many robots have a special case for dealing with disabled opponents. This is important, because a gun that ignores the fact that the opponent is disabled could assume that it will continue acting normally and miss, or worse yet, record data based on the opponent being a sitting duck. This utility bot tests your bot's reaction to a disabled opponent.
import robocode.*;
public class SelfDisablingBot extends AdvancedRobot {
@Override
public void run() {
setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
}
@Override
public void onScannedRobot(ScannedRobotEvent e) {
// Generic random movement.
setAhead((Math.random() - 0.5) * 200);
setTurnRightRadians((Math.random() - 0.5) * 2);
}
@Override
public void onStatus(StatusEvent e) {
if (e.getTime() > 100) {
// Stupid self disabling trick.
while (true) {
getX();
}
}
}
}