Skip to content

Instantly share code, notes, and snippets.

@berkes
Created September 25, 2015 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkes/3cccaddd4a3ad0a30499 to your computer and use it in GitHub Desktop.
Save berkes/3cccaddd4a3ad0a30499 to your computer and use it in GitHub Desktop.
package sample;
import java.util.Random;
import robocode.Robot;
import robocode.Rules;
import robocode.ScannedRobotEvent;
public class Klaas extends Robot {
private int scannedX;
private int scannedY;
private boolean homingIn;
public void run() {
// turn radar
while (true) {
turnRadarRight(45);
Random random = new Random();
if (!homingIn) {
int distance = random.nextInt() % 100;
ahead(distance);
turnRight(45);
}
}
}
// Called when we have scanned a robot
public void onScannedRobot(ScannedRobotEvent e) {
stop();
if (homingIn) return;
homingIn = true;
// Calculate the angle to the scanned robot
double angle = (getHeading() + e.getBearing()) % 360;
System.out.println(angle);
System.out.println(homingIn);
turnRight(angle);
fire(1.5);
// Calculate the coordinates of the robot
// scannedX = (int) (getX() + Math.sin(angle) * e.getDistance());
// scannedY = (int) (getY() + Math.cos(angle) * e.getDistance());
resume();
homingIn = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment