#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char A[4][4][7][4]={
"0000","0000","0000","0000","0000","0000","0000",
"1111","0100","0010","0110","0110","0100","0110",
"0000","0111","1110","0011","1100","1110","0110",
"0000","0000","0000","0000","0000","0000","0000"};
char E,far*q=1967+(char far*)0xB8000000;
char far*Q[250],C[7][4][4],e[256];
int D[250],Y[25],I[4],i[4];
int G[]={1,2,3,0};
int X[]={10,1,-1,0};
int M=9,c=250,p=6;
int L,a,g,n,x,y;
int main(){
textmode(C40);
_setcursortype(_NOCURSOR);
clock_t T;
srand(time(NULL));
printf("%c[3;28f Nivel=1",27);
e[77]=1,e[75]=2,e[72]=3;
for(y=25;y--;*(q+=22)=8,*--q=219,q-=81)
for(x=10;x--;*q--=219,D[c]=1)
*(Q[--c]=q--)=y%24*x?0:8;
for(c=48,g=4;g--;c=0)
for(a=7;a--;)
for(n=0,y=4;y--;)
for(x=4;x--;)
if(A[g][y][a][x]=A[G[g]][x][a][3-y]-c)
C[a][g][n++]=10*y+x;
Inicio:
printf("%c[5;28fLineas=%u",27,L);
for(n=4;n--;*(q+502)=0,D[I[n]=c]=0)
if(!*Q[c=4+C[a=p][g=0][n]]) *(q=Q[c])=a+1;
else while(1) if(getch()==27) return 0;
p=rand()%7;
for(c=n=4;n--;*--q=219)
*(q=Q[C[p][0][n]]+510)=p+1;
do{
if(n<0) T=clock();
do if(E=kbhit()) E=getch();
while(!E&&clock()-T<M);
if(E==27&&getch()==E) return 0;
if(y=(E=='-'&&M<10)-(E=='+'&&M>1))
printf("%c[3;35f%u",27,10-(M+=y));
y=X[x=e[E]]?g:G[g];
for(n=4;n--;)
if(*Q[i[n]=c+C[a][y][n]+X[x]]&&D[i[n]]) break;
if(n+1) continue;
for(n=4;n--;)
if(D[i[n]]--) *Q[i[n]]=a+1;
for(n=4;n--;I[n]=i[n])
if(++D[I[n]]) *Q[I[n]]=0;
x>2?g=y:c+=X[x++];
}while(x);
for(n=4;n--;Y[y=I[n]/10]++,D[I[n]]=1);
do while(Y[x=y]>8)
do for(L+=x==y,c=10*x--,n=9;n--;)
*Q[++c]=x?*Q[c-10]:0;
while(Y[x+1]=Y[x]);
while(Y[--y]);
goto Inicio;
}