Help:Code Shrinking
Jump to navigation
Jump to search
I was trying to make a nano bot and it is 313 bytes =( I am not very good at shrinking code and this is the best that I could do. If you find a way to shrink it please tell it to me.
package dsekercioglu;
import robocode.AdvancedRobot; import robocode.HitByBulletEvent; import robocode.HitWallEvent; import robocode.ScannedRobotEvent; import static robocode.util.Utils.normalRelativeAngle;
public class Hammer
extends AdvancedRobot {
int[] mp = new int[30]; //movePattern int i1 = 0; String history = "�������������������������������������������������������������������������������������������";
public void run() { setAdjustGunForRobotTurn(true); setAdjustRadarForGunTurn(true); for (int i = 0; i < 30; i++) { mp[i] = (Math.random() < 0.5 ? 1 : -1); } setTurnRadarRightRadians(Double.POSITIVE_INFINITY); }
public void onScannedRobot(ScannedRobotEvent e) { setTurnRight(e.getBearing() + 90); setAhead(mp[i1 / 8] * 20); i1 = (i1 + 1) % 240; int a = (int) Math.round(e.getVelocity() * Math.sin(e.getBearingRadians())) + 8; history += (char) (a); byte b = (byte) (e.getDistance() / 14); int matchi = history.substring(0, history.length() - b).lastIndexOf(a); double c = 0; int d = 0; while ((++d) < b) { c += ((double) history.codePointAt(matchi + d) - 8) / b / 14; } setTurnGunRightRadians(normalRelativeAngle(e.getBearingRadians() + getHeadingRadians() + c - getGunHeadingRadians())); setFire(2); setTurnRadarLeftRadians(getRadarTurnRemaining()); }
public void onHitWall(HitWallEvent e) { onHitByBullet(null); }
public void onHitByBullet(HitByBulletEvent e) { mp[i1 / 8] *= -1; }
}
Shrienked a bit more 262 bytes (no juice left=])
package dsekercioglu;
import robocode.*; import robocode.util.Utils;
public class Hammer extends AdvancedRobot {
static int[] mp = new int[15]; static int i1; static String history = "������������������������������������������������������������������������������������������������";
public void run() { i1 = 0; setTurnRadarRightRadians(Double.POSITIVE_INFINITY); }
public void onScannedRobot(ScannedRobotEvent e) { double be; setTurnRightRadians((be = e.getBearingRadians()) + 1.5707963267948966D); setAhead((mp[(i1 / 10)] % 2) * 40 - 20); i1 = (i1 + 1) % 150; be += getHeadingRadians(); int a = (int) Math.round(e.getVelocity() * Math.sin(be - e.getHeadingRadians())) + 8; history += (char) a; byte b = (byte) (e.getDistance() / 14); int matchi = history.substring(0, history.length() - b).lastIndexOf(a); a = 0; while (++a < b) { be -= (history.codePointAt(matchi + a) - 8.0) / b / 14; } setTurnGunRightRadians(Utils.normalRelativeAngle(be - getGunHeadingRadians())); setFire(2.0D); setTurnRadarLeftRadians(getRadarTurnRemaining()); }
public void onHitWall(HitWallEvent e) { onHitByBullet(null); }
public void onHitByBullet(HitByBulletEvent e) { mp[(i1 / 10)] += 1; }
}