原链接:https://gist.github.com/justecorruptio/9967738
代码:
1 M[16],X=16,W,k; 2 main(){ 3 T(system("stty cbreak")); 4 puts(W&1?"WIN":"LOSE"); 5 } 6 K[]={2,3,1}; 7 s(f,d,i,j,l,P) 8 { 9 for(i=4;i--;) 10 for(j=k=l=0;k<4;) 11 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); 12 } 13 w(d,i,j){return d?w(d-1,j,3-i):4*i+j;} 14 T(i){ 15 for(i=X+rand()%X;M[i%X]*i;i--); 16 i?M[i%X]=2<<rand()%2:0; 17 for(W=i=0;i<4;) 18 s(0,i++); 19 for(i=X,puts("e[2Je[H"); 20 i--;i%4||puts(""))printf(M[i]?"%4d|":" |",M[i]); 21 W-2||read(0,&k,3)|T(s (1,K[(k>>X)%4])); 22 }//[2048]