Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created February 6, 2017 17:04
Show Gist options
  • Save Caaz/6947a1448a55d04fd0e1a30204087f23 to your computer and use it in GitHub Desktop.
Save Caaz/6947a1448a55d04fd0e1a30204087f23 to your computer and use it in GitHub Desktop.
A robocode bot.
package bitdev;
import robocode.*;
import java.awt.Color;
// Neko - a robot by Caaz
public class Neko extends AdvancedRobot {
private boolean direction = true;
public void run() {
setColors(Color.black, Color.black, Color.white);
while(true) {
setTurnRadarRight(360);
execute();
}
}
public void dodge() {
setAhead(30 * ((direction)?1:-1));
if (Math.random() > 0.9) direction = !direction;
}
public void onScannedRobot(ScannedRobotEvent e) {
double scanBearing = e.getBearing();
double bearing = scanBearing + getHeading();
double[] adjusts = new double[]{
bearing - getGunHeading(),
bearing - getRadarHeading()
};
for(int i = 0; i<2; i++) adjusts[i] = adjusts[i] % 180;
setTurnGunRight(adjusts[0]);
setTurnRadarRight(adjusts[1]);
double distance = e.getDistance();
if(distance > 300) {
setTurnRight(scanBearing);
setAhead(distance - 100);
} else {
setTurnRight(scanBearing + 90);
dodge();
}
execute();
if((getGunTurnRemaining() == 0) && (getGunHeat() == 0)) {
setFire(Rules.MAX_BULLET_POWER/2);
execute();
}
}
public void onHitWall(HitWallEvent e) {
direction = !direction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment