Skip to content

Instantly share code, notes, and snippets.

@DanielJenkyn
Last active November 12, 2019 22:43
Show Gist options
  • Save DanielJenkyn/e60435f781cb51aa54c84cba9b23300b to your computer and use it in GitHub Desktop.
Save DanielJenkyn/e60435f781cb51aa54c84cba9b23300b to your computer and use it in GitHub Desktop.
Cave Generation on a Business Card

Cave Generation on a Business Card

  • Uses cellular automata to generate small caves
  • Compiles on macOS Mojave using clang with no errors

Varibles you can tweak

  • 'chce2StAlv' - Sets how dense the initial grid is with living cells.
  • 'stps' - Number of times we run simulation
  • 'const c' - Size of map
  • 'dthLmt' - If living cells are surrounded by less than dthLmt cells they die
  • 'brthLmt' - if dead cells are near at least brthLmt cells they become alive
#include<stdlib.h> // @DanielJenkyn
typedef int i;i const c=50,r=c/2;
#define l(a,b) for(i a=0;a<b;a++)
#define v void
float const chce2StAlv=.4f;pid_t ge\
tpid(v);i printf(const char *format,
...);i const dthLmt=2,brthLmt=4,stps=
2;v prtMp(i[r][c]);v initMp(i[r][c]);
v stp(i[r][c],i[r][c]);i cntLveNbs(i
[r][c],i x,i y);v cpyMp(i[r][c],i[r][
c]);i main(){srand(getpid());i mp[r][
c],nMp[r][c];initMp(mp);l(a,stps){stp
(mp,nMp);cpyMp(mp,nMp);}prtMp(nMp);}v
prtMp(i mp[r][c]){l(x,r){l(y,c){mp[x]
[y]==1?printf("#"):printf(".");}prin\
tf("\n");}}v initMp(i mp[r][c]){l(x,r
){l(y,c){((((float)rand())/RAND_MAX)<
chce2StAlv)?(mp[x][y]=1):(mp[x][y]=0)
;}}}v stp(i mp[r][c],i nMp[r][c]){l(x
,r){l(y,c){i nbrs=cntLveNbs(mp,x,y);
if(mp[x][y]){nbrs<dthLmt?nMp[x][y]=0:
(nMp[x][y]=1);}else{nbrs>brthLmt?nMp[
x][y]=1:(nMp[x][y]=0);}}}}i cntLveNbs
(i mp[r][c],i a,i b){i cnt=0;for(i x=
-1;x<2;x++){for(i y=-1;y<2;y++){i nb\
rX=a+x,nbrY=b+y;if(x==0&&y==0){}else
if(mp[nbrX][nbrY]!=1&&mp[nbrX][nbrY]
!=0){cnt++;}else if(mp[nbrX][nbrY]==1
){cnt++;}}}return cnt;}v cpyMp(i oMp[
r][c],i nMp[r][c]){l(x,r){l(y,c){oMp[
x][y]=nMp[x][y];}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment