#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
_AX=0x13;
geninterrupt(0x10);
clock_t T=clock();
srand(time(NULL));
char far*Q=(char far*) 0xA0000000L;
char E,A[][12]={"000000111100",
"000020222000",
"000300033300",
"000400444000",
"000055055000",
"006600066000",
"000770077000"};
int B[]={6,2,10,7,3,0,11,8,4,1,9,5};
int N[]={1,2,11,12,13,14,22,23,24,25,34,35};
int e[49],i[275],C[12],D[12],Y[25];
int c,d,n,x,y,a=6,L,I=433,M=9;
for(c=49;c--;) e[c]=c/7*320+c%7;
for(L=y=0;y<25;y++,I+=2472)
for(Y[y]=x=0;x<11;x++,I+=8)
for(n=x*y<1||y>23||x>9?8:0,i[++c]=I,d=49;d--;)
*(Q+I+e[d])=n;
Inicio:
printf("%c[3;28f Nivel=%u",27,10-M);
printf("%c[5;28fLineas=%u",27,L);
x=c=4;
for(n=0;n<12;n++)
if(C[n]=A[a][n]-48)
if(*(Q+i[N[n]+c])) break;
else for(I=i[N[n]+c],d=49;d--;)
*(Q+I+e[d])=C[n];
while(n<12)
if(getch()==27) return 0;
a=rand()%7;
while(n--)
for(I=17835+N[n]/11*2560+N[n]%11*8,d=49;d--;)
*(Q+I+e[d])=A[a][n]-48;
while(x+1){while(E=kbhit()) getch();
do if(kbhit()) E=getch();
while(!E&&clock()-T<M);
if(E==27&&getch()==27) return 0;
if(E=='-') M+=M<10;
if(E=='+') M-=M>1;
if(E==77||E==75) x+=E-76;
else if(E!=72) x+=E=11;
for(n=12;n--;D[n]=c!=x?C[n]:C[B[n]])
for(I=i[N[n]+c],d=C[n]?49:0;d--;)
*(Q+I+e[d])=0;
while(++n<12)
if(D[n]&&*(Q+i[N[n]+x])) x=-1;
if(x+1) c=x;
while(n--)
if(C[n]=x+1?D[n]:C[n])
for(I=i[N[n]+c],d=49;d--;)
*(Q+I+e[d])=C[n];
if(E==11) T=clock();
else x=c;
}
while(++n<12)
if(C[n]) Y[(N[n]+c)/11]++;
for(y=24;--y;)
while(Y[y]>8)
for(L++,x=y;x--;Y[x]-=Y[x+1]=Y[x])
for(c=11*x+1,n=9;n--;c++)
if((I=x?*(Q+i[c]):0)||*(Q+i[c+11]))
for(d=49;d--;)
*(Q+i[c]+e[d])-=*(Q+i[c+11]+e[d])=I;
goto Inicio;
}