[Home]Squirrel/Nano

Robo Home | Squirrel | Changes | Preferences | AllPages

package bayen.nano;

import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import robocode.HitByBulletEvent;
import robocode.BulletHitEvent;
import robocode.BulletMissedEvent;
import robocode.HitWallEvent;

import robocode.util.Utils;

/**
 * Squirrel - a robot by Bayen
 *
 * Codesize Reduction Help:  Voidious (Thanks!)
 */
public class Squirrel extends AdvancedRobot
{
	//CONSTANTS:
	private static final double RADAR_EXTRA = 2.2;
	private static final double RIGHT_ANGLE = 1.57;
	private static final double POSITIVE_ONE = 1.0;
	private static final double NEGATIVE_ONE = -1.0;
	private static final double ZERO = 0.0;
	private static final double BULLET_POWER = 3.0;
	private static final double EIGHT = 8.0;
	private static final double SIXTEEN = 16.0;
	private static final double ONE_TENTH = 0.1;
	private static final double HUNDRED = 100.0;
	private static final double THIRTEEN = 13.0;

	//VARIALBES:
	private static double _gunCheck;
	private static double _moveCheck = POSITIVE_ONE;
	private static double _energy = HUNDRED;
	private static double _direction = POSITIVE_ONE;
	
	public void run() {
		do {
			turnRadarRightRadians(HUNDRED);
		} while(true);
	}

	public void onScannedRobot(ScannedRobotEvent e) {
		double bearing;
		
		setTurnRadarRightRadians(Utils.normalRelativeAngle(((bearing = e.getBearingRadians()) + getHeadingRadians() - getRadarHeadingRadians()) * RADAR_EXTRA));
		
		setTurnRightRadians(bearing + RIGHT_ANGLE);
		
		double difference;
		if((difference = _energy - (_energy = e.getEnergy())) <= BULLET_POWER && difference > ONE_TENTH) {
			_direction *= _moveCheck;
		}
		setAhead(HUNDRED * _direction);

		bearing += getHeadingRadians();
		setTurnGunRightRadians(Utils.normalRelativeAngle(bearing - getGunHeadingRadians() + ((_gunCheck<ZERO?((Math.random() * SIXTEEN) - EIGHT):e.getVelocity()) * Math.sin(e.getHeadingRadians() - bearing) / THIRTEEN)));
		
		setFire(POSITIVE_ONE);
	}

	public void onHitByBullet(HitByBulletEvent e) {
		_moveCheck *= NEGATIVE_ONE;
	}
	
	public void onBulletHit(BulletHitEvent e) {
		_gunCheck -= (_gunCheck<ZERO?POSITIVE_ONE:NEGATIVE_ONE);
	}
	
	public void onBulletMissed(BulletMissedEvent e) {
		_gunCheck += (_gunCheck<ZERO?POSITIVE_ONE:NEGATIVE_ONE);
	}
	
	public void onHitWall(HitWallEvent e) {
		_direction *= NEGATIVE_ONE;
	}
	
}


Here it is at 249 bytes. You could gain 2 more by doing "energy diff > 0" instead of "energy diff > 0.1".

package bayen.nano;

import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import robocode.HitByBulletEvent;
import robocode.BulletHitEvent;
import robocode.BulletMissedEvent;
import robocode.HitWallEvent;

import robocode.util.Utils;
//import java.awt.Color;

/**
 * Squirrel - a robot by (your name here)
 */
public class Squirrel extends AdvancedRobot
{
	//private static final double INFINITY = Double.POSITIVE_INFINITY;
	private static final double RADAR_EXTRA = 2.2;
	private static final double RIGHT_ANGLE = 1.57;
	private static final double POSITIVE_ONE = 1.0;
	private static final double NEGATIVE_ONE = -1.0;
	private static final double ZERO = 0.0;
	private static final double BULLET_POWER = 3.0;
	private static final double EIGHT = 8.0;
	private static final double SIXTEEN = 16.0;
	//private static final double THREE = 3.0;
	private static final double ONE_TENTH = 0.1;
	private static final double HUNDRED = 100.0;
	private static final double THIRTEEN = 13.0;
	
	private static double _gunCheck;
	private static double _moveCheck = POSITIVE_ONE;
	private static double _energy = HUNDRED;
	private static double _direction = POSITIVE_ONE;
	
	/**
	 * run: Squirrel's default behavior
	 */
	public void run() {
		// After trying out your robot, try uncommenting the import at the top,
		// and the next line:
		//setColors(Color.red,Color.blue,Color.green);
		do {
			turnRadarRightRadians(HUNDRED);
		} while(true);
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		double bearing;
		
		setTurnRadarRightRadians(Utils.normalRelativeAngle(((bearing = e.getBearingRadians()) + getHeadingRadians() - getRadarHeadingRadians()) * RADAR_EXTRA));
		
		setTurnRightRadians(bearing + RIGHT_ANGLE);
		
		double difference;
                // Voidious: this looks funky, but it really does work the same like this and is smaller.
		if((difference = _energy - (_energy = e.getEnergy())) <= BULLET_POWER && difference > ONE_TENTH) {
			_direction *= _moveCheck;
		}
		setAhead(HUNDRED * _direction);

                // Voidious: don't need an extra variable here
		bearing += getHeadingRadians();
		setTurnGunRightRadians(Utils.normalRelativeAngle(bearing - getGunHeadingRadians() + ((_gunCheck<ZERO?((Math.random() * SIXTEEN) - EIGHT):e.getVelocity()) * Math.sin(e.getHeadingRadians() - bearing) / THIRTEEN)));
		
		setFire(BULLET_POWER);
	}

	/**
	 * onHitByBullet: What to do when you're hit by a bullet
	 */
	public void onHitByBullet(HitByBulletEvent e) {
		_moveCheck *= NEGATIVE_ONE;
	}
	
	/**
	 * onBulletHit: What to do when a bullet hits
	 */
	public void onBulletHit(BulletHitEvent e) {
		_gunCheck -= (_gunCheck<ZERO?POSITIVE_ONE:NEGATIVE_ONE);
	}
	
	/**
	 * onBulletMissed: What to do when a bullet misses
	 */
	public void onBulletMissed(BulletMissedEvent e) {
		_gunCheck += (_gunCheck<ZERO?POSITIVE_ONE:NEGATIVE_ONE);
	}
	
	/**
	 * onHitWall: What to do when you hit a wall
	 */
	public void onHitWall(HitWallEvent e) {
		_direction *= NEGATIVE_ONE;
	}
	
}

Whoah, sorry, I missed your previous update. Anyway, the second change I made could still apply, I think. Good luck. -- Voidious

No, I prefer your changes because I don't have to sacrifice the accuracy of the Energy Drop prediction. So thanks! I replaced the code up top with the finalized version. --Bayen


Robo Home | Squirrel | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited October 31, 2006 13:22 EST by Bayen (diff)
Search: