Informatikunterricht

am Gymnasium Kirchenfeld

Benutzer-Werkzeuge

Webseiten-Werkzeuge


robocode:tracking

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
robocode:tracking [2015/03/30 14:33] rosrobocode:tracking [2020/10/13 14:25] (aktuell) – Externe Bearbeitung 127.0.0.1
Zeile 6: Zeile 6:
  
 <code java> <code java>
 +import robocode.*;
 +import robocode.util.*;
 +
 public class EnemyRobot public class EnemyRobot
 { {
     private Robot ownRobot;     private Robot ownRobot;
     private String name;     private String name;
 +    private double velocity;
     private double x;     private double x;
     private double y;     private double y;
Zeile 20: Zeile 24:
     public void update(ScannedRobotEvent e) {     public void update(ScannedRobotEvent e) {
         double headingToRobot = ownRobot.getHeading() + e.getBearing();         double headingToRobot = ownRobot.getHeading() + e.getBearing();
 +        velocity = e.getVelocity();
         x = ownRobot.getX() + e.getDistance() * Math.sin(Math.toRadians(headingToRobot));         x = ownRobot.getX() + e.getDistance() * Math.sin(Math.toRadians(headingToRobot));
         y = ownRobot.getY() + e.getDistance() * Math.cos(Math.toRadians(headingToRobot));         y = ownRobot.getY() + e.getDistance() * Math.cos(Math.toRadians(headingToRobot));
 +    }
 +
 +    public double getDistance() {
 +        double dx = x - ownRobot.getX();
 +        double dy = y - ownRobot.getY();
 +        return Math.sqrt(dx*dx + dy*dy);
 +    }
 +
 +    public double getHeading() {
 +        return Utils.normalAbsoluteAngleDegrees(Math.toDegrees(Math.atan2(x - ownRobot.getX(), y - ownRobot.getY())));
     }     }
  
     public String getName() {     public String getName() {
         return name;         return name;
 +    }
 +
 +    public double getVelocity() {
 +        return velocity;
     }     }
  
Zeile 52: Zeile 71:
         enemies = new HashMap<String, EnemyRobot>();         enemies = new HashMap<String, EnemyRobot>();
         this.ownRobot = ownRobot;         this.ownRobot = ownRobot;
 +    }
 +
 +    // Idea by Michael Marti
 +    public EnemyRobot getClosestEnemy() {
 +        EnemyRobot closest = null;
 +        for (EnemyRobot enemy : getEnemies()) {
 +            if (closest == null || enemy.getDistance() < closest.getDistance()) {
 +                closest = enemy;
 +            }
 + }
 +
 +        return closest;
     }     }
  
robocode/tracking.1427718825.txt.gz · Zuletzt geändert: 2020/10/13 14:25 (Externe Bearbeitung)