#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
char e[7][8],k[12][12],p[8][576];
char N[65][8][8]; //Origen, direccion, alcance=Destino
char H[7][65][8]; //Tipo, casilla, direccion=Alcance
int A[33]={7},C[33],T[33]={-1}; //Tipo,casilla,bando
int G[9],C1[9],C2[9],C3[9]; //(0PB,1PN,2R,3C,4A,5T,6D)
int B[65],K[65],P[65],Z[256];
int main()
{
_AX=0x13;
geninterrupt(0x10);
time_t ti;
char E,far*Q[65];
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,114};
int 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],mm[2][2][9][6]; //Secuencia de mate
int aa[9],dd[9],hh[9]; //Rutina principal
int pc1[9],pc2[9],pc3[9]; //Piezas
int pr[2],R[]={5,61}; //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 i[576],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,h=0,t=0,M=3;
int d,j,n,v,w,x,y,c1,c2;
printf("%c[2;26fBlancas Negras",27);
while(a--) Z[z[a]]=a;
for(v=0;v<8;v++)
for(w=8;w--;){ // posicion[v][?=0,1,2][w]
B[c]=b[v];
k[9-v][w+2]=c--;
if(!Z[a=posicion[v][1][w]]) continue;
y=I[x=Z[a]>7][!x]-=L[x];
T[y]=x;
A[y]=(Z[a]-1)%7;
P[C[y]=c+1]=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{
y=c/8;
x=c-8*y;
Q[++c]=q+7680*(7-y)+24*x+960;
f[c]=4-f[c-1];
if(!x) f[c]=4-f[c];
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--;){
if(n=k[(v=y+2)+iy[d+8]][(w=x+2)+ix[d+8]])
N[c][d][H[3][c][d]--]=n;
while(n=k[v+=iy[d]][w+=ix[d]])
N[c][d][++h]=n;
if(!h) continue;
H[4][c][d]=H[5][c][d]=H[6][c][d]=h;
H[h=0][c][d]=1+(B[c]==2&&d==1);
H[1][c][d]=1+(B[c]==1&&d==5);
H[2][c][d]=2-(c!=5&&c!=61||d!=3&&d!=7);
}
}while(c<64);
Inicio:
ti=time(NULL)-1;
for(j=v=0;v<M;v++)
for(w=0;w++<M-v;JJ[w][v+w]=LL[w]);
pc1[n=1]=I[t][0]; //Primera pieza
Origen:
C1[n]=C[pc1[n]]; //Casilla de la pieza
if(!C1[n]) 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:
hh[n]=H[aa[n]][C1[n]][dd[n]];
if(!hh[n]) goto Direccion;
G[n]=aa[n]!=3;
Destino:
c2=C2[n]=N[c=C1[n]][dd[n]][G[n]]; //Casilla destino
x=pc2[n]=P[c2]; //Pieza en destino
if(T[x]==t) goto Direccion; //Mismo bando
if(aa[n]<2) //Peon
if(D[dd[n]]==T[x]){
if(x||B[c]+t+1) goto Direccion;
if(aa[n-1]>1||G[n-1]<2) goto Direccion;
if(c2+c2!=C1[n-1]+C2[n-1]) goto Direccion;
C3[n]--; //Simular captura al paso
pc3[n]=P[C2[n-1]];
C[pc3[n]]=P[C2[n-1]]=0;
}
else if(B[c]==t+1) A[pc1[n]]=6; //Promocion
if(aa[n]==2&&G[n]>1) //Enroque
if(x||K[c]>1) goto Direccion;
else{
for(d=0;d<8;d++)
if(A[x=P[N[c][d][h=0]]]==3&&T[x]!=t) d=8;
else while(h<H[6][c][d])
if(x=P[N[c][d][++h]])
if(T[x]!=t&&h<e[A[x]][d]) h=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];
}
Simulacion:
C[pc1[n]]=C2[n]; //Pieza a destino
C[pc2[n]]=0;
P[C2[n]]=pc1[n];
c=C[pr[t]];
for(d=0;d<8;d++)
if(A[x=P[N[c][d][h=0]]]==3&&T[x]!=t) d=8;
else while(h<H[6][c][d])
if(x=P[N[c][d][++h]])
if(T[x]!=t&&h<e[A[x]][d]) h=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(C3[n]<0){ //Al paso
C[pc3[n]]=C2[n-1];
P[C2[n-1]]=pc3[n];
C3[n]++;
}
if(C3[n]){ //Enroque
C[pc3[n]]=C3[n];
P[C3[n]]=pc3[n];
P[(R[t]+C2[n])/2]=C3[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]-(h=JJ[v][n+v]);
if(L[v++]!=(x>0)-(x<0)) continue;
JJ[v][n+v-1]=h;
if(v==4&&n<4) //Confirma secuencia
for(y=2;y--;)
for(w=n+1;w--;)
mm[y][0][n+3][w]=mm[y][1][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]=mm[y][0][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])
if(--A[pc1[n]]>2) goto Simulacion;
else A[pc1[n]]=t; //Subpromocion
if(!pc2[n]&&G[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[c][d][h=0]]]==3&&T[x]!=t) d=8;
else while(h<H[6][c][d])
if(x=P[N[c][d][++h]])
if(T[x]!=t&&h<e[A[x]][d]) h=d=8;
else break;
y=JJ[1][n]=d=d<9;
for(v=2;v<n;v++) //Secuencia de mate
if(aa[v]+1==B[C1[v]])
mm[y][n>4][n][v-2]=-100*A[pc1[v]]-C2[v];
else mm[y][n>4][n][v-2]=100*aa[v]+C2[v];
}
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);
h=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)) x=y;
else if(v==w&&S[x][3]>S[y][3]) x=y;
}
v=J[x][w=S[x][3]];
gotoxy(37-(t+M)%2*11,h+=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");
w=S[x][2]-1;
printf("%c[%u;32f%c%c",27,h,97+w%8,49+w/8);
S[x][3]=0;
n=552+2560*h;
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,h=4+2*M,26+11*t,M);
printf("%c[%u;30f%5.f%c",27,h,difftime(time(NULL),ti),34);
c1=c2=R[t]; //Casilla del rey en origen
while(kbhit()) getch();
Cursor:
if(c2){
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");
}
c=c1; //Casilla de origen
c2=x=0;
for(v=576;v--;)
if(*(Q[0]=Q[c]+i[v])==f[c]) *Q[0]=7+t;
while(!(E=getch()));
if(E==27&&!j) return 0;
if(E!=13){
if(E==77&&c1<64) c1++;
if(E==75&&c1>1) c1--;
if(E==72&&c1<57) c1+=8;
if(E==80&&c1>8) c1-=8;
for(v=576;v--;)
if(*(Q[0]=Q[c]+i[v])==7+t) *Q[0]=f[c];
if(y=(E=='+'&&M<8)-(E=='-'&&M>2)) M+=y;
else goto Cursor;
goto Inicio;
}
Eleccion:
if((x+=E!=75)>j||(x-=E==75)<1)
if(!c2) goto Cursor;
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-1)%8,49+(c2-1)/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>1){
n=586+2560*(h=4+2*--c);
y=m[x][c-1];
if(y>0) n-=34;
else y=-y;
w=y%100-1;
printf("%c[%u;32f%c%c",27,h,97+w%8,49+w/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
G[0]=L[t]*(c=c1-c2)/8;
if(a>6&&aa[0]<2)
if(c%8){
P[c]=C[P[c=C2[0]]]=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[c1]]=c2;
C[P[c2]]=0;
P[c2]=P[c1];
P[c1]=c=0;
for(v=576;v--;*(Q[c1]+i[v])=f[c1]);
a=A[P[c2]]; //Pieza
F[t][0]=f[c2];
for(v=576;v--;*(Q[c2]+i[v])=F[t][p[a][v]]);
if(a==2) if(c2-c1==2) c=c2+1;
else if(c1-c2==2) c=c2-2;
c2=(c1+c2)/2;
}while(c1=c);
t=!t;
goto Inicio;
}
Comentarios sobre la versión: 2.1 (0)
No hay comentarios