A mini version of 2048 games written in C, only 500 characters

Source: Internet
Author: User

Jay Chan wrote a mini version of 2048 games in C with only 487 characters. Come and watch the crowd.

M[16],x=16,w,k;main () {T (System ("Stty cbreak");p UTS (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));}

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #define Grid_len 16int m[grid_len];int X = grid_len;int w;int k;int k[] = {2, 3, 1};int W (int d, int i, int j) {if (d <= 0) {RET  Urn 4 * i + j; } Return W (D-1, J, 3-i);}  void S (int f, int d) {int i = 4, J, L, P; for (; i--;)    {j = k = L = 0; for (; k < 4;)        {if (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; } else {f?        M[w (d, I, k)] = l:0;        ++k;        W |= 2 *!l;      L = 0;  }}}}void T () {int i = x + rand ()% x; for (; M[i% X] * i;  i--); I?  M[i% X] = 2 << rand ()% 2:0;  W = i = 0;  for (; i < 4; i++) {s (0, I);  }//Prints the tiles onto the terminal i = X;  Puts ("\e[2j\e[h"); for (; i--;)    {if (M[i]) {printf ("%4d|", M[i]);  } else {    printf ("%s", "|");    }//Every 4th cell is followed by a line-break if (0 = = (I & 3)) {Putchar (' \ n '); }}//Read input from keyboard if (! (    W-2) {Read (0, &k, 3);    S (1, k[(K >> X)% 4]);  T ();  }}int Main (void) {//Uses Stty to clear the screen in preparation for the game//system ("Stty cbreak");  /* intializes random number generator */Srand ((unsigned) time (NULL));  T (); Game have finished by this point/If win, display "win".  Otherwise, display "Lose". Puts (W & 1?)  "WIN": "Lose"); return 0;}


A mini version of 2048 games written in C, only 500 characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.