Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.List;
- import com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl;
- import ihs.apcs.spacebattle.*;
- import ihs.apcs.spacebattle.commands.*;
- public class MyFirstShip extends BasicSpaceship {
- final static int SCANNING = 1;
- final static int HUNTING = 2;
- final static int AIMING = 3;
- final static int FIRING = 4;
- private int mode = SCANNING;
- private ObjectStatus target = null;
- @Override
- public RegistrationData registerShip(int numImages, int worldWidth, int worldHeight)
- {
- return new RegistrationData("all your base are belong to us", new Color(0, 255, 255), 0);
- }
- //Send a radar ping to see what's out there
- private ShipCommand scan() {
- mode = HUNTING;
- return new RadarCommand(4); //Find objects
- }
- //Determine which target you'll fire at
- private ShipCommand hunt(BasicEnvironment env) {
- Point myPosition = env.getShipStatus().getPosition();
- List<ObjectStatus> asteroids = env.getRadar().getByType("Asteroid");
- if (asteroids.size() == 0) {
- return new RadarCommand(4); //Scan again if no asteroids have been found
- }
- ObjectStatus closest = asteroids.get(0);
- double closestDistance = myPosition.getDistanceTo(closest.getPosition());
- for (ObjectStatus s : asteroids) {
- double distance = myPosition.getDistanceTo(s.getPosition());
- if (distance < closestDistance) {
- closest = s;
- closestDistance = distance;
- }
- }
- target = closest;
- mode = AIMING;
- return new RadarCommand(3, target.getId());
- }
- private ShipCommand aim(BasicEnvironment env) {
- Point myPosition = env.getShipStatus().getPosition();
- if (target != null) {
- ObjectStatus targetStatus = env.getRadar().getById(target.getId());
- Point targetPosition = targetStatus.getPosition();
- mode = FIRING;
- return new RotateCommand(myPosition.getAngleTo(targetPosition));
- } else {
- return scan();
- }
- }
- private ShipCommand fire() {
- mode = SCANNING;
- return new FireTorpedoCommand('F');
- }
- @Override
- public ShipCommand getNextCommand(BasicEnvironment env)
- {
- Point myPosition = env.getShipStatus().getPosition();
- if (mode == SCANNING) {
- return scan();
- }
- else if (mode == HUNTING) {
- return hunt(env);
- } else if (mode == AIMING) {
- return aim(env);
- } else if (mode == FIRING) {
- return fire();
- }
- return scan(); //default behavior
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement