Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tiny 2048 in C!
M[16],X=16,W,k;main(){T(system("stty cbreak")
);puts(W&1?"WIN":"LOSE");}K[]={2,3,1};s(f,d,i
,j,l,P){for(i=4;i--;)for(j=k=l=0;k<4;)j<4?P=M
[w(d,i,j++)],W|=P>>11,l*P&&(f?M[w(d,i,k)]=l<<
(l==P):0,k++),l=l?P?l-P?P:0:l:P:(f?M[w(d,i,k)
]=l:0,++k,W|=2*!l,l=0);}w(d,i,j){return d?w(d
-1,j,3-i):4*i+j;}T(i){for(i=X+rand()%X;M[i%X]
*i;i--);i?M[i%X]=2<<rand()%2:0;for(W=i=0;i<4;
)s(0,i++);for(i=X,puts("\e[2J\e[H");i--;i%4||
puts(""))printf(M[i]?"%4d|":" |",M[i]);W-2
||read(0,&k,3)|T(s (1,K[(k>>X)%4]));}//[2048]
@derekchiang

This comment has been minimized.

Show comment
Hide comment
@derekchiang

derekchiang Apr 5, 2014

This is awesome! Could you also post the unminified code?

derekchiang commented Apr 5, 2014

This is awesome! Could you also post the unminified code?

@gvt

This comment has been minimized.

Show comment
Hide comment
@gvt

gvt Apr 5, 2014

LOL. I am impressed. You could get a job at Twitter with these algorithm skills. (I'm assuming it works haha)

gvt commented Apr 5, 2014

LOL. I am impressed. You could get a job at Twitter with these algorithm skills. (I'm assuming it works haha)

@bronson

This comment has been minimized.

Show comment
Hide comment
@bronson

bronson Apr 5, 2014

Very nice! It works great. If someone takes the time to reformat the code, post it in another gist? It would be nice to see what makes it tick.

bronson commented Apr 5, 2014

Very nice! It works great. If someone takes the time to reformat the code, post it in another gist? It would be nice to see what makes it tick.

@m1dnight

This comment has been minimized.

Show comment
Hide comment
@m1dnight

m1dnight Apr 5, 2014

Just when I kicked my addiction..

m1dnight commented Apr 5, 2014

Just when I kicked my addiction..

@anirudh-chhangani

This comment has been minimized.

Show comment
Hide comment
@anirudh-chhangani

anirudh-chhangani Apr 5, 2014

https://gist.github.com/anirudh-chhangani/9989292
Reformated...please help to reformat it further and add comments in the code so that it becomes self explanatory.

anirudh-chhangani commented Apr 5, 2014

https://gist.github.com/anirudh-chhangani/9989292
Reformated...please help to reformat it further and add comments in the code so that it becomes self explanatory.

@jcubic

This comment has been minimized.

Show comment
Hide comment
@jcubic

jcubic Apr 5, 2014

gcc give warnings but it compile and work. Nice.

jcubic commented Apr 5, 2014

gcc give warnings but it compile and work. Nice.

@Peter-Barrett

This comment has been minimized.

Show comment
Hide comment
@Peter-Barrett

Peter-Barrett Apr 5, 2014

This is very impressive! So is your tiny Conways game of life, brilliant!

Peter-Barrett commented Apr 5, 2014

This is very impressive! So is your tiny Conways game of life, brilliant!

@michaelmcmillan

This comment has been minimized.

Show comment
Hide comment
@michaelmcmillan

michaelmcmillan Apr 5, 2014

Impressive, but it lets me execute invalid moves.

michaelmcmillan commented Apr 5, 2014

Impressive, but it lets me execute invalid moves.

@kr094

This comment has been minimized.

Show comment
Hide comment
@kr094

kr094 Apr 5, 2014

This is great, works fine in devcpp on windows.

kr094 commented Apr 5, 2014

This is great, works fine in devcpp on windows.

@akarpus

This comment has been minimized.

Show comment
Hide comment
@akarpus

akarpus Apr 5, 2014

Quite a few warnings from gcc but seems to work fine.... good job!

akarpus commented Apr 5, 2014

Quite a few warnings from gcc but seems to work fine.... good job!

@ldong

This comment has been minimized.

Show comment
Hide comment
@ldong

ldong commented Apr 5, 2014

Cool.

@srini156

This comment has been minimized.

Show comment
Hide comment
@srini156

srini156 Apr 5, 2014

This is super cool. Great job :)

srini156 commented Apr 5, 2014

This is super cool. Great job :)

