robocode:tracking
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
robocode:tracking [2015/03/30 14:25] – 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 38: | Zeile 57: | ||
</ | </ | ||
- | Die Klasse '' | + | Die Klasse '' |
<code java> | <code java> | ||
- | import java.util.HashMap; | + | import java.util.*; |
- | import java.util.Map; | + | |
import robocode.*; | import robocode.*; | ||
- | public class EnemyRobots | + | public class EnemyTracker |
{ | { | ||
private Map< | private Map< | ||
private Robot ownRobot; | private Robot ownRobot; | ||
- | public | + | public |
enemies = new HashMap< | enemies = new HashMap< | ||
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; | ||
+ | } | ||
+ | |||
+ | public Collection< | ||
+ | return enemies.values(); | ||
} | } | ||
Zeile 62: | Zeile 96: | ||
enemies.get(e.getName()).update(e); | 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.1427718307.txt.gz · Zuletzt geändert: 2020/10/13 14:25 (Externe Bearbeitung)