A random dungeon generator that fits on a business card (2019)
gist.github.com
gist.github.com
Here's a bash version in 50 characters (https://codegolf.stackexchange.com/a/26011/7934):
w=(╱ ╲);while :;do echo -n ${w[RANDOM%2]};done
and how it looks like: 10 PRINT CHR$(205.5+RND(1)); : GOTO 10I can't recommend it without reservation, but if you're curious what a whole book about one line of code is like, you can find out.
In fact, as the size of the maze goes to infinity, the probability of solvability goes to zero. Source: https://cstheory.stackexchange.com/a/32381/20581
Here is my orthogonal version:
`w=(├┤┴┬─\|);while :;do echo -n ${w:$RANDOM%6:1};done`
It is based on the 1-liner discussed at <https://www.youtube.com/watch?v=oPtjcSsS2MI>, and uses the PETSCII characters within Unicode.
Screenshot: <https://imgur.com/a/YrvpAqZ> (from `cool-retro-term`'s Apple II profile resized to 80x25, to sort of replicate a Commodore PET's look)
The code was too large for a comment, so here's the gist:
https://gist.github.com/paskozdilar/48d7532733ccd11144bb43fe...
The approach is quite interesting - it (ab)uses the preprocessor to shorten the syntax of for loops:
#define l(a, b, c, d) for (int y = a; y < b; y++) for (int x = c; x < d; x++)
And then it does the heavy lifting by iterating over the whole map thousand times, randomly selecting a rectangle on the map and checking if the selected area is adjacent to an already-generated cave. It's a probabilistic algorithm, so there's a tiny chance that it will just print a single cave in the middle and nothing else.EDIT: Then I found that many other people also de-obfuscated it in the gist comments. :') This guy did a much better job than me:
https://gist.github.com/Joker-vD/cc5372a349559b9d1a3b220d5ea...
Yeah, it's surprising how well this approach works. Performance is actually decent.
There is a chance of "dud" levels that only have a couple of rooms, but it's rare. The nice thing about this approach is that it guarantees by construction that every room in the dungeon will be reachable from every other one.
Related: https://news.ycombinator.com/item?id=6425714
Of course there's the demoscene productions where binary size is the measure (no cheating by using lots of libraries); and in a 1K binary, comparable in size to this source, they've gone much further with what can be generated.
https://news.ycombinator.com/item?id=19290215 - original post referenced above - 23 comments
A random dungeon generator that fits on a business card - https://news.ycombinator.com/item?id=19290215 - March 2019 (64 comments)
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();
(but also, thank you for sharing this!)
Whenever I'd run an adventure, my players would often try me out, and open a door of some sorts. But the 2D maps feel very artificial. Guillaume Tavernier on Patreon makes maps with plateaus and multiple stories: https://www.patreon.com/dearchitecturart
A generator like that, would be awesome.
https://gist.github.com/Joker-vD/cc5372a349559b9d1a3b220d5ea...
Pretty interesting reading this code. Also nice to see things written in C around here.
I really liked this output from a very logical process (although I'll admit, I needed to use one of the commented versions linked in the discussion!)
Here it is: https://declann.observablehq.cloud/calculang-dx-pattern-exam...
The output reminds me of nethack.
https://bunsen.itch.io/the-snake-temple-by-rax
([1] - The 10-line BASIC competition is happening again this year, details here: https://gkanold.wixsite.com/homeputerium/copy-of-rules)