@happybot

This comment has been minimized.

Show comment
Hide comment
@happybot

happybot Apr 5, 2014

Obfuscated. I mean cool.

happybot commented Apr 5, 2014

Obfuscated. I mean cool.

@seanoop

This comment has been minimized.

Show comment
Hide comment
@seanoop

seanoop Apr 5, 2014

我擦, 太niubility了

seanoop commented Apr 5, 2014

我擦, 太niubility了

@seanoop

This comment has been minimized.

Show comment
Hide comment
@seanoop

seanoop Apr 5, 2014

我擦, 太niubility了

seanoop commented Apr 5, 2014

我擦, 太niubility了

@seanoop

This comment has been minimized.

Show comment
Hide comment
@seanoop

seanoop Apr 5, 2014

我擦, 太niubility了

seanoop commented Apr 5, 2014

我擦, 太niubility了

@seanoop

This comment has been minimized.

Show comment
Hide comment
@seanoop

seanoop Apr 5, 2014

我擦, 太niubility了

seanoop commented Apr 5, 2014

我擦, 太niubility了

@seanoop

This comment has been minimized.

Show comment
Hide comment
@seanoop

seanoop Apr 5, 2014

我擦, 太niubility了

seanoop commented Apr 5, 2014

我擦, 太niubility了

@hughrawlinson

This comment has been minimized.

Show comment
Hide comment
@hughrawlinson

hughrawlinson Apr 5, 2014

This is great! One issue is that when you execute an invalid move (i.e. a move that should not cause anything to move) a new tile is spawned.

hughrawlinson commented Apr 5, 2014

This is great! One issue is that when you execute an invalid move (i.e. a move that should not cause anything to move) a new tile is spawned.

@loriopatrick

This comment has been minimized.

Show comment
Hide comment
@loriopatrick

loriopatrick Apr 5, 2014

This is by far the coolest implementation of the 2048 game. Pure Awesome!

loriopatrick commented Apr 5, 2014

This is by far the coolest implementation of the 2048 game. Pure Awesome!

@sebm

This comment has been minimized.

Show comment
Hide comment
@sebm

sebm Apr 5, 2014

works on mac. nice!

sebm commented Apr 5, 2014

works on mac. nice!

@mayurvandra

This comment has been minimized.

Show comment
Hide comment
@mayurvandra

mayurvandra Apr 5, 2014

Just played the game on terminal. Nice one dude.

mayurvandra commented Apr 5, 2014

Just played the game on terminal. Nice one dude.

@blockjon

This comment has been minimized.

Show comment
Hide comment
@blockjon

blockjon commented Apr 5, 2014

Woah!

@davidnormo

This comment has been minimized.

Show comment
Hide comment
@davidnormo

davidnormo Apr 5, 2014

Great, nice work. hughrawlinson is correct though

davidnormo commented Apr 5, 2014

Great, nice work. hughrawlinson is correct though

@chc2259

This comment has been minimized.

Show comment
Hide comment
@chc2259

chc2259 commented Apr 6, 2014

wow~

@cheergo

This comment has been minimized.

Show comment
Hide comment
@cheergo

cheergo Apr 6, 2014

so niubility.

cheergo commented Apr 6, 2014

so niubility.

@diabloneo

This comment has been minimized.

Show comment
Hide comment
@diabloneo

diabloneo Apr 6, 2014

awesome and great.

diabloneo commented Apr 6, 2014

awesome and great.

@jiashuw

This comment has been minimized.

Show comment
Hide comment
@jiashuw

jiashuw Apr 6, 2014

This is legit.

jiashuw commented Apr 6, 2014

This is legit.

@hanxiaomax

This comment has been minimized.

Show comment
Hide comment
@hanxiaomax

hanxiaomax Apr 6, 2014

awesome work

hanxiaomax commented Apr 6, 2014

awesome work

@zhouqiji

This comment has been minimized.

Show comment
Hide comment
@zhouqiji

zhouqiji Apr 6, 2014

fantastic

zhouqiji commented Apr 6, 2014

fantastic

@HalZf

This comment has been minimized.

Show comment
Hide comment
@HalZf

HalZf Apr 6, 2014

