[Home]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).
 */
public class MicroCatbert extends AdvancedRobot
{
	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);
		setAdjustGunForRobotTurn(true);
		setTurnRadarRight(Double.POSITIVE_INFINITY);
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		//all purpose vars
		double inf=getHeadingRadians()+e.getBearingRadians();
		//targeting
		//velocity for targeting init
		if(vi+getRoundNum()*getOthers()==1) {
			try {
				BufferedReader r = new BufferedReader(new FileReader(getDataFile("v"+(name=e.getName())+".dat")));
				// Try to get the avg vel
				v = Double.parseDouble(r.readLine());
			} catch (Exception a) {}
			vi=1;
		}
		v = 0.995*v + 0.005*e.getVelocity()*Math.cos(e.getHeadingRadians()-getHeadingRadians());
		setTurnGunLeftRadians(Math.sin(getGunHeadingRadians() - (inf + (e.getEnergy() == 0 ? 0 : -Math.sin(inf)*Math.asin(v/(20-3*3))))));
		//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));

		//radar
		setTurnRadarLeftRadians(getRadarTurnRemaining());
		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
Edit text of this page | View other revisions
Last edited July 13, 2003 17:17 EST by Dil (diff)
Search: