中国象棋源代码C语言小程序.docx
《中国象棋源代码C语言小程序.docx》由会员分享,可在线阅读,更多相关《中国象棋源代码C语言小程序.docx(17页珍藏版)》请在冰豆网上搜索。
![中国象棋源代码C语言小程序.docx](https://file1.bdocx.com/fileroot1/2023-6/20/8e4c8724-ad34-4012-80fe-645f318371d3/8e4c8724-ad34-4012-80fe-645f318371d31.gif)
中国象棋源代码C语言小程序
中国象棋源代码-C语言小程序
*--------------------chess.c----------------------*/
#include"dos.h"
#include"stdio.h"
/*----------------------------------------------------*/
#defineRED7
#defineBLACK14
#definetrue1
#definefalse0
#defineSELECT0
#defineMOVE1
#defineRED_UP0x1100
#defineRED_DOWN0x1f00
#defineRED_LEFT0x1e00
#defineRED_RIGHT0x2000
#defineRED_DO0x3900
#defineRED_UNDO0x1000
#defineBLACK_UP0x4800
#defineBLACK_DOWN0x5000
#defineBLACK_LEFT0x4b00
#defineBLACK_RIGHT0x4d00
#defineBLACK_DO0x1c00
#defineBLACK_UNDO0x2b00
#defineESCAPE0x0100
#defineRED_JU1
#defineRED_MA2
#defineRED_XIANG3
#defineRED_SHI4
#defineRED_JIANG5
#defineRED_PAO6
#defineRED_BIN7
#defineBLACK_JU8
#defineBLACK_MA9
#defineBLACK_XIANG10
#defineBLACK_SHI11
#defineBLACK_JIANG12
#defineBLACK_PAO13
#defineBLACK_BIN14
/*----------------------------------------------------*/
intfirsttime=1;
intsavemode;
charpage_new=0,page_old=0;
intfinish=false,turn=BLACK,winner=0;
intkey;
intredstate=SELECT,blackstate=SELECT;
intboard[10][9];
/*----------------------------------------------------*/
char*chessfile[15]={"",
"bmp\\rju.wfb","bmp\\rma.wfb","bmp\\rxiang.wfb","bmp\\rshi.wfb",
"bmp\\rjiang.wfb","bmp\\rpao.wfb","bmp\\rbin.wfb",
"bmp\\bju.wfb","bmp\\bma.wfb","bmp\\bxiang.wfb","bmp\\bshi.wfb",
"bmp\\bjiang.wfb","bmp\\bpao.wfb","bmp\\bbin.wfb"
};
char*boardfile[10][9]={
{"bmp\\11.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\14.wfb","bmp\\15.wfb","bmp\\16.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\19.wfb"},
{"bmp\\21.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\24.wfb","bmp\\25.wfb","bmp\\26.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\29.wfb"},
{"bmp\\21.wfb","bmp\\3a.wfb","bmp\\3t.wfb","bmp\\34.wfb","bmp\\3t.wfb","bmp\\36.wfb","bmp\\3t.wfb","bmp\\3a.wfb","bmp\\29.wfb"},
{"bmp\\41.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},
{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\56.wfb","bmp\\5t.wfb","bmp\\58.wfb","bmp\\59.wfb"},
{"bmp\\61.wfb","bmp\\62.wfb","bmp\\6t.wfb","bmp\\64.wfb","bmp\\6t.wfb","bmp\\66.wfb","bmp\\6t.wfb","bmp\\68.wfb","bmp\\69.wfb"},
{"bmp\\71.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},
{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\86.wfb","bmp\\8t.wfb","bmp\\8a.wfb","bmp\\89.wfb"},
{"bmp\\91.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\95.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},
{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb","bmp\\106.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}
};
charcursor[14][14]={
0,0,0,0,0,0,0,0,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,1,1,1,
0,255,255,255,255,255,255,255,0,0,1,1,1,1,
0,255,255,255,255,255,255,0,0,1,1,1,1,1,
0,255,255,255,255,255,255,0,0,1,1,1,1,1,
0,255,255,255,255,255,255,255,0,0,1,1,1,1,
0,255,255,255,255,255,255,255,255,0,0,1,1,1,
0,255,255,255,255,255,255,255,255,255,0,0,1,1,
0,255,255,0,255,255,255,255,255,255,255,0,0,1,
0,255,0,1,1,0,255,255,255,255,255,255,255,0,
0,0,1,1,1,1,0,255,255,255,255,255,0,1,
0,1,1,1,1,1,1,0,255,255,255,0,1,1,
1,1,1,1,1,1,1,1,0,255,0,1,1,1,
1,1,1,1,1,1,1,1,1,0,1,1,1,1
};
structpos{
intx;
inty;
}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos;
/*----------------------------------------------------*/
selectpage(registercharpage)/*换页函数*/
{
unionREGSr;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page;/*选择页面*/
int86(0x10,&r,&r);
}
unsignedcharset_SVGA_mode(intvmode)/*设置SVGA屏幕模式*/
{
unionREGSr;
r.x.ax=0x4f02;
printf("Error!
Can'topenfile!
");
getch();
return;
}
fseek(fp,28,SEEK_SET);
fread(&i,2,1,fp);
if(i!
=8)/*检查是否为256色位图*/
{
puts("Error!
Can'tfindbitmap!
");
fclose(fp);
getch();
exit(0);
}
fseek(fp,18,SEEK_SET);
fread(&width,4,1,fp);
fread(&length,4,1,fp);
if(firsttime)
{
fseek(fp,54,SEEK_SET);
for(i=0;i<256;i++)/*按照该图片的DAC色表设置色彩寄存器*/
{
b=fgetc(fp);g=fgetc(fp);r=fgetc(fp);/*获取R、G、B分量*/
outportb(0x3c8,i);
outportb(0x3c9,r>>2);/*右移是要转化为VGA的6位寄存器形式*/
outportb(0x3c9,g>>2);
outportb(0x3c9,b>>2);
fgetc(fp);
}
}
else
fseek(fp,300,SEEK_SET);
k=(width%4)?
(4-width%4):
0;/*宽度修正值*/
for(j=length-1+start_x;j>=start_x;j--)
{
fread(buffer,width,1,fp);
for(i=start_y,n=0;i{
position=j*640l+i;/*计算要显示点的显存位置*/
page_new=position/65536;/*计算显示页*/
if(page_new!
=page_old)/*当显示页不同时更换页面,提高一定的输出速度*/
{selectpage(page_new);page_old=page_new;}
pokeb(0xa000,position%65536,buffer[n]);/*写到显存位置*/
}
fseek(fp,k,SEEK_CUR);/*每行绘制完后修正宽度*/
}
fclose(fp);
}
init()
{
savemode=get_SVGA_mode();/*先保存原来的屏幕模式*/
set_SVGA_mode(0x101);/*硬件无关性初始化屏幕为640*480256色模式*/
}
end()
{
set_SVGA_mode(savemode);/*恢复屏幕*/
}
/*----------------------------------------------------*/
initpos()
{
inti,j;
for(i=0;i<10;i++)
for(j=0;j<9;j++)
{
position[i][j].x=35+i*39;
position[i][j].y=43+j*40;
}
}
initchessmap()
{
board[0][0]=BLACK_JU;
board[0][1]=BLACK_MA;
board[0][2]=BLACK_XIANG;
board[0][3]=BLACK_SHI;
board[0][4]=BLACK_JIANG;
board[0][5]=BLACK_SHI;
board[0][6]=BLACK_XIANG;
board[0][7]=BLACK_MA;
board[0][8]=BLACK_JU;
board[2][1]=BLACK_PAO;
board[2][7]=BLACK_PAO;
board[3][0]=BLACK_BIN;
board[3][2]=BLACK_BIN;
board[3][4]=BLACK_BIN;
board[3][6]=BLACK_BIN;
board[3][8]=BLACK_BIN;
board[9][0]=RED_JU;
board[9][1]=RED_MA;
board[9][2]=RED_XIANG;
board[9][3]=RED_SHI;
board[9][4]=RED_JIANG;
board[9][5]=RED_SHI;
board[9][6]=RED_XIANG;
board[9][7]=RED_MA;
board[9][8]=RED_JU;
board[7][1]=RED_PAO;
board[7][7]=RED_PAO;
board[6][0]=RED_BIN;
board[6][2]=RED_BIN;
board[6][4]=RED_BIN;
board[6][6]=RED_BIN;
board[6][8]=RED_BIN;
}
initdrawchess()
{
inti,j;;
for(i=0;i<10;i++)
for(j=0;j<9;j++)
{
if(board[i][j])
drawbmp(position[i][j].x,position[i][j].y,chessfile[board[i][j]]);
}
}
drawcursor(structposp)
{
inti,j,n,m,x,y;
longthisposition;
x=position[p.x][p.y].x+20;
y=position[p.x][p.y].y+25;
for(j=13-1+x,m=13;j>=x;j--,m--)
{
for(i=y,n=0;i<13+y;i++,n++)
{
thisposition=j*640l+i;/*计算要显示点的显存位置*/
page_new=thisposition/65536;/*计算显示页*/
if(page_new!
=page_old)/*当显示页不同时更换页面,提高一定的输出速度*/
{selectpage(page_new);page_old=page_new;}
if(cursor[m][n]!
=1)
if(cursor[m][n]==0)
pokeb(0xa000,thisposition%65536,0);
else
if(turn==RED)
pokeb(0xa000,thisposition%65536,153);
else
pokeb(0xa000,thisposition%65536,255);
}
}
}
drawselecursor(structposp)
{
inti,j,n,m,x,y;
longthisposition;
x=position[p.x][p.y].x+20;
y=position[p.x][p.y].y+25;
for(j=13-1+x,m=13;j>=x;j--,m--)
{
for(i=y,n=0;i<13+y;i++,n++)
{
thisposition=j*640l+i;/*计算要显示点的显存位置*/
page_new=thisposition/65536;/*计算显示页*/
if(page_new!
=page_old)/*当显示页不同时更换页面,提高一定的输出速度*/
{selectpage(page_new);page_old=page_new;}
if(cursor[m][n]!
=1)
pokeb(0xa000,thisposition%65536,0);
}
}
}
/*----------------------------------------------------*/
intgetkey()
{
intpress;
while(bioskey
(1)==0);
press=bioskey(0);
press=press&0xff00;
return(press);
}
/*--------------------红方操作--------------------*/
intredcanselect()
{
intx,y;
x=redcurpos.x;
y=redcurpos.y;
if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)
return1;
else
return0;
}
intredcanmove()
{
inti,j,min,max,oldx,oldy,x,y;
oldx=redoldpos.x;
oldy=redoldpos.y;
x=redcurpos.x;
y=redcurpos.y;
/*case1目标位置是否是自己人*/
if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)
return0;
/*军、马、炮、相、士、将、卒的走法正确性的判断*/
switch(board[oldx][oldy])
{
caseRED_BIN:
/*完成*/
if(oldx>=5)
{if(y!
=oldy||(oldx-x)!
=1)return0;}
else
{if(x==(oldx-1)&&y==oldy)return1;
else
if(x==oldx&&y==(oldy+1))return1;
else
if(x==oldx&&y==(oldy-1))return1;
else
return0;
}
break;
caseRED_JIANG:
/*完成*/
if(x!
=oldx&&y!
=oldy)return0;
if(x!
=oldx)
if((x-oldx)>1||(oldx-x)>1)return0;
elseif(x<7)return0;
elseif(y!
=oldy)
if((y-oldy)>1||(oldy-y)>1)return0;
elseif(y<3||y>5)return0;
break;
caseRED_JU:
/*完成*/
if(x!
=oldx&&y!
=oldy)return0;
elseif(x!
=oldx)
{min=(x>oldx)?
oldx:
x;
max=(x>oldx)?
x:
oldx;
for(i=min+1;iif(board[i][y]!
=0)return0;
}
elseif(y!
=oldy)
{min=(y>oldy)?
oldy:
y;
max=(y>oldy)?
y:
oldy;
for(i=min+1;iif(board[x][i]!
=0)return0;
}
break;
caseRED_MA:
/*完成*/
if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1))
{
if(board[oldx+1][oldy]!
=0)return0;
}else
if((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1))
{
if(board[oldx-1][oldy]!
=0)return0;
}else
if((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1))
{
if(board[oldx][oldy+1]!
=0)return0;
}else
if((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1))
{
if(board[oldx][oldy-1]!
=0)return0;
}else
return0;
break;
caseRED_PAO:
/*完成*/
if(x!
=oldx&&y!
=oldy)return0;
if(board[x][y]==0)
{
if(x!
=oldx)
{min=(x>oldx)?
oldx:
x;
max=(x>oldx)?
x:
oldx;
for(i=min+1;iif(board[i][y]!
=0)return0;
}
elseif(y!
=oldy)
{min=(y>oldy)?
oldy:
y;
max=(y>oldy)?
y:
oldy;
for(i=min+1;iif(board[x][i]!
=0)return0;
}
}
else
{
if(x!
=oldx)
{min=(x>oldx)?
oldx:
x;
max=(x>oldx)?
x:
oldx;
for(i=min+1,j=0;iif(board[i][y]!
=0)j++;
if(j!
=1)return0;
}
elseif(y!
=oldy)
{min=(y>oldy)?
oldy:
y;
max=(y>oldy)?
y:
oldy;
for(i=min+1,j=0;iif(board[x][i]!
=0)j++;
if(j!
=1)return0;
}
}
break;
caseRED_SHI:
/*完成*/
if(oldx==9||oldx==7)
{if(x!
=8||y!
=4)return0;}
elseif(oldx==8)
{if(x==9&&y==3)return1;
else
if(x==9&&y==5)return1;
else
if(x==7&&y==3)return1;
else
if(x==7&&y==5)return1;
elsereturn0;
}elsereturn0;
break;
caseRED_XIANG:
/*完成*/
if(x<5)return0;
if(x!
=oldx&&y!
=oldy)
{
if((x-oldx)==2&&(y-oldy)==2)
{i=oldx+1;j=oldy+1;}
elseif((x-oldx)==2&&(oldy-y)==2)
{i=oldx+1;j=oldy-1;}
elseif((oldx-x)==2&&(y-oldy)==2)
{i=oldx-1;j=oldy+1;}
elseif((oldx-x)==2&&(oldy-y)==2)
{i=oldx-1;j=oldy-1;}