Пример #1
0
 /**
  * init() will be called on each ant at creation time. This will be before the first move of the
  * match
  */
 public void init() {
   id = turn = 0;
   x = y = Constants.BOARD_SIZE;
   hasFood = false;
   board = new Board(this);
   trail = new Trail();
   north = new ZSquare(this, Direction.north);
   northeast = new ZSquare(this, Direction.northeast);
   east = new ZSquare(this, Direction.east);
   southeast = new ZSquare(this, Direction.southeast);
   south = new ZSquare(this, Direction.south);
   southwest = new ZSquare(this, Direction.southwest);
   west = new ZSquare(this, Direction.west);
   northwest = new ZSquare(this, Direction.northwest);
   here = new ZSquare(this, Direction.here);
   neighbors = new ArrayList<ZSquare>();
   cells = new ArrayList<ZSquare>();
   neighbors.add(north);
   neighbors.add(northeast);
   neighbors.add(east);
   neighbors.add(southeast);
   neighbors.add(south);
   neighbors.add(southwest);
   neighbors.add(west);
   neighbors.add(northwest);
   cells.add(north);
   cells.add(northeast);
   cells.add(east);
   cells.add(southeast);
   cells.add(south);
   cells.add(southwest);
   cells.add(west);
   cells.add(northwest);
   cells.add(here);
   trail.add(this);
 }
Пример #2
0
 /**
  * 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;
 }