Skip to content

Instantly share code, notes, and snippets.

@rosenpin
Created February 25, 2016 09:38
Show Gist options
  • Save rosenpin/e9441e10b78cd06ffd4e to your computer and use it in GitHub Desktop.
Save rosenpin/e9441e10b78cd06ffd4e to your computer and use it in GitHub Desktop.
/**
* Created by tomer on 2/25/16.
*/
import java.util.ArrayList;
import java.util.List;
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;
ArrayList<Integer> usedTreasures;
ArrayList<PirateToMove> pirateToMoves;
@Override
public void doTurn(PirateGame game) {
this.game = game;
pirateToMoves = new ArrayList<>();
matchPirateToLocation();
for (PirateToMove pirateToMove : pirateToMoves){
if (pirateToMove.isValid()){
game.setSail(pirateToMove.getPirate(),pirateToMove.getDestination());
}
}
}
public void matchPirateToLocation() {
for (Pirate pirate : game.allMyPirates()) {
if (!pirate.hasTreasure()) {
int closest = 10000000;
Treasure closestTreasure = null;
for (Treasure treasure : game.treasures()) {
if (isTreassureValid(treasure)) {
if (game.distance(pirate.getLocation(), treasure.getLocation()) < closest) {
closestTreasure = treasure;
closest = game.distance(pirate.getLocation(), treasure.getLocation());
}
}
}
if (closestTreasure != null) {
usedTreasures.add(closestTreasure.getId());
}
pirateToMoves.add(new PirateToMove(pirate, closestTreasure.getLocation()));
}
}
}
public boolean isTreassureValid(Treasure treasure) {
for (int id : usedTreasures) {
if (id == treasure.getId()) {
return false;
}
}
return true;
}
class PirateToMove {
Pirate pirate;
Location destination;
boolean valid = true;
public PirateToMove(Pirate pirate, Location destination) {
this.pirate = pirate;
if (destination == null) {
this.valid = false;
}
this.destination = destination;
}
public Pirate getPirate() {
return pirate;
}
public Location getDestination() {
return destination;
}
public boolean isValid() {
return valid;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment