java实现中国象棋5:代码合集

前言

做完了中国象棋的几部分,可能前面有些地方叙述不清,故写一篇代码合集,可以对照发现是否有写错的地方。共分为5个文件,分别为DrawUI.java,Listener.java,init.java,BlackWin.java,RedWin.java.其中前三个文件在前面的文章中均有介绍,后两个是胜利后分别出来的弹窗,因简单故不再多加叙述。
请不要直接复制粘贴,这样没有任何意义!
另:代码与棋盘图片是配套的,如果使用自己的棋盘图片不行的话就改一下init里面的数据。
关注微信公众号:图灵完备,回复中国象棋即可获得图片及代码资源。

DrawUI

DrawUI.java文件

package 中国象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class DrawUI extends JPanel {
	
	private static final long serialVersionUID = 1L;
	Listener ls = new Listener();
	public void initui() {
		// 创建面板
		JFrame jf = new JFrame();
		// 设置面板属性
		jf.setSize(1240, 860);
		jf.setTitle("中国象棋");
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jf.getContentPane().setBackground(Color.WHITE);
		jf.setLocationRelativeTo(null);
		jf.setResizable(false);// 设置窗体不可放缩
		// 添加JPanel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(450, 1));
		jp.setBackground(Color.white);
		jp.setLayout(null);
		jf.add(jp, BorderLayout.EAST);
		// 添加JLabel
		JLabel jl = new JLabel("中国象棋") {
			private static final long serialVersionUID = 1L;
			Image jli = new ImageIcon(this.getClass().getResource("image\\"+"中国象棋.png")).getImage();
			public void paint(Graphics g) {
				g.drawImage(jli, 0, 0,400, 204, null);
			}
		};
		jl.setBounds(0,0, 400, 204);
		jp.add(jl);
		// 把this添加到JFrame中
		this.setBackground(Color.white);
		jf.add(this);
		// 添加按钮
		String[] ShapeBtn = { "开始游戏", "重新开始", "悔棋" };
		for (int i = 0; i < ShapeBtn.length; i++) {
			String name = ShapeBtn[i];
			JButton jbt = new JButton(name) {
				private static final long serialVersionUID = 1L;
				Image jbti = new ImageIcon(this.getClass().getResource("image\\"+name+".png")).getImage();
				public void paint(Graphics g) {
					g.drawImage(jbti, 0, 0,250, 100, null);
				}
			};
			jbt.setBounds(100, 260+150*i, 250, 100);
			jbt.addActionListener(ls);
			jp.add(jbt);
		}
		// 给画板添加监听器
		jf.addMouseListener(ls);
		jf.setVisible(true);
		Graphics g = jf.getGraphics();
		ls.setG(g);
		ls.setUI(this);
	}

	// 重绘
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(new ImageIcon(getClass().getResource("image\\"+"棋盘.jpg")).getImage(), 90, 60, 625, 700, this);
		// 根据flag画棋子
		for (int i = 0; i < init.row; i++) {
			for (int j = 0; j < init.column; j++) {
				if (ls.flag[i][j] > 0) {
					g.drawImage(new ImageIcon(getClass().getResource("image\\"+(Integer.toString(ls.flag[i][j])) + ".png")).getImage(), init.y0 + j * init.size - init.chesssize / 2,init.x00 + i * init.size - init.chesssize / 2,init.chesssize, init.chesssize, this);
				}
			}
		}
		
		if(ls.r != -1) {
			if(ls.flag[ls.r][ls.c] > 0) {
				if(ls.chessflag == 1&ls.flag[ls.r][ls.c] > 10 | ls.chessflag == 2&ls.flag[ls.r][ls.c] < 10) {
					int newexsize = 8;
					g.drawImage(new ImageIcon(getClass().getResource("image\\"+(Integer.toString(ls.flag[ls.r][ls.c])) + ".png")).getImage(), init.y0 + ls.c * init.size - (init.chesssize+newexsize) / 2,init.x00 + ls.r * init.size - (init.chesssize+newexsize) / 2,init.chesssize+newexsize, init.chesssize+newexsize, this);
				}				
			}
		}
	}

	public static void main(String args[]) {
		DrawUI ui = new DrawUI();
		ui.initui();
	}
}

