1/2KiB roguelike

22 views
Skip to first unread message

sfuerst

unread,
Sep 21, 2009, 12:28:45 PM9/21/09
to
Hello, back in 2006 I made the 2KiB roguelike, the first of the really
small roguelikes. I then left the roguelike world for a few years. A
few weeks ago, I was making a search for my old code, and noticed that
others have taken up the challenge, and there now are a few 1KiB
roguelikes, half the size of the original. Not to be out done, I then
endeavoured to make another halving, and create a roguelike that was
less than 512 bytes of source code.

The task took two days, just like the original 2KiB game. So I
present "Monster Caves":

Decend downwards, and kill as many monsters as you can. Beware!
Hitting an already dead monster causes it to arise as a nearly
unkillable Lich! How many monsters can you kill before your luck runs
out, or you find the mysterious moving stairwell? Note that the game
doesn't have enough bytes to remember (and print out) your kill total,
so you'll have to do that yourself. You can use the numeric keypad
(with num-lock on) to move about. Move into a monster to attack it,
and move into a stairway to decend another level. If you get stuck in
a disconnected part of a level, try pressing some other keys... you
may just be able to teleport out.

Here is the source code, all 493 bytes of it:
#include<stdlib.h>
#define F(n)for(j=0;j<n;j++)
#define r rand()
int main(){int x,s=46,n,i,j,z=77,l[z];char m[z*s],h[z];initscr();raw
();F(z*s)j[
m]=35;F(s)for(j[l]=i=(r%4+3)*z+(n=r%17*z+r%s+z);n<=i;n+=z)for(x=n;x<=n
+j/2;m[++
x]=s);F(9)l[j][m]=z,j[h]=2;m[*l]=64;*h=5;l[j][m]=62;F(z){x=n=l[i++,i
%=9];if(i)!
i[h]||*l^(n+=r%3+r%3*z+~z)||--*h?0:abort();else{F(25)mvaddnstr(j,i,m
+j*z,z);j=s
-getch();m[n+=j/3*z-j%3+153]^62||main();F(9)l[j+1]^n||--h[j+1]||n
[m]--;}n[m]^s
||(m[l[i]=n]=x[m],x[m]=s);}}

To compile the source code use gcc -O2 source.c -o monster_caves -
lcurses to link with the curses library. Note that one of the tricks
used to shrink the source is to not include the curses header file.
The functions called in the curses library are lucky enough to have
the correct signature without it.

To those interested in some of the tricks used to shrink the source
down so much, I've written an article at http://www.locklessinc.com/articles/512byte_roguelike/

Steven

Björn Ritzl

unread,
Sep 21, 2009, 3:42:37 PM9/21/09
to
sfuerst skrev:

This is so amazing it maks my head hurt! Well done!

/Bj�rn
--
Are you bored while waiting for the bus? Is your flight delayed?
Play a roguelike on you cellphone while waiting!
http://roguebasin.roguelikedevelopment.org/index.php?title=Dweller

Jakub Debski

unread,
Sep 21, 2009, 3:52:36 PM9/21/09
to
on 2009-09-21, Bj�rn Ritzl supposed :

> This is so amazing it maks my head hurt! Well done!

I still prefer the "A journey to hell":
http://cymonsgames.com/ajth/

regards,
Jakub


Jakub Debski

unread,
Sep 21, 2009, 3:54:31 PM9/21/09
to
(supersedes <mn.ad207d99f...@wp.pl>)

on 2009-09-21, Bj�rn Ritzl supposed :
> This is so amazing it maks my head hurt! Well done!

I still prefer the "A journey to hell":
http://cymonsgames.com/ajth/

The original source code is here:
http://groups.google.com/group/rec.games.roguelike.development/browse_thread/thread/2a44ec42d1f28b91

regards,
Jakub


konijn_

unread,
Sep 21, 2009, 5:21:12 PM9/21/09
to

So,

seeing this is hosted on a commercial site,
how much would one have to pay to have the last Zangband cleaned up
and released ?

T,

sfuerst

unread,
Sep 22, 2009, 12:35:32 PM9/22/09
to
On Sep 21, 12:52 pm, Jakub Debski <debski.ja...@wp.pl> wrote:
> on 2009-09-21, Björn Ritzl supposed :

>
> > This is so amazing it maks my head hurt! Well done!
>
> I still prefer the "A journey to hell":http://cymonsgames.com/ajth/
>
> regards,
> Jakub

I like it better as well. However, if you try to shrink it to half of
its size you'll find that the feature list probably needs to shrink
also. With a limit of 1kib you can create a compact virtual machine,
and then write the game itself in some sort of size-optimized byte-
code. The byte-code lets you add heaps of features for very little
size cost. That was my original plan for this... however, if you add
up the overhead for the virtual machine implementation, there wasn't
quite enough room at 512 bytes to include it and actually have a
working game.

