0

Have tried debugging by using System.out to check whether a method is run or not. The run method executes fine and the radar begins spinning with the robot console displaying Hello. onScannedRobot seems to be never called. Completely out of a clue of how to resolve. In the battle, the robot compiles fine into the game and it definitely is spinning its radar across other bots.

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) {

    }

}
Loofer
  • 6,841
  • 9
  • 61
  • 102
kevin shen
  • 61
  • 6

1 Answers1

1

Trivial Test

Changing OnScannedRobot to this allows it to execute normally. So the robot is catching the on scan events:

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

Diagnose the Problem

The issue is that if a robot fails to complete his turn in the time allotted, the turn will be skipped. Now the question is, what piece of the OnScannedRobot method is time inefficient?

Resolution

As it turns out, the mostDangerIndex calculation (that includes the while loop) is the culprit. So to fix the OnScannedRobot method, I replaced the mostDangerIndex calculation (that includes the while loop) with:

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

Now it works! shen_robot is working!

NonlinearFruit
  • 2,509
  • 1
  • 25
  • 27