Listener

Listener.java文件

package 中国象棋; // 自己的包的名字

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Listener extends MouseAdapter implements ActionListener {
	Graphics g;
	String action;
	int x1, y1, x2, y2, index, beindex;
	int r = -1;
	int c = -1;
	int chessflag = 1;// 红方先走为1
	DrawUI ui;
	int[][] lianbiao = new int[99999][6];// 棋子初始位置,现在的位置,棋子的编号,棋子占的位本来的棋子的编号
	int[] curchess = new int[3];
	int[] beforechess = new int[3];
	int[][] flag = new int[10][9]; // 初始化棋盘

	// 将画布传递过来
	public void setG(Graphics g) {
		this.g = g;
	}

	public void setUI(DrawUI ui) {
		this.ui = ui;
	}

	// 得到现在点击的位置
	public void getcr() {
		x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
		y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
		// 当前点击的位置
		c = (x2 - init.x0) / init.size;
		r = (y2 - init.y0) / init.size;
	}

	// 更新悔棋列表
	public void setLb() {
		lianbiao[index][0] = beforechess[0];
		lianbiao[index][1] = beforechess[1];
		lianbiao[index][2] = r;
		lianbiao[index][3] = c;
		lianbiao[index][4] = beforechess[2];
		lianbiao[index][5] = flag[r][c];
		index++;
	}

	// 更新现在点中的棋子
	public void recurchess() {
		if (r != -1) {
			curchess[0] = r;
			curchess[1] = c;
			curchess[2] = flag[r][c];
		}
	}

	// 更新上一次点中的棋子
	public void rebec() {
		//System.arraycopy(src, srcPos, dest, destPos, length);复制数组
		//%Arrays.copyOf(original, newLength);复制数组
		beforechess[0] = curchess[0];
		beforechess[1] = curchess[1];
		beforechess[2] = curchess[2];
	}

	// 更新黑方红方
	public void rechessflag() {
		if (chessflag == 1) {
			chessflag = 2;
		} else if (chessflag == 2) {
			chessflag = 1;
		}
	}
	
	public void walk(){
		setLb();// 把此棋子的前后位置保存下来
		flag[r][c] = beforechess[2];
		flag[beforechess[0]][beforechess[1]] = 0;
		ifwin();
		curchess = new int[3]; // 走完一步后curchess变为0
		beforechess = new int[3];
		c = -1;
		r = -1;
		rechessflag();
		ui.repaint();
	}

	public void mouseClicked(MouseEvent e) {
		System.out.println("点击");
		x1 = e.getX();
		y1 = e.getY();
		if (x1 > init.x0 - init.size / 2 && y1 > init.y0 - init.size / 2
				&& x1 < init.x0 + init.size / 2 + init.column * init.size
				&& y1 < init.y0 + init.row * init.size + init.size / 2) {
			x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
			y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
			// 当前点击的位置
			getcr();// 获得此时点击处的位置
			System.out.println("grtcr"+flag[r][c]);
			
			rebec();// 更新前一颗棋子
			ui.repaint();
			recurchess();
			if (r != -1) {
				if (curchess[2] == 0 & chessflag == 1 & beforechess[2] > 10 & ifwalk(beforechess[2]) == 1
						| curchess[2] == 0 & chessflag == 2 & beforechess[2] < 10 & ifwalk(beforechess[2]) == 1) {// 如果此时点的地方没有棋子,直接替换
					System.out.println("走空位");
					walk();
				} else if (beforechess[2] > 10 & curchess[2] < 10 & chessflag == 1 & flag[r][c] < 10
						& ifwalk(beforechess[2]) == 1) {
					if (curchess[2] != 0) {// 如果手中有棋子
						System.out.println("红棋吃黑棋");
						walk();
					}
				} else if (beforechess[2] < 10 & curchess[2] > 10 & beforechess[2] > 0 & chessflag == 2
						& flag[r][c] > 10 & ifwalk(beforechess[2]) == 1) {
					if (curchess[2] != 0) {// 如果手中有棋子
						System.out.println("黑棋吃红棋");
						walk();
					}
				}
			}
		}
	}

	public int ifwalk(int who) {
		int ifflag = 0;
		// 将的走法
		if (who == 5) {
			if (r < 3 & c < 6 & c > 2) {
				if(beforechess[0] == curchess[0] & Math.abs(beforechess[1] - curchess[1]) == 1
						| beforechess[1] == curchess[1] & Math.abs(beforechess[0] - curchess[0]) == 1){
					ifflag = 1;
				}
			}
		}
		// 帅的走法
		else if (who == 55) {
			if (r > 6 & c < 6 & c > 2) {
				if (beforechess[0] == curchess[0] & Math.abs(beforechess[1] - curchess[1]) == 1
						| beforechess[1] == curchess[1] & Math.abs(beforechess[0] - curchess[0]) == 1) {
					ifflag = 1;
				}
			}
		}
		// 車的走法
		else if (who == 1 | who == 11) {
			if (beforechess[0] == curchess[0] | beforechess[1] == curchess[1]) {
				if (findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 0) {
					ifflag = 1;
				}
			}
		}
		// 马的走法
		else if (who == 2 | who == 22) {
			if(beforechess[0] > 0) {
				if (beforechess[0] - curchess[0] == 2 & Math.abs(beforechess[1] - curchess[1]) == 1
						& flag[beforechess[0] - 1][beforechess[1]] == 0) {
					ifflag = 1;// 向上走日
				}
			}
			if(beforechess[0] < 9) {
				if (beforechess[0] - curchess[0] == -2 & Math.abs(beforechess[1] - curchess[1]) == 1
						& flag[beforechess[0] + 1][beforechess[1]] == 0) {
					ifflag = 1;// 向下走日
				}
			}
			if(beforechess[1] < 8) {
				if (beforechess[1] - curchess[1] == -2 & Math.abs(beforechess[0] - curchess[0]) == 1
						& flag[beforechess[0]][beforechess[1] + 1] == 0) {
					ifflag = 1;// 向右走日
				}
			}
			if(beforechess[1] > 0) {
				if (beforechess[1] - curchess[1] == 2 & Math.abs(beforechess[0] - curchess[0]) == 1
						& flag[beforechess[0]][beforechess[1] - 1] == 0) {
					ifflag = 1;// 向左走日
				}
			}
		}
		// 象的走法
		else if (who == 3 | who == 33) {
			if(beforechess[0] > 0&beforechess[1] > 0) {
				if (beforechess[0] - curchess[0] == 2 & beforechess[1] - curchess[1] == 2
						& flag[beforechess[0] - 1][beforechess[1] - 1] == 0) {
					ifflag = 1;// 向左上角走田
				}
			}
			if(beforechess[0] < 9&beforechess[1]  > 0) {
				if (beforechess[0] - curchess[0] == -2 & beforechess[1] - curchess[1] == 2
						& flag[beforechess[0] + 1][beforechess[1] - 1] == 0) {
					ifflag = 1;// 向左下角走田
				}
			}
			if(beforechess[0] > 0&beforechess[1] < 8) {
				if (beforechess[0] - curchess[0] == 2 & beforechess[1] - curchess[1] == -2
						& flag[beforechess[0] - 1][beforechess[1] + 1] == 0) {
					ifflag = 1;// 向右上角走田
				}
			}
			if(beforechess[0] < 9&beforechess[1] < 8) {
				if (beforechess[0] - curchess[0] == -2 & beforechess[1] - curchess[1] == -2
						& flag[beforechess[0] + 1][beforechess[1] + 1] == 0) {
					ifflag = 1;// 向右下角走田
				}
			}
		}
		// 士的走法
		else if (who == 4) {
			if (r < 3 & c < 6 & c > 2) {
				if(Math.abs(beforechess[1] - curchess[1])==1 & Math.abs(beforechess[0] - curchess[0])==1) {
					ifflag = 1;
				}
			}
		}
		// 仕的走法
		else if(who == 44) {
			if (r > 6 & c < 6 & c > 2) {
				if(Math.abs(beforechess[1] - curchess[1])==1 & Math.abs(beforechess[0] - curchess[0])==1) {
					ifflag = 1;
				}
			}
		}
		// 炮的走法
		else if(who == 6|who == 66) {
			if (beforechess[0] == curchess[0] | beforechess[1] == curchess[1]) {
				if (findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 1&curchess[2]!=0|findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 0&curchess[2]==0) {
					ifflag = 1;
				}
			}
		}
		// 卒的走法
		else if(who == 7) {
			if(beforechess[0]<5&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==-1|beforechess[0]>4&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==-1|beforechess[0]>4&beforechess[0]==curchess[0]&Math.abs(beforechess[1]-curchess[1])==1) {
				ifflag = 1;	
			}
		}
		// 兵的走法
		else if(who == 77) {
			if(beforechess[0]>4&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==1|beforechess[0]<5&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==1|beforechess[0]<5&beforechess[0]==curchess[0]&Math.abs(beforechess[1]-curchess[1])==1) {
				ifflag = 1;	
			}
		}
		System.out.println("ifflag="+ifflag);
		return ifflag;
	}

	// 找到某一起点到终点中含有的棋子数
	public int findnumb(int r1, int c1, int r2, int c2) {
		int numb = 0;
		if (r1 == r2) {
			for (int i = Math.min(c1, c2) + 1; i < Math.max(c1, c2); i++) {
				if (flag[r1][i] > 0) {
					numb++;
				}
			}
		} else if (c1 == c2) {
			for (int i = Math.min(r1, r2) + 1; i < Math.max(r1, r2); i++) {
				if (flag[i][c1] > 0) {
					numb++;
				}
			}
		}
		return numb;
	}
	
	public void ifwin() {
		if(chessflag == 1 & curchess[2] == 5) {
			System.out.println("红方胜利");
			new RedWin().init();
		}else if(chessflag == 2 & curchess[2] == 55) {
			System.out.println("黑方胜利");
			new BlackWin().init();
		}
	}
	
	public void renew() {
		flag = new int[][] { { 1, 2, 3, 4, 5, 4, 3, 2, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 0, 6, 0, 0, 0, 0, 0, 6, 0 }, { 7, 0, 7, 0, 7, 0, 7, 0, 7 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 77, 0, 77, 0, 77, 0, 77, 0, 77 }, { 0, 66, 0, 0, 0, 0, 0, 66, 0 },
			{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 11, 22, 33, 44, 55, 44, 33, 22, 11 } };
		chessflag = 1;
		r = -1;
		x1=0;y1=0;x2=0;y2=0;index=0;beindex=0;
		chessflag = 1;// 红方先走为1
		lianbiao = new int[99999][6];// 棋子初始位置,现在的位置,棋子的编号,棋子占的位本来的棋子的编号
		curchess = new int[3];
		beforechess = new int[3];
	}
	
	public void Regret_Chess(){
		r = -1;
		if (index > 0) {
			flag[lianbiao[index - 1][0]][lianbiao[index - 1][1]] = lianbiao[index - 1][4];
			flag[lianbiao[index - 1][2]][lianbiao[index - 1][3]] = lianbiao[index - 1][5];
			rechessflag();
			index--;
		}
	}

	public void actionPerformed(ActionEvent e) {
		// 获取按钮上的文字
		action = e.getActionCommand();

		if (action.equals("开始游戏")) {
			System.out.println("开始游戏");
			renew();
			ui.repaint();
		} else if (action.equals("重新开始")) {
			System.out.println("重新开始");
			renew();
			ui.repaint();
		} else if (action.equals("悔棋")) {
			System.out.println("悔棋");
			Regret_Chess();
			ui.repaint();
		}
	}

}

