简体   繁体   中英

Robocode HashTable/method modifying values in it issues

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??

Storing Calculated Values

In a lot of cases, it is better to store the information to calculate a value rather than a calculated value itself. For instance, instead of storing a person's age (eg 20 years old), one would store the person's birthday (eg 5-5-95). If you store someone's age, you have to update it every year. If you store the birthday, you can simply calculate the age whenever you want.

Robocode

Back to the problem at hand. Instead of storing how old a piece of information is (age), store when the information was created (birthday). ScannedRobotEvent has a getTime() method that you can use to get the "birthday" of the scan event. Store this number. Then if you need to know how old the stored ScannedRobotEvent is, subtract this stored time from current time. This will bypass the need for an advanceTime method. To implement this update enemy.lastSeen to this:

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 = e.getTime();

Exception

There are some cases where you want to store a calculated value. GPA is a good example. GPA is a calculated value, but to calculate it you need to access every course someone has taken. In this case, if could be faster to store GPA and remember to update it whenever someone completes a new course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM