Skip to content

Instantly share code, notes, and snippets.

@femto113
Forked from munificent/generate.c
Last active March 3, 2019 01:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femto113/28f69626acddc70a002ecead0b311da4 to your computer and use it in GitHub Desktop.
Save femto113/28f69626acddc70a002ecead0b311da4 to your computer and use it in GitHub Desktop.
A random dungeon generator that (no longer) fits on a business card
#include <time.h> // Robert Nystrom
#include <stdio.h> // @munificentbob
#include <stdlib.h> // for Ginny
// 2008-2019
const int H = 40;
const int W = 80;
int m[H][W];
int g(int x)
{
return rand() % x;
}
void cave(int s)
{
int w = g(10) + 5;
int h = g(6) + 3;
int t = g(W - w - 2) + 1;
int u = g(H - h - 2) + 1;
for (int y = u - 1; y < u + h + 2; y++)
for (int x = t - 1; x < t + w + 2; x++)
if (m[y][x] == '.')
return;
int d = 0;
int e, f;
if (!s) {
for (int y = u - 1; y < u + h + 2; y++)
for (int x = t - 1; x < t + w + 2; x++) {
int s = x < t || x > t + w;
int t = y < u || y > u + h;
if (s ^ t && m[y] [x] == '#') {
d++;
if (g(d) == 0)
e = x, f = y;
}
}
if (d == 0) return;
}
for (int y = u - 1; y < u + h + 2; y++)
for (int x = t - 1; x < t + w + 2; x++) {
int s = x < t || x > t + w;
int t = y < u || y > u + h;
m[y][x] = s && t ? '!' : s ^ t ? '#' : '.' ;
}
if (d > 0)
m[f][e] = g(2) ? '\'' : '+';
for (int j = 0; j < (s ? 1 : g(6) + 1); j++)
m[g(h) + u][g(w) + t] = s ? '@' : g(4) == 0 ? '$' : 65 + g(62);
}
int main(int argc, const char *argv[]) {
srand((int) time(NULL));
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
m[y][x] = ' ';
for (int j = 0; j < 1000; j++)
cave(j == 0);
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
int c = m[y][x];
putchar(c == '!' ? '#' : c);
if (x == W - 1)
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment