3 ms·
A maze generator in the shape of a maze whose corridors spell a 4-letter word: char*M,A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf(M="%d",&C); --
by tromp 9mo ago
A maze generator in the shape of a maze whose corridors spell a 4-letter word:
char*M,A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf(M="%d",&C);
-- E; J[ E] =T
[E ]= E) printf("._"); for(;(A-=Z=!Z) || (printf("\n|"
) , A = 39 ,C --
) ; Z || printf (M ))M[Z]=Z[A-(E =A[J-Z])&&!C
& A == T[ A]
|6<<27<rand()||!C&!Z?J[T[E]=T[A]]=E,J[T[A]=A-Z]=A,"_.":" |"];}
Generates a maze on the fly after entering the desired height of the maze. This compiled fine back in 1988 when I submitted it to the IOCCC (having rediscovered Eller's algorithm). Modern C compilers don't allow constant strings to be overwritten, which can be avoided by changing the first line to
char M[3],A,Z,E=40,J[40],T[40];main(C){for(*J=A=scanf("%d",&C);
The code is explained in detail at https://tromp.github.io/maze.html https://tromp.github.io/maze.html
- binaryturtle 9mo agoIf I squeeze the eyes I can read the "MAZE". :) Sadly neither version works here with an older clang on OS X. Both variants build fine with 9 warnings each. But the old variant dies with a "Bus Error: 10", and the new variant with "Segmentation fault: 11". Same with gcc (albeit only 8 warnings.) /edit OK, just wrong user input. You gotta feed it a number, and not a "foobar" or another random string.
- MaskRay 9mo agoAmazing! I read you article in 2012 when the link was https://homepages.cwi.nl/~tromp/maze.html https://homepages.cwi.nl/~tromp/maze.html I was learning Haskell and Ocaml and wrote my own article in Chinese then https://maskray.me/blog/2012-11-02-perfect-maze-generation https://maskray.me/blog/2012-11-02-perfect-maze-generation Now I should fix the link.
- munificent 9mo agoNeat! Related: Here's a C program that draws random dungeons sort of like you use in a roguelike dungeon crawler: #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;}