init

init.java文件

package 中国象棋; // 自己的包的名字

public interface init {

	public int x00 = 70;
	public int x0 = 105;
	public int y0 = 100;
	public int row = 10;
	public int column = 9;
	public int chesssize = 67;
	public int size = 76;
}

BlackWin

Black.java文件

package 中国象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class BlackWin extends JFrame {

	private static final long serialVersionUID = 1L;
	private Listener ls;

	public void init() {
		// 设置窗口属性
		this.setSize(400, 100);
		this.setDefaultCloseOperation(3);
		this.setLayout(new BorderLayout());
		this.setLocationByPlatform(true);
		this.setResizable(false);

		// 设置窗口居中显示
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);

		// 添加JPanel
		JPanel jp = new JPanel();

		// 添加静态文本并设置其属性
		JLabel jl = new JLabel("游戏结束,黑方胜利");
		jl.setSize(200, 30);
		jl.setFont(new Font("微软雅黑", Font.BOLD, 16));
		jl.setHorizontalAlignment(SwingConstants.CENTER);

		// 添加按钮
		JButton jbt1 = new JButton("退出");
		JButton jbt2 = new JButton("返回");
		Listener re = new Listener();
		// 设置当点击退出时退出游戏
		jbt1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		// 设置当点击确定时重新开始游戏
		jbt2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				re.renew();
				setVisible(false); //关闭窗口
				dispose(); //消除进程
			}
		});

		// 把JPanel和文本添加到窗体上,并把按钮添加到JPanel上
		this.add(jl, BorderLayout.CENTER);
		this.add(jp, BorderLayout.SOUTH);
		jp.add(jbt1);
		jp.add(jbt2);

		// 创建监听器对象
		ls = new Listener();

		// 添加按钮监听器
		jbt1.addActionListener(ls);
		jbt2.addActionListener(ls);

		// 设置窗体可见
		this.setVisible(true);

		// 获取画布命名为g
		Graphics g = this.getGraphics();

		// 设置监听器监听的的画布是g画布
		ls.setG(g);
	}

	public static void mian(String[] args) {
		BlackWin ot = new BlackWin();
		ot.init();
	}
}

