robocode:tracking
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
robocode:tracking [2015/03/30 14:24] – angelegt ros | robocode:tracking [2020/10/13 14:25] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== Tracking | + | ====== Tracking ====== |
- | Für viele Techniken ist es wichtig, Informationen über die gegnerischen | + | Für viele Techniken ist es wichtig, Informationen über die Roboter zu haben. Dazu werden die Informationen, |
Die Klasse '' | Die Klasse '' | ||
<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(), | ||
} | } | ||
public String getName() { | public String getName() { | ||
return name; | return name; | ||
+ | } | ||
+ | |||
+ | public double getVelocity() { | ||
+ | return velocity; | ||
} | } | ||
Zeile 34: | Zeile 53: | ||
public double getY() { | public double getY() { | ||
return y; | return y; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Die Klasse '' | ||
+ | |||
+ | <code java> | ||
+ | import java.util.*; | ||
+ | import robocode.*; | ||
+ | |||
+ | public class EnemyTracker | ||
+ | { | ||
+ | private Map< | ||
+ | private Robot ownRobot; | ||
+ | |||
+ | public EnemyTracker(Robot ownRobot) { | ||
+ | enemies = new HashMap< | ||
+ | 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; | ||
+ | } | ||
+ | |||
+ | public Collection< | ||
+ | return enemies.values(); | ||
+ | } | ||
+ | |||
+ | public void update(ScannedRobotEvent e) { | ||
+ | if (!enemies.containsKey(e.getName())) { | ||
+ | // Gegner noch nicht bekannt | ||
+ | enemies.put(e.getName(), | ||
+ | } | ||
+ | |||
+ | enemies.get(e.getName()).update(e); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Im eigenen Roboter wird ein '' | ||
+ | |||
+ | <code java> | ||
+ | public class MyRobot extends Robot { | ||
+ | |||
+ | private EnemyTracker enemyTracker; | ||
+ | |||
+ | public void run() { | ||
+ | enemyTracker = new EnemyTracker(this); | ||
+ | // Weitere Aktionen | ||
+ | } | ||
+ | | ||
+ | public void onScannedRobot(ScannedRobotEvent e) { | ||
+ | enemyTracker.update(e); | ||
+ | // Weitere Aktionen | ||
} | } | ||
} | } | ||
</ | </ |
robocode/tracking.1427718248.txt.gz · Zuletzt geändert: 2020/10/13 14:25 (Externe Bearbeitung)