TorusAgent implements ILocated2D { public TorusAgent closestAgent(TorusAgent[] others) { TorusAgent closest = null; double closestDistance = Integer.MAX_VALUE; for (TorusAgent other : others) { double manhattanDistance = distance(other); if (manhattanDistance < closestDistance) { closestDistance = manhattanDistance; closest = other; } } return closest; } 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 testClosestAgent() { TorusAgent[] preys = new TorusAgent[4]; TorusAgent[] preds = new TorusAgent[4]; preds[0] = new TorusAgent(new TorusWorld(100, 100), 10, 10, 0); preds[1] = new TorusAgent(new TorusWorld(100, 100), 20, 20, 0); preds[2] = new TorusAgent(new TorusWorld(100, 100), 30, 30, 0); preys[0] = new TorusAgent(new TorusWorld(100, 100), 30, 10, 1); preys[1] = new TorusAgent(new TorusWorld(100, 100), 40, 40, 1); preys[2] = new TorusAgent(new TorusWorld(100, 100), 10, 25, 1); assertSame(pred.closestAgent(preds), preds[0]); assertSame(pred.closestAgent(preys), preys[2]); assertSame(prey.closestAgent(preds), preds[0]); assertSame(prey.closestAgent(preys), preys[2]); assertSame(preds[1].closestAgent(preds), preds[1]); assertSame(preys[2].closestAgent(preys), preys[2]); assertSame(preds[0].closestAgent(preys), preys[2]); assertSame(preds[1].closestAgent(preys), preys[2]); assertSame(preds[2].closestAgent(preys), preys[0]); assertSame(preys[0].closestAgent(preds), preds[0]); assertSame(preys[1].closestAgent(preds), preds[2]); assertSame(preys[2].closestAgent(preds), preds[0]); TorusAgent negPred = new TorusAgent(new TorusWorld(100, 100), 96, 95, 0); TorusAgent negPrey = new TorusAgent(new TorusWorld(100, 100), 93, 94, 1); preds[3] = negPred; preys[3] = negPrey; assertSame(pred.closestAgent(preds), preds[3]); assertSame(pred.closestAgent(preys), preys[3]); assertSame(prey.closestAgent(preds), preds[3]); assertSame(prey.closestAgent(preys), preys[3]); }