nice!

HalZf commented Apr 6, 2014

nice!

@freelrvy

This comment has been minimized.

Show comment
Hide comment
@freelrvy

freelrvy commented Apr 6, 2014

greart!

@hemoely

This comment has been minimized.

Show comment
Hide comment
@hemoely

hemoely Apr 6, 2014

I'm slow ╮(╯▽╰)╭

hemoely commented Apr 6, 2014

I'm slow ╮(╯▽╰)╭

@ggyegou

This comment has been minimized.

Show comment
Hide comment
@ggyegou

ggyegou Apr 6, 2014

Incredible

ggyegou commented Apr 6, 2014

Incredible

@Randy-jun

This comment has been minimized.

Show comment
Hide comment
@Randy-jun

Randy-jun Apr 6, 2014

很不错哦。

Randy-jun commented Apr 6, 2014

很不错哦。

@eastlhu

This comment has been minimized.

Show comment
Hide comment
@eastlhu

eastlhu Apr 6, 2014

干得漂亮!

eastlhu commented Apr 6, 2014

干得漂亮!

@aegiryy

This comment has been minimized.

Show comment
Hide comment
@aegiryy

aegiryy Apr 6, 2014

very excellent job!

aegiryy commented Apr 6, 2014

very excellent job!

@crimx

This comment has been minimized.

Show comment
Hide comment
@crimx

crimx Apr 6, 2014

Nice work! Very impressive!

Reformatted code. https://gist.github.com/Crimx/10001638

crimx commented Apr 6, 2014

Nice work! Very impressive!

Reformatted code. https://gist.github.com/Crimx/10001638

@sheimi

This comment has been minimized.

Show comment
Hide comment
@sheimi

sheimi commented Apr 6, 2014

Cool

@ryc111

This comment has been minimized.

Show comment
Hide comment
@ryc111

ryc111 commented Apr 6, 2014

Nice

@zerob13

This comment has been minimized.

Show comment
Hide comment
@zerob13

zerob13 Apr 6, 2014

Awesome!

zerob13 commented Apr 6, 2014

Awesome!

@TankCool

This comment has been minimized.

Show comment
Hide comment
@TankCool

TankCool commented Apr 6, 2014

WoW.

@ezel

This comment has been minimized.

Show comment
Hide comment
@ezel

ezel Apr 6, 2014

Good Job!

ezel commented Apr 6, 2014

Good Job!

@ezel

This comment has been minimized.

Show comment
Hide comment
@ezel

ezel Apr 6, 2014

Good Job!

ezel commented Apr 6, 2014

Good Job!

@pyguy

This comment has been minimized.

Show comment
Hide comment
@pyguy

pyguy Apr 6, 2014

Nice Job Dude :)

pyguy commented Apr 6, 2014

Nice Job Dude :)

@dazhao

This comment has been minimized.

Show comment
Hide comment
@dazhao

dazhao Apr 6, 2014

why read(0,&k,3)? the arrow keys only need 2bytes.

dazhao commented Apr 6, 2014

why read(0,&k,3)? the arrow keys only need 2bytes.

@icylord

This comment has been minimized.

Show comment
Hide comment
@icylord

icylord Apr 6, 2014

good job !!!

icylord commented Apr 6, 2014

good job !!!

@weedge

This comment has been minimized.

Show comment
Hide comment
@weedge

weedge commented Apr 6, 2014

cool~~

@balaziks

This comment has been minimized.

Show comment
Hide comment
@balaziks

balaziks Apr 6, 2014

There is a small bug in this implementation:
When there is nothing to move in some direction but I press the arrow to that direction it'll spawn new tile. Original 2048 wouldn't let me do this.

Example:
Before: http://balazik.cz/before.png
After arrow-down: http://balazik.cz/after.png

balaziks commented Apr 6, 2014

There is a small bug in this implementation:
When there is nothing to move in some direction but I press the arrow to that direction it'll spawn new tile. Original 2048 wouldn't let me do this.

Example:
Before: http://balazik.cz/before.png
After arrow-down: http://balazik.cz/after.png

@k1ll4m3

This comment has been minimized.

Show comment
Hide comment
@k1ll4m3

k1ll4m3 Apr 6, 2014

good job!

k1ll4m3 commented Apr 6, 2014

good job!

@sadid

This comment has been minimized.

Show comment
Hide comment
@sadid

