23 comments

[ 3.2 ms ] story [ 58.5 ms ] thread
I 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.
For me this is the appeal of C and I feel it as well. Any unix will have a C compiler supporting at least C89
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

                                                               ###############  
                                                               #.......^...$.#  
  ###################                ##############            #.....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...#..............# #.......#   #......#                                
   #.......#..............# #########   ########                                
   #########..............#                                                     
           ################
My 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.
I fixed the technicality in the latest version of the Gist. Interestingly, Clang doesn't seem to have any problems with the original code.
If like me you're interested in looking at how the code actually works I put a deobfuscated fork here

https://gist.github.com/femto113/28f69626acddc70a002ecead0b3...

Just expands the macros/typedefs and applies standard indenting, code is otherwise same as the original.

Is 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.

I 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

You can go to online GDB and run it : 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;}

Why use an executable when you can run it on someone else's computer™: https://repl.it/repls/NiceEvilBoard

I swear the URL wasn't chosen by me.

Thanks for an actually constructive answer! That's pretty neat. I hadn't heard of Repl.it before.
But then you need to trust that the executable is the same as the source!
Here, you can just disassemble the binary and it will be more readable than the code.
If you don't have a compiler, you probably don't have a disassembler...
If he'd posted an executable, unless it was for exactly your platform, wouldn't you just have complained you didn't currently have a machine running the correct operating system on the correct hardware?
Not at all. Tiny textmode programs do not generally require specific hardware or OS versions. Windows/Mac/Linux should cover the large majority of users. Or you could just do Windows, since Unix machines (including Macs) most likely have gcc installed already. Or you could skip all that and include a Repl.it link, like Yorwba thoughtfully provided in the sibling comment.

If it did require specific hardware or software, that would be different, which is why I specifically said "simple, standalone code."

Or you could "just" install a C compiler.
Hang on, I'll get you an executable. Be sure to run it, okay?
This 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... [2] https://sites.google.com/site/1024brl/