Difference between revisions of "Help:Help/Code Shrinking"

From Robowiki
Jump to navigation Jump to search
(Source)
Line 2: Line 2:
  
 
: Post the source.  I'm a past master at reducing codesize on bots.  Hell, I wrote the original articles over at the repository and codesize tricks that spurred the nanobot explosion.  I'd love some new blood in the nano area while I grit my teeth over Infinity/Dustbunny and how to make them just 1% better :) --[[User:Miked0801|Miked0801]] 03:56, 30 April 2009 (UTC)
 
: Post the source.  I'm a past master at reducing codesize on bots.  Hell, I wrote the original articles over at the repository and codesize tricks that spurred the nanobot explosion.  I'd love some new blood in the nano area while I grit my teeth over Infinity/Dustbunny and how to make them just 1% better :) --[[User:Miked0801|Miked0801]] 03:56, 30 April 2009 (UTC)
 +
 +
::Okay.
 +
 +
<pre>
 +
package awesomeness;
 +
 +
import robocode.*;
 +
import robocode.util.*;
 +
import java.util.Random;
 +
import static robocode.util.Utils.normalRelativeAngleDegrees;
 +
//import java.awt.Color;
 +
 +
/**
 +
* Elite - a robot by Awesomeness
 +
*/
 +
public class Elite extends AdvancedRobot {
 +
 +
int previousEnergy = 100;
 +
int count;
 +
int countAdd;// The amount to add to the count
 +
byte movementDirection = 1;
 +
byte alternate;
 +
Random generator = new Random(); //This makes random numbers
 +
 +
/**
 +
* run: Elite's default behavior
 +
*/
 +
public void run() {
 +
 +
setAdjustRadarForGunTurn(true);
 +
setAdjustGunForRobotTurn(true);
 +
 +
 +
// After trying out your robot, try uncommenting the import at the top,
 +
// and the next line:
 +
//setColors(Color.red,Color.blue,Color.green);
 +
while(true) {
 +
turnRadarRightRadians(Double.POSITIVE_INFINITY);
 +
}
 +
}
 +
 +
/**
 +
* onScannedRobot: What to do when you see another robot
 +
*/
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
 +
//The absolute bearing, this is used a lot
 +
double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
 +
 +
///////////////////////////////////////////////////////
 +
////////////////////Movement Code//////////////////////
 +
 +
//If there's a change in energy, it probably fired
 +
int changeInEnergy = previousEnergy- (int) e.getEnergy();
 +
 +
if (changeInEnergy>0 && changeInEnergy<=3) {
 +
alternate();
 +
}
 +
 +
 +
// Stay at right angles to the opponent
 +
setTurnRight(e.getBearing()+90/*-5*movementDirection*/);
 +
 +
countAdd += (generator.nextInt(2)*2-1) * 3;
 +
 +
count += countAdd;
 +
 +
setMaxVelocity(8);
 +
 +
if (count >= 100) {
 +
count = 0;
 +
countAdd = 0;
 +
}
 +
 +
setAhead(50*movementDirection);
 +
 +
// Track the energy level
 +
previousEnergy = (int) e.getEnergy();
 +
 +
////////////////////Movement Code//////////////////////
 +
///////////////////////////////////////////////////////
 +
 +
///////////////////////////////////////////////////////
 +
///////////////////////Gun Code////////////////////////
 +
 +
//Pretty simple...
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(absoluteBearing -
 +
    getGunHeadingRadians() + (e.getVelocity() * Math.sin(e.getHeadingRadians() -
 +
    absoluteBearing) / 13.2)));
 +
if (getGunHeat() == 0) { // Only try to fire if we can-
 +
setFire(2.0); // otherwise we do much worse
 +
}
 +
 +
///////////////////////Gun Code////////////////////////
 +
///////////////////////////////////////////////////////
 +
 +
 +
 +
///////////////////////////////////////////////////////
 +
/////////////////////Radar Code////////////////////////
 +
 +
setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
 +
 +
 +
/////////////////////Radar Code////////////////////////
 +
///////////////////////////////////////////////////////
 +
 +
 +
 +
}
 +
