Difference between revisions of "Talk:Kitten"

From Robowiki
Jump to navigation Jump to search
(Revised)
Line 62: Line 62:
  
 
The String/Builder/Buffer thing. Each person use what he/she likes ;) But I must say, using StringBuffer over StringBuilder seem really silly since the usage is really the same, as did the API =) My point is, use String in nano/micro robot and use StringBuilder in mini/mega, micro if you can fit. &raquo; <span style="font-size:0.9em;color:darkgreen;">[[User:Nat|Nat]] | [[User_talk:Nat|Talk]]</span> &raquo; 13:18, 18 June 2009 (UTC)
 
The String/Builder/Buffer thing. Each person use what he/she likes ;) But I must say, using StringBuffer over StringBuilder seem really silly since the usage is really the same, as did the API =) My point is, use String in nano/micro robot and use StringBuilder in mini/mega, micro if you can fit. &raquo; <span style="font-size:0.9em;color:darkgreen;">[[User:Nat|Nat]] | [[User_talk:Nat|Talk]]</span> &raquo; 13:18, 18 June 2009 (UTC)
 +
 +
Yes, I've only used StringBuilder in my code. I forgot what the difference with StringBuffer is briefly. I revise my recommendation with a FunkyChicken gun, to be change the Buffer to Builder, AND get rid of the unneeded toString(), then you get something between the codesize of a FunkyChicken and a WeekendObsession, and with no spikes in time used (meaning, lowest chance of skipped turns if you use a lot of cpu in the bot) --[[User:Rednaxela|Rednaxela]] 13:24, 18 June 2009 (UTC)

Revision as of 14:24, 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)


Actually, for repeated appending, StringBuffer is FAR faster than a normal String once the string gets large! This is because a normal String will have to re-allocate a bigger chunk of memory, and re-copy EVERYTHING every time the string goes past container size. The reason that the FunkyChicken gun is slow, is because of enemyLog.toString().indexOf(), because that forces it to re-copy the string every tick no matter what, which is worse than the occasional re-copy that String appending forces. Funny thing is, StringBuffer has it's own indexOf() method, so really, that line in the various FunkyChicken guns should really be enemyLog.indexOf(), which is both slightly lower codesize AND will make the gun have less spikes in time used than WeekendObsession, due to any and all re-copies being totally eliminated. --Rednaxela 12:46, 18 June 2009 (UTC)

I think that for nano/micro, the codesize is more important. And String is cheaper than StringBuffer/Builder in term of codesize. For FunkyChicken, IIRC, I think that time StringBuffer/Builder doesn't have indexOf() yet. but using of StringBuffer is not really recommended, consider use of StringBuilder instead. Or it will be around 6 times slower during the synchronization. » Nat | Talk » 13:01, 18 June 2009 (UTC)

If you intend to keep your distance at a roughly fixed length, Moebius's gun is nice and cheaper than the standard gun. And I just don't recall on the string/stringbuilder/stringbuffer conversation which was better. :P --Miked0801 13:10, 18 June 2009 (UTC)

Why don't you join it? » Nat | Talk » 13:18, 18 June 2009 (UTC)

The String/Builder/Buffer thing. Each person use what he/she likes ;) But I must say, using StringBuffer over StringBuilder seem really silly since the usage is really the same, as did the API =) My point is, use String in nano/micro robot and use StringBuilder in mini/mega, micro if you can fit. » Nat | Talk » 13:18, 18 June 2009 (UTC)

Yes, I've only used StringBuilder in my code. I forgot what the difference with StringBuffer is briefly. I revise my recommendation with a FunkyChicken gun, to be change the Buffer to Builder, AND get rid of the unneeded toString(), then you get something between the codesize of a FunkyChicken and a WeekendObsession, and with no spikes in time used (meaning, lowest chance of skipped turns if you use a lot of cpu in the bot) --Rednaxela 13:24, 18 June 2009 (UTC)