
2048 implemented in 487 bytes of C  - adamnemecek
https://gist.github.com/justecorruptio/9967738
======
nullc
Like the small JS version this also has the bug where you can summon more
tiles by pushing again in a direction where movement isn't allowed.

~~~
Almaviva
He shouldn't call his game "2048" if it has that bug. It's a much easier game
than the original one, as you are never forced to move a big number out of a
corner and you never fear the dreaded position where you have exactly one open
row and you are forced to move all your big numbers away from the edge.

------
deletes
Doesn't run correctly under windows. The code relies on the system call "stty
cbreak", which is linux specific.

 _' stty' is not recognized as an internal or external command, operable
program or batch file._

I would love to read comments instead of downvotes. Where am I wrong?

~~~
ghewgill
`stty` is not Linux specific, it's common to all Unix-derived systems
(including Mac OS X, for example). Windows is not Unix-derived.

~~~
deletes
HN got really trigger happy in the last few weeks.

That part of the comment only is partially correct, and not was even the main
point of my comment; what I really meant was that it doesn't belong in
windows, and the main point was that the code is not portable.

~~~
pseut
A reaction to the new moderation proposals, maybe?

------
mden
Huh, was not aware of the "Implicit Int Rule" \-
[http://stackoverflow.com/questions/11064292/declaring-
variab...](http://stackoverflow.com/questions/11064292/declaring-variables-
without-any-data-type-in-c).

------
ika
beautified one [http://pastebin.com/1Vj0jUrR](http://pastebin.com/1Vj0jUrR)

~~~
geocar
How is that more beautiful?

~~~
dsego
uglified = minifed

------
brandonbloom
While 487 bytes is impressive, it feels like you should round up to the
nearest power of two...

------
QuantumChaos
That's nothing. Let me know when someone manage to implement 2048 in 2048
lines of Java

------
userbinator
I haven't tried it, but a game of this complexity looks like it could be
implemented in roughly the same number of bytes of binary, meaning it fits in
a bootsector...

Coincidentally, a sector on optical media is typically 2048 bytes.

~~~
akira2501
ISO 9660 is 2352 bytes, actually. The logical size is 2048 bytes, but the
other bytes are needed for error correction and other headers.

------
jevinskie

        puts(W&1?"WIN":"LOSE");
    

Shouldn't exit(W&1); be "good enough" while saving some bytes?

------
ghewgill
Unfortunately it's the same every time it is played. It needs some srand()
love in there somewhere.

------
corruptio
Author here. Per many requests, here is an annotated version
[https://gist.github.com/justecorruptio/10248923](https://gist.github.com/justecorruptio/10248923)

------
3327
well, bugs or not, 487 bytes is a new high bar... I guess the lisp lads might
need to curl up their sleeves.

~~~
bjterry
On any modern filesystem, the source will take up substantially more than 487
bytes anyway. Perhaps you should round up to the nearest block size.

------
pkrumins
I'd understand 2048 implemented in 2048 bytes of C, but this I can't
understand.

------
mariuscoto
Can I build this using gcc ?

~~~
ghewgill
Sure:

    
    
        gcc -o 2048 2048.c
    

Ignore all the warnings.

~~~
mariuscoto
That was the first command I have tried, but I get many of those: error: use
of undeclared identifier 'X'. I am running OSX.

~~~
sampo
If you haven't installed gcc on your OSX, the command gcc actually calls
clang.

[http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-
li...](http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-
clang)

------
randv
nothing is returned from function s?

~~~
mschuster91
the only usage of the "s" function is down in T(i), and the "i" parameter
passed to T() is ignored anyway ( _" for (i = X + rand() % X;"_). It's just
needed to save a int i; in the function itself.

------
ohmygeek
This is called sheer brilliance!

------
sawmebabe
wow. im more than impressed 0.0 wonderful

------
BorisMelnik
more readable:

[http://pastebin.com/TD9d1Kym](http://pastebin.com/TD9d1Kym)

~~~
myliverhatesme
No

------
plg
I never understood why this sort of thing is interesting. Let's say you can
program 2048 in 487 bytes of C ... and then someone else comes along and they
can program it in 387 bytes of C. So what? Is there a use case in the year
2014 where 100 bytes matter?

How many bytes would it take to code it in C in a human readable fashion? Such
that some other programmer could look at it, understand it, modify it, fix
bugs, etc?

Is the savings in bytes going from the latter case to the former case really
an issue in 2014, and does it outweigh the downside (which I think is
enormous) of generating obfuscated code?

Having said all that, as an exercise purely in "how small can by code be,
regardless of how obfuscated I make it", I suppose it is ... interesting?

but even still I have to admit I don't get the appeal

~~~
umanwizard
Dude, it's just for fun. It's done in the same spirit as making speed runs for
video games. Does everything have to be completely utilitarian?

~~~
userbinator
It's also very good mental exercise, since it forces you to think even more
about the corner cases of the language and its syntax. Even if you won't be
using all these corner cases in production code (hopefully), it will make you
a better programmer.

I find that reading minified/obfuscated code _really_ helps with spotting
syntax errors and related subtle bugs in regularly formatted code. Instead of
relying on cues like indentation, you begin to parse more like a compiler, and
things like missing semicolons stand out.

