Code
1 //Original file by Jay Chan:2 // https://gist.github.com/justecorruptio/99677383 4#include <stdio.h>5#include <stdlib.h>6#include <unistd.h>7#include <time.h>8 9 #defineGrid_len 16Ten One intM[grid_len]; A intX =Grid_len; - intW; - intK; the intK[] = {2,3,1 }; - - - int +W (intDintIintj) - { + if(d <=0) { A return 4* i +J; at } - - returnW (D-1J3-i); - } - - void inS (intFintd) - { to inti =4, J, L, P; + - for(; i--;) { thej = k = L =0; * $ for(; k <4;) {Panax Notoginseng if(J <4) { -P = M[w (d, I, J + +)]; theW |= P >> One; +L *p && (f? M[w (d, I, k)] = L << (l = = P):0, k++); AL = l? P? (l-p?) P:0): L): P; the } + Else { -F? M[w (d, I, k)] = L:0; $++K; $W |=2* !l; -L =0; - } the } - }Wuyi } the - void Wu T () - { About inti = X + rand ()%X; $ - for(; M[i% X] * i; i--); - -I? M[i% X] =2<< rand ()%2:0; AW = i =0; + the for(; I <4; i++) { -S (0, i); $ } the the //Prints The tiles onto the terminal thei =X; thePuts ("\e[2j\e[h"); - in for(; i--;) { the if(M[i]) { theprintf ("%4d|", M[i]); About}Else { theprintf ("%s"," |"); the } the + //every 4th cell is followed by a line-break - if(0= = (I &3)) { thePutchar ('\ n');Bayi } the } the - //read input from keyboard - if(! W2)) { theRead (0, &k,3); theS (1, k[(K >> X)%4]); the T (); the } - } the the int theMain (void)94 { the //Uses Stty to clear the screens in preparation for the game theSystem ("Stty Cbreak"); the 98 /*intializes random number generator*/ About Srand ((unsigned) time (NULL)); - 101 T ();102 103 //Game have finished by this point104 //If win, display "win". Otherwise, display "Lose". thePuts (W &1?"WIN":"lose");106 107 return 0;108}
Can be compiled via GCC and play under console.
ref:https://gist.github.com/anirudh-chhangani/9989292
Modified by Catnull
Compact version 2048