3 ms·
I heard you like javascript so here you go: const H = 40; const W = 80; let m = new Array(H).fill().map(() => new Array(W).fill(" ")); function g(x) { retur
by ttyyzz 3y ago
I heard you like javascript so here you go:
const H = 40;
const W = 80;
let m = new Array(H).fill().map(() => new Array(W).fill(" "));
function g(x) {
return Math.floor(Math.random() * x);
}
function cave(s) {
const w = g(10) + 5;
const h = g(6) + 3;
let t = g(W - w - 2) + 1;
let u = g(H - h - 2) + 1;
for (let y = u - 1; y < u + h + 2; y++) {
for (let x = t - 1; x < t + w + 2; x++) {
if (m[y][x] === ".") return;
}
}
let d = 0;
let e, f;
if (!s) {
for (let y = u - 1; y < u + h + 2; y++) {
for (let x = t - 1; x < t + w + 2; x++) {
const s = x < t || x > t + w;
const v = y < u || y > u + h;
if (s ^ v && m[y][x] === "#") {
d++;
if (g(d) === 0) {
e = x;
f = y;
}
}
}
}
if (d === 0) return;
}
for (let y = u - 1; y < u + h + 2; y++) {
for (let x = t - 1; x < t + w + 2; x++) {
const s = x < t || x > t + w;
const v = y < u || y > u + h;
m[y][x] = s && v ? "!" : s ^ v ? "#" : ".";
}
}
if (d > 0) m[f][e] = g(2) ? "'" : "+";
for (let j = 0; j < (s ? 1 : g(6) + 1); j++) {
m[g(h) + u][g(w) + t] = s
? "@"
: g(4) === 0
? "$"
: String.fromCharCode(65 + g(62));
}
}
function main() {
for (let y = 0; y < H; y++) {
for (let x = 0; x < W; x++) {
m[y][x] = " ";
}
}
for (let j = 0; j < 1000; j++) {
cave(j === 0);
}
for (let y = 0; y < H; y++) {
let row = "";
for (let x = 0; x < W; x++) {
const c = m[y][x];
row += c === "!" ? "#" : c;
}
console.log(row);
}
}
main();
- MenhirMike 3y agoYour business cards must be huge.
- ammanley 3y agoI don't like this business card :( -- me, a JS dev far too often (but also, thank you for sharing this!)
- actionfromafar 3y agoHaha thanks for this laugh!