#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<conio.h>
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 icono[][5][13]={
" ' "," ' ' "," '''''''' "," ''' ''' ''' "," ''' ''' ",
" ''' "," '''' "," ' ' "," ' ''' ''' ' ","'' ''' ''' ''",
" ''' ' ''' "," ' ' "," ''''' ' "," ' ' ","'' ' ' ''",
"' ' ' ' '"," ' ' ' "," ' ' '"," ''''''''''' "," ' ' ' ' ",
"' ' ' ' ' '"," ' ' ' ' "," ' ' '"," ' ' "," ' '' '' ' ",
"' ' ' ' '"," ' ' ' "," ' ' ' ' '"," ' ' "," '' '' '' '' ",
"' ' ' '"," ' ' '","' ' ' ' '"," ' ' "," ' ' ' ' ' ",
" ' ' ' ' ","' '' ' '"," ' ' ' ' '"," ' ' "," ' ' ",
" ' ' "," '''' ' ' '"," ' ' '"," ' ' "," ' ' ",
" ''''''''' "," ' ' '"," ' ' ''"," ''''''' "," ''''''''' ",
" ' ' "," ' ' '"," ' ' '"," ' ' "," ' ' ",
"' '"," ' ' '"," ' ''' "," ' ' ","' '",
"'''''''''''''"," ''''''''''"," ''''' "," ''''''''''' ","'''''''''''''"};
char posicion[][6][8]={
"........","......T.","..R.....","........",".....C.r",".....dc.",
"..p.p...","..R.....","T.......",".......p",".....Ppp","t.cCa...",
"..T.CrP.","........","...Pr...","..A....D","........","pD.p.p..",
"..P.....","........","....p...",".P.p....","........","p..r....",
"...P....",".....p..","...PPA..",".....CC.","..D...p.","....T..C",
".p.A.P.C","......p.","...P.P..",".....r..","........","..p...P.",
".P..P...",".D...Cr.","........","........","........","..PP...p",
"RD......","........","........",".R......","a......R","R..A...."};
char far*Q,far*q=(char far*)0xA0000000;
char ii[]={".P.RCATD.prcatd"};
char e[7][8],p[8][576];
char H[512],h[4][512]; //Tipo,casilla+direccion=Alcance
char E,N[512][8]; //Origen+direccion,alcance=Destino
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 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 pc1[9],pc2[9],pc3[9]; //(0PB,1PN,2R,3C,4A,5T,6D)
int aa[9],dd[9],gg[9],hh[9]; //Rutina principal
int G[64][8],K[12][12],P[64],pr[2]; //Pieza del rey
int A[33]={7},C[33]={64},T[33]={2}; //Tipo,casilla,bando
int F[65]={1},X[]={1156,1188,1244}; //Tabulacion
int B[64],b[]={0,3,-1,4,4,-2,2,1}; //Promocion y al paso
int S[][3]={0,0,15,0,15,0,0,0,0}; //Fondo, perfil
int L[100][9],Y[9][9],l[100]; //Control de jugadas
int I1[]={1,33},I2[]={0,32}; //Blancas de 1 a I2[0]
int Z[9],M[100][9],m[9][9]; //Secuencia
int D[]={2,1,2,2,2,0,2,2}; //Peon al frente
int C1[9],C2[9],C3[9]; //Piezas
int V[9],W[9],i[576];
int g=14,y=57595,ply=4;
int a,c,d,j,n,t,v,w,x,c1,c2;
void asignar(f){
P[c]=x=a>6?--I1[y=1]:++I2[y=0];
if(f==2) pr[y]=x;
A[x]=f;
C[x]=c;
T[x]=y;
}
void borrar(f,f1){
for(g=f;g--;)
for(d=f1*!g;d<3-f1*!g;d++)
for(n=X[d]+5120*g+9920,v=15;v--;n-=320)
for(w=d==1?55:31;w--;*(q+n+w)=0);
}
void figura(f,f1,f2,f3){
S[f1][0]=f3;
Q=q+54720+f%8*24-f/8*7680;
for(w=576;w--;*(Q+i[w])=S[f1][p[f2][w]]);
}
void tablero(f){
if(f%8) F[f+1]=F[f]=4-F[f-1];
figura(f,T[P[f]],A[P[f]],F[f]);
}
void jugada(f,f1){
n=5120*f+f1/448*34+10792;
a=f1%448/64;
c2=f1%64;
for(v=a>1?13:0;v--;n-=320)
for(w=13;w--;*(q+n+w)=icono[v][a-2][w]-32);
gotoxy(32,4+2*f);
if(f1) printf("%c%u",97+c2%8,1+c2/8);
}
void total(f,f1,f2){
gotoxy(26+11*f1,4+2*g);
if(f>1) printf("%3u",f/2);
else printf(f?"1/2":t==f2%2?"0-1":"1-0");
}
void jaque(f){
c=C[pr[f]];
for(v=d=0;d<8;d++,v=0)
if(h[3][g=G[c][d]]&&A[w=P[N[g][0]]]==3&&T[w]!=f) d=8;
else while(H[g]>v++)
if(w=P[N[g][v]])
if(T[w]!=f&&v<e[A[w]][d]) d=v=8;
else break;
}
int main(){
_AX=0x13;
geninterrupt(0x10);
time_t ti;
while(i[ii[g]]=g--);
for(v=20;v--;w=v%2,V[v]=w-!w,W[v]=448*!w);
printf("%c[2;26fBlancas Negras",27);
for(w=2,v=12;v--;w=2,y-=5000)
do{
x=!v&&w?1219:X[w]+5120*v-1;
for(n=16;n--;*(q+x+320*n)=*(q+y+320*n)=7);
for(n=y+1-x,y=x;n--;*(q+x+n)=*(q+x+n+5120)=7);
}while(w--);
for(a=7;a--;)
for(d=d1[a];d<=d2[a];d+=d3[a])
if(a>3) e[a][d]=8;
else if(a>1) e[2][d]=6-2*a;
else if(D[d]>1) e[!a][d]=2;
for(c=63,v=0;v<8;v++)
for(w=8;w--;K[9-v][w+2]=c+1,B[c--]=b[v])
if(a=i[posicion[v][2][w]]) //posicion[v][?=0,1,...][w]
asignar(--a%7);
for(v=w=y=0;y<24;y++,w+=296)
for(x=0;x<24;x++,i[v++]=w++)
if(y>1&&y<22&&x>1&&x<22)
for(n=6;n--;)
if(a=pieza[y-2][n][x-2]-32)
p[n+1][v]=p[n+1-!n][v]=a-16;
for(d=7,c=0;c<64;tablero(c++),d=7)
do{
G[c][d]=++g;
x=c%8+2;
y=c/8+2;
if(j=K[y+iy[d+8]][x+ix[d+8]])
N[g][h[3][g]--]=j-1;
while(j=K[y+=iy[d]][x+=ix[d]])
N[g][++H[g]]=j-1;
for(a=H[g]?3:0;a--;h[a][g]++)
h[a][g]=a<2&&B[c]==a+2&&d==4*a+1;
}while(d--);
Inicio:
ti=time(NULL)-1;
for(j=v=0;v<ply;v++)
for(w=0;ply-v>w++;Y[w][v+w]=W[w]);
pc1[n=1]=I1[t]; //Primera pieza
Origen:
c=C1[n]=C[pc1[n]]; //Casilla de la pieza
if(c>63) goto Siguiente;
P[c]=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:
c=C2[n]=N[gg[n]][Z[n]]; //Casilla destino
if(pc2[n]=P[c])
if(T[P[c]]==t) goto Direccion;
if(aa[n]!=t) goto Simulacion;
if(D[dd[n]]!=T[P[c]]) A[pc1[n]]=B[c]==t?6:t; //Promocion
else{ // Al paso
if(P[c]||B[c]+t+1||aa[x=n-1]>1) goto Direccion;
if(Z[x]<2||c+c!=C1[x]+C2[x]) goto Direccion;
c=C3[n]=C2[x];
pc3[n]=P[c];
C[P[c]]=64;
P[c]=0;
}
Simulacion:
if(pc2[n]) C[pc2[n]]=64;
C[pc1[n]]=C2[n]; //Pieza a destino
P[C2[n]]=pc1[n];
jaque(t);
if(d<9)
if(n<ply)
switch(n){
case 5: jaque(1);
if(d<9) break;
default: pc1[++n]=I1[t=!t];
goto Origen;
}
else Y[1][n]+=2;
Restaurar:
P[C2[n]]=pc2[n];
if(pc2[n]) C[pc2[n]]=C2[n];
if(pc3[n]){
C[pc3[n]]=C3[n];
P[C3[n]]=pc3[n];
pc3[n]=0;
}
if(A[pc1[n]]>aa[n])
if(--A[pc1[n]]<3) A[pc1[n]]=t;
else goto Simulacion;
if(!pc2[n]&&hh[n]>Z[n]++) goto Destino;
Direccion:
dd[n]+=d3[aa[n]];
if(dd[n]<=d2[aa[n]]) goto Alcance;
C[pc1[n]]=C1[n]; //Restaura origen
P[C1[n]]=pc1[n];
Siguiente:
if(I2[t]>pc1[n]++) goto Origen; //Pieza siguiente
if(!Y[1][x=n--])
if(t){
jaque(1);
if(d<9) Y[1][x]=t=n<2;
else if(Y[n-n%2][y=x])
while(--y>1)
a=A[pc1[y]],
m[n][y-1]=C2[y]+64*a+W[a==aa[y]];
if(!t) goto Restaurar;
}
Y[1][n]+=2;
switch(n){
case 0: goto Plasmar;
case 1: for(x=1;ply>x++;x=v<2?ply:x)
v=L[j][l[j]=y]=Y[y=x-1][x];
L[j][y+1]=448-v;
L[j][0]=y+(y+x)%2;
while(--y) M[j][y+1]=!v?m[l[j]][y]:0;
a=A[pc1[1]];
M[j][1]=C2[1]+64*a+W[a==aa[1]];
M[j++][0]=C1[1];
break;
default: for(y=0;ply-n>y++;x++)
if(v=Y[y+1][x]-Y[y][x])
if(V[y]==(v>0)-(v<0)) Y[y+1][x]-=v;
}
for(v=n;v<ply;v++) //Reinicio de contadores
for(w=0;ply-v>w++;Y[w][v+w]=W[w]);
t=!t;
goto Restaurar;
Plasmar:
borrar(11,0);
total(j?j+j:Y[1][1],t,g=0);
gotoxy(26+11*t,4+2*ply);
printf("N=%u",g=ply);
gotoxy(30,4+2*g);
printf("%5.f%c",difftime(time(NULL),ti),34);
if(y=j)
do{ //Ordenar maximos o minimos
for(c=x=j;x--;)
if(l[x]>0)
if(c==j) c=x;
else if(v=L[c][L[c][0]]-L[x][L[x][0]])
c=V[ply]==(v>0)-(v<0)?c:x;
x=l[c]*=x;
if(++g>10) continue;
jugada(g,M[c][1]);
total(L[c][-x],(!t+ply)%2,-x%2);
}while(--y);
c1=4+56*t;
Teclado:
figura(c2=c1,T[x],A[x=P[c1]],7+t);
while(E=kbhit()) getch();
while(!E) E=getch();
if(E==27&&getch()==E) return 0;
if(E==75&&c1) c1--;
if(E==80&&c1>7) c1-=8;
if(E==77&&c1<63) c1++;
if(E==72&&c1<56) c1+=8;
if(c=E!=13) figura(c2,T[x],A[x],F[c2]);
ply+=d=(E=='+'&&ply<6-t)-(E=='-'&&ply>2);
if(d) goto Inicio;
if(c--) goto Teclado;
Eleccion:
c+=V[E!=75];
if(c==j||c<0)
if(c1==c2) goto Teclado;
else c=c<0?j-1:0;
if(M[c][0]!=c1) goto Eleccion;
for(g=1-l[c];--g;)
jugada(g-1,M[c][g]),
total(L[c][g],(g+t)%2,g%2);
figura(c2,t,a,8-t);
do E=getch();
while(!E);
borrar(ply,1);
figura(c2,T[y],A[y=P[c2]],F[c2]);
if(E==27) goto Teclado;
if(E!=13) goto Eleccion;
Z[0]=(c2-c1)/8*V[!t]; //Posibilitar al paso
if(a==t&&(c2-c1)%8) //Al paso
if(!y){
figura(c,2,7,F[c=C2[0]]);
C[P[c]]=64;
P[c]=0;
}
figura(c1,2,7,F[c1]);
figura(c2,t,a,F[c2]);
aa[0]=A[x]=a; //Promocion
C1[0]=c1;
C2[0]=c2;
C[x]=c2;
C[y]=64;
P[c1]=0;
P[c2]=x;
t=!t;
ply-=ply>2;
goto Inicio;
}