Public
Edited
Nov 26, 2022
Importers
1 star
Insert cell
Insert cell
Insert cell
gameboard = {
const SAND = 0;
const OASIS = 1;
const SWAMP = 2;
const ROCK = 3;
let one_of = function(items) { return items[Math.floor(Math.random()*items.length)]; };
let g = Object.create(game);
g.board = empty_board();
g.assign(OASIS,g.d6(),g.d4());
for (let n = 0; n < 32; n++) {
g.add(one_of([SWAMP,SWAMP,ROCK,SWAMP]));
}
return g;
}
Insert cell
cell_color = ["#D6CAA7","#3333FF","#338822","#999999","#FF8800","#FF0000"]
Insert cell
game = {
let p = {
add: function(value) {
let direction = this.d6();
let ring = this.d4();
this.assign(value, direction, ring);
},
assign: function(value, direction, ring) {
this.log += ` @(${direction},${ring}):`
let q = this.fromPolar(direction, ring);
if (this.board[q[0]][q[1]]==0) {
this.board[q[0]][q[1]]=value;
this.log += `:add`;
} else {
this.log += `:nope`;
}
},
d4: function() { return Math.floor(Math.random() * 4); },
d6: function() { return Math.floor(Math.random() * 6); },
log: "log:",
move: function(point, direction) {
let odd = (n) => n % 2;
let dx = 0;
let dy = 0;
switch (direction % 6) {
//up 0 -1
case 0: dx=0;dy=-1; break;
//right up +1 -1|0
case 1: dx=1;dy = odd(point[0]) ? -1 : 0; break;
// right down +1 0|+1
case 2: dx=1;dy = odd(point[0]) ? 0 : +1; break;
// down 0 +1
case 3: dx = 0; dy = 1; break;
// left down -1 0|+1
case 4: dx = -1; dy = odd(point[0]) ? 0 : +1; break;
// left up -1 -1|0
case 5: dx = -1; dy = odd(point[0]) ? -1 : 0; break;
}
return [point[0]+dx, point[1]+dy];
},
fromPolar: function(direction, ring) {
// start at the center
let here = [4,4];
for (let r = ring; r >= 0; r--) {
here = this.move(here, direction);
this.log += `<<${here[0]},${here[1]}>>`
}
return here;
}
}

return p;
}
Insert cell
empty_board = function(){
return [[128,128,0,0,0,0,0,128,128],
[128,128,0,0,0,0,0,0,128],
[128,0,0,0,0,0,0,0,128],
[128,0,0,0,0,0,0,0,0],
[0,0,0,0,4,0,0,0,0],
[128,0,0,0,0,0,0,0,0],
[128,0,0,0,0,0,0,0,128],
[128,128,0,0,0,0,0,0,128],
[128,128,0,0,0,0,0,128,128]]
}
Insert cell
manual_board = [[128,128,2,0,0,0,0,128,128],
[128,128,2,1,0,0,0,0,128],
[128,0,2,0,0,2,0,3,128],
[128,0,0,0,0,2,0,0,0],
[0,0,2,0,4,0,0,0,0],
[128,0,0,0,2,0,0,0,0],
[128,0,0,2,0,0,0,0,128],
[128,128,0,0,0,0,0,0,128],
[128,128,0,0,0,0,0,128,128]]
Insert cell
gameboard.log
Insert cell
[game.d6(),game.d4()]
Insert cell
import {p5, ParticleSystem} from "@tmcw/p5"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more