3 ms·
Except a recent gcc will refuse to compile that code because of a technicality. Here's some modified code that runs on my machine if pasted directly into bash:
by yorwba 8y ago
Except a recent gcc will refuse to compile that code because of a technicality.
Here's some modified code that runs on my machine if pasted directly into bash:
gcc -x c - <<EOF
#include <time.h> // Robert Nystrom
#include <stdio.h> // @munificentbob
#include <stdlib.h> // for Ginny
#define r return // 2008-2019
#define l(a, b, c, d) for (i y=a;y\
<b; y++) for (int x = c; x < d; x++)
typedef int i;const i H=40;const i W
=80;i m[40][80];i g(i x) {r rand()%x;}
void cave(i s){i w=g(10)+5;i h=g(6)+
3;i t=g(W-w-2)+1;i u=g(H-h-2)+1;l(u-
1,u+h+2,t-1, t+w+2)if(m[y
][x]=='.') r;i d=0;
i e,f; if(!s){l(u -1,u+h
+2,t-1 ,t+w+2){i s=x<t|| x>t+
w; i t=y<u|| y> u+h;
if(s ^t&& m[ y]
[x ]=='#' ){d++; if(g (d
) ==0) e=x,f=y; }}if (d
== 0)r; }l(u-1,u +h+2 ,t
-1 ,t+w +2){i s= x< t ||
x> t+w; i t= y<u ||y> u+
h; m[y] [x]= s &&t? '!'
:s^t ?'#' :'.'
;}if (d>0)m [f][
e]=g(2 )?'\'':'+';for(i j=0;j<(s?
1:g(6) +1);j++)m[g(h)+u][g(w)
+t]=s?'@' :g(4) ==0?
'$':65+g(62) ;}i main(i
argc, const char* argv[]) {srand((i)
time(NULL));l(0, H, 0,W)m[y][x]=' ';
for(i j=0;j<1000;j++)cave(j==0);l(0,
H,0,W) {i c=m[y][x]; putchar(c=='!'?
'#':c);if(x==W-1)printf("\n");}r 0;}
EOF
./a.out
- philshem 8y ago############### #.......^...$.# ################### ############## #.....Z.......# #..$...#..........# #............# #.....e.......# #..a...#........$.# #...$....$...# ############.............# #......#.}........# ###########....y.......# #.........#######+######## #......#..........# #........##............# #.........##....~.# #......#..........# #........########'###### #....d....##......# #......+..........# #H....^$.#.....$...# #.........##......# ###########'####### #........+c......O.# #.........##......# #...........# ##########.........# #...$.....##.z$...# #.....x.$...# #.........# #.........##......######## #...........# #.....Q...# #.........##Q.....+......# #...........# ########### #.........# #.........##......#......# ######'######### #.........# #.........# ###########'###+######......# #........`.# #.....H...# #.........# #...............# #.C....# #..R.......# #.........####+############~........P.....# #..d...# #..........# #..{......#...............#...............# #......# #..$.......# #.........#...............#....t..........# #......# #..........# #.........#...............#...............# #...`V.# #######+###### #.........+...............#...............# #......# #....$...# #.........#...............+....v..........# ######## #fT......# ###########....@..........#...............# #........# #...............#########'#####'############ #...H....# #...............# #......#..............# #........# ########+#######'####### #..d.b.#..............# #........# #...X...s.# #....$.# #.J....#..............# #######'#'##############..|....E.# #...]a.# #......#..............# #.......#..............+$........# #......# ########..............# #.......#..............#.........# #......# #..............# #.......#......~.......#########'# #......# #$.............# #.......#..v...........# #...\...# #......# #..............# #n......#........}.....# #.......# #......# #..............# #....$..#......`.......# #....u$.# #......# ################ #...r...#..............# #.......# #......# #.......#..............# ######### ######## #########..............# ################
- munificent 8y agoMy favorite part is that you get a totally different dungeon each time you run it. It really is a working procedural generator, and not something more like a demo that only produces a single carefully selected result.
- munificent 8y agoI fixed the technicality in the latest version of the Gist. Interestingly, Clang doesn't seem to have any problems with the original code.