package example; import robocode.*; import java.awt.Color; /** * Radar - a robot by (your name here) */ public class Radar extends Robot { /** * run: Radar's default behavior */ public void run() { // After trying out your robot, try uncommenting the import at the top, // and the next line: setColors(Color.red,Color.red,Color.red); setAdjustRadarForRobotTurn(true); while(true) { // Replace the next 4 lines with any behavior you would like ahead(100); turnRadarRight(360); back(100); turnRadarRight(360); } } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { double absoluteBearing = getHeading() + e.getBearing(); double bearingFromGun = normalRelativeAngle(absoluteBearing - getGunHeading()); turnGunRight(bearingFromGun); fire(3); } /** * onHitByBullet: What to do when you're hit by a bullet */ public void onHitByBullet(HitByBulletEvent e) { turnLeft(90 - e.getBearing()); ahead(200); } public void onHitWall(HitWallEvent e) { turnLeft(130 - e.getBearing()); ahead(200); } /** * normalRelativeAngle: Returns angle such that -180 < angle <= 180 * if the angle is 270 degrees we don't want to turnRight 270 it would be faster to turnRight -90 degrees * id the angle is -300 degrees we don't want to turnRight -300 it would be faster to turnRight 60 degrees */ public double normalRelativeAngle(double angle) { if (angle > -180 && angle <= 180) { return angle; } double fixedAngle = angle; /* e.g -300 degrees add 360 to it becomes +60 degrees, turnRight +60 is faster than turnRight -300 */ while (fixedAngle <= -180) { fixedAngle += 360; } /* e.g 270 degrees subtract 360 from it becomes -90 degrees, turnRight -90 is faster than turnRight +270 */ while (fixedAngle > 180) { fixedAngle -= 360; } return fixedAngle; } }