population sim

by wrxsti85

HTML

<pre class="stats"></pre>
<pre class="people"></pre>

JavaScript

class Person {

    constructor() {
        this._name = this.guid();
        this._alive = true;
        this._killed = [];
        this._killedBy = null;
		this._done = false;
    }

    get name() 		{ return this._name; 	 }
    get alive() 	{ return this._alive; 	 }
    get killed() 	{ return this._killed; 	 }
    get killedBy() 	{ return this._killedBy; }
	get done()		{ return this._done;	 }

    set alive(status) 	{ this._alive = status; 	}
    set killed(name) 	{ this._killed.push(name); 	}
    set killedBy(name) 	{ this._killedBy = name; 	}
	set done(status)	{ this._done = status; 		}

    guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
                .toString(16)
                .substring(1);
        }
        return s4() + '-' + s4() + '-' + s4() + '-' + s4();
    }

}

class Sim {

    constructor() {
		this._iterations = 0;
		this._population = 0;
		this._done = 0;		
		this._people = [];
        this._dead = [];
        this._finished = []; 
	}

    init(population) {
        this.generatePopulation(population);
        this.deathLoop();
    }

    generatePopulation(population) {
		this._population = population;
        for (let i = population; i > 0; i--) {
            this._people.push(new Person());
        }
    }
    
    kill(killerIndex, victimIndex) {
    	let killer = this._people[killerIndex],
        	victim = this._people[victimIndex];
        if (killer.name != victim.name && killer.alive && victim.alive && killer.killed.length < 2) {
            killer.killed = victim.name;
            if(killer.killed.length === 2 && !killer.done){
                killer.done = true;
                this._done++;
            }
            this.die(killerIndex, victimIndex)
        }
    }
    
    die(killerIndex, victimIndex, success) {
    	let killer = this._people[killerIndex],
        	victim = this._people[victimIndex];
        victim.alive = false;
        victim.killedBy = killer.name;
       ...