From: Elijah Cohen Date: Mon, 21 Nov 2022 18:50:41 +0000 (-0600) Subject: wow adding life for the first time? X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=69095fcfa09cc73c6a2296e44d2759291b5b0a05;p=hexgrid wow adding life for the first time? --- diff --git a/life.html b/life.html new file mode 100644 index 0000000..97b7338 --- /dev/null +++ b/life.html @@ -0,0 +1,89 @@ + + + + + + + + + + + + hexagonal grids + + +
+ + + + + + + + +
+ rule +
+ born + + 1 + 2 + 3 + 4 + 5 + 6 + +
+
+ survives + + 1 + 2 + 3 + 4 + 5 + 6 + +
+
+
+ + + + + + + + + + + + + + + + + +
+
+
+ + diff --git a/life.js b/life.js new file mode 100644 index 0000000..6bd3c50 --- /dev/null +++ b/life.js @@ -0,0 +1,127 @@ + +// this one is sweet +//let SURVIVAL = []; +//let PRODUCES = [1]; + + +// the real one tho i think +//let SURVIVAL = [3,4]; +//let PRODUCES = [2]; + +// alt? +let SURVIVAL = [3]; +let PRODUCES = [2]; + + +const LifeState = { + OFF:'off', + ON:'on', + NIL:'nil' +} +Object.freeze(LifeState); + +function nextKey(k,fwd=true) { + switch(k) { + case LifeState.OFF: + return fwd? LifeState.ON : LifeState.NIL; + case LifeState.ON: + return fwd? LifeState.NIL : LifeState.OFF; + case LifeState.NIL: + default: + return fwd? LifeState.OFF : LifeState.ON; + } +} + +class LifeHex extends Hex { + constructor(state,q,r,s) { + super(q,r,s); + this.state = state; + } + clone() { + return new LifeHex(this.state,this.q,this.r,this.s); + } + static fromJSON(o) { + return new LifeHex(o.state,o.q,o.r); + } + drawState(ctx,x,y,sz) { + switch (this.state) { + case LifeState.OFF: + drawHexagon(ctx,x,y,sz,'rgb(0,0,0)'); + break; + case LifeState.ON: + drawHexagon(ctx,x,y,sz,'rgb(0,0,255)'); + break; + case LifeState.NIL: + default: + return; + } + } +} + +class LifeGrid extends Grid { + constructor() { + super(LifeHex, LifeState.NIL); + } + step() { + let cellcp = this.cells.map(c=>c.clone()); + function stateAtClone(cl,q,r) { + let cells = cl.filter(c => c.q==q && c.r==r); + if(cells.length == 0) return LifeState.OFF; + return cells[0].state; + } + for(var i=0; i { + let s = stateAtClone(cellcp,b.q,b.r); + let v = s == LifeState.ON ? 1 : 0; + return a + v; + } + let nbrct = nbrs.reduce(reducefn,0); + if(cellcp[i].state == LifeState.ON) { + if(!SURVIVAL.includes(nbrct)) { + this.cells[i].state = LifeState.OFF; + } + } + if(cellcp[i].state == LifeState.OFF) { + if(PRODUCES.includes(nbrct)) { + this.cells[i].state = LifeState.ON; + } + } + } + } + } +} + +g = new LifeGrid(); + + +function blank(q,r,g) { + for(let i=0;i