package org.nusco.swimmers.application;

import java.util.Comparator;
import java.util.List;

import org.nusco.swimmers.creature.Narjillo;
import org.nusco.swimmers.pond.Pond;
import org.nusco.swimmers.shared.utilities.Chronometer;
import org.nusco.swimmers.shared.utilities.RanGen;

public class Experiment {

	private static final int CYCLES = 1_000_000_000;
	private static final long SEED = 2648718169735535616l;

	private static final Chronometer ticksChronometer = new Chronometer();

	public static void main(String... args) {
		RanGen.seed(SEED);

		System.out.println("Starting...");
		long startTime = System.currentTimeMillis();

		runExperiment();

		long endTime = System.currentTimeMillis();
		double timeInSeconds = ((double) (endTime - startTime)) / 1000;
		System.out.println("Done (" + timeInSeconds + "s)");
	}

	private static void runExperiment() {
		Pond pond = new Cosmos();
		for (int i = 0; i < CYCLES; i++) {
			pond.tick();
			ticksChronometer.tick();
			if (i % 10_000 == 0)
				System.out.println(getStatusString(pond, i));
		}
	}

	private static Narjillo getMostProlificNarjillo(Pond pond) {
		List<Narjillo> narjillos = pond.getNarjillos();
		// use sort() instead
		Narjillo result = null;
		for (Narjillo narjillo : narjillos) {
			if (narjillo.getNumberOfDescendants() > result.getNumberOfDescendants())
				result = narjillo;
		}
		return result;
	}

	private static String getStatusString(Pond pond, int tick) {
		Narjillo mostProlificNarjillo = getMostProlificNarjillo(pond);

		return 	mostProlificNarjillo.getNumberOfDescendants() + ", " +
				mostProlificNarjillo.getGenes() + ", " +
				tick + ", " +
				pond.getNumberOfNarjillos() + ", " +
				pond.getNumberOfFoodPieces() + ", " +
				ticksChronometer.getTicksInLastSecond();
	}
}
@@ -1172,62 +1172,86 @@
 ;%0A%09%09
-// use sort() instead%0A%09%09Narjillo result = null;%0A%09%09for 
+narjillos.sort(new Comparator%3CNarjillo%3E() %7B%0A%09%09%09@Override%0A%09%09%09public int compare
 (Nar
@@ -1257,24 +1257,28 @@
 rjillo n
+1, N
 arjillo 
 : narjil
@@ -1273,38 +1273,27 @@
 llo 
-: narjillos) %7B%0A%09%09%09if (narjillo
+n2) %7B%0A%09%09%09%09return n2
 .get
@@ -1318,16 +1318,12 @@
 s() 
-%3E result
+- n1
 .get
@@ -1347,36 +1347,20 @@
 ts()
-)
+;
 %0A%09%09%09
-%09result = narjillo;
+%7D
 %0A%09%09%7D
+);
 %0A%09%09r
@@ -1369,14 +1369,24 @@
 urn 
-result
+narjillos.get(0)
 ;%0A%09%7D

Revert "Temporarily make Experiment compatible with Java 7"