sadid Apr 6, 2014

awesome!
it has a warning (gcc) but works...amazing

sadid commented Apr 6, 2014

awesome!
it has a warning (gcc) but works...amazing

@djlinks

This comment has been minimized.

Show comment
Hide comment
@djlinks

djlinks Apr 6, 2014

cool...so niubility

djlinks commented Apr 6, 2014

cool...so niubility

@amad

This comment has been minimized.

Show comment
Hide comment
@amad

amad Apr 6, 2014

loool fantastic :))

amad commented Apr 6, 2014

loool fantastic :))

@yifanes

This comment has been minimized.

Show comment
Hide comment
@yifanes

yifanes Apr 7, 2014

干的漂亮

yifanes commented Apr 7, 2014

干的漂亮

@liuhuajin

This comment has been minimized.

Show comment
Hide comment
@liuhuajin

liuhuajin Apr 7, 2014

干的漂亮

liuhuajin commented Apr 7, 2014

干的漂亮

@kunahe

This comment has been minimized.

Show comment
Hide comment
@kunahe

kunahe Apr 7, 2014

饿妹子樱!

kunahe commented Apr 7, 2014

饿妹子樱!

@armagad

This comment has been minimized.

Show comment
Hide comment
@armagad

armagad Apr 7, 2014

It is more like horse shoes than golf.

Maybe if there was some code hand grenading contest.

armagad commented Apr 7, 2014

It is more like horse shoes than golf.

Maybe if there was some code hand grenading contest.

@moham

This comment has been minimized.

Show comment
Hide comment
@moham

moham Apr 8, 2014

WoW 8O
very impressive!

moham commented Apr 8, 2014

WoW 8O
very impressive!

@xdqi

This comment has been minimized.

Show comment
Hide comment
@xdqi

xdqi Apr 11, 2014

Compiled successfully using MinGW and VC11 but unusable.

xdqi commented Apr 11, 2014

Compiled successfully using MinGW and VC11 but unusable.

@hellboysdnu

This comment has been minimized.

Show comment
Hide comment
@hellboysdnu

hellboysdnu commented Apr 17, 2014

Nice job

@thisiswei

This comment has been minimized.

Show comment
Hide comment
@thisiswei

thisiswei Apr 18, 2014

马上有牛!

thisiswei commented Apr 18, 2014

马上有牛!

@xiangshouding

This comment has been minimized.

Show comment
Hide comment
@xiangshouding

xiangshouding commented Jun 27, 2015

Nice job. +1

@JiaxiangZheng

This comment has been minimized.

Show comment
Hide comment
@JiaxiangZheng

JiaxiangZheng commented Jun 27, 2015

Awesome!

@JerryWang304

This comment has been minimized.

Show comment
Hide comment
@JerryWang304

JerryWang304 commented Jun 27, 2015

Awesome!

@xiaobohaha

This comment has been minimized.

Show comment
Hide comment
@xiaobohaha

xiaobohaha commented Jun 27, 2015

碉堡了

@jagosw

This comment has been minimized.

Show comment
Hide comment
@jagosw

jagosw Apr 23, 2016

This is awsome, works well on mac!!

jagosw commented Apr 23, 2016

This is awsome, works well on mac!!

@ironmaniiith

This comment has been minimized.

Show comment
Hide comment
@ironmaniiith

ironmaniiith Jun 19, 2016

THIS IS JUST AWESOME.
I tried to code 2048 in c in my 1st year of college (right now in 2nd) and I did that with around 700 lines.
AND YOU
JUST AMAZINGLY BRILLIANTTTTTTT.
If you were a girl, I would have proposed you ( wouldn't care if rejected though 😜 ).
Seriously HATS OFF DUDE.

Edit:
Just saw your other works as well.
You are not normal human, you are GOD.
Please tell me what is your daily routine.

ironmaniiith commented Jun 19, 2016

THIS IS JUST AWESOME.
I tried to code 2048 in c in my 1st year of college (right now in 2nd) and I did that with around 700 lines.
AND YOU
JUST AMAZINGLY BRILLIANTTTTTTT.
If you were a girl, I would have proposed you ( wouldn't care if rejected though 😜 ).
Seriously HATS OFF DUDE.

Edit:
Just saw your other works as well.
You are not normal human, you are GOD.
Please tell me what is your daily routine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment