// Number of ants in all 8 neighboring squares (excluding 'here') protected int neighboringAnts(ZSquare excluded) { int n = 0; for (ZSquare s : neighbors) { if (s != excluded) n += s.getNumberOfAnts(); } return n; }
// Neighboring square with food on it, if any (but only "far enough" from the nest) protected ZSquare squareWithFood(ZSquare excluded1, ZSquare excluded2) { for (ZSquare s : cells) { if (!s.isNest() && !s.isNextToNest() && s != excluded1 && s != excluded2 && s.hasFood()) return s; } return null; }
protected ZSquare bestSquareToAvoidConflict(ZSquare target) { if (isAroundNest()) return null; // Don't avoid conflict when nearby own nest double dist = 0; ZSquare best = null; for (ZSquare s : neighbors) { if (s.isPassable() && s.getNumberOfAnts() == 0) { double d = Constants.normalDistance(target.x - s.x, target.y - s.y); if (d > dist) best = s; } } if (best != null) assert best.isPassable(); return best; }
/** * On each turn, act will be called to allow your ant to take some action in the world. * * @param environment a description of the 9 squares around this ant (one in each direction + the * one the ant is currently on) * @param events A list of Strings describing actions that affected this ant in the last turn. In * particular, if any ants said anything or fought with this ant, they will show up here * @return an implementation of the Action class indicating what this ant should do */ public Action act(Environment environment, List<WorldEvent> events) { // L long elapsedTimeMillis = System.currentTimeMillis(); // Logger. Action act = null; turn++; here.update(environment); if (role == null) { assert id == 0; assert turn == 1; assert here.isNest(); id = here.scent.a + 1; Scent s = new Scent(); s.a = id; initializeState(); assert role != null; return new Write(s.getValue()); } northeast.update(environment); east.update(environment); southeast.update(environment); south.update(environment); southwest.update(environment); west.update(environment); northwest.update(environment); north.update(environment); if (here.scent.stinky) act = new Write(null); // Erase opponent's writing if (act == null) act = role.act(); if (act == null) act = new Pass(); if (act instanceof Move) { ZSquare s = square(act); assert s.isPassable(); x += s.deltaX; y += s.deltaY; trail.add(this); // L Logger.trace(this, "move " + s.dir.name()); } else if (act instanceof GetFood) { assert !hasFood; ZSquare s = square(act); assert s.isPassable() && s.hasFood(); hasFood = true; // L Logger.trace(this, String.format("takes food %s %d,%d", s.dir.name(), s.x, s.y)); } else if (act instanceof DropFood) { assert hasFood; ZSquare s = square(act); assert s.isPassable(); hasFood = false; // L Logger.trace(this, String.format("drops food %s %d,%d", s.dir.name(), s.x, s.y)); // L } else if (act instanceof Write) { // Logger. // L Logger.trace(this, String.format("writes: %s", (new Scent(act)).toString())); // L } else if (act instanceof Say) { // Logger. // L Logger.trace(this, String.format("Says: '%s'", act.toString())); // L } else if (act instanceof Pass) { // Logger. // Do nothing // L } else { // Logger. // L Logger.trace(this, "check action " + Constants.className(act)); // L assert false; // Logger. } // L elapsedTimeMillis = System.currentTimeMillis() - elapsedTimeMillis; // Logger. // L Logger.logRunTime(this, elapsedTimeMillis); // L if (turn % 1000 == 0) Logger.dumpBoard(this); return act; }
protected boolean isNextToNest() { return here.isNextToNest(); }
// Are we relatively close to the nest? protected boolean isAroundNest() { return here.isAroundNest(); }
protected ZSquare nest() { assert here.isNest() || isNextToNest(); if (here.isNest()) return here; return squareTo(Constants.BOARD_SIZE, Constants.BOARD_SIZE); }