⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chess.java

📁 java编写的中国象棋源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;

class ChessPanel extends JPanel
{
	final int blue_car_1=1;//32个棋子分别用1-32来区分 
	final int blue_horse_1=2; 
	final int blue_elef_1=3; 
	final int blue_nurse_1=4;
	final int blue_master_1=5;
	final int blue_nurse_2=6; 
	final int blue_elef_2=7; 
	final int blue_horse_2=8;
	final int blue_car_2=9; 
	final int blue_pao_1=10;
	final int blue_pao_2=11;
	final int blue_bing_1=12;
	final int blue_bing_2=13;
	final int blue_bing_3=14;
	final int blue_bing_4=15;
	final int blue_bing_5=16;
	
	final int red_car_1=17; 
	final int red_horse_1=18;
	final int red_elef_1=19; 
	final int red_nurse_1=20;
	final int red_master_1=21;
	final int red_nurse_2=22; 
	final int red_elef_2=23; 
	final int red_horse_2=24;
	final int red_car_2=25; 
	final int red_pao_1=26; 
	final int red_pao_2=27; 
	final int red_bing_1=28;
	final int red_bing_2=29;
	final int red_bing_3=30;
	final int red_bing_4=31;
	final int red_bing_5=32;
	
	final int red=1;    //在分辩该谁走棋时用到这两个常量
	final int blue=-1;
	static int a1=15,a2=56,a3=30,a4=45;//28,56,42,51
	int now_ChessPiece=0,goto_l,goto_h;//now_ChessPiece是当前选中的棋子的编号,goto_l和_h是将要到的地方的行,列值
	static Image ChesssBoard_pic=Toolkit.getDefaultToolkit().getImage("ChesssBoard.gif");//棋盘图片
	static Image ChessPiece_pic[]=new Image[33];//32个棋子的图片
	static Image[][] everybox=new Image[9][10];//棋盘每一块的图片
	int[][] ChesssBoard=new int[9][10];//把当前棋盘的情况用此数组表示
	int[][] ChessPiece=new int[33][5];
	int moveChessPiece_l_num,moveChessPiece_h_num;//当用鼠标脱动棋子时,用它两个表示当前的位置
	int mouse_sign=-1;//用来确认当前是选中其子还是放(定位)棋子
	int turn_number=red;//该谁走
	//datainti()是娄据的初始化
	public void datainti()
	{
		for(int i=1;i<33;i++)
		{ 
			ChessPiece[i][0]=i;//ChessPiece[i][0]表示是哪 个子
			ChessPiece[i][3]=0;//ChessPiece[i][3]表示这个棋子是否正在脱动状态
			ChessPiece[i][4]=0;//ChessPiece[i][4]表示这个其子是否死亡.1死,0活
		}  
		ChessPiece[1][1]=0; ChessPiece[1][2]=0;//各各棋子的位置
		ChessPiece[2][1]=1; ChessPiece[2][2]=0;
		ChessPiece[3][1]=2; ChessPiece[3][2]=0;
		ChessPiece[4][1]=3; ChessPiece[4][2]=0;
		ChessPiece[5][1]=4; ChessPiece[5][2]=0; 
		ChessPiece[6][1]=5; ChessPiece[6][2]=0;
		ChessPiece[7][1]=6; ChessPiece[7][2]=0;
		ChessPiece[8][1]=7; ChessPiece[8][2]=0;
		ChessPiece[9][1]=8; ChessPiece[9][2]=0;
		ChessPiece[10][1]=1; ChessPiece[10][2]=2;
		ChessPiece[11][1]=7; ChessPiece[11][2]=2;
		ChessPiece[12][1]=0; ChessPiece[12][2]=3;
		ChessPiece[13][1]=2; ChessPiece[13][2]=3;
		ChessPiece[14][1]=4; ChessPiece[14][2]=3;
		ChessPiece[15][1]=6; ChessPiece[15][2]=3;
		ChessPiece[16][1]=8; ChessPiece[16][2]=3;
		
		ChessPiece[17][1]=0; ChessPiece[17][2]=9;
		ChessPiece[18][1]=1; ChessPiece[18][2]=9;
		ChessPiece[19][1]=2; ChessPiece[19][2]=9;
		ChessPiece[20][1]=3; ChessPiece[20][2]=9;
		ChessPiece[21][1]=4; ChessPiece[21][2]=9;
		ChessPiece[22][1]=5; ChessPiece[22][2]=9;
		ChessPiece[23][1]=6; ChessPiece[23][2]=9;
		ChessPiece[24][1]=7; ChessPiece[24][2]=9;
		ChessPiece[25][1]=8; ChessPiece[25][2]=9;
		ChessPiece[26][1]=1; ChessPiece[26][2]=7;
		ChessPiece[27][1]=7; ChessPiece[27][2]=7;
		ChessPiece[28][1]=0; ChessPiece[28][2]=6;
		ChessPiece[29][1]=2; ChessPiece[29][2]=6;
		ChessPiece[30][1]=4;ChessPiece[30][2]=6;
		ChessPiece[31][1]=6; ChessPiece[31][2]=6;
		ChessPiece[32][1]=8; ChessPiece[32][2]=6;
		//装载图片
		ChessPiece_pic[blue_car_1]=Toolkit.getDefaultToolkit().getImage("blue_car.gif");
		ChessPiece_pic[blue_car_2]=Toolkit.getDefaultToolkit().getImage("blue_car.gif");
		ChessPiece_pic[blue_horse_1]=Toolkit.getDefaultToolkit().getImage("blue_horse.gif");
		ChessPiece_pic[blue_horse_2]=Toolkit.getDefaultToolkit().getImage("blue_horse.gif");
		ChessPiece_pic[blue_elef_1]=Toolkit.getDefaultToolkit().getImage("blue_elef.gif");
		ChessPiece_pic[blue_elef_2]=Toolkit.getDefaultToolkit().getImage("blue_elef.gif");
		ChessPiece_pic[blue_nurse_1]=Toolkit.getDefaultToolkit().getImage("blue_nurse.gif");
		ChessPiece_pic[blue_nurse_2]=Toolkit.getDefaultToolkit().getImage("blue_nurse.gif");
		ChessPiece_pic[blue_master_1]=Toolkit.getDefaultToolkit().getImage("blue_master.gif");
		ChessPiece_pic[blue_pao_1]=Toolkit.getDefaultToolkit().getImage("blue_pao.gif");
		ChessPiece_pic[blue_pao_2]=Toolkit.getDefaultToolkit().getImage("blue_pao.gif");
		ChessPiece_pic[blue_bing_1]=Toolkit.getDefaultToolkit().getImage("blue_bing.gif");
		ChessPiece_pic[blue_bing_2]=Toolkit.getDefaultToolkit().getImage("blue_bing.gif");
		ChessPiece_pic[blue_bing_3]=Toolkit.getDefaultToolkit().getImage("blue_bing.gif");
		ChessPiece_pic[blue_bing_4]=Toolkit.getDefaultToolkit().getImage("blue_bing.gif");
		ChessPiece_pic[blue_bing_5]=Toolkit.getDefaultToolkit().getImage("blue_bing.gif");
		ChessPiece_pic[red_car_1]=Toolkit.getDefaultToolkit().getImage("red_car.gif");
		ChessPiece_pic[red_car_2]=Toolkit.getDefaultToolkit().getImage("red_car.gif");
		ChessPiece_pic[red_horse_1]=Toolkit.getDefaultToolkit().getImage("red_horse.gif");
		ChessPiece_pic[red_horse_2]=Toolkit.getDefaultToolkit().getImage("red_horse.gif");
		ChessPiece_pic[red_elef_1]=Toolkit.getDefaultToolkit().getImage("red_elef.gif");
		ChessPiece_pic[red_elef_2]=Toolkit.getDefaultToolkit().getImage("red_elef.gif");
		ChessPiece_pic[red_nurse_1]=Toolkit.getDefaultToolkit().getImage("red_nurse.gif");
		ChessPiece_pic[red_nurse_2]=Toolkit.getDefaultToolkit().getImage("red_nurse.gif");
		ChessPiece_pic[red_master_1]=Toolkit.getDefaultToolkit().getImage("red_master.gif");
		ChessPiece_pic[red_pao_1]=Toolkit.getDefaultToolkit().getImage("red_pao.gif");
		ChessPiece_pic[red_pao_2]=Toolkit.getDefaultToolkit().getImage("red_pao.gif");
		ChessPiece_pic[red_bing_1]=Toolkit.getDefaultToolkit().getImage("red_bing.gif"); 
		ChessPiece_pic[red_bing_2]=Toolkit.getDefaultToolkit().getImage("red_bing.gif");
		ChessPiece_pic[red_bing_3]=Toolkit.getDefaultToolkit().getImage("red_bing.gif");
		ChessPiece_pic[red_bing_4]=Toolkit.getDefaultToolkit().getImage("red_bing.gif");
		ChessPiece_pic[red_bing_5]=Toolkit.getDefaultToolkit().getImage("red_bing.gif");
		for(int i=0;i<10;i++)
			for(int j=0;j<9;j++)
			{
				ImageFilter filterObj=new CropImageFilter(ltoL(j)-a1,htoH(i)-a1,a2,a2);
				ImageProducer producerObj=new FilteredImageSource(ChesssBoard_pic.getSource(),filterObj);
				everybox[j][i]=createImage(producerObj);
			} 
		for(int i=0;i<9;i++)
			for(int j=0;j<10;j++)
				ChesssBoard[i][j]=0;
		for(int i=1;i<33;i++)
			ChesssBoard[ChessPiece[i][1]][ChessPiece[i][2]]=ChessPiece[i][0];
		MediaTracker tracker=new MediaTracker(this);
		tracker.addImage(ChesssBoard_pic,0);
		for(int i=1;i<33;i++)
			tracker.addImage(ChessPiece_pic[i],i);
		int n=33;
		for(int i=0;i<10;i++)
			for(int j=0;j<9;j++)
				tracker.addImage(everybox[j][i],n++);
		try
		{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){}
	}
	//将分辨率转成棋盘
	static public int Htoh(int h)
	{
		int o_h=a3;
		int every_h=a2;
		int h_num=(h-o_h+every_h/2)/every_h;
		return(h_num);
	}
	//L
	static public int Ltol(int l)
	{
		int o_l=a4;
		int every_l=a2;
		int l_num=(l-o_l+every_l/2)/every_l;
		return(l_num);
	}
	//棋盘转成分辩率
	static public int ltoL(int L)
	{
		int o_l=a4;
		int every_l=a2;
		int l_pix=o_l+L*every_l;
		return(l_pix);
	}
	
	static public int htoH(int H)
	{
		int o_h=a3;
		int every_h=a2;
		int H_pix=o_h+H*every_h;
		return(H_pix);
	}
	//l,h处是否有子
	public boolean haveChessPiece(int l,int h)
	{
		if(ChesssBoard[l][h]!=0)
		{
			if(ChessPiece[ChesssBoard[l][h]][4]==1)return(false);
			return(true);
		}  
		else 
			return(false);
	}
	//A&B 是一伙的吗?
	public boolean samefamily(int first_ChessPiece,int second_ChessPiece)
	{
		if((isred(first_ChessPiece)==true&&isred(second_ChessPiece)==true)||(isred(first_ChessPiece)==false&&isred(second_ChessPiece)==false))
			return(true);
		else 
			return(false);
	}
	//是红棋吗
	public boolean isred(int aChessPiece)
	{
		if(aChessPiece<=16)
			return(false);
		else 
			return(true);
	}
	//是兵吗
	public boolean isbing(int a_ChessPiece)
	{
		if((a_ChessPiece<=16&&a_ChessPiece>=12)||(a_ChessPiece<=32&&a_ChessPiece>=28))
			return(true);
		else
			return(false);
	}
	//当前棋子可以放在(goto_l,goto_h)这吗?  
	public boolean isOK(int goto_l,int goto_h) 
	{
		int now_l=ChessPiece[now_ChessPiece][1],now_h=ChessPiece[now_ChessPiece][2];
		if(goto_l==now_l&&goto_h==now_h)return(false);
		if((now_ChessPiece==blue_car_1||now_ChessPiece==blue_car_2||now_ChessPiece==red_car_1||now_ChessPiece==red_car_2||now_ChessPiece==blue_pao_1||now_ChessPiece==blue_pao_2||now_ChessPiece==red_pao_1||now_ChessPiece==red_pao_2||now_ChessPiece==blue_master_1||now_ChessPiece==red_master_1||(now_ChessPiece>=12&&now_ChessPiece<=16)||(now_ChessPiece>=28&&now_ChessPiece<=32))&&now_l!=goto_l&&now_h!=goto_h)return(false);
		if(haveChessPiece(goto_l,goto_h)&&samefamily(ChesssBoard[goto_l][goto_h],now_ChessPiece))return(false);
		if((now_ChessPiece==blue_car_1||now_ChessPiece==blue_car_2||now_ChessPiece==red_car_1||now_ChessPiece==red_car_2)&&between(now_l,now_h,goto_l,goto_h)!=0)return(false);
		if((now_ChessPiece==blue_pao_1||now_ChessPiece==blue_pao_2||now_ChessPiece==red_pao_1||now_ChessPiece==red_pao_2)&&between(now_l,now_h,goto_l,goto_h)!=0&&between(now_l,now_h,goto_l,goto_h)!=1)return(false);
		if((now_ChessPiece==blue_pao_1||now_ChessPiece==blue_pao_2||now_ChessPiece==red_pao_1||now_ChessPiece==red_pao_2)&&between(now_l,now_h,goto_l,goto_h)==1&&(haveChessPiece(goto_l,goto_h)&&samefamily(now_ChessPiece,ChesssBoard[goto_l][goto_h])||(!haveChessPiece(goto_l,goto_h))))return(false);
		if((now_ChessPiece==blue_pao_1||now_ChessPiece==blue_pao_2||now_ChessPiece==red_pao_1||now_ChessPiece==red_pao_2)&&between(now_l,now_h,goto_l,goto_h)==0&&haveChessPiece(goto_l,goto_h))return(false);
		if((now_ChessPiece==blue_master_1||now_ChessPiece==red_master_1)&&(goto_l<3||goto_l>5))return(false);
		if(now_ChessPiece==blue_master_1&&goto_h>2)return(false);
		if(now_ChessPiece==red_master_1&&goto_h<7)return(false);
		if((now_ChessPiece==red_master_1||now_ChessPiece==blue_master_1)&&now_h!=goto_h&&Math.abs(goto_h-now_h)!=1)return(false);
		if((now_ChessPiece==red_master_1||now_ChessPiece==blue_master_1)&&now_l!=goto_l&&Math.abs(goto_l-now_l)!=1)return(false);
		if((now_ChessPiece==blue_nurse_1||now_ChessPiece==blue_nurse_2||now_ChessPiece==red_nurse_1||now_ChessPiece==red_nurse_2)&&(Math.abs(goto_h-now_h)!=1||Math.abs(goto_l-now_l)!=1))return(false);
		if((now_ChessPiece==blue_nurse_1||now_ChessPiece==blue_nurse_2||now_ChessPiece==red_nurse_1||now_ChessPiece==red_nurse_2)&&(goto_l<3||goto_l>5))return(false);
		if((now_ChessPiece==blue_nurse_1||now_ChessPiece==blue_nurse_2)&&goto_h>2)return(false);
		if((now_ChessPiece==red_nurse_1||now_ChessPiece==red_nurse_2)&&goto_h<7)return(false);
		if((now_ChessPiece==blue_elef_1||now_ChessPiece==blue_elef_2)&&goto_h>4)return(false);
		if((now_ChessPiece==red_elef_1||now_ChessPiece==red_elef_2)&&goto_h<5)return(false);
		if((now_ChessPiece==blue_elef_1||now_ChessPiece==blue_elef_2||now_ChessPiece==red_elef_1||now_ChessPiece==red_elef_2)&&(Math.abs(goto_h-now_h)!=2||Math.abs(goto_l-now_l)!=2))return(false);
		if((now_ChessPiece==blue_elef_1||now_ChessPiece==blue_elef_2||now_ChessPiece==red_elef_1||now_ChessPiece==red_elef_2)&&ChesssBoard[(goto_l+now_l)/2][(goto_h+now_h)/2]!=0)return(false);
		if((now_ChessPiece<=16&&now_ChessPiece>=12)&&goto_h<now_h)return(false);  
		if((now_ChessPiece<=32&&now_ChessPiece>=28)&&goto_h>now_h)return(false);  
		if(isbing(now_ChessPiece)&&now_h!=goto_h&&Math.abs(goto_h-now_h)!=1)return(false);
		if(isbing(now_ChessPiece)&&now_l!=goto_l&&Math.abs(goto_l-now_l)!=1)return(false);
		if((now_ChessPiece<=16&&now_ChessPiece>=12)&&now_h<5&&goto_l!=now_l)return(false);  
		if((now_ChessPiece<=32&&now_ChessPiece>=28)&&now_h>4&&goto_l!=now_l)return(false);  
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&(Math.abs(now_h-goto_h)!=2&&Math.abs(now_h-goto_h)!=1))return(false);
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&(Math.abs(now_l-goto_l)!=1&&Math.abs(now_l-goto_l)!=2))return(false);
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&Math.abs(now_l-goto_l)==Math.abs(now_h-goto_h))return(false);
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&now_h-goto_h==2&&ChesssBoard[now_l][now_h-1]!=0)return(false);
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&now_h-goto_h==-2&&ChesssBoard[now_l][now_h+1]!=0)return(false);  
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&now_l-goto_l==2&&ChesssBoard[now_l-1][now_h]!=0)return(false);
		if((now_ChessPiece==blue_horse_1||now_ChessPiece==blue_horse_2||now_ChessPiece==red_horse_1||now_ChessPiece==red_horse_2)&&now_l-goto_l==-2&&ChesssBoard[now_l+1][now_h]!=0)return(false);
		return(true);
	}
	
	public void carry(int goto_l,int goto_h)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -