C语言编写象棋程序代码
更新时间:2023-05-30 16:58:01 阅读量: 实用文档 文档下载
可编辑/*--------------------chess.c----------------------*/
#include "dos.h"
#include "stdio.h"
/*----------------------------------------------------*/
#define RED 7
#define BLACK 14
#define true 1
#define false 0
#define SELECT 0
#define MOVE 1
#define RED_UP 0x1100
#define RED_DOWN 0x1f00
#define RED_LEFT 0x1e00
#define RED_RIGHT 0x2000
#define RED_DO 0x3900
#define RED_UNDO 0x1000
#define BLACK_UP 0x4800
#define BLACK_DOWN 0x5000
#define BLACK_LEFT 0x4b00
#define BLACK_RIGHT 0x4d00
#define BLACK_DO 0x1c00
#define BLACK_UNDO 0x2b00
#define ESCAPE 0x0100
#define RED_JU 1
#define RED_MA 2
#define RED_XIANG 3
#define RED_SHI 4
#define RED_JIANG 5
#define RED_PAO 6
#define RED_BIN 7
#define BLACK_JU 8
#define BLACK_MA 9
#define BLACK_XIANG 10
#define BLACK_SHI 11
#define BLACK_JIANG 12
#define BLACK_PAO 13
#define BLACK_BIN 14
/*----------------------------------------------------*/
int firsttime=1;
int savemode;
.
可编辑
char page_new=0,page_old=0;
int finish=false,turn=BLACK,winner=0;
int key;
int redstate=SELECT,blackstate=SELECT;
int board[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\\10 6.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}
};
.
可编辑
char cursor[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
};
struct pos{
int x;
int y;
}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*----------------------------------------------------*/
selectpage(register char page) /*换页函数*/
{
union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page; /*选择页面*/
int86(0x10,&r,&r);
}
unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/
{
union REGS r;
r.x.ax=0x4f02;
r.x.bx=vmode;
int86(0x10,&r,&r);
return(r.h.ah);
}
unsigned int get_SVGA_mode() /*获取当前SVGA屏幕模式*/
{
union REGS r;
.
可编辑
r.x.ax=0x4f03;
int86(0x10,&r,&r);
return(r.x.bx);
}
drawbmp(int start_x,int start_y,char filename[])
{
char buffer[640];
int i,j,k,n,r,g,b,width,length;
long position;
FILE *fp;
if((fp=fopen(filename,"rb"))==NULL)
{
printf("Error! Can't open file!");
getch();
return;
}
fseek(fp,28,SEEK_SET);
fread(&i,2,1,fp);
if(i!=8) /*检查是否为256色位图*/
{
puts("Error!Can't find bitmap!");
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<width+start_y;i++,n++)
{
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*480 256色模式*/
}
end()
{
set_SVGA_mode(savemode); /*恢复屏幕*/
}
/*----------------------------------------------------*/
initpos()
{
int i,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()
{
int i,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(struct pos p)
.
可编辑
{
int i,j,n,m,x,y;
long thisposition;
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(struct pos p)
{
int i,j,n,m,x,y;
long thisposition;
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);
}
}
}
.
可编辑
/*----------------------------------------------------*/
int getkey()
{
int press;
while(bioskey(1) == 0);
press=bioskey(0);
press=press&0xff00;
return(press);
}
/*--------------------红方操作--------------------*/
int redcanselect()
{
int x,y;
x=redcurpos.x;
y=redcurpos.y;
if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)
return 1;
else
return 0;
}
int redcanmove()
{
int i,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)
return 0;
/* 军、马、炮、相、士、将、卒的走法正确性的判断*/
switch(board[oldx][oldy])
{
case RED_BIN: /*完成*/ if(oldx>=5)
{ if(y!=oldy||(oldx-x)!=1) return 0;}
else
{ if(x==(oldx-1)&&y==oldy) return 1;
else
if(x==oldx&&y==(oldy+1)) return 1;
else
if(x==oldx&&y==(oldy-1)) return 1;
else
.
可编辑
return 0;
}
break;
case RED_JIANG: /*完成*/ if(x!=oldx&&y!=oldy) return 0;
if(x!=oldx)
if((x-oldx)>1||(oldx-x)>1) return 0;
else if(x<7) return 0;
else if(y!=oldy)
if((y-oldy)>1||(oldy-y)>1) return 0;
else if(y<3||y>5) return 0;
break;
case RED_JU: /*完成*/ if(x!=oldx&&y!=oldy) return 0;
else if(x!=oldx)
{ min=(x>oldx)?oldx:x;
max=(x>oldx)?x:oldx;
for(i=min+1;i<max;i++)
if(board[i][y]!=0) return 0;
}
else if(y!=oldy)
{ min=(y>oldy)?oldy:y;
max=(y>oldy)?y:oldy;
for(i=min+1;i<max;i++)
if(board[x][i]!=0) return 0;
}
break;
case RED_MA: /*完成*/ if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1))
{
if(board[oldx+1][oldy]!=0) return 0;
}else
if((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1))
{
if(board[oldx-1][oldy]!=0) return 0;
}else
if((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1))
{
if(board[oldx][oldy+1]!=0) return 0;
}else
if((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1))
{
if(board[oldx][oldy-1]!=0) return 0;
.
可编辑
}else
return 0;
break;
case RED_PAO: /*完成*/ if(x!=oldx&&y!=oldy) return 0;
if(board[x][y]==0)
{
if(x!=oldx)
{ min=(x>oldx)?oldx:x;
max=(x>oldx)?x:oldx;
for(i=min+1;i<max;i++)
if(board[i][y]!=0) return 0;
}
else if(y!=oldy)
{ min=(y>oldy)?oldy:y;
max=(y>oldy)?y:oldy;
for(i=min+1;i<max;i++)
if(board[x][i]!=0) return 0;
}
}
else
{
if(x!=oldx)
{ min=(x>oldx)?oldx:x;
max=(x>oldx)?x:oldx;
for(i=min+1,j=0;i<max;i++)
if(board[i][y]!=0) j++;
if(j!=1) return 0;
}
else if(y!=oldy)
{ min=(y>oldy)?oldy:y;
max=(y>oldy)?y:oldy;
for(i=min+1,j=0;i<max;i++)
if(board[x][i]!=0) j++;
if(j!=1) return 0;
}
}
break;
case RED_SHI: /*完成*/ if(oldx==9||oldx==7)
{if(x!=8||y!=4) return 0;}
else if(oldx==8)
{if(x==9&&y==3) return 1;
else
.
可编辑
if(x==9&&y==5) return 1;
else
if(x==7&&y==3) return 1;
else
if(x==7&&y==5) return 1;
else return 0;
}else return 0;
break;
case RED_XIANG: /*完成*/
if(x<5) return 0;
if(x!=oldx&&y!=oldy)
{
if((x-oldx)==2&&(y-oldy)==2)
{i=oldx+1;j=oldy+1;}
else if((x-oldx)==2&&(oldy-y)==2)
{i=oldx+1;j=oldy-1;}
else if((oldx-x)==2&&(y-oldy)==2)
{i=oldx-1;j=oldy+1;}
else if((oldx-x)==2&&(oldy-y)==2)
{i=oldx-1;j=oldy-1;}
else return 0;
if(board[i][j]!=0) return 0;
}
else return 0;
break;
}
return 1;
}
redup()
{
int x,y,n;
if(redcurpos.x>0)
{
redcurpos.x--;
x=position[redtemppos.x][redtemppos.y].x;
y=position[redtemppos.x][redtemppos.y].y;
if(board[redtemppos.x][redtemppos.y]==0)
drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);
else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {
n=board[redtemppos.x][redtemppos.y];
drawbmp(x,y,chessfile[n]);
}
.
可编辑
if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)
drawselecursor(redoldpos);
drawcursor(redcurpos);
redtemppos.x=redcurpos.x;
redtemppos.y=redcurpos.y;
}
}
reddown()
{
int x,y,n;
if(redcurpos.x<9)
{
redcurpos.x++;
x=position[redtemppos.x][redtemppos.y].x;
y=position[redtemppos.x][redtemppos.y].y;
if(board[redtemppos.x][redtemppos.y]==0)
drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);
else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) {
n=board[redtemppos.x][redtemppos.y];
drawbmp(x,y,chessfile[n]);
}
if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)
drawselecursor(redoldpos);
drawcursor(redcurpos);
redtemppos.x=redcurpos.x;
redtemppos.y=redcurpos.y;
}
}
redleft()
{
int x,y,n;
if(redcurpos.y>0)
{
redcurpos.y--;
x=position[redtemppos.x][redtemppos.y].x;
y=position[redtemppos.x][redtemppos.y].y;
if(board[redtemppos.x][redtemppos.y]==0)
drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);
.
正在阅读:
C语言编写象棋程序代码05-30
A FISHERIES STUDY ON THE SECTIONAL CIRCULATION IN THE EAST CHINA SEA06-10
加强乡镇卫生院管理通知02-23
一次难忘的经历作文450字06-20
初三英语话题复习课的评课稿06-17
我眼中的曹操高考作文(优秀7篇)03-27
红外光测油仪和手持式糖度计价格05-11
移动通信衰落信道的仿真分析08-31
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 象棋
- 编写
- 语言
- 代码
- 程序
- 关注留守儿童 龙会 总结1
- 温州晚报报导娄绍昆---温州娄氏父女de“经方奇缘”
- 管道与检查井连接
- 香料香精审查细则
- 小学班级读书会活动记录
- 2011中考物理试题分类汇——杠杆和其他简单机械
- 第二节__神奇的激素(第2课时)
- 2010年江西省中考真题--数学(word版含答案)
- 五年级数学下册 整理与复习教案 北师大版
- 陕西省咸阳市武功县普集高级中学2019-2020学年高一下学期第二次月考地理试题
- 《知识产权法律基础》(网络课程)介绍
- 小学语文三年级上册全套说课稿
- 完美人生健康讲座完美先生
- 论国际商务谈判的文化障碍
- 金水区小学数学学业抽评试卷质量分析报告
- 公积金转移办理流程指南
- 一定物质的量浓度溶液配制导学案
- 赴淮南学习调研心得文档
- 武汉市房屋安全管理条例
- (2016年秋季版)《y w》教学设计3