Difference between revisions of "Talk:Kitten"

From Robowiki
Jump to navigation Jump to search
(squeeze!)
Line 3: Line 3:
  
 
Ah, I'm enlightened. :) First codesize-reducing tip: (I haven't looked at the code yet) Use WeekendObsession's gun instead of Assertive's which is Funkychicken's which is... etc. :D I suggest looking at BlackWidow 1.3's code. It's very tidy and well-commented and it has WeekendObsession's gun. You will gain some bytes and the execution of your bot will be a lot faster. :) StringBuffers are very slow, at least use StringBuilder. --[[User:Robar|HUNRobar]] 08:28, 18 June 2009 (UTC)
 
Ah, I'm enlightened. :) First codesize-reducing tip: (I haven't looked at the code yet) Use WeekendObsession's gun instead of Assertive's which is Funkychicken's which is... etc. :D I suggest looking at BlackWidow 1.3's code. It's very tidy and well-commented and it has WeekendObsession's gun. You will gain some bytes and the execution of your bot will be a lot faster. :) StringBuffers are very slow, at least use StringBuilder. --[[User:Robar|HUNRobar]] 08:28, 18 June 2009 (UTC)
 +
 +
To be more exact, use String instead of StringBuilder/Buffer gain 2 bytes (or 3? IIRC, 2). Sqyeezed code (it have really much to squeeze so I don't wanna list it, doing the code diff if you want):
 +
<pre>
 +
package spinnercat;
 +
 +
import robocode.*;
 +
import robocode.util.Utils;
 +
 +
public class Kitten extends AdvancedRobot {
 +
static String pattern = "00000000000000000000000000000";
 +
 +
public void run(){
 +
// have this out will gain you 5 bytes,
 +
// and gain you more score if your gun isn't tuned up to be very accurate.
 +
// setAdjustGunForRobotTurn(true);
 +
setTurnRadarRight(Double.POSITIVE_INFINITY);
 +
}
 +
 +
public void onScannedRobot(ScannedRobotEvent e){
 +
        double absBearing;
 +
int matchLength = 30; //try out other numbers?
 +
      // Answer: try near 45, at last I didn't found any difference of them...
 +
double dist;
 +
 +
if(setFireBullet(3) != null || getDistanceRemaining() == 0){
 +
setTurnRightRadians(Math.cos(absBearing = e.getBearingRadians())+(2*Math.random()-1)*.5);
 +
setAhead((Math.random()<.5?1:-1)*(Math.random()/2+.5)*(dist/4));
 +
}
 +
 +
setTurnRadarLeftRadians(getRadarTurnRemaining());
 +
 +
pattern = String.valueOf((char)(int)(e.getVelocity()*Math.sin(e.getHeadingRadians() - (absBearing += getHeadingRadians())))).concat(pattern);
 +
 +
int index;
 +
while ((index = pattern.toString().indexOf(pattern.substring(0, matchLength--), 1)) < 0);
 +
 +
matchLength = index - (int)(( dist = e.getDistance()) / 11);
 +
 +
do
 +
absBearing += ((double)(byte)pattern.charAt(index--) / dist);
 +
while (index >= Math.max(0, matchLength));
 +
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
 +
}
 +
}
 +
</pre>
 +
Not test yet, but I think it will be compilable. Anyway, I think WeekendsObsession's is smaller by around 2-3 bytes. And don't mind the move of setFireBullet to be before the gun turn, the turn will not be execute until the next tick so if you turn and fire at the same tick, not matter the order are, it still execute the same. (hope this make sense) 12:23, 18 June 2009 (UTC)

Revision as of 13:23, 18 June 2009

Thanks you :) --Miked0801 01:01, 18 June 2009 (UTC)

no problem at all. Spinnercat 01:09, 18 June 2009 (UTC)

Ah, I'm enlightened. :) First codesize-reducing tip: (I haven't looked at the code yet) Use WeekendObsession's gun instead of Assertive's which is Funkychicken's which is... etc. :D I suggest looking at BlackWidow 1.3's code. It's very tidy and well-commented and it has WeekendObsession's gun. You will gain some bytes and the execution of your bot will be a lot faster. :) StringBuffers are very slow, at least use StringBuilder. --HUNRobar 08:28, 18 June 2009 (UTC)

To be more exact, use String instead of StringBuilder/Buffer gain 2 bytes (or 3? IIRC, 2). Sqyeezed code (it have really much to squeeze so I don't wanna list it, doing the code diff if you want):

package spinnercat;

import robocode.*;
import robocode.util.Utils;

public class Kitten extends AdvancedRobot {
	static String pattern = "00000000000000000000000000000";

	public void run(){
		// have this out will gain you 5 bytes,
		// and gain you more score if your gun isn't tuned up to be very accurate.
		// setAdjustGunForRobotTurn(true);
		setTurnRadarRight(Double.POSITIVE_INFINITY);
	}
	
	public void onScannedRobot(ScannedRobotEvent e){
        	double absBearing;
		int matchLength = 30; //try out other numbers?
				      // Answer: try near 45, at last I didn't found any difference of them...
		double dist;
		
		if(setFireBullet(3) != null || getDistanceRemaining() == 0){
			setTurnRightRadians(Math.cos(absBearing = e.getBearingRadians())+(2*Math.random()-1)*.5);
			setAhead((Math.random()<.5?1:-1)*(Math.random()/2+.5)*(dist/4));
		}

		setTurnRadarLeftRadians(getRadarTurnRemaining());

 		pattern = String.valueOf((char)(int)(e.getVelocity()*Math.sin(e.getHeadingRadians() - (absBearing += getHeadingRadians())))).concat(pattern);

		int index;
		while ((index = pattern.toString().indexOf(pattern.substring(0, matchLength--), 1)) < 0);

		matchLength = index - (int)(( dist = e.getDistance()) / 11);

		do
			absBearing += ((double)(byte)pattern.charAt(index--) / dist);
		while (index >= Math.max(0, matchLength));

		setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()));
	}
}

Not test yet, but I think it will be compilable. Anyway, I think WeekendsObsession's is smaller by around 2-3 bytes. And don't mind the move of setFireBullet to be before the gun turn, the turn will not be execute until the next tick so if you turn and fire at the same tick, not matter the order are, it still execute the same. (hope this make sense) 12:23, 18 June 2009 (UTC)