Skip to content

Instantly share code, notes, and snippets.

@rosenpin
Created February 25, 2016 11:26
Show Gist options
  • Save rosenpin/c77b17f4bfe2a53d8f65 to your computer and use it in GitHub Desktop.
Save rosenpin/c77b17f4bfe2a53d8f65 to your computer and use it in GitHub Desktop.
package bots;
import java.util.ArrayList;
import java.util.List;
import org.omg.PortableInterceptor.LOCATION_FORWARD;
import pirates.game.Direction;
import pirates.game.Location;
import pirates.game.Treasure;
import pirates.game.Pirate;
import pirates.game.PirateBot;
import pirates.game.PirateGame;
public class MyBot implements PirateBot {
PirateGame game;
@Override
public void doTurn(PirateGame game) {
this.game = game;
inValidTreasures = new ArrayList<>();
Pirate pirate;
Treasure treasure;
for (int i = 0; i < 3; i++) {
pirate = game.allMyPirates().get(i);
treasure = getClosestTreasure(pirate);
if (!pirate.hasTreasure()) {
if (treasure != null) {
List<Location> sailOptions = game.getSailOptions(pirate, treasure.getLocation(), 2);
game.setSail(pirate, sailOptions.get(0));
} else {
game.setSail(pirate, Home(pirate, 2));
}
} else {
game.setSail(pirate, Home(pirate, 1));
}
}
}
public Treasure getClosestTreasure(Pirate pirate) {
Treasure closest = null;
int distance = Integer.MAX_VALUE;
for (Treasure treasure : game.treasures()) {
if (game.distance(pirate.getLocation(), treasure.getLocation()) < distance && isValid(treasure)) {
distance = game.distance(pirate.getLocation(), treasure.getLocation());
closest = treasure;
}
}
if (closest != null)
invalidate(closest);
return closest;
}
public ArrayList<Integer> inValidTreasures;
public boolean isValid(Treasure treasure) {
for (int id : inValidTreasures) {
if (treasure.getId() == id) {
return false;
}
}
return true;
}
public void invalidate(Treasure treasure) {
inValidTreasures.add(treasure.getId());
}
public Location Home(Pirate pirate, int speed) {
return game.getSailOptions(pirate, pirate.getInitialLocation(), speed).get(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment