#include // TWOK is a C implemnetation of 2048 game #define ut8 unsigned char static ut8 twok_buf[4][4]; static int score =0; static int moves =0; #define INTERNAL static INTERNAL void twok_init() { int i, j; score = 0; for (i=0;i<4;i++) for (j=0;j<4;j++) twok_buf[i][j] = 0; } INTERNAL void twok_add() { int i, j, min = 1; int holes = 0; for (i=0;i<4;i++) { for (j=0;j<4;j++) { if (twok_buf[i][j] > 1) { min = 2; break; } } } for (i=0;i<4;i++) for (j=0;j<4;j++) if (!twok_buf[i][j]) { holes = 1; break; } if (holes) for (;;) { i = r_num_rand (4); j = r_num_rand (4); if (!twok_buf[i][j]) { twok_buf[i][j] = r_num_rand (min)+1; break; } } } INTERNAL int twok_fin() { int i,j; for (i=0;i<4;i++) for (j=0;j<4;j++) if (!twok_buf[i][j]) return 1; for (i=0;i<4;i++) for (j=0;j<3;j++) if (twok_buf[i][j] == twok_buf[i][j+1]) return 1; for (i=0;i<3;i++) for (j=0;j<4;j++) if (twok_buf[i][j] == twok_buf[i+1][j]) return 1; return 0; } INTERNAL void twok_move(int d) { int i, j, k; moves++; if (d=='a') { twok_add (); } else for (k=0;k<4;k++) { switch (d) { case 'h': // left // for each row for (i=0; i<4; i++) { for (j=0; j<3; j++) { if (twok_buf[i][j] == 0) { twok_buf[i][j] = twok_buf[i][j+1]; twok_buf[i][j+1] = 0; } else if (twok_buf[i][j] == twok_buf[i][j+1]) { twok_buf[i][j] ++; score += (1<0; j--) { if (twok_buf[i][j] == 0) { twok_buf[i][j] = twok_buf[i][j-1]; twok_buf[i][j-1] = 0; } else if (twok_buf[i][j] == twok_buf[i][j-1]) { twok_buf[i][j] ++; score += (1<0; i--) { if (twok_buf[i][j] == 0) { twok_buf[i][j] = twok_buf[i-1][j]; twok_buf[i-1][j] = 0; } else if (twok_buf[i][j] == twok_buf[i-1][j]) { twok_buf[i][j] ++; score += (1<