Difference between revisions of "Talk:Kitten"

From Robowiki
Jump to navigation Jump to search
(StringBuffer speed)
m (fix conflict that I wasn't notified about...)
Line 4: Line 4:
 
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)
  
: Actually, for repeated appending, StingBuffer 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 <code>enemyLog.toString().indexOf()</code>, 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 <code>enemyLog.indexOf()</code>, 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. --[[User:Rednaxela|Rednaxela]] 12:46, 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)
 +
 
 +
 
 +
Actually, for repeated appending, StingBuffer 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 <code>enemyLog.toString().indexOf()</code>, 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 <code>enemyLog.indexOf()</code>, 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. --[[User:Rednaxela|Rednaxela]] 12:46, 18 June 2009 (UTC)

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