RedWin

Red.java文件

package 中国象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class RedWin extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Listener ls;
	
	public void init() {
		this.setSize(400, 100);
		this.setDefaultCloseOperation(3);
		this.setLayout(new BorderLayout());
		this.setLocationByPlatform(true);
		this.setResizable(false);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);

		ls = new Listener();
		Listener re = new Listener();
		JPanel jp = new JPanel();
		jp.setLayout(new FlowLayout());
		JLabel jl = new JLabel("游戏结束,红方胜利");
		JButton jt1= new JButton("退出");
		JButton jt2= new JButton("返回");
		jl.setFont(new Font("微软雅黑",Font.BOLD,16));
		jl.setHorizontalAlignment(SwingConstants.CENTER);
		jt1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		jt2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				re.renew();
				setVisible(false); //关闭窗口
				dispose(); //消除进程
			}
		});
		
		this.add(jp, BorderLayout.SOUTH);
		this.add(jl, BorderLayout.NORTH);
		jp.add(jt1);
		jp.add(jt2);
		jt1.addActionListener(ls);
		jt2.addActionListener(ls);
		this.setVisible(true);
		Graphics g = this.getGraphics();
		ls.setG(g);
	}
	
	public static void mian(String[] args) {
		RedWin ot = new RedWin();
		ot.init();
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_44547562/article/details/103616354
今日推荐