TorusAgent implements ILocated2D { @Override public double distance(ILocated2D other) { if (other == null) { return Double.POSITIVE_INFINITY; } return world.shortestXDistance(x, (int) other.getX()) + world.shortestYDistance(y, (int) other.getY()); } TorusAgent(TorusWorld world, int startX, int startY, int type); int getAgentType(); @Override Tuple2D getPosition(); @Override double distance(ILocated2D other); @Override double getX(); @Override double getY(); void move(int dx, int dy); Color getColor(); boolean isCoLocated(TorusAgent other); boolean isCoLocated(TorusAgent[] others); TorusAgent closestAgent(TorusAgent[] others); double[] distances(TorusAgent[] others); double shortestXOffset(TorusAgent torusAgent); double shortestYOffset(TorusAgent torusAgent);  }
@Test public void testDistance() { TorusAgent otherPred = new TorusAgent(new TorusWorld(100, 100), 48, 24, 0); TorusAgent otherPrey = new TorusAgent(new TorusWorld(100, 100), 79, 33, 1); assertEquals(prey.distance(prey), 0, doubleThreshold); assertEquals(prey.distance(pred), 0, doubleThreshold); assertEquals(prey.distance(otherPred), 72.0, doubleThreshold); assertEquals(prey.distance(otherPrey), 54.0, doubleThreshold); assertEquals(pred.distance(pred), 0, doubleThreshold); assertEquals(pred.distance(prey), 0, doubleThreshold); assertEquals(pred.distance(otherPred), 72.0, doubleThreshold); assertEquals(pred.distance(otherPrey), 54.0, doubleThreshold); assertEquals(otherPrey.distance(otherPrey), 0, doubleThreshold); assertEquals(otherPrey.distance(otherPred), 40, doubleThreshold); assertEquals(otherPrey.distance(prey), 54.0, doubleThreshold); assertEquals(otherPrey.distance(pred), 54, doubleThreshold); assertEquals(otherPred.distance(otherPrey), 40, doubleThreshold); assertEquals(otherPred.distance(otherPred), 0, doubleThreshold); assertEquals(otherPred.distance(prey), 72.0, doubleThreshold); assertEquals(otherPred.distance(pred), 72.0, doubleThreshold); TorusAgent eatenPrey = new TorusAgent(new TorusWorld(100, 100), 12, 99, 1); eatenPrey = null; assertEquals(prey.distance(eatenPrey), Double.POSITIVE_INFINITY, doubleThreshold); assertEquals(pred.distance(eatenPrey), Double.POSITIVE_INFINITY, doubleThreshold); assertEquals(otherPrey.distance(eatenPrey), Double.POSITIVE_INFINITY, doubleThreshold); assertEquals(otherPred.distance(eatenPrey), Double.POSITIVE_INFINITY, doubleThreshold); }