
c语言-游戏(象棋)
ze_16,color_FFFFFF,t_70)
发布日期:2021-05-04 14:57:11
浏览次数:16
分类:技术文章
本文共 7639 字,大约阅读时间需要 25 分钟。
开发工具-熊猫devc++
由于做象棋需要用到很多图形库函数的东西,所以使用EGE的图形库方便很多(熊猫devc++自带ege的图形库(#include <graphics.h>)) 棋子走动规则已经完善代码如下,解释以后再说
还要导入自己的图片在项目文件夹中

效果展示图


#include#include #include #include #include PIMAGE rche,rma,rpao,rbing,rshi,rshuai,rxiang;PIMAGE bche,bma,bpao,bbing,bshi,bshuai,bxiang;int lenth=70;//单位长度int k[5];int b=1;//判断先后 int rwin,bwin; //判断输赢 int pan[10][9]={ { -3,-4,-5,-6,-7,-6,-5,-4,-3}, { 0,0,0,0,0,0,0,0,0}, { 0,-2,0,0,0,0,0,-2,0}, { -1,0,-1,0,-1,0,-1,0,-1}, { 0,0,0,0,0,0,0,0,0}, { 0,0,0,0,0,0,0,0,0}, { 1,0,1,0,1,0,1,0,1}, { 0,2,0,0,0,0,0,2,0}, { 0,0,0,0,0,0,0,0,0}, { 3,4,5,6,7,6,5,4,3} };//定义棋盘里面每一个格子的类容;//红色方 兵=1, 炮=2, 车=3, 马=4, 相=5, 士=6, 帅=7; //黑色方 兵=-1,炮=-2,车=-3,马=-4,相=-5,士=-6,帅=-7; void showline(); //画棋盘的线条 void showqizi(); void shubiao(); void zhanshi();void ruler();int main(){ initgraph(13*lenth, 11*lenth ,0); setbkcolor(YELLOW); //cleardevice(); showline(); showqizi(); for(;;){ //zhanshi(); shubiao(); cleardevice(); showline(); ruler(); showqizi(); if(rwin==0){ //closegraph(); setbkcolor(YELLOW); initgraph(10*lenth, 10*lenth); setbkcolor(YELLOW); xyprintf(5*lenth,5*lenth,"黑方胜利"); getch(); break; } if(bwin==0){ //closegraph(); setbkcolor(YELLOW); initgraph(10*lenth, 10*lenth); setbkcolor(YELLOW); xyprintf(5*lenth,5*lenth,"红方胜利"); getch(); break; } }}void showline(){ int i=1,j=1; for(;i<=9;i++){ setcolor(BLACK); //设置纵线条颜色 line(i*lenth,lenth,i*lenth,10*lenth); } for(;j<=10;j++){ setcolor(BLACK); //设置列线条颜色 line(lenth,j*lenth,9*lenth,j*lenth); } for(int w=2;w<9;w++){ setcolor(YELLOW); line(w*lenth,5*lenth,w*lenth,6*lenth); } setcolor(BLACK); line(4*lenth,1*lenth,6*lenth,3*lenth); line(4*lenth,8*lenth,6*lenth,10*lenth); line(6*lenth,1*lenth,4*lenth,3*lenth); line(6*lenth,8*lenth,4*lenth,10*lenth); line(0.9*lenth,0.9*lenth,9.1*lenth,0.9*lenth); line(0.9*lenth,10.1*lenth,9.1*lenth,10.1*lenth); line(0.9*lenth,0.9*lenth,0.9*lenth,10.1*lenth); line(9.1*lenth,0.9*lenth,9.1*lenth,10.1*lenth);} void showqizi(){ int i=0,j=0; rche=newimage(),rma=newimage(),rpao=newimage(),rbing=newimage(),rshi=newimage(),rshuai=newimage(),rxiang=newimage(); bche=newimage(),bma=newimage(),bpao=newimage(),bbing=newimage(),bshi=newimage(),bshuai=newimage(),bxiang=newimage(); getimage(rche,"3.png"); getimage(rma,"4.png"); getimage(rxiang,"5.png"); getimage(rshi,"6.png"); getimage(rshuai,"7.png"); getimage(rpao,"2.png"); getimage(rbing,"1.png"); getimage(bche,"-3.png"); getimage(bma,"-4.png"); getimage(bxiang,"-5.png"); getimage(bshi,"-6.png"); getimage(bshuai,"-7.png"); getimage(bpao,"-2.png"); getimage(bbing,"-1.png"); rwin=0; bwin=0; for(;i<=8;i++){ for(;j<=9;j++){ if(pan[j][i]==1){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rbing);} if(pan[j][i]==2){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rpao);} if(pan[j][i]==3){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rche);} if(pan[j][i]==4){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rma);} if(pan[j][i]==5){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rxiang);} if(pan[j][i]==6){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rshi);} if(pan[j][i]==7){ putimage((0.5+i)*lenth,(0.5+j)*lenth,rshuai);rwin=1;} if(pan[j][i]==-1){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bbing);} if(pan[j][i]==-2){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bpao);} if(pan[j][i]==-3){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bche);} if(pan[j][i]==-4){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bma);} if(pan[j][i]==-5){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bxiang);} if(pan[j][i]==-6){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bshi);} if(pan[j][i]==-7){ putimage((0.5+i)*lenth,(0.5+j)*lenth,bshuai);bwin=1;} } j=0; }}void shubiao(){ int flag=0,a=0; int x2,y2,x1,y1; int n11,n22,m11,m22; double n1,n2,m1,m2; for(;flag!=1;){ int x,y; mouse_msg mso; mousepos(&x,&y); mso=getmouse(); if(mso.is_left() && mso.is_down()){ a+=2; flag=1; } if(flag==1){ x1=x;y1=y; n1=x1*1.0/lenth; m1=y1*1.0/lenth; n11=x1/lenth; m11=y1/lenth; n1=n1-n11; m1=m1-m11; if(n1>=0.5){ n11=n11+1;} if(m1>=0.5){ m11=m11+1;} k[a-1]=n11-1; k[a]=m11-1; flag=0; //xyprintf(12*lenth,a*lenth,"%d %d",k[a-1],k[a]); //xyprintf(12*lenth,a*lenth,"%d %d",n11,m11); } if(a==4){ flag=1; } } } void zhanshi(){ int i=0,j=0; for(;i<=9;i++){ for(;j<=8;j++){ xyprintf(j*lenth+0.8*lenth,i*lenth+0.8*lenth," %d",pan[i][j]); } j=0; } }void ruler(){ int change1,a; //判断先后手 if(pan[k[2]][k[1]]==0)return; if(pan[k[2]][k[1]]>0&&b==0){ xyprintf(11*lenth,5.5*lenth,"请黑棋下"); return; } if(pan[k[2]][k[1]]<0&&b==1){ xyprintf(11*lenth,5.5*lenth,"请红棋下"); return; } //判断车的走位 if(pan[k[2]][k[1]]==3){ if(pan[k[4]][k[3]]>0){ return; } if(k[4]!=k[2]&&k[1]!=k[3]){ return; } if(k[4]==k[2]){ if(k[3]>k[1]){ for(int i=k[1]+1;i k[2]){ for(int i=k[2]+1;i k[1]){ for(int i=k[1]+1;i k[2]){ for(int i=k[2]+1;i k[1]){ for(int i=k[1]+1;i 0){ return; } if(k[2]>=5){ if(k[2]-1!=k[4]||k[1]!=k[3]){ return; } } if(k[1]-k[3]>1||k[2]-k[4]>1||k[4]>k[2])return; } if(pan[k[2]][k[1]]==-1){ if(pan[k[4]][k[3]]<0){ return; } if(k[2]<=4){ if(k[2]+1!=k[4]||k[1]!=k[3]){ return; } } if(k[3]-k[1]>1||k[4]-k[2]>1||k[2]>k[4])return; } int rrpao=0,bbpao=0; //判断炮的走位 if(pan[k[2]][k[1]]==2){ if(pan[k[4]][k[3]]>0){ //不能吃有方棋子 return; } if(k[4]!=k[2]&&k[1]!=k[3]){ //只能直走 return; } if(k[4]==k[2]){ //如果在同一排 if(k[3]>k[1]){ for(int i=k[1]+1;i k[2]){ for(int i=k[2]+1;i k[1]){ for(int i=k[1]+1;i 0)return; } if(rrpao==1&&pan[k[4]][k[3]]>0){ goto yes; } if(rrpao!=0){ return; } } if(k[3]==k[1]){ //如果在同一列 if(k[4]>k[2]){ for(int i=k[2]+1;i 0)return; } if(rrpao==1&&pan[k[4]][k[3]]>0){ goto yes; } if(rrpao!=0){ return; } } } //判断士的走位 if(pan[k[2]][k[1]]==6){ if(k[2]==8&&k[1]==4){ if(k[3]==3&&(k[4]==7||k[4]==9)); else if(k[3]==5&&(k[4]==7||k[4==9])); else return; } if(k[2]==9||k[2]==7){ if(k[3]==4&&k[4]==8); else return; } } if(pan[k[2]][k[1]]==-6){ if(k[2]==1&&k[1]==4){ if(k[3]==3&&(k[4]==0||k[4]==2)); else if(k[3]==5&&(k[4]==0||k[4==2])); else return; } if(k[2]==0||k[2]==2){ if(k[3]==4&&k[4]==1); else return; } } //判断马的走位 if(pan[k[2]][k[1]]==4||pan[k[2]][k[1]]==-4){ if(k[2]>k[4]&&k[2]-k[4]==2){ if(k[2]-k[4]==2&&(k[1]-k[3]==1||k[1]-k[3]==-1)){ if(pan[k[2]-1][k[1]]!=0)return; } else return; } else if(k[4]>k[2]&&k[2]-k[4]==-2){ if(k[2]-k[4]==-2&&(k[1]-k[3]==1||k[1]-k[3]==-1)){ if(pan[k[2]+1][k[1]]!=0)return; } else return; } else if(k[1]>k[3]&&k[1]-k[3]==2){ if(k[2]-k[4]==1||k[2]-k[4]==-1){ if(pan[k[2]][k[1]-1]!=0)return; } else return; } else if(k[1] =5){ if(k[4]<5)return; } if(k[2]<=4){ if(k[4]>4)return; } } else return; } //判断将的走位 if(pan[k[2]][k[1]]==7||pan[k[2]][k[1]]==-7){ if(k[4]>=3&&k[4]<=6)return; if(k[3]>=6||k[3]<=2)return; if(k[3]!=k[1]&&k[4]!=k[2])return; if(abs(k[3]-k[1])>1||abs(k[4]-k[2])>1)return; } //判断红黑吃的情况 yes:if((pan[k[2]][k[1]]>0&&pan[k[4]][k[3]]>0)||(pan[k[2]][k[1]]<0&&pan[k[4]][k[3]]<0))return; if(pan[k[2]][k[1]]>0&&pan[k[4]][k[3]]<0){ pan[k[4]][k[3]]=0; } if(pan[k[2]][k[1]]<0&&pan[k[4]][k[3]]>0){ pan[k[4]][k[3]]=0; } change1=pan[k[4]][k[3]]; pan[k[4]][k[3]]=pan[k[2]][k[1]]; pan[k[2]][k[1]]=change1; if(pan[k[4]][k[3]]>0)b=0; if(pan[k[4]][k[3]]<0)b=1;}
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年03月24日 12时22分30秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
H5页面授权获取微信授权(openId,微信nickname等信息)
2019-03-03
SpringBoot的URL是如何拼接的
2019-03-03
2018年年终总结
2019-03-03
解决checkbox未选中不传递value的多种方法
2019-03-03
【pgsql-参数详解1】PostgreSQL默认参数值
2019-03-03
PostgreSQL11-Hash哈希分区数量的设定标准
2019-03-03
HTTP协议(1)_入门的一些教程和资源
2019-03-03
如何利用Hbuilder真机调试安卓手机【小米、华为】
2019-03-03
IDEA version Control 下没有git菜单
2019-03-03
2021年春季ACM训练赛第3场
2019-03-03
Go-编码规范(注释,变量名等基本规则)
2019-03-03
Go-常用命令go的使用(build、env、run、fmt等)
2019-03-03
钉钉登录及常用的URL及IP
2019-03-03
钉钉IP及域名列表
2019-03-03
CENTOS 删除nginx
2019-03-03
【redis键过期删除策略】很高兴再次认识你
2019-03-03
【工具篇】EasyExcel的应用
2019-03-03
SSM发送手机验证码——以网建SMS为例
2019-03-03
大范围卫星影像快速处理
2019-03-03
监控264后缀文件播放
2019-03-03