简体   繁体   English

Robocode HashTable /方法在其中修改值的问题

[英]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. 当前在RObocode中,我有一个哈希表,该哈希表的名称为键,而point2D对象为值。 One of the properties of these objects is the double lastSeen which is the time since the robot has been seen. 这些对象的属性之一是double lastSeen,这是自看到机器人以来的时间。 Everytime I scan a robot, I would set this value to 0, this value also helps my radar become the oldest scanned radar. 每次扫描机器人时,我都会将此值设置为0,此值也有助于我的雷达成为最早的扫描雷达。

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; 上面的代码是在扫描机器人时将其作为对象推送到哈希表中并将该对象的lastseen属性设置为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. 我已经提出了一种方法,该方法通过返回所有对象的lastSeen变量的枚举并将其添加到每个对象,来将每个对象的lastSeen变量的值(增加1)。 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. 但是,如果哈希表中没有任何内容,则无法调用此方法,这就是为什么如果哈希表中没有任何内容,我就设置了if来阻止该方法执行的原因。 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). 例如,与其存储一个人的年龄(例如20岁),不如存储一个人的生日(例如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. ScannedRobotEvent具有getTime()方法,可用于获取扫描事件的“生日”。 Store this number. 存储此号码。 Then if you need to know how old the stored ScannedRobotEvent is, subtract this stored time from current time. 然后,如果您需要知道存储的ScannedRobotEvent年龄,请从当前时间中减去此存储时间。 This will bypass the need for an advanceTime method. 这将绕过对advanceTime方法的需要。 To implement this update enemy.lastSeen to this: 要实现此更新enemy.lastSeen ,请执行以下enemy.lastSeen

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是一个很好的例子。 GPA is a calculated value, but to calculate it you need to access every course someone has taken. GPA是一个计算值,但要计算该值,您需要访问某人参加的所有课程。 In this case, if could be faster to store GPA and remember to update it whenever someone completes a new course. 在这种情况下,存储GPA的速度可能会更快,并且记得在有人完成新课程时更新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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