Skip to content

Instantly share code, notes, and snippets.

@justecorruptio
Created April 4, 2014 03:49
Show Gist options
  • Save justecorruptio/9967738 to your computer and use it in GitHub Desktop.
Save justecorruptio/9967738 to your computer and use it in GitHub Desktop.
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]
@pyguy
Copy link

pyguy commented Apr 6, 2014

Nice Job Dude :)

@dazhao
Copy link

dazhao commented Apr 6, 2014

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

@icylord
Copy link

icylord commented Apr 6, 2014

good job !!!

@weedge
Copy link

weedge commented Apr 6, 2014

cool~~

@balaziks
Copy link

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
Copy link

k1ll4m3 commented Apr 6, 2014

good job!

@sadid
Copy link

sadid commented Apr 6, 2014

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

@djlinks
Copy link

djlinks commented Apr 6, 2014

cool...so niubility

@amad
Copy link

amad commented Apr 6, 2014

loool fantastic :))

@yifanes
Copy link

yifanes commented Apr 7, 2014

干的漂亮

@liuhuajin
Copy link

干的漂亮

@kunahe
Copy link

kunahe commented Apr 7, 2014

饿妹子樱!

@armagad
Copy link

armagad commented Apr 7, 2014

It is more like horse shoes than golf.

Maybe if there was some code hand grenading contest.

@moham
Copy link

moham commented Apr 8, 2014

WoW 8O
very impressive!

@xdqi
Copy link

xdqi commented Apr 11, 2014

Compiled successfully using MinGW and VC11 but unusable.

@hellboysdnu
Copy link

Nice job

@thisiswei
Copy link

马上有牛!

@oxUnd
Copy link

oxUnd commented Jun 27, 2015

Nice job. +1

@JiaxiangZheng
Copy link

Awesome!

@findo
Copy link

findo commented Jun 27, 2015

Awesome!

@xiaobohaha
Copy link

碉堡了

@jagosw
Copy link

jagosw commented Apr 23, 2016

This is awsome, works well on mac!!

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