Skip to content

Instantly share code, notes, and snippets.

@findo
Forked from justecorruptio/2048.c
Last active August 29, 2015 14:23
Show Gist options
  • Save findo/a46a8d20fca12e6e8f38 to your computer and use it in GitHub Desktop.
Save findo/a46a8d20fca12e6e8f38 to your computer and use it in GitHub Desktop.

Revisions

  1. @justecorruptio justecorruptio created this gist Apr 4, 2014.
    11 changes: 11 additions & 0 deletions 2048.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    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]