]> git.eli173.com Git - hexgrid/commitdiff
wow adding life for the first time?
authorElijah Cohen <eli@eli173.com>
Mon, 21 Nov 2022 18:50:41 +0000 (12:50 -0600)
committerElijah Cohen <eli@eli173.com>
Mon, 21 Nov 2022 18:50:41 +0000 (12:50 -0600)
life.html [new file with mode: 0644]
life.js [new file with mode: 0644]

diff --git a/life.html b/life.html
new file mode 100644 (file)
index 0000000..97b7338
--- /dev/null
+++ b/life.html
@@ -0,0 +1,89 @@
+<!doctype html>
+<meta charset="utf-8">
+<html>
+  <head>
+    <script src="grid.js"></script>
+               <script src="cell.js"></script>
+               <script src="play.js"></script>
+               <script src="life.js"></script>
+               <script>
+                       function run() {
+                                       lc();
+                       }
+               </script>
+               <style>
+                       span {
+                                       margin=left: 15px;
+                       }
+                       input[type=number] {
+                                       width: 30px;
+                                       margin-left: 15px;
+                       }
+                       select {
+                                       margin-left: 15px;
+                       }
+                       #es {
+                                       margin-left: 15px;
+                       }
+               </style>
+               <link rel="icon" type="image/png" href="icn.png"/>
+    <title>hexagonal grids</title>
+  </head>
+  <body onload="run()" style="background-color: #f8f8ff;">
+               <div class="ctrl">
+                       <span>
+                               <input type="button" id="step" value="step"/>
+                               <input type="button" id="playpause" value="play"/>
+                               <input type="range" id="tempo" value="150" min="20" max="500"/>
+                               <input type="button" id="clear" value="clear"/>
+                               <input type="button" id="reset" value="reset"/>
+                       </span>
+                       <span style="display:inline;">
+                               <details style="display:inline-block;">
+                                       <summary style="float:left;">rule</summary>
+                                       <details id="rule" style="float:left">
+                                               <summary style="float:left;">born</summary>
+                                               <span style="float:left;">
+                                                       <input type="checkbox" id="b1" name="1" >1
+                                                       <input type="checkbox" id="b2" name="2" checked>2
+                                                       <input type="checkbox" id="b3" name="3" >3
+                                                       <input type="checkbox" id="b4" name="4" >4
+                                                       <input type="checkbox" id="b5" name="5" >5
+                                                       <input type="checkbox" id="b6" name="6" >6
+                                               </span>
+                                       </details>
+                                       <details id="surv" style="float:left">
+                                               <summary style="float:left;">survives</summary>
+                                               <span style="float:left;">
+                                                       <input type="checkbox" id="s1" name="1" >1
+                                                       <input type="checkbox" id="s2" name="2" >2
+                                                       <input type="checkbox" id="s3" name="3" checked>3
+                                                       <input type="checkbox" id="s4" name="4" checked>4
+                                                       <input type="checkbox" id="s5" name="5" >5
+                                                       <input type="checkbox" id="s6" name="6" >6
+                                               </span>
+                                       </details>
+                               </details>
+                       </span>
+                       <span>
+                               <input type="number" id="rad" value="9" min="1" max="30" step="1"/>
+                               <input type="button" id="rot" value="rotate hex"/>
+                               <input type="button" id="flp" value="flip hex"/>
+                               <input type="button" id="rfl" value="reflect hex"/>
+                               <input type="button" id="copy" value="copy hex"/>
+                               <input type="button" id="paste" value="paste hex"/>
+                       </span>
+                       <span>
+                               <input type="number" id="slot" value="1" min="1" max="9" step="1"/>
+                               <input type="button" id="save" value="save state"/>
+                               <input type="button" id="load" value="load state"/>
+                       </span>
+                       <span>
+                               <input type="button" id="es" value="export state"/>
+                               <input type="button" id="is" value="import state"/>
+                       </span>
+                       <details><summary></summary><span id="outzone"></span></details>
+               </div>
+    <div><canvas width="2000" height="2000" id="c"></canvas></div>
+  </body>
+</html>
diff --git a/life.js b/life.js
new file mode 100644 (file)
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<cellcp.length; i++) {
+                                               if(cellcp[i].state != LifeState.NIL) {
+                                                               let nbrs = cellcp[i].neighbors();
+                                                               let reducefn = (a,b) => {
+                                                                               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<q;i++) {
+                               for(let j=0;j<r;j++) {
+                                               g.change(LifeState.OFF,i,j);
+                               }
+               }
+}
+function blankHex(q,r,sz,g) {
+               let h = g.copyHex(q,r,sz);
+               for(let c of h) {
+                               g.change(LifeState.OFF,c.q,c.r)
+               }
+}
+
+
+function randomize(q,r,g) {
+               // puts random elements in the grid
+               for(let i=0;i<q;i++) {
+                               for(let j=0; j<r;j++) {
+                                               let rv = Math.random();
+                                               if(rv < 0.5) {
+                                                               g.change(LifeState.OFF,i,j);
+                                               }
+                                               else {
+                                                               g.change(LifeState.ON,i,j);
+                                               }
+                               }
+               }
+}