package sample; import robocode.*; public class J07E03 extends Robot { public void run() { // allow the radar to rotate freely setAdjustRadarForRobotTurn(true); // allow the gun to rotate freely setAdjustGunForRobotTurn(true); // loop that runs when it's this robot's turn while (true) { // turn the radar a full circle turnRadarRight(360); } } // method that is called when a robot is detected public void onScannedRobot(ScannedRobotEvent e) { // only do the following if the Sitting Duck robot is detected //if (e.getName().equals("sample.SittingDuck")) { // get absolute bearing double absoluteBearing = getHeading() + e.getBearing(); // determine the bearing between the current heading // of the gun and the position it needs to be to point // to the detected robot double bearingFromGun = normalRelativeAngle(absoluteBearing - getGunHeading()); // turn the gun toward the detected robot turnGunRight(bearingFromGun); // fire the gun fire(1); //} } // method that is called when this robot is hit by a bullet public void onHitByBullet(HitByBulletEvent e) { } // helper method to generate a value between -180 and 180 public double normalRelativeAngle(double angle) { if (angle > -180 && angle <= 180) { return angle; } double fixedAngle = angle; while (fixedAngle <= -180) { fixedAngle += 360; } while (fixedAngle > 180) { fixedAngle -= 360; } return fixedAngle; } }