#include<stdio.h>
#include<graphics.h>
#define H (y-100)/10
#define L (x-100)/10
int Mark[20][20];
int Change=2;
void InitMark()
{int i,j;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
Mark[i][j]=0;
}
void Screen()
{int i;
settextstyle(0, 0, 2);
outtextxy(50,50,"BLACK WHITE CHESS");
settextstyle(0,0,1);
outtextxy(4,354,"ESC -->OVER!!");
outtextxy(4,364,"A -->Try Again!!");
setcolor(1);
rectangle(100,100,300,300);
rectangle(1,350,170,450);
for(i=1;i<21;i++)
{line(100+10*i,100,100+10*i,300);
line(100,100+10*i,300,100+10*i);
}
}
void PicBlock(int x,int y)
{if(Change==1) setcolor(RED);
else setcolor(YELLOW);
line(x+1,y+1,x+1,y+3);line(x+9,y+1,x+9,y+3);
line(x+1,y+9,x+1,y+7);line(x+9,y+9,x+9,y+7);
line(x+1,y+1,x+3,y+1);line(x+1,y+9,x+3,y+9);
line(x+9,y+1,x+7,y+1);line(x+9,y+9,x+7,y+9);
}
void CoverBlock(int x,int y)
{setcolor(BLACK);
line(x+1,y+1,x+1,y+3);line(x+9,y+1,x+9,y+3);
line(x+1,y+9,x+1,y+7);line(x+9,y+9,x+9,y+7);
line(x+1,y+1,x+3,y+1);line(x+1,y+9,x+3,y+9);
line(x+9,y+1,x+7,y+1);line(x+9,y+9,x+7,y+9);
}
void DrawBlack(int x,int y)
{
setcolor(RED);
line(x+5,y+3,x+2,y+7);
line(x+2,y+7,x+8,y+7);
line(x+8,y+7,x+5,y+3);
}
void DrawWhite(int x,int y)
{setcolor(YELLOW);
circle(x+5,y+5,3);
}
void CoverPic(int x,int y)
{setfillstyle(1,BLACK);
bar(x+2,y+2,x+8,y+8);
}
void Score()
{int i,j,M,a,b;
a=0;b=0;
DrawBlack(370,198);
DrawWhite(370,219);
setfillstyle(1,BLACK);
bar(419,189,449,229);
setcolor(WHITE);
rectangle(350,170,450,230);
settextstyle(0, 0, 1);
outtextxy(352,190,"Play I:");
outtextxy(352,200,"VS");
outtextxy(352,210,"Play II:");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{ if(Mark[i][j]==1) a++;
if(Mark[i][j]==2) b++;
}
j=0;
M=a;
for(i=0;i<2;i++)
{
switch (M/100)
{case 0:outtextxy(420,190+20*j,"0");break;
case 1:outtextxy(420,190+20*j,"1");break;
case 2:outtextxy(420,190+20*j,"2");break;
case 3:outtextxy(420,190+20*j,"3");break;
case 4:outtextxy(420,190+20*j,"4");break;
}
switch (M%100/10)
{case 0:outtextxy(430,190+20*j,"0");break;
case 1:outtextxy(430,190+20*j,"1");break;
case 2:outtextxy(430,190+20*j,"2");break;
case 3:outtextxy(430,190+20*j,"3");break;
case 4:outtextxy(430,190+20*j,"4");break;
case 5:outtextxy(430,190+20*j,"5");break;
case 6:outtextxy(430,190+20*j,"6");break;
case 7:outtextxy(430,190+20*j,"7");break;
case 8:outtextxy(430,190+20*j,"8");break;
case 9:outtextxy(430,190+20*j,"9");break;
}
switch (M%10)
{case 0:outtextxy(440,190+20*j,"0");break;
case 1:outtextxy(440,190+20*j,"1");break;
case 2:outtextxy(440,190+20*j,"2");break;
case 3:outtextxy(440,190+20*j,"3");break;
case 4:outtextxy(440,190+20*j,"4");break;
case 5:outtextxy(440,190+20*j,"5");break;
case 6:outtextxy(440,190+20*j,"6");break;
case 7:outtextxy(440,190+20*j,"7");break;
case 8:outtextxy(440,190+20*j,"8");break;
case 9:outtextxy(440,190+20*j,"9");break;
}
M=b;j++;
}
}
void MoveBlock(int x,int y)
{int k,Counter;
Counter=0;/*record the path walk*/
while(1)
{k=getch();
if(k==77&&x<290) {CoverBlock(x,y);x+=10;PicBlock(x,y);}
else if(k==75&&x>100) {CoverBlock(x,y);x-=10;PicBlock(x,y);}
else if(k==72&&y>100) {CoverBlock(x,y);y-=10;PicBlock(x,y);}
else if(k==80&&y<290) {CoverBlock(x,y);y+=10;PicBlock(x,y);}
else if(k==13&&Mark[H][L]==0)
{Counter=PreControl(x,y);
if(Change==1&&Counter)
{DrawBlack(x,y);
Mark[H][L]=1;
AftControl(Counter,x,y);
Change++;Counter=0;
Score();
}
else if(Change==2&&Counter)
{DrawWhite(x,y);
Mark[H][L]=2;
AftControl(Counter,x,y);
Change--;Counter=0;
Score();
}
}
else if(k==27) break;
delay(1000);
}
}
int PreControl(int x,int y)
{int TT,X,Y;
int T;
T=0;TT=1;X=x;Y=y;
while(x<290)/*right*/
{x+=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1) {T+=1;break;}
TT++;
}
TT=1;x=X;y=Y;
while(x<290&&y>100)/*right and up*/
{x+=10;y-=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1) {T+=2;break;}
TT++;
}
TT=1;x=X;y=Y;
while(y>100) /*up*/
{y-=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1){T+=4;break;}
TT++;
}
TT=1;x=X;y=Y;
while(x>100&&y>100) /*up and left*/
{x-=10;y-=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1){T+=8;break;}
TT++;
}
TT=1;x=X;y=Y;
while(x>100)/*left*/
{x-=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1){T+=16;break;}
TT++;
}
TT=1;x=X;y=Y;
while(x>100&&y<290)/*left and down*/
{x-=10;y+=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1){T+=32;break;}
TT++;
}
TT=1;x=X;y=Y;
while(y<290)/*down*/
{y+=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1){T+=64;break;}
TT++;
}
TT=1;x=X;y=Y;
while(x<290&&y<290)/*down and right*/
{x+=10;y+=10;
if(Mark[H][L]==0||(Mark[H][L]==Change&&TT==1)) break;
if(Mark[H][L]==Change&&TT!=1) {T+=128;break;}
TT++;
}
return T;
}
int AftControl(int Counter,int x,int y)
{int X,Y;
X=x;Y=y;
if(Counter-128>=0)
{Counter-=128;
x+=10;y+=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x+=10;y+=10;
}
}
x=X;y=Y;
if(Counter-64>=0)
{Counter-=64;
y+=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
y+=10;
}
}
x=X;y=Y;
if(Counter-32>=0)
{Counter-=32;
x-=10;y+=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x-=10;y+=10;
}
}
x=X;y=Y;
if(Counter-16>=0)
{Counter-=16;
x-=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x-=10;
}
}
x=X;y=Y;
if(Counter-8>=0)
{Counter-=8;
x-=10;y-=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x-=10;y-=10;
}
}
x=X;y=Y;
if(Counter-4>=0)
{Counter-=4;
y-=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
y-=10;
}
}
x=X;y=Y;
if(Counter-2>=0)
{Counter-=2;
x+=10;y-=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x+=10;y-=10;
}
}
x=X;y=Y;
if(Counter-1>=0)
{Counter-=1;
x+=10;
while(Mark[H][L]!=Change)
{CoverPic(x,y);
if(Change==1) DrawBlack(x,y);
else DrawWhite(x,y);
Mark[H][L]=Change;
x+=10;
}
}
if(Counter==0) outtextxy(100,320,"ok!");
else outtextxy(350,100,"wrong!");
}
void main()
{int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"");
Screen();
DrawBlack(190,200);Mark[10][9]=1;
DrawWhite(200,200);Mark[10][10]=2;
DrawBlack(200,210);Mark[11][10]=1;
Score();
PicBlock(200,200);
MoveBlock(200,200);
InitMark();
closegraph();
}