From 419f387105d566f3cedc95bd9d2c16f9261629d5 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Fri, 8 Jul 2022 13:35:21 -0500 Subject: [PATCH] added rps automata, groundwork for drawing better --- draw.js | 19 +++++++ edit.js | 7 +++ rps.html | 63 ++++++++++++++++++++++ rps.js | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+) create mode 100644 draw.js create mode 100644 edit.js create mode 100644 rps.html create mode 100644 rps.js diff --git a/draw.js b/draw.js new file mode 100644 index 0000000..d245f73 --- /dev/null +++ b/draw.js @@ -0,0 +1,19 @@ + + +DRAWHEX_RAD_SCALE = 1.18; + +function drawHexagon(ctx,x,y,sz,color) { + ctx.save(); + ctx.strokeStyle=color; + ctx.fillStyle=color; + ctx.beginPath(); + let r = sz * Math.sqrt(3)/2 * DRAWHEX_RAD_SCALE; + for(let i=0; i<=6;i++) { + let ang = Math.PI/6 + i*Math.PI/3; + let ix = x + r * Math.cos(ang); + let iy = y + r * Math.sin(ang); + ctx.lineTo(ix,iy); + } + ctx.fill(); + ctx.restore(); +} diff --git a/edit.js b/edit.js new file mode 100644 index 0000000..96b59cc --- /dev/null +++ b/edit.js @@ -0,0 +1,7 @@ + + + +function copyrow(num, kind) { + + +} diff --git a/rps.html b/rps.html new file mode 100644 index 0000000..d558937 --- /dev/null +++ b/rps.html @@ -0,0 +1,63 @@ + + + + + + + + + + + + + hexagonal grids + + +
+ + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + diff --git a/rps.js b/rps.js new file mode 100644 index 0000000..ac766d3 --- /dev/null +++ b/rps.js @@ -0,0 +1,155 @@ +// rps automata, seen via https://twitter.com/matthen2/status/1543226575604355072?s=20&t=a35j4jJSqZ6gY8BjybAFDw + +let NEIGHBOR_LIMIT = 2; + +const RPSState = { + OFF: 'off', + ROCK: 'rock', + PAPER: 'paper', + SCISSORS:'scissors' +} +Object.freeze(RPSState); + +function nextKey(k,fwd=true) { + switch(k) { + case RPSState.OFF: + return fwd? RPSState.ROCK : RPSState.SCISSORS; + case RPSState.ROCK: + return fwd? RPSState.PAPER: RPSState.OFF; + case RPSState.PAPER: + return fwd? RPSState.SCISSORS : RPSState.ROCK; + case RPSState.SCISSORS: + default: + return fwd? RPSState.OFF : RPSState.PAPER; + } +} + +class RPSHex extends Hex { + constructor(state,q,r,s) { + super(q,r,s); + this.state = state; + } + clone() { + return new RPSHex(this.state,this.q,this.r,this.s); + } + static fromJSON(o) { + return new RPSHex(o.state, o.q, o.r); + } + drawState(ctx,x,y,sz) { + switch(this.state) { + case RPSState.ROCK: + drawHexagon(ctx,x,y,sz,'rgb(255,0,0)'); + break; + case RPSState.PAPER: + drawHexagon(ctx,x,y,sz,'rgb(0,255,0)'); + break; + case RPSState.SCISSORS: + drawHexagon(ctx,x,y,sz,'rgb(0,0,255)'); + break; + case RPSState.OFF: + default: + return; + } + } + drawStateBad(ctx, x, y, sz) { + ctx.save(); + switch(this.state) { + case RPSState.OFF: + ctx.fillStyle = 'rgb(0,0,0)'; + ctx.restore(); + return; + break; + case RPSState.ROCK: + ctx.fillStyle = 'rgb(255,0,0)'; + break; + case RPSState.PAPER: + ctx.fillStyle = 'rgb(0,255,0)'; + break; + case RPSState.SCISSORS: + default: + ctx.fillStyle = 'rgb(0,0,255)'; + break; + } + ctx.beginPath(); + let r = sz * Math.sqrt(3)/2 * 0.9; + ctx.arc(x,y,r,0,Math.PI*2,true); + ctx.fill(); + ctx.stroke(); + ctx.restore(); + } +}; + +class RPSGrid extends Grid { + constructor() { + super(RPSHex, RPSState.OFF); + } + 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 RPSState.OFF; + return cells[0].state; + } + for(var i=0; i= NEIGHBOR_LIMIT) { + this.cells[i].state = RPSState.PAPER; + } + break; + case RPSState.PAPER: + var scissors = 0; + for(var j=0;j<6;j++) { + var st8 = stateAtClone(cellcp, nbrs[j].q, nbrs[j].r); + if(st8 == RPSState.SCISSORS) scissors++; + } + if(scissors >= NEIGHBOR_LIMIT) { + this.cells[i].state = RPSState.SCISSORS; + } + break; + case RPSState.SCISSORS: + var rocks = 0; + for(var j=0;j<6;j++) { + var st8 = stateAtClone(cellcp, nbrs[j].q, nbrs[j].r); + if(st8 == RPSState.ROCK) rocks++; + } + if(rocks >= NEIGHBOR_LIMIT) { + this.cells[i].state = RPSState.ROCK; + } + break; + case RPSState.OFF: + default: + break; + } + } + } +} + +g = new RPSGrid(); + + +function randomize(q,r,g) { + // puts random elements in the grid + for(let i=0;i