Created
September 25, 2015 09:37
-
-
Save berkes/3cccaddd4a3ad0a30499 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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