5 ms·
A random dungeon generator that fits on a business card
- msie 8y agoI like the old-school notion that I can just copy and paste it into vi and then compile with gcc. It just works. No complicated installation of a runtime or dependencies.
- jdmoreira 8y agoFor me this is the appeal of C and I feel it as well. Any unix will have a C compiler supporting at least C89
- yorwba 8y agoExcept 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...#..............# #.......# #......# #.......#..............# ######### ######## #########..............# ################
- femto113 8y agoIf like me you're interested in looking at how the code actually works I put a deobfuscated fork here https://gist.github.com/femto113/28f69626acddc70a002ecead0b311da4 https://gist.github.com/femto113/28f69626acddc70a002ecead0b3... Just expands the macros/typedefs and applies standard indenting, code is otherwise same as the original.
- munificent 8y agoThere are a few tweaks I made along the way while tightening it up, but here's the readable original I started from: https://gist.github.com/munificent/ce8f7a9e6b09938ca8d2d43fa62f9864 https://gist.github.com/munificent/ce8f7a9e6b09938ca8d2d43fa...
- PhasmaFelis 8y agoIs there an executable available somewhere? This is kind of a pet peeve of mine, but if you post simple, standalone code you really ought to post the compiled executable(s) as well. I don't currently have a C compiler installed on my home Windows box.
- davidjhall 8y agoYou can go to online GDB and run it : https://www.onlinegdb.com/ https://www.onlinegdb.com/ But you have to use the modified code in the comments above or you will get a : main.c:8:7: error: variably modified ‘m’ at file scope =80;i m[H][W];i g(i x) {r rand()%x;}
- falsedan 8y agoI hear Visual Studio Code is all right at doing compile stuff with C++ But more seriously, this is a toy implementation and the value is in reading it to see how it works, not in seeing its output
- yorwba 8y agoWhy use an executable when you can run it on someone else's computer™: https://repl.it/repls/NiceEvilBoard https://repl.it/repls/NiceEvilBoard I swear the URL wasn't chosen by me.
- PhasmaFelis 8y agoThanks for an actually constructive answer! That's pretty neat. I hadn't heard of Repl.it before.
- Insanity 8y agoBut then you need to trust that the executable is the same as the source!
- lixtra 8y agoHere, you can just disassemble the binary and it will be more readable than the code.
- harperlee 8y agoThis is not mine, but from the “clojure grams” twitter account: (defn maze [n] (->> (repeatedly (* n n) #(rand-nth "╱╲")) (partition n) (transduce (comp (map #(apply str %)) (interpose "\n")) str))) Simpler output but related and also quite interesting!
- Aardappel 8y agoLooks like a port of this Basic one-liner: http://www.slate.com/articles/technology/books/2012/11/computer_programming_10_print_chr_205_5_rnd_1_goto_10_from_mit_press_reviewed.html http://www.slate.com/articles/technology/books/2012/11/compu...
- coroxout 8y agoThis is really cool, nice work (and sorry about your dog). It reminds me that once upon a time the roguelike dev newsgroup had a "1k (source code) roguelike challenge". The thread is here [1] and submissions are here in a slightly annoying format [2] but sadly I can't find the entry I remember, which I thought was in C and actually quite playable, if slightly at the mercy of the random number generator to make each new level solvable. Perhaps it was for a later iteration of the same challenge. [1] https://rec.games.roguelike.development.narkive.com/3tm7xGpn/first-1kb-rl-challenge#post3 https://rec.games.roguelike.development.narkive.com/3tm7xGpn... [2] https://sites.google.com/site/1024brl/ https://sites.google.com/site/1024brl/