PreyFleeClosestPredatorController extends TorusPredPreyController { @Override public int[] getAction(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey) { TorusAgent closestPredator = me.closestAgent(preds); double[] moveDistances = new double[preyActions().length]; for (int i = 0; i < preyActions().length; i++) { double distance = closestPredator .distance(me.getPosition().add(new Tuple2D(preyActions()[i][0], preyActions()[i][1]))); moveDistances[i] = distance; } return preyActions()[StatisticsUtilities.argmax(moveDistances)]; } PreyFleeClosestPredatorController(); @Override int[] getAction(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey);  }
@Test public void testGetAction() { game = new TorusPredPreyGame(100, 100, 3, 2); world = new TorusWorld(100, 100); TorusAgent[] prey = game.getPrey(); TorusAgent[] preds = game.getPredators(); PreyFleeClosestPredatorController preyController = new PreyFleeClosestPredatorController(); preds[1].move((int) -preds[1].getX(), (int) -preds[1].getY()); preds[2].move((int) -preds[2].getX(), (int) -preds[2].getY()); prey[0].move((int) -prey[0].getX(), (int) -prey[0].getY()); prey[0].move(1, 1); preds[0].move((int) -preds[0].getX(), (int) -preds[0].getY()); preds[0].move(1, 0); assertFalse(preyController.getAction(prey[0], world, preds, prey)[1] == -1); prey[0].move(4, 4); preds[0].move(4, 0); preds[1].move(0, 5); preds[2].move(5, 10); int[] preyAction = preyController.getAction(prey[0], world, preds, prey); assertFalse(preyAction[1] == -1); prey[0].move(preyAction[0], preyAction[1]); assertTrue((prey[0].getX() == 4 && prey[0].getY() == 5) || (prey[0].getX() == 5 && prey[0].getY() == 6) || (prey[0].getX() == 6 && prey[0].getY() == 5)); prey[0].move((int) -prey[0].getX(), (int) -prey[0].getY()); prey[0].move(6, 11); preyAction = preyController.getAction(prey[0], world, preds, prey); assertFalse(preyAction[0] == -1); assertFalse(preyAction[1] == -1); assertTrue((preyAction[0] == 1 && preyAction[1] == 0) || (preyAction[0] == 0 && preyAction[1] == 1)); prey[0].move(preyAction[0], preyAction[1]); assertTrue((prey[0].getX() == 6 && prey[0].getY() == 12) || (prey[0].getX() == 7 && prey[0].getY() == 11)); }