Public
Edited
Nov 25, 2022
Insert cell
Insert cell
feature_counts = {
const SAND = 0;
const OASIS = 1;
const SWAMP = 2;
const ROCK = 3;
let count = function(item, board) {
return board
.map(row => row.filter(n => n===item).reduce((acc, n) => acc += 1, 0))
.reduce((a,b) => a += b, 0);
}
let one_of = function(items) { return items[Math.floor(Math.random() * items.length)]; }
// Add n features to the board,
// unless the cell is already taken.
let populate = function(board) {
let n = 32;
game.board = board;
game.assign(OASIS, game.d6(), game.d4());
for (let x = 0; x < n; x++) {
let feature = one_of([SWAMP, SWAMP, ROCK, SWAMP]);
game.assign(feature, game.d6(), game.d4());
}
return board;
}
let samples = [];
let N = 100;
for (let x = 0; x < N; x++) {
let board = populate(empty_board());
//samples.push({"sand":count(SAND, board), "swamp":count(SWAMP, board), "rock":count(ROCK, board)});
// get tidy!
samples.push({"category":"sand","count":count(SAND, board)});
samples.push({"category":"swamp","count":count(SWAMP, board)});
samples.push({"category":"rock","count":count(ROCK, board)});
}
return samples;
}
Insert cell
vl.markPoint().data(feature_counts)
.encode(
vl.x().fieldQ('count'),
vl.y().fieldN('category'),
vl.color().fieldN('category'),
vl.opacity().value(0.2)
).render();

Insert cell
import {game, empty_board} from "bba242c59fb03877"
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