package anthill; import robocode.*; import java.awt.Color; import java.io.*; /** * Ant_Fighter - a robot by (Joshua Cottrell) * One unit of the Ant project for Robocode */ public class Ant_Fighter extends TeamRobot { /** * run: Ant_Fighter's default behavior */ double meX; double meY; boolean movingForward; double distance; double tDirection; public void run() { // After trying out your robot, try uncommenting the import at the top, // and the next line: setColors(Color.black,Color.black,Color.black); movingForward=true; tDirection = 1; while(true) { setTurnRight(270 * tDirection); setAhead(100 * tDirection); execute(); tDirection = tDirection * -1; meX = getX(); meY = getY(); } } public void onScannedRobot(ScannedRobotEvent e) { if (isTeammate(e.getName())) { return; } if (e.getVelocity() == 0) { fire(1); } else { double trueBearings = normalRelativeAngle(getHeading() + e.getBearing() - getGunHeading()); turnGunRight(trueBearings); fire(1); } } public void onMessageReceived(MessageEvent e) { if (e.getMessage() instanceof where) { where h = (where)e.getMessage(); double dx = h.getX() - this.getX(); double dy = h.getY() - this.getY(); double theta = Math.toDegrees(Math.atan2(dx,dy)); distance = getRange(h.getX(),h.getY(),this.getX(),this.getY() - 10); turnRight(normalRelativeAngle(theta - getHeading())); ahead(distance); } if (e.getMessage() instanceof Attack) { Attack h = (Attack)e.getMessage(); double dx = h.getX() - this.getX(); double dy = h.getY() - this.getY(); double theta = Math.toDegrees(Math.atan2(dx,dy)); distance = getRange(h.getX(),h.getY(),this.getX(),this.getY() - 10); turnRight(normalRelativeAngle(theta - getHeading())); setAhead(distance/2 + distance); fire(3); } } /** * normalAbsoluteAngle: returns angle such that 0 <= angle < 360 */ public double normalAbsoluteAngle(double angle) { if (angle >= 0 && angle < 360) return angle; double fixedAngle = angle; while (fixedAngle < 0) fixedAngle += 360; while (fixedAngle >= 360) fixedAngle -= 360; return fixedAngle; } /** * normalRelativeAngle: returns angle such that -180<angle<=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; } double getRange(double x1,double y1, double x2,double y2) { double x = x2-x1; double y = y2-y1; double range = Math.sqrt(x*x + y*y); return range; } public void onDeath(DeathEvent e) { try { broadcastMessage(new where(meX,meY)); } catch (IOException ex) { System.out.println("Can't send help signal"); } } public void onHitRobot(HitRobotEvent e) { if (isTeammate(e.getName())) { bounce(100); return; } bounce(distance/2); } public void bounce(double howFar) { if (movingForward) { setBack(howFar); movingForward=false; } else { setAhead(howFar); movingForward=true; } } public void onHitWall(HitWallEvent e) { bounce(10); } }
package anthill; import robocode.*; import java.awt.Color; import java.io.*; /** * Ant_Queen - a robot by (Joshua Cottrell) */ public class Ant_Queen extends TeamRobot { /** * run: Ant_Queen's default behavior */ static int corner = 0; boolean movingForward; public void run() { // After trying out your robot, try uncommenting the import at the top, // and the next line: setColors(Color.yellow,Color.black,Color.black); movingForward=true; turnRight(normalRelativeAngle(corner - getHeading())); ahead(5000); turnLeft(90); ahead(5000); turnGunLeft(90); while(true) { turnGunRight(360); turnGunLeft(360); } } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { double eBearing = this.getHeading() + e.getBearing(); double nmeX = getX() + e.getDistance() * Math.sin(Math.toRadians(eBearing)); double nmeY = getY() + e.getDistance() * Math.cos(Math.toRadians(eBearing)); if (isTeammate(e.getName())) { return; } try { broadcastMessage(new Attack(nmeX,nmeY)); } catch (IOException ex) {} double trueBearings = normalRelativeAngle(getHeading() + e.getBearing() - getGunHeading()); turnGunRight(trueBearings); fire(.1); scan(); } /** * normalAbsoluteAngle: returns angle such that 0 <= angle < 360 */ public double normalAbsoluteAngle(double angle) { if (angle >= 0 && angle < 360) return angle; double fixedAngle = angle; while (fixedAngle < 0) fixedAngle += 360; while (fixedAngle >= 360) fixedAngle -= 360; return fixedAngle; } /** * normalRelativeAngle: returns angle such that -180<angle<=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; } double getRange(double x1,double y1, double x2,double y2) { double x = x2-x1; double y = y2-y1; double range = Math.sqrt(x*x + y*y); return range; } public void onHitRobot(HitRobotEvent e) { bounce(10); } public void onHitWall(HitWallEvent e) { bounce(10); } public void bounce(double howFar) { if (movingForward) { setBack(howFar); movingForward=false; } else { setAhead(howFar); movingForward=true; } } }
package anthill; import robocode.*; import java.awt.Color; import java.io.*; /** * Ant_Guard - a robot by (Joshua Cottrell) */ public class Ant_Guard extends TeamRobot { /** * run: Ant_Guard's default behavior */ boolean movingForward; public void run() { // After trying out your robot, try uncommenting the import at the top, // and the next line: setColors(Color.red,Color.black,Color.black); movingForward=true; while(true) { setTurnGunRight(360); if (movingForward) { ahead(20); back(10); } else { back(20); ahead(10); } } } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { if (isTeammate(e.getName())) { return; } fire(1); } /** * normalAbsoluteAngle: returns angle such that 0 <= angle < 360 */ public double normalAbsoluteAngle(double angle) { if (angle >= 0 && angle < 360) return angle; double fixedAngle = angle; while (fixedAngle < 0) fixedAngle += 360; while (fixedAngle >= 360) fixedAngle -= 360; return fixedAngle; } /** * normalRelativeAngle: returns angle such that -180<angle<=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; } double getRange(double x1,double y1, double x2,double y2) { double x = x2-x1; double y = y2-y1; double range = Math.sqrt(x*x + y*y); return range; } public void onHitRobot(HitRobotEvent e) { bounce(10); } public void onHitWall(HitWallEvent e) { bounce(10); } public void bounce(double howFar) { if (movingForward) { setBack(howFar); movingForward=false; } else { setAhead(howFar); movingForward=true; } } public void onMessageReceived(MessageEvent e) { double distance; if (e.getMessage() instanceof Guard) { Guard h = (Guard)e.getMessage(); double dx = h.getX() - this.getX(); double dy = h.getY() - this.getY(); double theta = Math.toDegrees(Math.atan2(dx,dy)); distance = getRange(h.getX(),h.getY(),this.getX(),this.getY() - 10); turnRight(normalRelativeAngle(theta - getHeading())); ahead(distance-20); } } }