/* cc -o tetris tetris.c */ /* Any define's or variables with longer than one-letter names were added */ /* by Spiros & Teris who also made the code (somewhat) more readable. */ #define HI ".tetris-high-scores" #define BACK ".tetris-backup" char *asctime(); long h[4], ltime; char cmd[500]; timer() { h[3]=h[3]-h[3]/3000; setitimer(0,h,0); } int c, d, speed, type, bin[8], drops, sigtab[] = {(int)timer, 0, 2}, score, s, I, K=0, i=276, j, k, q[276], Q[276], *p2piece=q, *m, x=17, pieces[] = { 7, -13, -12, 1, 8, -11, -12, -1, 9, -1, 1, 12, 3, -13, -12, -1, 12, -1, 11, 1, 15, -1, 13, 1, 18, -1, 1, 2, 0, -12, -1, 11, 1, -12, 1, 13, 10, -12, 1, 12, 11, -12, -1, 1, 2, -12, -1, 12, 13, -12, 12, 13, 14, -11, -1, 1, 4, -13, -12, 12, 16, -11, -12, 12, 17, -13, 1, -1, 5, -12, 12, 11, 6, -12, 12, 24}; u() { for(i=11;++i<264;) if((k=q[i])-Q[i]) { Q[i]=k; if(i-++I||i%12<1)printf("\033[%d;%dH",(I=i)/12,i%12*2+28); /* Locate */ printf("\033[%dm "+(K-k?0:5),k); K=k; } printf("\033[21;37H%d",score); /*print score*/ Q[263]=c=getchar(); } G(b) { for(i=4;i--;) if(q[i?b+p2piece[i]:b]) return 0; return 1; } g(b) { for(i=4;i--;q[i?x+p2piece[i]:x]=b); } main (argc, argv, a) char* *argv , *a; { h[3]=1000000/(speed=argc>1?atoi(argv[1]):2); for(a=argc>2?argv[2]:"jkl pq";i;i--)*p2piece++=i<25||i%12<2?7:0; srand(getpid()); /* initialize random numbers with pid */ system("stty cbreak -echo "); sigvec(14,sigtab,0); timer(); puts("\033[H\033[J"); /* Clear Screen */ bin[7]=1; for(++bin[type=rand()%7],p2piece=pieces+type*4;;g(7),u(),g(0)) { if(c<0) { if(G(x+12)) x+=12; else { g(7); ++score; ++drops; for(j=0;j<252;j=12*(j/12+1)) for(;q[++j];) if(j%12==10) { for(;j%12;q[j--]=0);u(); for(;--j;q[j+12]=q[j]);u(); } ++bin[7]; ++bin[type=rand()%7]; p2piece=pieces+type*4; G(x=17)||(c=a[5]); } } if(c==*a)G(--x)||++x; if(c==a[1])p2piece=pieces+4**(m=p2piece),G(x)||(p2piece=m); if(c==a[2])G(++x)||--x; if(c==a[3]){++drops;for(;G(x+12);++score)x+=12;} if(c==a[4]||c==a[5]) /* if q pressed */ { s=sigblock(8192); printf("\033[H\033[J\033[0mScore:%d\n",score); printf("Final level:%.1f\n",1000000.0/h[3]); printf("Average height dropped per piece:%.1f\n",(float)score/drops); printf(" Zig Zag Tee Square Rell Lell Stick Total\n"); for(j=0;j<8;++j)printf(" %4d ",bin[j]); printf("\n"); if(c==a[5])break; for(j=264;j--;Q[j]=0); /*else show score until p repressed*/ while(getchar()-a[4]); puts("\033[H\033[J\033[7m"); /*cls,inverse video*/ sigsetmask(s); } } sprintf (cmd, "stty -cbreak echo stop \023; sort -mnr -o %s - %s; head -15 %s; cp %s %s", BACK, HI, BACK, BACK, HI); d = popen (cmd, "w"); ltime = time(0); fprintf (d, "%5d from level %1d by %s on %s\n", score, speed, getlogin (), asctime (localtime (<ime))); pclose (d); }