Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created May 31, 2017 15:49
Show Gist options
  • Save cbilson/c2ae16363856a76c9e0646abd8eb5845 to your computer and use it in GitHub Desktop.
Save cbilson/c2ae16363856a76c9e0646abd8eb5845 to your computer and use it in GitHub Desktop.
import java.awt.Color;
import ihs.apcs.spacebattle.BasicEnvironment;
import ihs.apcs.spacebattle.BasicSpaceship;
import ihs.apcs.spacebattle.ObjectStatus;
import ihs.apcs.spacebattle.Point;
import ihs.apcs.spacebattle.RadarResults;
import ihs.apcs.spacebattle.RegistrationData;
import ihs.apcs.spacebattle.commands.BrakeCommand;
import ihs.apcs.spacebattle.commands.IdleCommand;
import ihs.apcs.spacebattle.commands.RadarCommand;
import ihs.apcs.spacebattle.commands.RotateCommand;
import ihs.apcs.spacebattle.commands.ShipCommand;
import ihs.apcs.spacebattle.commands.ThrustCommand;
public class SurvivorShip extends BasicSpaceship {
private RadarResults radar;
@Override
public ShipCommand getNextCommand(BasicEnvironment environment) {
Point pos = environment.getShipStatus().getPosition();
double speed = environment.getShipStatus().getSpeed();
int orientation = environment.getShipStatus().getOrientation();
if (environment.getRadar() != null)
radar = environment.getRadar();
if (radar == null) {
System.out.println("No radar. Ping");
return new RadarCommand(5);
}
// if anything is near me and in front of me, avoid it
for (int i = 0; i < radar.getNumObjects(); i++) {
ObjectStatus object = radar.get(i);
double distToObject = object.getPosition().getDistanceTo(pos);
if (distToObject < 100.0) {
double angleToObject =
pos.getAngleTo(object.getPosition());
double turn = angleToObject - orientation;
if (turn > 0 &&
turn < 60.0) {
if (speed > 10) {
System.out.println("Breaking to avoid "
+ object.getName());
return new BrakeCommand(0.01);
}
// avoid
System.out.println("Turning right to avoid "
+ object.getName());
return new RotateCommand(-70);
}
else if (turn < 0 &&
turn > -60.0) {
if (speed > 10) {
System.out.println("Breaking to avoid "
+ object.getName());
return new BrakeCommand(0.01);
}
System.out.println("Turning left to avoid "
+ object.getName());
return new RotateCommand(70);
}
}
}
if (speed < 30) {
System.out.println("Accelerating");
return new ThrustCommand('B', 1.0, 0.9);
}
System.out.println("Ping");
return new RadarCommand(5);
}
@Override
public RegistrationData registerShip(
int numImages,
int worldWidth,
int worldHeight) {
return new RegistrationData(
"1stSurvivor",
new Color(128, 128, 128),
6);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment