c语言-游戏(象棋)

开发工具-熊猫devc++
由于做象棋需要用到很多图形库函数的东西,所以使用EGE的图形库方便很多(熊猫devc++自带ege的图形库(#include <graphics.h>))
棋子走动规则已经完善

代码如下,解释以后再说

还要导入自己的图片在项目文件夹中
在这里插入图片描述

效果展示图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210308205923511.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzUyMzQ3NTQw,si在这里插入图片描述
ze_16,color_FFFFFF,t_70)
在这里插入图片描述

#include <graphics.h>
#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
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[3];i++){
					if(pan[k[4]][i]!=0){
						return;
					}
				}
			}
			if(k[3]<k[1]){
				for(int i=k[3]+1;i<k[1];i++){
					if(pan[k[4]][i]!=0){
						return;
					}
				}
			}			
		}
		if(k[3]==k[1]){
			
			if(k[4]>k[2]){
				for(int i=k[2]+1;i<k[4];i++){
					if(pan[i][k[3]]!=0){
						return;
					}
				}
			}
			if(k[4]<k[2]){
				for(int i=k[4]+1;i<k[2];i++){
					if(pan[i][k[3]]!=0){
						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[3];i++){
					if(pan[k[4]][i]!=0){
						return;
					}
				}
			}
			if(k[3]<k[1]){
				for(int i=k[3]+1;i<k[1];i++){
					if(pan[k[4]][i]!=0){
						return;
					}
				}
			}			
		}
		if(k[3]==k[1]){
			if(k[4]>k[2]){
				for(int i=k[2]+1;i<k[4];i++){
					if(pan[k[i]][k[3]]!=0){
						return;
					}
				}
			}
			if(k[3]>k[1]){
				for(int i=k[1]+1;i<k[3];i++){
					if(pan[k[i]][k[3]]!=0){
						return;
					}
				}
			}			
		}	
	}
	
	//判断兵的走位
	 if(pan[k[2]][k[1]]==1){
	 		if(pan[k[4]][k[3]]>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[3];i++){
					if(pan[k[4]][i]!=0){
						rrpao++;
					}
				}

			}
			if(k[3]<k[1]){   	 
				for(int i=k[3]+1;i<k[1];i++){
					if(pan[k[4]][i]!=0){
						rrpao++;
					}
				}

			}	
				if(rrpao==0){
					if(pan[k[4]][k[3]]<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<k[4];i++){
					if(pan[i][k[3]]!=0){
						rrpao++;
					}
				}

			}
			
			if(k[4]<k[2]){   	 
				for(int i=k[4]+1;i<k[2];i++){
					if(pan[i][k[3]]!=0){
						rrpao++;
					}
				}

			}	
				if(rrpao==0){
					if(pan[k[4]][k[3]]<0)return;
				}
				if(rrpao==1&&pan[k[4]][k[3]]<0){
					goto yes;
				}
				if(rrpao!=0){
					return;
				}
	}
			
	}
	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[3];i++){
					if(pan[k[4]][i]!=0){
						rrpao++;
					}
				}

			}
			if(k[3]<k[1]){   	 
				for(int i=k[3]+1;i<k[1];i++){
					if(pan[k[4]][i]!=0){
						rrpao++;
					}
				}

			}	
				if(rrpao==0){
					if(pan[k[4]][k[3]]>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<k[4];i++){
					if(pan[i][k[3]]!=0){
						rrpao++;
					}
				}

			}
			
			if(k[4]<k[2]){   	 
				for(int i=k[4]+1;i<k[2];i++){
					if(pan[i][k[3]]!=0){
						rrpao++;
					}
				}

			}	
				if(rrpao==0){
					if(pan[k[4]][k[3]]>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]<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 return;
		
	}
	
	//判断相的走位
	if(pan[k[2]][k[1]]==5||pan[k[2]][k[1]]==-5){
		if((k[2]-k[4]==2||k[2]-k[4]==-2)&&(k[1]-k[3]==2||k[1]-k[3]==-2)){
			if(pan[(k[2]+k[4])/2][(k[1]+k[3])/2]!=0)return;
			if(k[2]>=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;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
C语言C++实现象棋游戏可以采用人机对战的方式。一个简单的算法是使用遍历来获取所有人机方棋子能走的地方,然后根据评估函数找出对人机方来说收益最大的一步。这个算法的实现并不容易,因为评估函数需要具有代表性,这直接决定了人机的棋力,并且需要进行迭代遍历,这会导致时间复杂度较高。\[1\] 如果你想获取完整的代码素材和进行C/C++交流学习,你可以访问以下链接:\[2\] 在实现象棋游戏时,你还可以注册鼠标事件,通过鼠标点击不同位置来实现棋子的移动,按照象棋的行棋规范。例如,你可以使用mouseEvent函数来处理鼠标事件,其x和y表示点击的位置,button表示鼠标左右键和滑轮的事件,e表示点击或抬起的事件。\[3\] #### 引用[.reference_title] - *1* *3* [C/C++:实现象棋游戏](https://blog.csdn.net/linglingnana/article/details/125013081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C语言编码象棋(包教包会)](https://blog.csdn.net/qq958948605/article/details/121124104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值