#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
char e[7][8],k[12][12],p[8][576];
char N[512][8]; //Origen+direccion,alcance=Destino
char H[512],h[4][512]; //Tipo,casilla+direccion=Alcance
int A[33]={7},C[33],T[33]={-1}; //Tipo,casilla,bando
int Z[9],pc1[9],pc2[9],pc3[9]; //(0PB,1PN,2R,3C,4A,5T,6D)
int i[576],K[64],P[65],G[64][8];
int main()
{
_AX=0x13;
geninterrupt(0x10);
time_t ti;
char E,far*Q[64];
char far*q=(char far*)0xA0000000;
char pieza[][6][22]=
{" "," 22 "," 2 2 "," "," "," 22 22 ",
" "," 22 "," 22 22 "," 222222222222 "," "," 2222 2222 ",
" 2222 "," 222222 "," 22222 "," 222222222222 "," 222 2222 222 "," 22 2222 2222 22 ",
" 222222 "," 222222 "," 2222222 "," 222111111222 "," 222 2222 222 ","2222 22 22 2222",
" 222222 "," 22222 22 22222 "," 222222122 "," 22222221222 "," 222 2222 222 ","2222 2 2 2222",
" 222222 ","2211122 22 2211122"," 22222222122 "," 2222222221222 "," 22222222222222 "," 22 22 22 22 ",
" 2222 ","21222122211222122212"," 2222112222122 "," 2 2222222221222 "," 22222222222222 "," 22 22 22 22 ",
" 222222 ","21222212211221222212"," 2222122222122 "," 2212222222222122 "," 2111111112 "," 22 22 22 22 ",
" 22222222 ","22122221222212222122"," 22222222222122 "," 22221211222222122 "," 2222222222 "," 222 222222 222 ",
" 22222222 "," 221222212212222122 "," 2222222222221222 "," 222122122122222122 "," 2222222222 "," 22222222222222 ",
" 22222222 "," 2212222222222122 "," 22222222222222122 ","2221222212122222122 "," 2222222222 "," 22222222222222 ",
" 222222 "," 22222222222222 ","221222222222222122 "," 222122122122222122 "," 2222222222 "," 22222222222222 ",
" 22222222 "," 21111111111112 ","211222222212222122 "," 22221211222222122 "," 2222222222 "," 21111111111112 ",
" 2222222222 "," 2222222222222222 "," 222222 122222122 "," 2212222222222222 "," 2222222222 "," 2222222222222222 ",
" 2222222222 "," 222222222222222222 "," 22 22222221222 "," 2 222222222 22 "," 2222222222 "," 222222222222222222 ",
" 222222222222 "," 2222222222222222 "," 22222222122 "," 2222222 22 "," 2222222222 "," 2222222222222222 ",
" 222222222222 "," 21111111111112 "," 222222222122 "," 22222 2 "," 211111111112 "," 21111111111112 ",
" 222222222222 "," 22222222222222 "," 222222222122 "," 222222222 "," 22222222222222 "," 22222222222222 ",
" 222222222222 "," 2222222222222222 "," 2222222222122 "," 2222222222222 "," 2222222222222222 "," 2222222222222222 ",
" "," 2222222222222222 "," 2222222222222 "," 222222 22222 "," 2222222222222222 "," 2222222222222222 ",
" "," "," "," 22222 "," "," "};
char figura[][5][13]=
{" 0 "," 0 0 "," 00000000 "," 000 000 000 "," 000 000 ",
" 000 "," 0000 "," 0 0 "," 0 000 000 0 ","00 000 000 00",
" 000 0 000 "," 0 0 "," 00000 0 "," 0 0 ","00 0 0 00",
"0 0 0 0 0"," 0 0 0 "," 0 0 0"," 00000000000 "," 0 0 0 0 ",
"0 0 0 0 0 0"," 0 0 0 0 "," 0 0 0"," 0 0 "," 0 00 00 0 ",
"0 0 0 0 0"," 0 0 0 "," 0 0 0 0 0"," 0 0 "," 00 00 00 00 ",
"0 0 0 0"," 0 0 0","0 0 0 0 0"," 0 0 "," 0 0 0 0 0 ",
" 0 0 0 0 ","0 00 0 0"," 0 0 0 0 0"," 0 0 "," 0 0 ",
" 0 0 "," 0000 0 0 0"," 0 0 0"," 0 0 "," 0 0 ",
" 000000000 "," 0 0 0"," 0 0 00"," 0000000 "," 000000000 ",
" 0 0 "," 0 0 0"," 0 0 0"," 0 0 "," 0 0 ",
"0 0"," 0 0 0"," 0 000 "," 0 0 ","0 0",
"0000000000000"," 0000000000"," 00000 "," 00000000000 ","0000000000000"};
char posicion[][3][8]=
{"tcadract","......T.","........",
"pppppppp","..R.....","T.......",
"........","........","....rPP.",
"........","........","...ppp..",
"........",".....p..","...A.P..",
"........","......p.",".R.P.P..",
"PPPPPPPP",".D...Cr.","........",
"TCADRACT","........","........"};
int z[]={46,80,46,82,67,65,84,68,46,112,114,99,97,116,100};
int B[64],b[]={0,1,0,-1,-2,0,2,0}; //Promocion y al paso
int S[201][4],J[201][9],JJ[9][9]; //Control de jugadas
int m[201][6],m1[2][9][6],m2[2][9][6]; //Secuencia de mate
int aa[9],dd[9],gg[9],hh[9]; //Rutina principal
int C1[9],C2[9],C3[9]; //Piezas
int pr[2],R[]={4,60}; //Pieza y casilla del rey en inicio
int I[][2]={1,0,33,32}; //Blancas de 1 a I[0][1]
int f[65]={1},F[][3]={0,0,15,0,15,0}; //Fondo, perfil
int X[]={1156,1188,1245,1277}; //Tabulacion
int L[]={-1,1,-1,1,-1,1,-1,1,-1};
int LL[]={400,0,400,0,400,0,400,0,400};
int ix[]={-1,0,1,1,1,0,-1,-1,1,2,2,1,-1,-2,-2,-1};
int iy[]={1,1,1,0,-1,-1,-1,0,2,1,-1,-2,-2,-1,1,2};
int D[]={-1,1,-1,-1,-1,0,-1,-1}; //Peon al frente
int d1[]={0,4,0,0,0,1,0};
int d2[]={2,6,7,7,6,7,7};
int d3[]={1,1,1,1,2,2,1};
int a=15,c=64,g=0,t=0,M=3;
int d,j,n,v,w,x,y,c1,c2;
printf("%c[2;26fBlancas Negras",27);
while(a--) i[z[a]]=a;
for(v=0;v<8;v++)
for(w=8;w--;){ // posicion[v][?=0,1,2][w]
k[9-v][w+2]=c--;
B[c]=b[v];
if(!i[a=posicion[v][2][w]]) continue;
y=I[x=i[a]>7][!x]-=L[x];
A[y]=(i[a]-1)%7;
C[y]=c;
T[y]=x;
P[c]=y;
if(A[y]==2) pr[x]=y;
}
for(v=13;v--;)
for(d=4;d--;y=x){
if(!v&&d<3&&d) x=1219;
else x=5120*v+X[d]-1;
if(v>11||d>2) continue;
for(w=y-x+1;w--;*(q+x+w)=*(q+x+w+5120)=7);
for(w=16;w--;*(q+x+320*w)=*(q+y+320*w)=7);
}
for(v=576;v--;i[v]=320*y+v%24)
if((y=v/24)>1&&y<22&&(x=v%24)>1&&x<22)
for(w=6;w--;) if(a=pieza[y-2][w][x-2]-32)
p[w+1][v]=p[w+1-!w][v]=a-16;
for(v=13;v--;)
for(a=5;a--;)
for(w=13;w--;) if(figura[v][a][w]-=32)
figura[v][a][w]--;
for(a=7;a--;)
for(d=d1[a];d<=d2[a];d+=d3[a])
if(a==2) e[2][d]=2;
else if(a>3) e[a][d]=8;
else if(a<2&&D[d]<0) e[1-a][d]=2;
do{
Q[c]=q+54720-7680*(y=c/8)+24*(x=c%8);
if(x) f[c+1]=f[c]=4-f[c-1];
a=A[P[c]];
F[w=T[P[c]]>0][0]=f[c];
for(v=576;v--;*(Q[c]+i[v])=F[w][p[a][v]]);
for(d=8;d--;g++){
N[g][0]=64;
if(n=k[(v=y+2)+iy[d+8]][(w=x+2)+ix[d+8]])
N[g][h[3][g]--]=n-1;
while(n=k[v+=iy[d]][w+=ix[d]])
N[g][++H[g]]=n-1;
if(!H[G[c][d]=g]) continue;
h[0][g]=1+(B[c]==2&&d==1);
h[1][g]=1+(B[c]==1&&d==5);
h[2][g]=2-(c!=4&&c!=60||d!=3&&d!=7);
}
}while(++c<64);
Inicio:
ti=time(NULL)-1;
for(v=0;v<M;v++)
for(w=0;w++<M-v;JJ[w][v+w]=LL[w]);
pc1[n=1]=I[t][j=0]; //Primera pieza
Origen:
C1[n]=C[pc1[n]]; //Casilla de la pieza
if(C1[n]<0) goto Siguiente;
K[C1[n]]++; //Simulacion de origen
P[C1[n]]=0;
aa[n]=A[pc1[n]]; //Tipo de pieza
dd[n]=d1[aa[n]]; //Direccion inicial
Alcance:
gg[n]=G[C1[n]][dd[n]];
hh[n]=aa[n]<4?h[aa[n]][gg[n]]:H[gg[n]];
if(!hh[n]) goto Direccion;
Z[n]=aa[n]!=3;
Destino:
c2=C2[n]=N[gg[n]][Z[n]]; //Casilla destino
if(T[P[c2]]==t) goto Direccion; //Mismo bando
if(aa[n]<2) //Peon
if(D[dd[n]]==T[P[c2]]){ //Captura al paso
if(P[c2]||B[C1[n]]+t+1) goto Direccion;
if(aa[n-1]>1||Z[n-1]<2) goto Direccion;
if(2*c2!=C1[n-1]+C2[n-1]) goto Direccion;
pc3[n]=P[C3[n]=C2[n-1]];
C[pc3[n]]=-1;
P[C3[n]]=0;
}
else if(B[C1[n]]==t+1) A[pc1[n]]=6; //Promocion
if(aa[n]==2&&Z[n]>1) //Enroque
if(P[c2]||K[c=C1[n]]>1) goto Direccion;
else{
for(d=0;d<8;d++)
if(A[x=P[N[g=G[c][d]][y=0]]]==3&&T[x]!=t) d=8;
else while(y<H[g])
if(x=P[N[g][++y]])
if(T[x]!=t&&y<e[A[x]][d]) y=d=8;
else break;
if(d=d>8) goto Direccion;
if(c<c2)
if(K[c+3]||P[c+3]!=pr[t]+3*L[t]) goto Direccion;
else C3[n]=c+3; //Enroque corto
if(c>c2)
if(K[c-4]+P[c-3]||P[c-4]!=pr[t]-4*L[t]) goto Direccion;
else C3[n]=c-4;
pc3[n]=P[C3[n]]; //Simulacion de enroque
C[pc3[n]]=(c+c2)/2;
P[C3[n]]=0;
P[(c+c2)/2]=pc3[n];
}
pc2[n]=P[c2];
Simulacion:
C[pc1[n]]=C2[n]; //Pieza a destino
C[pc2[n]]=-1;
P[C2[n]]=pc1[n];
c=C[pr[t]];
for(d=0;d<8;d++)
if(A[x=P[N[g=G[c][d]][y=0]]]==3&&T[x]!=t) d=8;
else while(y<H[g])
if(x=P[N[g][++y]])
if(T[x]!=t&&y<e[A[x]][d]) y=d=8;
else break;
if(d<9&&n<M){ //Nivel siguiente
t=!t;
pc1[++n]=I[t][0];
goto Origen;
}
Restaurar:
C[pc2[n]]=C2[n];
P[C2[n]]=pc2[n];
if(pc3[n]){ //Al paso o enroque
if(A[pc3[n]]>1) P[(R[t]+C2[n])/2]=0;
C[pc3[n]]=C3[n];
P[C3[n]]=pc3[n];
pc3[n]=0;
}
if(d>8) hh[n]-=d=aa[n]==2; //No permite enroque
else{
JJ[v=1][n]+=2; //Jugadas posibles
if(n>1)
while(v<=M-n){ //Minimax
x=JJ[v+1][n+v]-(g=JJ[v][n+v]);
if(L[v++]!=(x>0)-(x<0)) continue;
JJ[v][n+v-1]=g;
if(v==4&&n<4) //Confirma secuencia
for(y=2;y--;)
for(w=n+1;w--;)
m1[y][n+3][w]=m2[y][n+3][w];
}
else{
S[++j][0]=(a=A[pc1[1]])>aa[1]?a:0;
S[j][1]=C1[1];
S[j][2]=C2[1];
S[j][3]=M-1;
while(v<M)
if((y=JJ[x=v++][x+1])>1) J[j][x]=y/2;
else{
J[j][x]=y-1;
J[j][x+1]=201-y;
S[j][3]=x+(L[x]==L[v=M]);
for(w=x-1;w--;m[j][w]=m1[y][x+1][w]);
}
}
}
for(v=n;v<M;v++) //Reinicio de contadores
for(w=0;w++<M-v;JJ[w][v+w]=LL[w]);
if(A[pc1[n]]>aa[n]) //Subpromocion
if(--A[pc1[n]]>2) goto Simulacion;
else A[pc1[n]]=t;
if(!pc2[n]&&Z[n]++<hh[n]) goto Destino;
Direccion:
dd[n]+=d3[aa[n]];
if(dd[n]<=d2[aa[n]]) goto Alcance;
K[C1[n]]--; //Restaura origen
C[pc1[n]]=C1[n];
P[C1[n]]=pc1[n];
Siguiente:
if(pc1[n]++<I[t][1]) goto Origen; //Pieza siguiente
if(!JJ[1][n]){
c=C[pr[t]];
for(d=0;d<8;d++)
if(A[x=P[N[g=G[c][d]][y=0]]]==3&&T[x]!=t) d=8;
else while(y<H[g])
if(x=P[N[g][++y]])
if(T[x]!=t&&y<e[A[x]][d]) y=d=8;
else break;
y=JJ[v=1][n]=d=d<9;
while(++v<n){ //Secuencia de mate o ahogado
if(aa[v]+1==B[C1[v]])
x=-100*A[pc1[v]]-C2[v];
else x=100*aa[v]+C2[v];
if(n<5) m1[y][n][v-2]=x;
else m2[y][n][v-2]=x;
}
}
if(--n){
t=!t;
goto Restaurar;
}
for(y=11-M;y--;)
for(d=3;d--;)
for(n=5120*(y+M+2)+X[d],v=15;v--;)
for(n-=320,w=d==1?55:31;w--;*(q+n+w)=0);
g=4+2*M;
for(c=10-M;c--;)
if(c<j){ //Ordenar maximos o minimos
for(x=y=0;y++<j;)
if(S[y][3])
if(!x) x=y;
else{
v=J[x][S[x][3]];
w=J[y][S[y][3]];
if(L[M]==(v<w)-(v>w)||v==w&&S[x][3]>S[y][3]) x=y;
}
v=J[x][w=S[x][3]];
gotoxy(37-(t+M)%2*11,g+=2);
if(v>0&&v<200) printf("%3u",v);
else if(v==0||v==200) printf("1/2");
else if(v==LL[w-t]/2-L[w-t]) printf("1-0");
else printf("0-1");
printf("%c[%u;32f%c%c",27,g,97+S[x][2]%8,49+S[x][2]/8);
S[x][3]=0;
n=552+2560*g;
if(a=S[x][0]) n+=34;
else a=A[P[S[x][1]]];
for(v=13*(a>1);v--;n-=320)
for(w=13;w--;*(q+n+w)=figura[v][a-2][w]);
}
printf("%c[%u;%ufN=%u",27,g=4+2*M,26+11*t,M);
printf("%c[%u;30f%5.f%c",27,g,difftime(time(NULL),ti),34);
c1=R[t]; //Casilla del rey en origen
while(kbhit()) getch();
Cursor:
for(y=M;y--;)
for(d=3;d--;)
for(n=5120*(y+2)+X[d],v=15;v--;)
for(n-=320,w=d==1?55:31;w--;*(q+n+w)=0);
gotoxy(26+11*t,4);
if(j) printf("%3u",j);
else if(JJ[1][1]) printf("1/2");
else if(t) printf("1-0");
else printf("0-1");
Teclado:
x=0;
c2=c=c1; //Casilla de origen
for(v=576;v--;)
if(*(Q[c]+i[v])==f[c]) *(Q[c]+i[v])=7+t;
while(!(E=getch()));
if(E==27&&!j) return 0;
if(E!=13){
if(E==77&&c1<63) c1++;
if(E==75&&c1>0) c1--;
if(E==72&&c1<56) c1+=8;
if(E==80&&c1>7) c1-=8;
for(v=576;v--;)
if(*(Q[c]+i[v])==7+t) *(Q[c]+i[v])=f[c];
if(y=(E=='+'&&M<8)-(E=='-'&&M>2)) M+=y;
else goto Teclado;
goto Inicio;
}
Eleccion:
if((x+=E!=75)>j||(x-=E==75)<1)
if(c2==c1) goto Teclado;
else x=x<1?j:1;
if(S[x][1]!=c1) goto Eleccion;
c2=S[x][2];
printf("%c[4;32f%c%c",27,97+c2%8,49+c2/8);
n=10792;
if(a=S[x][0]) n+=34;
else a=A[P[c1]];
for(v=13*(a>1);v--;n-=320)
for(w=13;w--;*(q+n+w)=figura[v][a-2][w]);
F[t][0]=8-t;
for(v=576;v--;*(Q[c2]+i[v])=F[t][p[a][v]]);
c=y=0;
while(++y<M){
for(d=3;d--;)
for(n=5120*(y+2)+X[d],v=15;v--;)
for(n-=320,w=d==1?55:31;w--;*(q+n+w)=0);
gotoxy(26+(y+t)%2*11,4+2*y);
if(!c) if(J[x][y]>0) printf("%3u",J[x][y]);
else if(!J[x][c=y]) printf("1/2");
else if((y+t)%2) printf("1-0");
else printf("0-1");
}
while(--c>0){
n=586+2560*(g=4+2*c);
y=m[x][c-1];
if(y>0) n-=34;
else y=-y;
printf("%c[%u;32f%c%c",27,g,97+y%100%8,49+y%100/8);
a=y/100;
for(v=13*(a>1);v--;n-=320)
for(w=13;w--;*(q+n+w)=figura[v][a-2][w]);
}
while(!(E=getch()));
a=A[P[c2]]; //Pieza a capturar
F[w=T[P[c2]]>0][0]=f[c2];
for(v=576;v--;*(Q[c2]+i[v])=F[w][p[a][v]]);
if(E==27) goto Cursor;
if(E!=13) goto Eleccion;
if(S[x][0]) A[P[c1]]=S[x][0]; //Promocion
aa[0]=A[P[c1]]; //Posibilitar al paso
Z[0]=L[t]*(c1-c2)/8;
if(a>6&&aa[0]<2)
if((c1-c2)%8){
C[P[c=C2[0]]]=-1;
P[c]=0;
for(v=576;v--;*(Q[c]+i[v])=f[c]);
}
K[C1[0]=c1]=K[C2[0]=c2]=1; //Casillas usadas
do{
C[P[c=c1]]=c2;
C[P[c2]]=-1;
P[c2]=P[c];
P[c]=0;
for(v=576;v--;*(Q[c]+i[v])=f[c]);
a=A[P[c2]];
F[t][0]=f[c2];
for(v=576;v--;*(Q[c2]+i[v])=F[t][p[a][v]]);
if(a==2) if(c2-c==2) c1+=3;
else if(c-c2==2) c1-=4;
c2=(c+c2)/2;
}while(c!=c1);
t=!t;
goto Inicio;
}