📄 bwchess.c
字号:
curr=r;curc=c;
if(canputchess(curr,curc)==1){
temp=1;
putchess(curr,curc,curchess);
drawedge(curr,curc,curchess);
wccount=0;bccount=0;
for(i=1;i<9;i++)
for(j=1;j<9;j++)
if(chess[i][j].flag==1){
if(chess[i][j].color==WC)wccount++;
else bccount++;
}
printscore();
}
}
if(temp==0)
if(curchess==WC)curchess=BC;
else curchess=WC;
}
/*可不可以放棋,0:不可放,1:可放*/
int canputchess(int r,int c)
{
int temp;
int tr,tc;
int i,j;
temp=0;
if(chess[r][c].flag==1)return(temp);/*此处有棋了*/
else{/*此处尚未有棋*/
{/*向上找*/
i=1;
while(chess[r-i][c].flag==1&&chess[r-i][c].color!=curchess&&(r-i)>0)i++;
if(i!=1){
if(chess[r-i][c].flag==1&&chess[r-i][c].color==curchess&&(r-i)>0){
temp=1;
for(j=r;j>=r-i;j--)
drawchessonly(j,c,curchess);
}
}
}
{/*向左找*/
i=1;
while(chess[r][c-i].flag==1&&chess[r][c-i].color!=curchess&&(c-i)>0)i++;
if(i!=1){
if(chess[r][c-i].flag==1&&chess[r][c-i].color==curchess&&(c-i)>0){
temp=1;
for(j=c;j>=c-i;j--)
drawchessonly(r,j,curchess);
}
}
}
{/*向下找*/
i=1;
while(chess[r+i][c].flag==1&&chess[r+i][c].color!=curchess&&(r+i)<9)i++;
if(i!=1){
if(chess[r+i][c].flag==1&&chess[r+i][c].color==curchess&&(r+i)<9){
temp=1;
for(j=r;j<=r+i;j++)
drawchessonly(j,c,curchess);
}
}
}
{/*向右找*/
i=1;
while(chess[r][c+i].flag==1&&chess[r][c+i].color!=curchess&&(c+i)<9)i++;
if(i!=1){
if(chess[r][c+i].flag==1&&chess[r][c+i].color==curchess&&(c+i)<9){
temp=1;
for(j=c;j<=c+i;j++)
drawchessonly(r,j,curchess);
}
}
}
{/*向左上找*/
i=1;
while(chess[r-i][c-i].flag==1&&chess[r-i][c-i].color!=curchess&&(r-i)>0&&(c-i)>0)i++;
if(i!=1){
if(chess[r-i][c-i].flag==1&&chess[r-i][c-i].color==curchess&&(r-i)>0&&(c-i)>0){
temp=1;
tr=r;tc=c;
while(tr>=r-i&&tc>=c-i)
drawchessonly(tr--,tc--,curchess);
}
}
}
{/*向右下找*/
i=1;
while(chess[r+i][c+i].flag==1&&chess[r+i][c+i].color!=curchess&&(r+i)<9&&(c+i)<9)i++;
if(i!=1){
if(chess[r+i][c+i].flag==1&&chess[r+i][c+i].color==curchess&&(r+i)<9&&(c+i)<9){
temp=1;
tr=r;tc=c;
while(tr<=r+i&&tc<=c+i)
drawchessonly(tr++,tc++,curchess);
}
}
}
{/*向左下找*/
i=1;
while(chess[r+i][c-i].flag==1&&chess[r+i][c-i].color!=curchess&&(r+i)<9&&(c-i)>0)i++;
if(i!=1){
if(chess[r+i][c-i].flag==1&&chess[r+i][c-i].color==curchess&&(r+i)<9&&(c-i)>0){
temp=1;
tr=r;tc=c;
while(tr<=r+i&&tc>=c-i)
drawchessonly(tr++,tc--,curchess);
}
}
}
{/*向右上找*/
i=1;
while(chess[r-i][c+i].flag==1&&chess[r-i][c+i].color!=curchess&&(r-i)>0&&(c+i)<9)i++;
if(i!=1){
if(chess[r-i][c+i].flag==1&&chess[r-i][c+i].color==curchess&&(r-i)>0&&(c+i)<9){
temp=1;
tr=r;tc=c;
while(tr>=r-i&&tc<=c+i)
drawchessonly(tr--,tc++,curchess);
}
}
}
return(temp);
}
}
/*若已经无法下子了,则自动下棋*/
void autochess()
{
int r,c,i,j,temp=0;
if(curchess==WC)curchess=BC;
else curchess=WC;
if(canputcurchess()==1)
drawedge(curr,curc,curchess);
else{
if(curchess==WC)curchess=BC;
else curchess=WC;
for(r=1;r<=8&&temp==0;r++)
for(c=1;c<=8&&temp==0;c++)
if(chess[r][c].flag==0){
putchess(r,c,curchess);
drawedge(r,c,curchess);
wccount=0;bccount=0;
for(i=1;i<9;i++)
for(j=1;j<9;j++)
if(chess[i][j].flag==1){
if(chess[i][j].color==WC)wccount++;
else bccount++;
}
printscore();
temp=1;
}
}
}
/*下棋等待按键*/
void waitkey()
{
int temp,i,j;
while((curkey=specialkey())!=ESC)
switch(curkey){
case UP:
drawedge(curr,curc,TABLECOLOR);
if(curr==1)curr=8;
else curr--;
drawedge(curr,curc,curchess);
break;
case DOWN:
drawedge(curr,curc,TABLECOLOR);
if(curr==8)curr=1;
else curr++;
drawedge(curr,curc,curchess);
break;
case LEFT:
drawedge(curr,curc,TABLECOLOR);
if(curc==1)curc=8;
else curc--;
drawedge(curr,curc,curchess);
break;
case RIGHT:
drawedge(curr,curc,TABLECOLOR);
if(curc==8)curc=1;
else curc++;
drawedge(curr,curc,curchess);
break;
case SPACE:/*放子*/
if(canputchess(curr,curc)==1){
putchess(curr,curc,curchess);
if(ptocflag==0) drawedge(curr,curc,curchess);
wccount=0;bccount=0;
for(i=1;i<9;i++)
for(j=1;j<9;j++)
if(chess[i][j].flag==1){
if(chess[i][j].color==WC)wccount++;
else bccount++;
}
printscore();
if(ptocflag==1){
drawedge(curr,curc,TABLECOLOR);
computerput();
}
if(canputcurchess()==0&&ptocflag==0)/*当前颜色的棋子没地方放了*/
autochess();
if(wccount+bccount>=TOTALCHESS)
callend();
}else printf("\7");/*报警*/
break;
}
callend();
menuselect();
}
/*比赛结束,给出结果*/
void callend()
{
int tx=430,ty=390;
Byte *result[]={"比赛结果:","黑棋胜","白棋胜","平局"};
int size=10;
flag16=open("hzk16",O_RDWR|O_BINARY);
if(flag16==-1){printf("Can not Open HZK16\n");exit(0);}
out16hz(tx,ty,size,LIGHTBLUE,*(result+0));
if(wccount==bccount) out16hz(tx+130,ty,size,WHITE,*(result+3));
if(wccount>bccount) out16hz(tx+130,ty,size,WHITE,*(result+2));
if(wccount<bccount) out16hz(tx+130,ty,size,WHITE,*(result+1));
close(flag16);
menuselect();
}
/*显示16点阵汉字*/
int out16hz(int x,int y,int z,int color ,char *p)
{
Word num,QM,WM,flag=0;
int record,i,j,k;
long pointer;
char Bit[32];
while((num=*p++)!=0)
{
if (num>0xa1)
if(flag==0){
QM=(num-0xa1)&0x07f;
flag=1;
}
else{
WM=(num-0xa1)&0x07f;
flag=0;
record=QM*94+WM;
pointer=record*32L;
lseek(flag16,pointer,SEEK_SET);
read(flag16,Bit,32);
for(i=0;i<16;i++)
for(j=0;j<2;j++)
for(k=0;k<8;k++)
if (convert(Bit[i*2+j],7-k))
putpixel(x+j*8+k,y+i,color);
x=x+z+16;
}
}
}
/*位转换*/
int convert(Byte ch,int n)
{
return((ch>>n)&0x01);
}
/*菜单选择*/
void menuselect()
{
int ck;
clearmenubox();
drawmenucur(curmenunum);
while(ck=specialkey())
switch(ck){
case UP:
clearmenubox();
if(curmenunum==1)curmenunum=3;
else curmenunum--;
drawmenucur(curmenunum);
break;
case DOWN:
clearmenubox();
if(curmenunum==3)curmenunum=1;
else curmenunum++;
drawmenucur(curmenunum);
break;
case ENTER:
selectmenunum=curmenunum;
clearmenubox();
drawmenuselect(selectmenunum);
drawbox(420,380,630,420,BLACK);
switch(selectmenunum){
case 1:/*人机大战*/
ptocflag=1;
ptoc();
break;
case 2:/*二人对战*/
ptocflag=0;
ptop();
break;
case 3:/*退出*/
exit(0);
}
}
}
/*二人对战*/
void ptop()
{
resettable();
printinfbw();
autoput();
drawmenuselect(selectmenunum);
waitkey();
}
/*人机大战*/
void ptoc()
{
ptop();
}
/*重新下棋*/
void resettable()
{
wccount=0;bccount=0;
setcolor(YELLOW);
drawtable(tablex0,tabley0);
}
/******************************************************************************/
/*主程序*/
main()
{
int driver,mode;
int tdet,titlex,titley;
int authorx,authory;
driver=DETECT;
mode=0;
initgraph(&driver,&mode,"");
/*初始化*/
setbkcolor(BKCOLOR);
tdet=150;
titlex=50;titley=20;
/*显示标题信息*/
drawmat(titleh,64,titlex,titley,YELLOW);
drawmat(titleb,64,titlex+tdet,titley,YELLOW);
drawmat(titleq,64,titlex+tdet*2,titley,YELLOW);
authorx=450;authory=60;
drawmat(author1,24,authorx,authory,LIGHTBLUE);
drawmat(author2,24,authorx+24,authory,LIGHTBLUE);
drawmat(author3,24,authorx+24*2,authory,LIGHTBLUE);
drawmat(authorn1,24,authorx+24*3,authory,WHITE);
drawmat(authorn2,24,authorx+24*3+24,authory,WHITE);
drawmat(authorn3,24,authorx+24*3+24*2,authory,WHITE);
tablex0=70;tabley0=110;/*棋盘的左上角坐标*/
printinfbw();/*初始化菜单与信息框*/
printscore();/* 显示当前黑白棋的个数分另为多少 */
resettable();/* 重置棋盘 */
menuselect();/* 菜单选择 */
closegraph();
}
/******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -