I've always been a fan of generating mazes by randomly printing /, \, or a space. It ends up slanted, and there's obviously no guarantees of solvability, but it's surprisingly good for how simple it is.
It's not much fun to solve because there's no guarantee that a path exists between any given openings on the edge, so it's mostly just "pick a point on the edge at random and see how far you can get". However, it does look very pretty.
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:
> 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.
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.
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.
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);
}
I'd love a random dungeon generator that includes multiple plateaus inside one room. For example, a dungeon with a waterfall or a tree.
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
Here's a random dungeon generator GAME (get potion, avoid snake, explore dungeon) that fits in 10 lines of BASIC [1] and would probably easily fit on a business card, too:
In what way is what ChatGPT outputs when asked about this code relevant? There are more than 20 LLMs, do we expect to see a comment from each LLM about code submissions?
I'm not proficient at reading minimized code. Maybe you're better at it than I am. The ChatGPT output helps kickstart a meaningful discussion about the content of the code.
I sort of understand the knee-jerk reactions to this. Hacker News is for humans, not robots, and there was an annoying trend of commenters posting LLM-generated summaries of articles, which deserve to get flagged off the page until they stop.
But I find it noteworthy, and in fact impressive, that ChatGPT can de-obfuscate and discern the meaning of this code. Letting the rest of us know that is a worthy contribution to the discussion.
I would suggest leading with a clear statement of why you're posting chatbot output, though. Folks around here have an allergy to that sort of thing for a reason.
ChatGPT is not de-obfuscating this code. For one thing:
> basic cellular automaton algorithm
is completely wrong. A cellular automaton is a standard approach to level-generation in a roguelike, but this code implements a different approach, the rooms-and-corridors one.
Another issue here is that ChatGPT is using a few key context clues to make its (incorrect but plausible) guess. Robert Nystrom, aka munificient, is well-known in the roguelikedev community for his game programming tutorials. I confirmed that ChatGPT knows who he is by asking "Who is @munificentbob and what is he known for?" The name of the function, "cave", that carves rooms makes it clear that this is some kind of level-generation algorithm - in fact, that's probably where the incorrect guess about cellular automata came from, because those algorithms are often used for "caves" (versus "dungeons" made with rooms-and-corridors). The characters printed are also roguelike standards.
If I remove @munificient's name and rename "cave" to "X", ChatGPT's output is worse. It says: "This code is a compact implementation of a simple game where characters move around a grid.", which is...vaguely in the ballpark.
Here's a bash version in 50 characters (https://codegolf.stackexchange.com/a/26011/7934):
and how it looks like:https://i.stack.imgur.com/fRX05.png