Currently in RObocode I have a hashtable that has names as the keys and point2D objects as the values. One of the properties of these objects is the double lastSeen which is the time since the robot has been seen. Everytime I scan a robot, I would set this value to 0, this value also helps my radar become the oldest scanned radar.
public void onScannedRobot(ScannedRobotEvent e) {
String name;
EnemyInfo enemy = (EnemyInfo) enemies.get(name = e.getName());
// if the enemy is not already on the hashtable, puts it on
if (enemy == null) {
enemies.put(name, enemy = new EnemyInfo());
}
enemy.bearing = e.getBearing();
enemy.velocity = e.getVelocity();
enemy.heading = e.getHeading();
enemy.energy = e.getEnergy();
enemy.lastSeen = 0;
above is the code that upon scanning a robot, shoves it into the hashtable as an object and sets the object's lastseen property to 0;
I have made a method that increases the value (by 1) of every object's lastSeen variable by returning an enumeration of all object's lastSeen variables and adding one to each of them. Method is below:
public static void advanceTime(EnemyInfo e) {
if (!enemies.isEmpty()) {
int i = 0;
Enumeration enum3 = enemies.elements();
do {
(e = (EnemyInfo) enum3.nextElement()).lastSeen = (e = (EnemyInfo) enum3
.nextElement()).lastSeen + 1;
i++;
System.out.println("Added one to.." + i);
} while (enum3.hasMoreElements());
}
}
However, I cannot call this method if there is nothing in the hashtable, which is why I put an if to stop the method from executing if there is nothing in the hashtable. Don't know the reason for this..
Any other method for doing this efficiently and effectively??