# MicroCatbert's Code

Robo Home | Changes | Preferences | AllPages

The code:

```package dil;
import robocode.*;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.io.*;
/**
* MicroCatbert - a robot by Dil
* I used some code from DuelistNanoMelee 1.0(goto function) and Calypso 3.6(some average Velocity targeting gun turning).
*/
{
static double v, vi;
static String name;
/**
* run: MicroCatbert's default behavior
*/
public void run() {
//Classsic Catbert colors
setColors(Color.red,Color.red,Color.white);
}

/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
//all purpose vars
//targeting
//velocity for targeting init
if(vi+getRoundNum()*getOthers()==1) {
try {
// Try to get the avg vel
} catch (Exception a) {}
vi=1;
}
//driving
double dX,dY,mX,mY,tA,nTA;
dX= getBattleFieldWidth() -((mX=getX())+Math.sin(inf)*e.getDistance());
dY= getBattleFieldHeight() -((mY=getY())+Math.cos(inf)*e.getDistance());
setTurnRightRadians(nTA = Math.atan(Math.tan(tA = ((Math.atan2(dX - mX, dY - mY) - (inf = getHeadingRadians())) + (7.0 * Math.PI)) % (2.0 * Math.PI) - Math.PI)));
setAhead((tA == nTA ? 1.0 : -1.0) * Point2D.Double.distance(mX, mY, dX, dY));

fire(Math.min(getEnergy()/5, 1200/e.getDistance()));
}

public void onDeath(DeathEvent e) {
if(name != null) {
try {
PrintStream w = new PrintStream(new RobocodeFileOutputStream(getDataFile("v"+ name +".dat")));
w.println(v);
w.close();
} catch (Exception a) {}
}
}
public void onRobotDeath(RobotDeathEvent e) {
onDeath(null);
}
}
```
Suggested changes:
```if(getRoundNum()==getNumRounds() && name != null)
```
As pretty and small as that code is, it will never return true. It's misdocumented in the Robocode API - getRoundNum?() returns a number from 0 from getNumRounds?()-1. And eventually you may find that a gun that learns as fast as this one isn't worth the space for saving data, of course you said that you were going to add a pattern-matcher anyways I think. -- Kawigi

It's completely unecessary to check the round number before writing the data anyways. Write it down after every round and you both save bytes and gain the convenience of being able to interrupt matches and still have the data file updated. -- PEZ

Data bug fixed, and the pattern matcher is coming. --Dil

if you want smaller code use robocode.util.Utils.normalRelativeAngle() to normalize your angles -- rozu

Robo Home | Changes | Preferences | AllPages