This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
M[16]; /* The board */ | |
const int brd_pos_cnt=16; | |
W; | |
k; | |
main(){ | |
system("stty cbreak"); | |
T(); | |
puts(W&1?"WIN":"LOSE"); | |
} | |
K[]={2,3,1}; | |
s(f,d){ | |
/* Looking like i and k are indicies into board */ | |
int i, j, l, P; | |
for(i=4;i--;) { | |
for(j=k=l=0;k<4;) { | |
if (j<4) { | |
P=M[w(d,i,j++)]; | |
W|=P>>11; | |
if (l*P) { | |
if (f) { | |
M[w(d,i,k)]=l<<(l==P); | |
} | |
k++; | |
} | |
//l=l ? (P ? (l-P)?P:0:l:P; | |
if (l) { | |
if (P) { | |
if (l-P) { | |
l=P; | |
} else { | |
l=0; | |
} | |
} else { | |
l = l; | |
} | |
} else { | |
l=P; | |
} | |
} else { | |
if (f) { | |
M[w(d,i,k)]=l; | |
} | |
++k; | |
W|=2*!l; | |
l=0; | |
} | |
} | |
} | |
} | |
/* The board coordinate (i,j) --> linear index | |
into the board array M I think. | |
The mapping is weird though. | |
d == direction to look? | |
*/ | |
w(d,i,j){ | |
if (d) { | |
return w(d-1,j,3-i); | |
} else { | |
return 4*i+j; | |
} | |
} | |
T(){ | |
int i; | |
for(i=brd_pos_cnt+rand()%brd_pos_cnt; M[i%brd_pos_cnt]*i; i--); | |
if (i) { | |
M[i%brd_pos_cnt]=2<<rand()%2; | |
} | |
for(W=i=0;i<4;) { | |
s(0,i++); | |
} | |
/* Clear screen and reset cursor */ | |
puts("\e[2J\e[H"); | |
for(i=brd_pos_cnt;i--;i%4||puts("")) { | |
printf(M[i]?"%1d|":"_|",M[i]); | |
} | |
if (!(W-2)) { | |
/* Read 3 bytes from STDIN to integer k | |
Get user input. | |
Note you can use any keys to play! | |
It just takes key value and mods | |
it with 4 to get the direction? | |
Note: K only has 3 elements, but | |
this access into K can | |
give an index of 3... | |
Right arrow: k=4414235, K index=3 | |
Left arrow: k=4479771, K index=0 | |
Up arrow: k=4283163, K index=1 | |
Down arrow: k=4348699, K index=2 | |
Looks like when K_index = 3 --> just rely on | |
compiler padding end of K with 0s for alignment | |
purposes so K[3] == 0 I think. | |
*/ | |
read(0,&k,3); | |
T(s(1,K[(k>>brd_pos_cnt)%4])); | |
} | |
}//[2048] | |
/* Interesting so far: | |
* Using bitwise OR (|) as semicolon! | |
* Some old C magic (?) that allows functions defined | |
* with 6 args to be called with only 2 (see original s func) | |
*/ | |
// Original for reference | |
// 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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment