class GnatSwarm extends Swarm { GnatSwarm(Environment e) { super(e); } void update() { super.update(); //remove dead gnats Particle p; Gnat gn; for(int i = 0; i < particles.size(); i++) { p = (Particle) particles.get(i); if (p instanceof Gnat) { gn = (Gnat) p; if (gn.isDead()) { particles.remove(i); i--; } } } //breed remaining gnats Particle p2; Gnat gn2; for(int i = 0; i < particles.size(); i++) { p = (Particle) particles.get(i); if (p instanceof Gnat) { gn = (Gnat) p; if (gn.isFertile()) { p2 = (Particle) particles.get((int)random(particles.size())); if (p2 instanceof Gnat) { gn2 = (Gnat) p2; if (gn2.isFertile()) { //println("before add particle " + particles.size()); addParticle(gn.reproduce(gn2)); //we allow for asexual reproduction, but both parents must be fertile. //println("after add particle " + particles.size()); } } } } } } }