public void onHitWall(HitWallEvent e) {
 +
i();
 +
}
 +
public void i() {
 +
setMaxVelocity(1);
 +
movementDirection = (byte) -movementDirection;
 +
}
 +
public void alternate() {
 +
i();//Length: 276
 +
alternate = (byte) -alternate;
 +
if (alternate == 1) {
 +
i();
 +
}
 +
}
 +
}
 +
</pre>

Revision as of 12:22, 30 April 2009

Annoying... I've made an Über-Nano bot that gets 99% against N, but I'm stuck at 283 bytes... Awesomeness 01:29, 30 April 2009 (UTC)

Post the source. I'm a past master at reducing codesize on bots. Hell, I wrote the original articles over at the repository and codesize tricks that spurred the nanobot explosion. I'd love some new blood in the nano area while I grit my teeth over Infinity/Dustbunny and how to make them just 1% better :) --Miked0801 03:56, 30 April 2009 (UTC)
Okay.
package awesomeness;

import robocode.*;
import robocode.util.*;
import java.util.Random;
import static robocode.util.Utils.normalRelativeAngleDegrees;
//import java.awt.Color;

/**
 * Elite - a robot by Awesomeness
 */
public class Elite extends AdvancedRobot {
	
	int previousEnergy = 100;
	int count;
	int countAdd;// The amount to add to the count
	byte movementDirection = 1;
	byte alternate;
	Random generator = new Random(); //This makes random numbers
	
	/**
	 * run: Elite's default behavior
	 */
	public void run() {
		
		setAdjustRadarForGunTurn(true);
		setAdjustGunForRobotTurn(true);
		
		
		// After trying out your robot, try uncommenting the import at the top,
		// and the next line:
		//setColors(Color.red,Color.blue,Color.green);
		while(true) {
			turnRadarRightRadians(Double.POSITIVE_INFINITY);
		}
	}

	/**
	 * onScannedRobot: What to do when you see another robot
	 */
	public void onScannedRobot(ScannedRobotEvent e) {
		
		//The absolute bearing, this is used a lot
		double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
		
		///////////////////////////////////////////////////////
		////////////////////Movement Code//////////////////////
		
		//If there's a change in energy, it probably fired
		int changeInEnergy = previousEnergy- (int) e.getEnergy();
		
		if (changeInEnergy>0 && changeInEnergy<=3) {
			alternate();
		}
		
							
		// Stay at right angles to the opponent	
		setTurnRight(e.getBearing()+90/*-5*movementDirection*/);	
		
		countAdd += (generator.nextInt(2)*2-1) * 3;
	
		count += countAdd;
		
		setMaxVelocity(8);
		
		if (count >= 100) {
			count = 0;
			countAdd = 0;
		}
		
		setAhead(50*movementDirection);
		
		// Track the energy level
		previousEnergy = (int) e.getEnergy();
		
		////////////////////Movement Code//////////////////////
		///////////////////////////////////////////////////////
		
		///////////////////////////////////////////////////////
		///////////////////////Gun Code////////////////////////
		
		//Pretty simple...
		setTurnGunRightRadians(Utils.normalRelativeAngle(absoluteBearing - 
    		getGunHeadingRadians() + (e.getVelocity() * Math.sin(e.getHeadingRadians() - 
    		absoluteBearing) / 13.2)));
		if (getGunHeat() == 0) { // Only try to fire if we can-
			setFire(2.0); // otherwise we do much worse
		}
		
		///////////////////////Gun Code////////////////////////
		///////////////////////////////////////////////////////
		
		
		
		///////////////////////////////////////////////////////
		/////////////////////Radar Code////////////////////////
			
		setTurnRadarLeftRadians(getRadarTurnRemainingRadians());
		
		
		/////////////////////Radar Code////////////////////////
		///////////////////////////////////////////////////////	
		
		
		
	}
	public void onHitWall(HitWallEvent e) {
		i();
	}
	public void i() {
		setMaxVelocity(1);
		movementDirection = (byte) -movementDirection;
	}
	public void alternate() {
		i();//Length: 276
		alternate = (byte) -alternate;
		if (alternate == 1) {
			i();
		}
	}
}