-
-
Save munificent/b1bcd969063da3e6c298be070a22b604 to your computer and use it in GitHub Desktop.
#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;} |
Ported to Python
Took a stab at writing a version in J.
NB. 111...666
NB. 0123456789012...789
NB. !#.'+@$ABCDE...|}~
'H W' =: 40 80
choose =: ] {~ [: ? [ # #@]
chooseidx =: $@] #: [ choose I.@,@]
boundary =: 4 : 'x {. (0 #~ ? x - y + 2) , 1 , (y # 2) , 1'
room =: (3 : 0)"0
room =. 3 <. (H boundary 3 + ? 6) */ (W boundary 5 + ? 10)
obj =. (y ~: 0) {:: 6 ; 7 + (+ [: ? 1 62 {~ ]) 0 ~: (>: ? 6) ?@# 4
obj (<"1 (# obj) chooseidx room = 3) } room
)
hasone =: 1 (e. ,) ]
merge =: 4 : 0
walls =. x *.&(2&=) y
if. (-. hasone walls) +. hasone (x ~: 0) *. y = 3 do. y
else. (4 + ? 2) (< , 1 chooseidx walls) } x + y * x = 0 end.
)
(' ##.''+@$' , a. {~ 65 + i. 62) {~ merge/ room |. i. 1000
Here's a Python version I was able to put together:
https://github.com/obxfisherman/connectedRooms
Very cool concept, Thanks for sharing!
@PluieElectrique wow, that's.. impressive. I have a love-desperation relationship with point free code ;)
Cool work, @obxfisherman! I send you a pull request with the correct formatting for the readme. ;)
Obligatory (badly-)golfed version:
#include <time.h> // Robert Nystrom
#define l for(y=u-1; y< u+h+2; y++)\
for (x = t - 1; x < t + w + 2; x++)
a,b,d,e,f,h,j,k,n,u,t,w,x,y;m[40][80
];g(x){return rand()%x;};cave(s){w=g
(10)+5;h=g(6)+3;t=g(80-w-2)+1;u=g(40
-h-2)+1;l if(m[y][x]==14)return;d=0;
if(!s){l{k=x<t||x>t+w;n=y<u||y>u+h ;
if(k^n&&m[y][x]==3){d++;if(!g(d))e=x
,f=y;}}if(!d)return;}l{a=x<t||x>t+w;
b=y<u||y>u+h;m[y][x]="/$ "[a+b]-33;}
if(d)m[f][e]=g(2)?7:11;for(j=0;j++<(
s?1:g(6)+1);)m[g(h)+u][g(w)+t]=s?32:
g(4)?33+g(62):4;}main(){srand(time(j
=0));for(;j<999;j++)cave(!j);for(y=0
;y<40;puts(""),y++)for(x=0;x<80;x++)
putchar(32 + (a = m[y][x])+4*!~a) ;}
Putting the @ back in left as an exercise for the reader because I'm too lazy.
Note that this relies heavily on UB as you can see from the screenful of warnings.
I found a bug where rooms are placed corner-to-corner and the door is not traversable:
..#
..'##
###..
Quick and dirty workaround (added - 1):
let s = x < left || x > left + width - 1;
let t = y < top || y > top + height - 1;
On @grownseed .js version.
CoffeeScript https://codepen.io/samme/pen/gOppRwV
Haha, I also de-obfuscated this code before coming here and seeing that other people did it too!
I've had a lot of fun in understanding how this piece of code works :)
Here's my version: https://gist.github.com/paskozdilar/48d7532733ccd11144bb43fed953c334
conversion to calculang formulas; visualizations with animated evolution: https://declann.observablehq.cloud/calculang-dx-pattern-exampling/dungeon/dungeon
truly beautiful, thank you for sharing this work of art
Clojure!
Tragically was doing this between meetings and from an early deobfuscation that I think may have had a quirk in it. Doors are chosen randomly from candidates, not moved with decreasing probability. Almost certainly other quirks 😄 .