Steven

sfuerst

unread,
Sep 22, 2009, 12:40:48 PM9/22/09
to

Fortunately for the world, Angband and its variants are under a non-
commercial license. This means that no one can ever charge money for
them. Unfortunately, that means that if you want your Zangband fix,
you'll need to look at playing with the fork until I get more hobby
time. :-(

Steven

Gelatinous Mutant Coconut

unread,
Sep 22, 2009, 3:41:55 PM9/22/09
to
On Sep 21, 12:28 pm, sfuerst <svfue...@gmail.com> wrote:
> To compile the source code use gcc -O2 source.c -o monster_caves -
> lcurses to link with the curses library.  Note that one of the tricks
> used to shrink the source is to not include the curses header file.
> The functions called in the curses library are lucky enough to have
> the correct signature without it.

Could you explain how this works? Does #include<stdlib.h> somehow
include the function prototypes for initscr(), raw(), mvaddnstr() and
getch()?

Also: you recursively call main()? Whoa, I didn't even know that was
possible. If you play the game long enough won't you end up with a
stack overflow?

Gelatinous Mutant Coconut

unread,
Sep 22, 2009, 4:40:14 PM9/22/09
to
Since you are using GCC, you can save even more by removing your
#define and #include lines and changing your compile command to:

gcc -include stdlib.h -D 'F(n)=for(j=0;j<n;j++)' -D 'r=rand()' -O2
source.c -o monster_caves -lcurses

(Obviously this could be abused to write a deceptively small program.
GCC rejected my attempt at a one-character roguelike (the one
character was of course '@', which was to be redefined with -D) for
being too short, but it will compile the complete program "int main()
{}", which is only 12 characters long.)

I guess this raises an important issue with ultra-small coding
challenges: What are you allowed to #include, and what command-line
statements are allowed during the build process?

Message has been deleted

Elig

unread,
Sep 23, 2009, 5:54:56 PM9/23/09
to
Fantastic job! I might try to do this.. I tried to write a small
roguelike by line count, but it seemed to always require over 25 lines
unless I was putting lines ontop of each other. But if you go by the
bytes of the source, that seems like a much more reasonable metric
than line number. Great job!

Andrew Doull

unread,
Sep 24, 2009, 7:24:33 AM9/24/09
to

You may have missed this during your absence but Angband has
successfully relicensed to the GPL. Some other variants are attempting
to do so as well.

Andrew

David Damerell

unread,
Sep 24, 2009, 10:37:03 AM9/24/09
to
Quoting Andrew Doull <andre...@gmail.com>:
>On Sep 23, 2:40=A0am, sfuerst <svfue...@gmail.com> wrote:

>>On Sep 21, 2:21=A0pm, konijn_ <kon...@gmail.com> wrote:
>>>seeing this is hosted on a commercial site,
>>>how much would one have to pay to have the last Zangband cleaned up
>>>and released ?
>>Fortunately for the world, Angband and its variants are under a non-
>>commercial license. =A0 This means that no one can ever charge money for
>>them.

>You may have missed this during your absence but Angband has
>successfully relicensed to the GPL.

In any case even the old Angband license would not prevent you from hiring
a contractor to work on the source.
--
David Damerell <dame...@chiark.greenend.org.uk> Distortion Field!
Yesterday was Thursday, September.
Today is Friday, September.
Tomorrow will be Saturday, September - a weekend.

konijn_

unread,
Sep 24, 2009, 1:58:43 PM9/24/09
to
On Sep 24, 10:37 am, David Damerell <damer...@chiark.greenend.org.uk>
wrote:

> Quoting  Andrew Doull  <andrewdo...@gmail.com>:
>
> >On Sep 23, 2:40=A0am, sfuerst <svfue...@gmail.com> wrote:
> >>On Sep 21, 2:21=A0pm, konijn_ <kon...@gmail.com> wrote:
> >>>seeing this is hosted on a commercial site,
> >>>how much would one have to pay to have the last Zangband cleaned up
> >>>and released ?
> >>Fortunately for the world, Angband and its variants are under a non-
> >>commercial license. =A0 This means that no one can ever charge money for
> >>them.
> >You may have missed this during your absence but Angband has
> >successfully relicensed to the GPL.
>
> In any case even the old Angband license would not prevent you from hiring
> a contractor to work on the source.

Thanks, that was what I meant. How much would a Maecenas need to pay
to get it finished ?

T.

> --
> David Damerell <damer...@chiark.greenend.org.uk> Distortion Field!

Reply all
Reply to author
Forward
0 new messages