I'm surprised I didn't see anyone post this, so here it is:
This is a version of `Wild.java` that speeds up the implementation by many times. It was taking ~3 minutes with the number of wolves I had, but with this it takes < 4 seconds. It simply calculates the result before going into swing:
<!-- language: lang-java -->
package wild;
import animals.*;
import javax.swing.JFrame; //star imports are bad!
import javax.swing.JLabel;
public class Wild {
private static final Class[] classes
= { //Listed like this to make commenting out easy: CTRL-/
Bear.class,
Lion.class,
Stone.class,
Wolf.class
};
public static final int MAP_SIZE = Math.round((float) Math.sqrt(classes.length + 3) * 20);
public static void main(String[] args) {
int size = MAP_SIZE; //This was redundant. Removed.
Game game = new Game(size);
Statistics stats = new Statistics(game, classes);
for (Class c : classes) {
game.populate(c, 100);
}
stats.update();
for (int i = 0; i < 1000; i++) {
game.iterate();
stats.update();
}
//If you want to run multiple times and average, run until here several times.
JFrame frame = new JFrame();
frame.add(new JLabel(stats.toString()));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Here is a version that will output the average of ten runs (rounded to the nearest integer):
<!-- language: lang-java -->
package wild;
import animals.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Wild {
private static final Class[] classes
= {
Bear.class,
Lion.class,
Stone.class,
Wolf.class
};
public static final int MAP_SIZE = Math.round((float) Math.sqrt(classes.length + 3) * 20);
public static void main(String[] args) {
int size = MAP_SIZE;
double[] avgs = new double[classes.length];
for (int i = 0; i < avgs.length; i++) {
avgs[i] = 0;
}
for (int n = 0; n < 10; n++) { // change 10 to the number of runs you'd like to make.
Game game = new Game(size);
Statistics stats = new Statistics(game, classes);
for (Class c : classes) {
game.populate(c, 100);
}
stats.update();
for (int i = 0; i < 1000; i++) {
game.iterate();
stats.update();
}
int[] livings = stats.nums();
if (n != 0) {
for (int i = 0; i < livings.length; i++) {
avgs[i] = (avgs[i] * n + livings[i]) / (n + 1);
}
} else {
for (int i = 0; i < livings.length; i++) {
avgs[i] = livings[i];
}
}
}
String s = "<html>";
for (int i = 0; i < classes.length; i++) {
s += classes[i] + " - " + Math.round(avgs[i]) + "<br>";
}
s += "</html>";
JFrame frame = new JFrame();
frame.add(new JLabel(s));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}