永远不会调用onScannedRobot方法

时间:2015-11-04 16:57:16

标签: robocode

尝试使用System.out进行调试,以检查方法是否运行。 run方法执行正常,雷达开始旋转,机器人控制台显示Hello。似乎永远不会调用onScannedRobot。完全脱离了如何解决的线索。在战斗中,机器人可以精确地编入游戏中,它肯定会将其雷达旋转到其他机器人身上。

package ke_shen;

import robocode.util.*;
import robocode.*;
import java.util.*;
import java.awt.Color;
import java.awt.geom.Point2D;

//Oldest Scanned Radar
//Functions by spinning until all robots have been scanned
//then begins to scan in the opposite direction until 
//all robots have been scanned again
//this minimizes the time in between all robots in the battlefield
//can be scanned, maximizing speed of scanning
public class shen_robot extends AdvancedRobot {

    // the use of a linked hash map is deal here to store the enemy
    // robot's names (the key)and their respective absolute bearings (thevalue)

    static double scanDirection;
    static Object sought;
    static Object mostDanger = null;
    static double distance = 50000;
    static int tempindex = 0;
    static int mostDangerIndex;
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Double> distanceArray = new ArrayList<Double>();
    ArrayList<Double> velocityArray = new ArrayList<Double>();
    ArrayList<Double> headingArray = new ArrayList<Double>();

    public void run() {
        setAdjustRadarForRobotTurn(true);
        setAdjustGunForRobotTurn(true);
        setAdjustRadarForGunTurn(true);
        setAllColors(Color.BLUE);
        System.out.println("Hello.");
        scanDirection = 1;

            // below, scanDirection will be become either negative or positive
            // this changes the direction of the scan from initially
            // clockwise to counterclockwise and vice versa;
            setTurnRadarRightRadians(scanDirection * Double.POSITIVE_INFINITY);
            scan();
            // linearTargeting();
            // execute();

    }

    // removes the robot from the hash map when it dies
    public void onRobotDeathEvent(RobotDeathEvent e) {
        int index = names.indexOf(e.getName());
        names.remove(e.getName());
        distanceArray.remove(index);
        velocityArray.remove(index);
        headingArray.remove(index);
    }

    public void onScannedRobot(ScannedRobotEvent e) {
        System.out.println("Helo.");
        // RADAR
        // the radar will spin in a full circle once in the beginning of the
        // battle
        // and add all the robots to the hash map
        // the second rotation, once it reaches the last robot in the hash map,
        // because the radar heading is now greater than the normalRelative
        // angle
        // scanDirection will become negative, resulting in the radar spinning
        // in the other
        // direction due to the code above in line 31

        // UPDATES PROPERTIES AFTER THE INITIAL 360 degree SCAN
        String name = e.getName();
        if (names.contains(name) == true) {
            tempindex = names.indexOf(name);
            headingArray.remove(tempindex);
            headingArray.add(tempindex, e.getHeadingRadians());
            velocityArray.remove(tempindex);
            velocityArray.add(tempindex, e.getVelocity());
            distanceArray.remove(tempindex);
            distanceArray.add(tempindex, e.getDistance());
        }

        // HEADING
        else {
        int index = names.size()-1;
        headingArray.add(e.getHeadingRadians());
        if (names.size() == getOthers()) {
            scanDirection = Utils.normalRelativeAngle(headingArray.get(index) - getRadarHeadingRadians());
        }

        // VELOCITY
        velocityArray.add(e.getVelocity());

        // DISTANCE & MOSTDANGEROUS
        distanceArray.add(e.getDistance());

        }

        while (distanceArray.iterator().hasNext()) {
            if (distanceArray.iterator().next() < distance) {
                distance = distanceArray.iterator().next();
            }
        }

        mostDangerIndex = distanceArray.indexOf(distance);
    }


    public void addInfo(String name, int number) {

    }

}

1 个答案:

答案 0 :(得分:1)

琐碎的测试

将OnScannedRobot更改为允许它正常执行。所以机器人正在捕捉扫描事件:

 public void onScannedRobot(ScannedRobotEvent e) {
    System.out.println("Helo.");
 }

诊断问题

问题是如果机器人在分配的时间内未能完成转弯,则将跳过转弯。现在问题是,OnScannedRobot方法的哪一部分时间效率低?

分辨率

事实证明,mostDangerIndex计算(包括while循环)是罪魁祸首。因此,为了修复OnScannedRobot方法,我将mostDangerIndex计算(包括while循环)替换为:

mostDangerIndex = distanceArray.indexOf(Collections.min(distanceArray));

现在它有效! shen_robot is working!