public void doTurn() { Ants ants = getAnts(); for (Tile myAnt : ants.getMyAnts()) { for (Aim direction : Aim.values()) { if (ants.getIlk(myAnt, direction).isPassable()) { ants.issueOrder(myAnt, direction); break; } } } }
public class Ants { private final Set<Tile> employed = new HashSet<Tile>(); //...
public void clearMyAnts() { employed.clear(); //...
public boolean issueOrder(Tile myAnt, Aim direction) { Order order = new Order(myAnt, direction); Tile newPosition = getTile(myAnt, direction); if(getIlk(myAnt, direction).isPassable() && !employed.contains(newPosition)) { orders.add(order); // , employed.add(newPosition); System.out.println(order); System.out.flush(); return true; } return false; }
public void doTurn() { Ants ants = getAnts(); for (Tile myAnt : ants.getMyAnts()) { for (Aim direction : Aim.values()) { if (ants.issueOrder(myAnt, direction)) break; } } }
public class Ant { protected Ants ants; protected Tile tile; protected int waitStep = 0; public Ant(Ants ants, Tile tile) { this.tile = tile; this.ants = ants; } public Tile getTile() { return tile; } // ( !) public boolean issueOrder(Aim direction) { if(ants.issueOrder(tile, direction)) { tile = ants.getTile(tile, direction); return true; } else return false; } // , public boolean waiting() { return waitStep-- > 0; } // public void think() { for(Aim direction : Aim.values()) { if(ants.issueOrder(direction)) break; } } }
public void doTurn() { getAnts().think(); // }
private final Set<Ant> myAnts = new HashSet<Ant>(); //... public void think() { for(Ant ant : myAnts) { if(ant.waiting()) // , continue; ant.think(); // } }
public enum Ilk { MY_DEAD, ENEMY_DEAD, // DEAD, //... public boolean isUnoccupied() { return this == LAND || this == MY_DEAD || this == ENEMY_DEAD; } }
public void removeAnt(int row, int col, int owner) { ants.update(owner > 0 ? Ilk.ENEMY_DEAD : Ilk.MY_DEAD, new Tile(row, col)); }
public void update(Ilk ilk, Tile tile) { map[tile.getRow()][tile.getCol()] = ilk; switch (ilk) { case FOOD: foodTiles.add(tile); break; case MY_ANT: myAnts.add(new Ant(this, tile)); break; case ENEMY_ANT: enemyAnts.add(tile); break; case MY_DEAD: myAnts.remove(new Ant(this, tile)); } }
public class Ant { //... public void think() { if(!doFood()) // , doRandom(); // } protected boolean doRandom() { while(!issueOrder(Aim.getRandom())); // return true; } protected boolean doFood() { return moveToObject(ants.getFoodTiles()); } // protected boolean moveToObject(Set<Tile> objects) { Tile object = findObject(objects); if(object != null) { issueOrder(ants.getDirections(object, tile).get(0)); return true; } return false; } // protected Tile findObject(Set<Tile> objects) { Tile find = null; int distance = 0; int minDistance = ants.MAX_MAP_SIZE; for(Tile aspt : objects) { distance = ants.getDistance(aspt, tile); if(minDistance > distance) { find = aspt; minDistance = distance; } } return find; } }
public enum Aim { private static final Random random = new Random(); //... public static Aim getRandom() { return values()[random.nextInt(values().length)]; } }
public List<Aim> getDirections(Tile t1, Tile t2) { List<Aim> directions = new ArrayList<Aim>(); if (t1.getRow() < t2.getRow()) { if (t2.getRow() - t1.getRow() >= rows / 2) { directions.add(Aim.SOUTH); // Aim.NORTH } else { directions.add(Aim.NORTH); // Aim.SOUTH } } else if (t1.getRow() > t2.getRow()) { if (t1.getRow() - t2.getRow() >= rows / 2) { directions.add(Aim.NORTH); // ... } else { directions.add(Aim.SOUTH); //... ( ) } } if (t1.getCol() < t2.getCol()) { if (t2.getCol() - t1.getCol() >= cols / 2) { directions.add(Aim.EAST); // ... } else { directions.add(Aim.WEST); //... ( ) } } else if (t1.getCol() > t2.getCol()) { if (t1.getCol() - t2.getCol() >= cols / 2) { directions.add(Aim.WEST); // ... } else { directions.add(Aim.EAST); //... ( ) } } return directions; }
Source: https://habr.com/ru/post/130722/
All Articles